Tusker/Pachyderm/TimelineSegment.swift

58 lines
1.3 KiB
Swift

//
// TimelineSegment.swift
// Pachyderm
//
// Created by Shadowfacts on 7/29/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import Foundation
public struct TimelineSegment<Type: Identifiable> {
private var ids: [String]
public init(objects: [Type]) {
self.ids = objects.map { $0.id }
}
public mutating func insertAtBeginning(ids: [String]) {
self.ids.insert(contentsOf: ids, at: 0)
}
public mutating func insertAtBeginning(objects: [Type]) {
insertAtBeginning(ids: objects.map { $0.id })
}
public mutating func append(ids: [String]) {
self.ids.append(contentsOf: ids)
}
public mutating func append(objects: [Type]) {
append(ids: objects.map { $0.id })
}
}
extension TimelineSegment: RandomAccessCollection {
public typealias Index = Int
public subscript(index: Int) -> String {
return ids[index]
}
public var startIndex: Int {
return ids.startIndex
}
public var endIndex: Int {
return ids.endIndex
}
}
// todo: remove me when i update to beta 5, Identifiable is now part of Swift stdlib
public protocol Identifiable {
var id: String { get }
}
extension Status: Identifiable {}
extension Notification: Identifiable {}