Skip to content

Commit

Permalink
when brexiting from the middle of an item, split the item in two #13
Browse files Browse the repository at this point in the history
  • Loading branch information
sobri909 committed May 30, 2018
1 parent e158f3c commit a56cb58
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
4 changes: 4 additions & 0 deletions LocoKit/Base/Timelines/Items/TimelineItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ open class TimelineItem: TimelineObject, Hashable, Comparable, Codable {
}
}

// MARK: - Item metadata copying

open func copyMetadata(from otherItem: TimelineItem) {}

// MARK: - Hashable, Comparable

public var hashValue: Int { return itemId.hashValue }
Expand Down
42 changes: 29 additions & 13 deletions LocoKit/LocalStore/PersistentProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,12 @@ public class PersistentProcessor {
overlapper.nextItem = nil
modifiedItems.append(overlapper)
}

// if only extracted from middle, split the item in two
if !lostPrevEdge && !lostNextEdge && !samplesToSteal.isEmpty {
// TODO: split the item in two
print("TODO: split the item in two")
}
}

// create the new item
let newItem = createItem(from: segment, in: store)
let newItem = segment.activityType == .stationary
? store.createVisit(from: segment.samples)
: store.createPath(from: segment.samples)

// add the stolen samples to the new item
if !samplesToSteal.isEmpty {
Expand All @@ -74,9 +70,35 @@ public class PersistentProcessor {

// delete any newly empty items
for modifiedItem in modifiedItems where modifiedItem.samples.isEmpty {
print("Deleting a newly empty item")
modifiedItem.delete()
}

// if the new item is inside an overlapper, split that overlapper in two
for overlapper in overlappers where !overlapper.deleted {
guard let newItemRange = newItem.dateRange else { break }
guard let overlapperRange = overlapper.dateRange else { continue }
guard let intersection = overlapperRange.intersection(with: newItemRange) else { continue }
guard intersection.duration < overlapper.duration else { continue }

print("Splitting an overlapping item in two")

// get all samples from overlapper up to the point of overlap
let samplesToExtract = overlapper.samples.prefix { $0.date < newItemRange.start }

// create a new item from those samples
let splitItem = overlapper is Path
? store.createPath(from: Array(samplesToExtract))
: store.createVisit(from: Array(samplesToExtract))
modifiedItems.append(splitItem)

// detach the edge to allow proper reconnect at healing time
overlapper.previousItem = nil

// copy metadata to the splitter
splitItem.copyMetadata(from: overlapper)
}

// attempt to connect up the new item
healEdges(of: newItem)

Expand All @@ -92,12 +114,6 @@ public class PersistentProcessor {
}
}

private static func createItem(from segment: ItemSegment, in store: PersistentTimelineStore) -> TimelineItem {
return segment.activityType == .stationary
? store.createVisit(from: segment.samples)
: store.createPath(from: segment.samples)
}

// MARK: - Item edge healing

public static func healEdges(of items: [TimelineItem]) {
Expand Down
1 change: 1 addition & 0 deletions LocoKit/LocalStore/TimelineSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class TimelineSegment: TransactionObserver {
}

private func update() {
guard updatingEnabled else { return }
queue.async { [weak self] in
guard self?.updatingEnabled == true else { return }
if self?.hasChanged == true {
Expand Down

0 comments on commit a56cb58

Please sign in to comment.