metatext/DB/Sources/DB/Entities/Timeline.swift

107 lines
2.9 KiB
Swift
Raw Normal View History

2020-08-18 05:13:37 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
2020-10-01 02:35:06 +00:00
import Mastodon
2020-08-18 05:13:37 +00:00
2020-08-30 23:33:11 +00:00
public enum Timeline: Hashable {
2020-08-18 05:13:37 +00:00
case home
case local
case federated
2020-09-26 03:28:50 +00:00
case list(List)
2020-08-30 19:50:34 +00:00
case tag(String)
2020-10-05 22:50:05 +00:00
case profile(accountId: Account.Id, profileCollection: ProfileCollection)
2020-12-01 03:07:38 +00:00
case favorites
2020-12-01 18:40:19 +00:00
case bookmarks
2020-08-18 05:13:37 +00:00
}
2020-08-30 23:33:11 +00:00
public extension Timeline {
2020-10-05 22:50:05 +00:00
typealias Id = String
2020-09-09 05:40:49 +00:00
static let unauthenticatedDefaults: [Timeline] = [.local, .federated]
2021-01-20 23:33:53 +00:00
static let authenticatedDefaults: [Timeline] = [.home, .local, .federated]
2020-12-01 03:07:38 +00:00
var filterContext: Filter.Context? {
switch self {
case .home, .list:
return .home
case .local, .federated, .tag:
return .public
case .profile:
return .account
2020-12-01 03:07:38 +00:00
default:
return nil
}
}
2021-01-31 15:42:44 +00:00
var ordered: Bool {
switch self {
case .favorites, .bookmarks:
return true
default:
return false
}
}
}
2020-08-29 03:50:58 +00:00
extension Timeline: Identifiable {
2020-10-05 22:50:05 +00:00
public var id: Id {
2020-08-18 05:13:37 +00:00
switch self {
case .home:
2020-08-29 03:50:58 +00:00
return "home"
2020-08-18 05:13:37 +00:00
case .local:
2020-08-29 03:50:58 +00:00
return "local"
2020-08-18 05:13:37 +00:00
case .federated:
2020-08-29 03:50:58 +00:00
return "federated"
2020-08-18 05:13:37 +00:00
case let .list(list):
2020-10-01 02:35:06 +00:00
return "list-".appending(list.id)
2020-08-30 19:50:34 +00:00
case let .tag(tag):
2020-10-01 02:35:06 +00:00
return "tag-".appending(tag).lowercased()
case let .profile(accountId, profileCollection):
return "profile-\(accountId)-\(profileCollection)"
2020-12-01 03:07:38 +00:00
case .favorites:
return "favorites"
2020-12-01 18:40:19 +00:00
case .bookmarks:
return "bookmarks"
2020-08-18 05:13:37 +00:00
}
}
}
2020-12-02 19:39:42 +00:00
extension Timeline {
init?(record: TimelineRecord) {
switch (record.id,
record.listId,
record.listTitle,
record.tag,
record.accountId,
record.profileCollection) {
case (Timeline.home.id, _, _, _, _, _):
self = .home
case (Timeline.local.id, _, _, _, _, _):
self = .local
case (Timeline.federated.id, _, _, _, _, _):
self = .federated
case (_, .some(let listId), .some(let listTitle), _, _, _):
self = .list(List(id: listId, title: listTitle))
case (_, _, _, .some(let tag), _, _):
self = .tag(tag)
case (_, _, _, _, .some(let accountId), .some(let profileCollection)):
self = .profile(accountId: accountId, profileCollection: profileCollection)
case (Timeline.favorites.id, _, _, _, _, _):
self = .favorites
case (Timeline.bookmarks.id, _, _, _, _, _):
self = .bookmarks
default:
return nil
}
}
func ephemeralityId(id: Identity.Id) -> String? {
switch self {
case .tag, .favorites, .bookmarks:
return "\(id)-\(self.id)"
default:
return nil
}
}
}