Skip to content

Commit

Permalink
added "source" property to samples and items, and sample-with-locatio…
Browse files Browse the repository at this point in the history
…n creation methods #26
  • Loading branch information
sobri909 committed Jul 11, 2018
1 parent 0c3bf17 commit 5203324
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 12 deletions.
17 changes: 12 additions & 5 deletions LocoKit/Base/LocomotionSample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ open class LocomotionSample: ActivityTypeTrainable, TimelineObject, Codable {

public let sampleId: UUID

public var source: String = "LocoKit"

/// The timestamp for the weighted centre of the sample period. Equivalent to `location.timestamp`.
public let date: Date

Expand Down Expand Up @@ -188,8 +190,9 @@ open class LocomotionSample: ActivityTypeTrainable, TimelineObject, Codable {
store.add(self)
}

public convenience init(date: Date, recordingState: RecordingState, in store: TimelineStore) {
self.init(date: date, recordingState: recordingState)
public convenience init(date: Date, location: CLLocation? = nil, movingState: MovingState = .uncertain,
recordingState: RecordingState, in store: TimelineStore) {
self.init(date: date, location: location, movingState: movingState, recordingState: recordingState)
self.store = store
store.add(self)
}
Expand Down Expand Up @@ -254,21 +257,25 @@ open class LocomotionSample: ActivityTypeTrainable, TimelineObject, Codable {
self.confirmedType = nil
}

if let source = dict["source"] as? String, !source.isEmpty {
self.source = source
}

self.rawLocations = nil
self.filteredLocations = nil
}

/// For recording samples to mark special events such as app termination.
public required init(date: Date, recordingState: RecordingState) {
public required init(date: Date, location: CLLocation? = nil, movingState: MovingState = .uncertain, recordingState: RecordingState) {
self.sampleId = UUID()

self.recordingState = recordingState
self.movingState = .uncertain
self.movingState = movingState
self.date = date

self.filteredLocations = []
self.rawLocations = []
self.location = nil
self.location = location

self.stepHz = nil
self.courseVariance = nil
Expand Down
5 changes: 5 additions & 0 deletions LocoKit/Base/Timelines/Items/TimelineItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ open class TimelineItem: TimelineObject, Hashable, Comparable, Codable {

public let itemId: UUID

public var source: String = "LocoKit"

open var isMergeLocked: Bool {
if isCurrentItem && !isWorthKeeping { return true }
return false
Expand Down Expand Up @@ -627,6 +629,9 @@ open class TimelineItem: TimelineObject, Hashable, Comparable, Codable {
_modeActivityType = activityType
}
}
if let source = dict["source"] as? String, !source.isEmpty {
self.source = source
}
store.add(self)
}

Expand Down
2 changes: 1 addition & 1 deletion LocoKit/Base/Timelines/TimelineObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public protocol TimelineObject: class {

var objectId: UUID { get }

var source: String { get set }
var store: TimelineStore? { get }

}
Expand Down
5 changes: 3 additions & 2 deletions LocoKit/Base/Timelines/TimelineStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import os.log
import LocoKitCore
import CoreLocation

public extension NSNotification.Name {
public static let processingStarted = Notification.Name("processingStarted")
Expand Down Expand Up @@ -77,8 +78,8 @@ open class TimelineStore {
return LocomotionSample(from: sample, in: self)
}

open func createSample(date: Date, recordingState: RecordingState) -> LocomotionSample {
return LocomotionSample(date: date, recordingState: recordingState, in: self)
open func createSample(date: Date, location: CLLocation? = nil, recordingState: RecordingState) -> LocomotionSample {
return LocomotionSample(date: date, location: location, recordingState: recordingState, in: self)
}

open func add(_ timelineItem: TimelineItem) {
Expand Down
1 change: 1 addition & 0 deletions LocoKit/LocalStore/PersistentPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ open class PersistentPath: Path, PersistentObject {
container["lastSaved"] = transactionDate ?? lastSaved ?? Date()
container["deleted"] = deleted
container["isVisit"] = false
container["source"] = source
let range = _dateRange ?? dateRange
container["startDate"] = range?.start
container["endDate"] = range?.end
Expand Down
7 changes: 5 additions & 2 deletions LocoKit/LocalStore/PersistentSample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import GRDB
import LocoKitCore
import CoreLocation

open class PersistentSample: LocomotionSample, PersistentObject {

Expand All @@ -29,8 +30,9 @@ open class PersistentSample: LocomotionSample, PersistentObject {

public required init(from sample: ActivityBrainSample) { super.init(from: sample) }

public required init(date: Date, recordingState: RecordingState) {
super.init(date: date, recordingState: recordingState)
public required init(date: Date, location: CLLocation? = nil, movingState: MovingState = .uncertain,
recordingState: RecordingState) {
super.init(date: date, location: location, movingState: movingState, recordingState: recordingState)
}

// MARK: Decodable
Expand Down Expand Up @@ -74,6 +76,7 @@ open class PersistentSample: LocomotionSample, PersistentObject {

open func encode(to container: inout PersistenceContainer) {
container["sampleId"] = sampleId.uuidString
container["source"] = source
container["date"] = date
container["deleted"] = deleted
container["lastSaved"] = transactionDate ?? lastSaved ?? Date()
Expand Down
10 changes: 10 additions & 0 deletions LocoKit/LocalStore/PersistentTimelineStore+Migrations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ internal extension PersistentTimelineStore {
END
""")
}

// add source fields, so that imported data can be distinguished from recorded data
migrator.registerMigration("6.0.0 source") { db in
try db.alter(table: "TimelineItem") { table in
table.add(column: "source", .text).defaults(to: "LocoKit").indexed()
}
try db.alter(table: "LocomotionSample") { table in
table.add(column: "source", .text).defaults(to: "LocoKit").indexed()
}
}
}

}
5 changes: 3 additions & 2 deletions LocoKit/LocalStore/PersistentTimelineStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os.log
import GRDB
import LocoKitCore
import CoreLocation

open class PersistentTimelineStore: TimelineStore {

Expand Down Expand Up @@ -79,8 +80,8 @@ open class PersistentTimelineStore: TimelineStore {
return PersistentSample(from: sample, in: self)
}

open override func createSample(date: Date, recordingState: RecordingState) -> PersistentSample {
return PersistentSample(date: date, recordingState: recordingState, in: self)
open override func createSample(date: Date, location: CLLocation? = nil, recordingState: RecordingState) -> PersistentSample {
return PersistentSample(date: date, location: location, recordingState: recordingState, in: self)
}

public func object(for row: Row) -> TimelineObject {
Expand Down
1 change: 1 addition & 0 deletions LocoKit/LocalStore/PersistentVisit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ open class PersistentVisit: Visit, PersistentObject {
container["lastSaved"] = transactionDate ?? lastSaved ?? Date()
container["deleted"] = deleted
container["isVisit"] = true
container["source"] = source
let range = _dateRange ?? dateRange
container["startDate"] = range?.start
container["endDate"] = range?.end
Expand Down

0 comments on commit 5203324

Please sign in to comment.