Sync timeline position using iCloud

This commit is contained in:
Shadowfacts 2022-12-23 16:35:58 -05:00
parent 619878ac85
commit 60b182ac18
9 changed files with 280 additions and 120 deletions

View File

@ -219,6 +219,7 @@
D6969EA0240C8384002843CE /* EmojiLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6969E9F240C8384002843CE /* EmojiLabel.swift */; }; D6969EA0240C8384002843CE /* EmojiLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6969E9F240C8384002843CE /* EmojiLabel.swift */; };
D6A00B1D26379FC900316AD4 /* PollOptionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A00B1C26379FC900316AD4 /* PollOptionsView.swift */; }; D6A00B1D26379FC900316AD4 /* PollOptionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A00B1C26379FC900316AD4 /* PollOptionsView.swift */; };
D6A3A380295515550036B6EF /* ProfileHeaderButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A3A37F295515550036B6EF /* ProfileHeaderButton.swift */; }; D6A3A380295515550036B6EF /* ProfileHeaderButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A3A37F295515550036B6EF /* ProfileHeaderButton.swift */; };
D6A3A3822956123A0036B6EF /* TimelinePosition.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A3A3812956123A0036B6EF /* TimelinePosition.swift */; };
D6A3BC7C232195C600FD64D5 /* ActionNotificationGroupTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A3BC7A232195C600FD64D5 /* ActionNotificationGroupTableViewCell.swift */; }; D6A3BC7C232195C600FD64D5 /* ActionNotificationGroupTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A3BC7A232195C600FD64D5 /* ActionNotificationGroupTableViewCell.swift */; };
D6A3BC7D232195C600FD64D5 /* ActionNotificationGroupTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D6A3BC7B232195C600FD64D5 /* ActionNotificationGroupTableViewCell.xib */; }; D6A3BC7D232195C600FD64D5 /* ActionNotificationGroupTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D6A3BC7B232195C600FD64D5 /* ActionNotificationGroupTableViewCell.xib */; };
D6A3BC802321B7E600FD64D5 /* FollowNotificationGroupTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A3BC7E2321B7E600FD64D5 /* FollowNotificationGroupTableViewCell.swift */; }; D6A3BC802321B7E600FD64D5 /* FollowNotificationGroupTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A3BC7E2321B7E600FD64D5 /* FollowNotificationGroupTableViewCell.swift */; };
@ -602,6 +603,7 @@
D6969E9F240C8384002843CE /* EmojiLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiLabel.swift; sourceTree = "<group>"; }; D6969E9F240C8384002843CE /* EmojiLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiLabel.swift; sourceTree = "<group>"; };
D6A00B1C26379FC900316AD4 /* PollOptionsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PollOptionsView.swift; sourceTree = "<group>"; }; D6A00B1C26379FC900316AD4 /* PollOptionsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PollOptionsView.swift; sourceTree = "<group>"; };
D6A3A37F295515550036B6EF /* ProfileHeaderButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileHeaderButton.swift; sourceTree = "<group>"; }; D6A3A37F295515550036B6EF /* ProfileHeaderButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileHeaderButton.swift; sourceTree = "<group>"; };
D6A3A3812956123A0036B6EF /* TimelinePosition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelinePosition.swift; sourceTree = "<group>"; };
D6A3BC7A232195C600FD64D5 /* ActionNotificationGroupTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionNotificationGroupTableViewCell.swift; sourceTree = "<group>"; }; D6A3BC7A232195C600FD64D5 /* ActionNotificationGroupTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionNotificationGroupTableViewCell.swift; sourceTree = "<group>"; };
D6A3BC7B232195C600FD64D5 /* ActionNotificationGroupTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ActionNotificationGroupTableViewCell.xib; sourceTree = "<group>"; }; D6A3BC7B232195C600FD64D5 /* ActionNotificationGroupTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ActionNotificationGroupTableViewCell.xib; sourceTree = "<group>"; };
D6A3BC7E2321B7E600FD64D5 /* FollowNotificationGroupTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowNotificationGroupTableViewCell.swift; sourceTree = "<group>"; }; D6A3BC7E2321B7E600FD64D5 /* FollowNotificationGroupTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowNotificationGroupTableViewCell.swift; sourceTree = "<group>"; };
@ -916,6 +918,7 @@
D61F759A29384F9C00C0B37F /* FilterMO.swift */, D61F759A29384F9C00C0B37F /* FilterMO.swift */,
D61F75AA293AF11400C0B37F /* FilterKeywordMO.swift */, D61F75AA293AF11400C0B37F /* FilterKeywordMO.swift */,
D6D706A62948D4D0000827ED /* TimlineState.swift */, D6D706A62948D4D0000827ED /* TimlineState.swift */,
D6A3A3812956123A0036B6EF /* TimelinePosition.swift */,
D68A76E229524D2A001DA1B3 /* ListMO.swift */, D68A76E229524D2A001DA1B3 /* ListMO.swift */,
D68A76D929511CA6001DA1B3 /* AccountPreferences.swift */, D68A76D929511CA6001DA1B3 /* AccountPreferences.swift */,
D60E2F2D244248BF005F8713 /* MastodonCachePersistentStore.swift */, D60E2F2D244248BF005F8713 /* MastodonCachePersistentStore.swift */,
@ -1984,6 +1987,7 @@
D61F75B7293C119700C0B37F /* Filterer.swift in Sources */, D61F75B7293C119700C0B37F /* Filterer.swift in Sources */,
D64AAE9526C88C5000FC57FB /* ToastableViewController.swift in Sources */, D64AAE9526C88C5000FC57FB /* ToastableViewController.swift in Sources */,
D6895DE928D962C2006341DA /* TimelineLikeController.swift in Sources */, D6895DE928D962C2006341DA /* TimelineLikeController.swift in Sources */,
D6A3A3822956123A0036B6EF /* TimelinePosition.swift in Sources */,
D6E0DC8E216EDF1E00369478 /* Previewing.swift in Sources */, D6E0DC8E216EDF1E00369478 /* Previewing.swift in Sources */,
D6B93667281D937300237D0E /* MainSidebarMyProfileCollectionViewCell.swift in Sources */, D6B93667281D937300237D0E /* MainSidebarMyProfileCollectionViewCell.swift in Sources */,
D61F75BD293D099600C0B37F /* Lazy.swift in Sources */, D61F75BD293D099600C0B37F /* Lazy.swift in Sources */,

View File

@ -18,6 +18,8 @@ fileprivate let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, catego
class MastodonCachePersistentStore: NSPersistentCloudKitContainer { class MastodonCachePersistentStore: NSPersistentCloudKitContainer {
private let accountInfo: LocalData.UserAccountInfo?
private static let managedObjectModel: NSManagedObjectModel = { private static let managedObjectModel: NSManagedObjectModel = {
let url = Bundle.main.url(forResource: "Tusker", withExtension: "momd")! let url = Bundle.main.url(forResource: "Tusker", withExtension: "momd")!
return NSManagedObjectModel(contentsOf: url)! return NSManagedObjectModel(contentsOf: url)!
@ -50,6 +52,8 @@ class MastodonCachePersistentStore: NSPersistentCloudKitContainer {
let relationshipSubject = PassthroughSubject<String, Never>() let relationshipSubject = PassthroughSubject<String, Never>()
init(for accountInfo: LocalData.UserAccountInfo?, transient: Bool = false) { init(for accountInfo: LocalData.UserAccountInfo?, transient: Bool = false) {
self.accountInfo = accountInfo
let group = DispatchGroup() let group = DispatchGroup()
var instancesToMigrate: [URL]? = nil var instancesToMigrate: [URL]? = nil
var hashtagsToMigrate: [Hashtag]? = nil var hashtagsToMigrate: [Hashtag]? = nil
@ -153,12 +157,17 @@ class MastodonCachePersistentStore: NSPersistentCloudKitContainer {
} }
if description.configuration == "Cloud" { if description.configuration == "Cloud" {
self.backgroundContext.perform { let context = self.backgroundContext
context.perform {
instancesToMigrate?.forEach({ url in instancesToMigrate?.forEach({ url in
_ = SavedInstance(url: url, account: accountInfo!, context: self.backgroundContext) if !context.objectExists(for: SavedInstance.fetchRequest(url: url, account: accountInfo!)) {
_ = SavedInstance(url: url, account: accountInfo!, context: self.backgroundContext)
}
}) })
hashtagsToMigrate?.forEach({ hashtag in hashtagsToMigrate?.forEach({ hashtag in
_ = SavedHashtag(hashtag: hashtag, account: accountInfo!, context: self.backgroundContext) if !context.objectExists(for: SavedHashtag.fetchRequest(name: hashtag.name, account: accountInfo!)) {
_ = SavedHashtag(hashtag: hashtag, account: accountInfo!, context: self.backgroundContext)
}
}) })
self.save(context: self.backgroundContext) self.save(context: self.backgroundContext)
} }
@ -435,9 +444,12 @@ class MastodonCachePersistentStore: NSPersistentCloudKitContainer {
} }
} }
func getTimelineState(timeline: Timeline) -> TimelineState? { func getTimelinePosition(timeline: Timeline) -> TimelinePosition? {
guard let accountInfo else {
return nil
}
do { do {
let req = TimelineState.fetchRequest(timeline: timeline) let req = TimelinePosition.fetchRequest(timeline: timeline, account: accountInfo)
return try viewContext.fetch(req).first return try viewContext.fetch(req).first
} catch { } catch {
return nil return nil
@ -483,7 +495,6 @@ class MastodonCachePersistentStore: NSPersistentCloudKitContainer {
return changes return changes
} }
// the remote change notifications only handle deletes, inserts get handled by the regular managed object did change notifications
@objc private func remoteChanges(_ notification: Foundation.Notification) { @objc private func remoteChanges(_ notification: Foundation.Notification) {
guard let token = notification.userInfo?[NSPersistentHistoryTokenKey] as? NSPersistentHistoryToken else { guard let token = notification.userInfo?[NSPersistentHistoryTokenKey] as? NSPersistentHistoryToken else {
return return
@ -497,27 +508,31 @@ class MastodonCachePersistentStore: NSPersistentCloudKitContainer {
if let result = try? self.backgroundContext.execute(req) as? NSPersistentHistoryResult, if let result = try? self.backgroundContext.execute(req) as? NSPersistentHistoryResult,
let transactions = result.result as? [NSPersistentHistoryTransaction] { let transactions = result.result as? [NSPersistentHistoryTransaction] {
var changes: (hashtags: Bool, instances: Bool) = (false, false) var changes: (hashtags: Bool, instances: Bool) = (false, false)
var changedTimelinePositions: [NSManagedObjectID] = []
outer: for transaction in transactions { outer: for transaction in transactions {
for change in transaction.changes ?? [] { for change in transaction.changes ?? [] {
if change.changedObjectID.entity.name == "SavedHashtag" { if change.changedObjectID.entity.name == "SavedHashtag" {
changes.hashtags = true changes.hashtags = true
} else if change.changedObjectID.entity.name == "SavedInstance" { } else if change.changedObjectID.entity.name == "SavedInstance" {
changes.instances = true changes.instances = true
} } else if change.changedObjectID.entity.name == "TimelinePosition" {
if changes.hashtags && changes.instances { changedTimelinePositions.append(change.changedObjectID)
break outer
} }
} }
} }
if changes.hashtags { DispatchQueue.main.async {
DispatchQueue.main.async { if changes.hashtags {
NotificationCenter.default.post(name: .savedHashtagsChanged, object: nil) NotificationCenter.default.post(name: .savedHashtagsChanged, object: nil)
} }
} if changes.instances {
if changes.instances {
DispatchQueue.main.async {
NotificationCenter.default.post(name: .savedInstancesChanged, object: nil) NotificationCenter.default.post(name: .savedInstancesChanged, object: nil)
} }
for id in changedTimelinePositions {
guard let timelinePosition = try? self.viewContext.existingObject(with: id) as? TimelinePosition else {
continue
}
NotificationCenter.default.post(name: .timelinePositionChanged, object: timelinePosition)
}
} }
} }
} }
@ -525,3 +540,7 @@ class MastodonCachePersistentStore: NSPersistentCloudKitContainer {
} }
} }
extension Foundation.Notification.Name {
static let timelinePositionChanged = Notification.Name("timelinePositionChanged")
}

View File

@ -0,0 +1,83 @@
//
// TimelinePosition.swift
// Tusker
//
// Created by Shadowfacts on 12/23/22.
// Copyright © 2022 Shadowfacts. All rights reserved.
//
import Foundation
import CoreData
import Pachyderm
@objc(TimelinePosition)
public final class TimelinePosition: NSManagedObject {
@nonobjc class func fetchRequest(timeline: Timeline, account: LocalData.UserAccountInfo) -> NSFetchRequest<TimelinePosition> {
let req = NSFetchRequest<TimelinePosition>(entityName: "TimelinePosition")
req.predicate = NSPredicate(format: "accountID = %@ AND timelineKind = %@", account.id, toTimelineKind(timeline))
return req
}
@NSManaged public var accountID: String
@NSManaged private var timelineKind: String
@NSManaged public var centerStatusID: String?
@NSManaged private var statusIDsData: Data?
@LazilyDecoding(arrayFrom: \TimelinePosition.statusIDsData)
var statusIDs: [String]
var timeline: Timeline {
get { fromTimelineKind(timelineKind) }
set { timelineKind = toTimelineKind(newValue) }
}
convenience init(timeline: Timeline, account: LocalData.UserAccountInfo, context: NSManagedObjectContext) {
self.init(context: context)
self.timeline = timeline
self.accountID = account.id
}
}
// blergh, this is the simplest way of getting the Timeline into a format that A) CoreData can handle and B) is usable in the predicate
func toTimelineKind(_ timeline: Timeline) -> String {
switch timeline {
case .home:
return "home"
case .public(local: true):
return "local"
case .public(local: false):
return "federated"
case .direct:
return "direct"
case .tag(hashtag: let name):
return "hashtag:\(name)"
case .list(id: let id):
return "list:\(id)"
}
}
func fromTimelineKind(_ kind: String) -> Timeline {
if kind == "home" {
return .home
} else if kind == "local" {
return .public(local: true)
} else if kind == "federated" {
return .public(local: false)
} else if kind == "direct" {
return .direct
} else if kind.starts(with: "hashtag:") {
return .tag(hashtag: String(trimmingPrefix("hashtag:", of: kind)))
} else if kind.starts(with: "list:") {
return .list(id: String(trimmingPrefix("list:", of: kind)))
} else {
fatalError("invalid timeline kind \(kind)")
}
}
// replace with Collection.trimmingPrefix
@available(iOS, obsoleted: 16.0)
private func trimmingPrefix(_ prefix: String, of str: String) -> Substring {
return str[str.index(str.startIndex, offsetBy: prefix.count)...]
}

View File

@ -13,10 +13,8 @@ import Pachyderm
@objc(TimelineState) @objc(TimelineState)
public final class TimelineState: NSManagedObject { public final class TimelineState: NSManagedObject {
@nonobjc public class func fetchRequest(timeline: Timeline) -> NSFetchRequest<TimelineState> { @nonobjc public class func fetchRequest() -> NSFetchRequest<TimelineState> {
let req = NSFetchRequest<TimelineState>(entityName: "TimelineState") return NSFetchRequest<TimelineState>(entityName: "TimelineState")
req.predicate = NSPredicate(format: "timelineKind = %@", toTimelineKind(timeline))
return req
} }
@NSManaged private var timelineKind: String @NSManaged private var timelineKind: String
@ -25,65 +23,10 @@ public final class TimelineState: NSManagedObject {
var timeline: Timeline { var timeline: Timeline {
get { fromTimelineKind(timelineKind) } get { fromTimelineKind(timelineKind) }
set { timelineKind = toTimelineKind(newValue) }
} }
var statusMOs: [StatusMO] { var statusMOs: [StatusMO] {
statuses.array as! [StatusMO] statuses.array as! [StatusMO]
} }
convenience init(timeline: Timeline, context: NSManagedObjectContext) {
self.init(context: context)
self.timeline = timeline
}
func setStatuses(_ statusIDs: [String]) {
let context = managedObjectContext!
// todo: this feels really inefficient, but I'm not sure if it's better or worse than doing a single "id IN %@" fetch and sorting after
let mos = statusIDs.compactMap { try? context.fetch(StatusMO.fetchRequest(id: $0)).first }
self.statuses = NSOrderedSet(array: mos)
}
}
// blergh, this is the simplest way of getting the Timeline into a format that A) CoreData can handle and B) is usable in the predicate
private func toTimelineKind(_ timeline: Timeline) -> String {
switch timeline {
case .home:
return "home"
case .public(local: true):
return "local"
case .public(local: false):
return "federated"
case .direct:
return "direct"
case .tag(hashtag: let name):
return "hashtag:\(name)"
case .list(id: let id):
return "list:\(id)"
}
}
private func fromTimelineKind(_ kind: String) -> Timeline {
if kind == "home" {
return .home
} else if kind == "local" {
return .public(local: true)
} else if kind == "federated" {
return .public(local: false)
} else if kind == "direct" {
return .direct
} else if kind.starts(with: "hashtag:") {
return .tag(hashtag: String(trimmingPrefix("hashtag:", of: kind)))
} else if kind.starts(with: "list:") {
return .list(id: String(trimmingPrefix("list:", of: kind)))
} else {
fatalError("invalid timeline kind \(kind)")
}
}
// replace with Collection.trimmingPrefix
@available(iOS, obsoleted: 16.0)
private func trimmingPrefix(_ prefix: String, of str: String) -> Substring {
return str[str.index(str.startIndex, offsetBy: prefix.count)...]
} }

View File

@ -117,6 +117,12 @@
</uniquenessConstraint> </uniquenessConstraint>
</uniquenessConstraints> </uniquenessConstraints>
</entity> </entity>
<entity name="TimelinePosition" representedClassName="TimelinePosition" syncable="YES">
<attribute name="accountID" optional="YES" attributeType="String"/>
<attribute name="centerStatusID" optional="YES" attributeType="String"/>
<attribute name="statusIDsData" optional="YES" attributeType="Binary" valueTransformerName="TimelinePositionStatusIDsTransformer"/>
<attribute name="timelineKind" optional="YES" attributeType="String"/>
</entity>
<entity name="TimelineState" representedClassName="TimelineState" syncable="YES"> <entity name="TimelineState" representedClassName="TimelineState" syncable="YES">
<attribute name="centerStatusID" optional="YES" attributeType="String"/> <attribute name="centerStatusID" optional="YES" attributeType="String"/>
<attribute name="timelineKind" attributeType="String" valueTransformerName="pachydermTimeline" customClassName="Tusker.TimelineContainer"/> <attribute name="timelineKind" attributeType="String" valueTransformerName="pachydermTimeline" customClassName="Tusker.TimelineContainer"/>
@ -126,6 +132,7 @@
<memberEntity name="SavedHashtag"/> <memberEntity name="SavedHashtag"/>
<memberEntity name="SavedInstance"/> <memberEntity name="SavedInstance"/>
<memberEntity name="AccountPreferences"/> <memberEntity name="AccountPreferences"/>
<memberEntity name="TimelinePosition"/>
</configuration> </configuration>
<configuration name="Local"> <configuration name="Local">
<memberEntity name="Account"/> <memberEntity name="Account"/>

View File

@ -131,7 +131,7 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate, TuskerSceneDelegate
minDate.addTimeInterval(-7 * 24 * 60 * 60) minDate.addTimeInterval(-7 * 24 * 60 * 60)
let statusReq: NSFetchRequest<NSFetchRequestResult> = StatusMO.fetchRequest() let statusReq: NSFetchRequest<NSFetchRequestResult> = StatusMO.fetchRequest()
statusReq.predicate = NSPredicate(format: "((lastFetchedAt = nil) OR (lastFetchedAt < %@)) AND (timelines.@count = 0)", minDate as NSDate) statusReq.predicate = NSPredicate(format: "(lastFetchedAt = nil) OR (lastFetchedAt < %@)", minDate as NSDate)
let deleteStatusReq = NSBatchDeleteRequest(fetchRequest: statusReq) let deleteStatusReq = NSBatchDeleteRequest(fetchRequest: statusReq)
deleteStatusReq.resultType = .resultTypeCount deleteStatusReq.resultType = .resultTypeCount
if let res = try? context.execute(deleteStatusReq) as? NSBatchDeleteResult { if let res = try? context.execute(deleteStatusReq) as? NSBatchDeleteResult {

View File

@ -25,6 +25,7 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
private(set) var collectionView: UICollectionView! private(set) var collectionView: UICollectionView!
private(set) var dataSource: UICollectionViewDiffableDataSource<Section, Item>! private(set) var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
private var cancellables = Set<AnyCancellable>()
private var contentOffsetObservation: NSKeyValueObservation? private var contentOffsetObservation: NSKeyValueObservation?
private var activityToRestore: NSUserActivity? private var activityToRestore: NSUserActivity?
// the last time this VC disappeared or the scene was backgrounded while it was active, used to decide if we want to check for present when reappearing // the last time this VC disappeared or the scene was backgrounded while it was active, used to decide if we want to check for present when reappearing
@ -121,6 +122,21 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
NotificationCenter.default.addObserver(self, selector: #selector(sceneWillEnterForeground), name: UIScene.willEnterForegroundNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(sceneWillEnterForeground), name: UIScene.willEnterForegroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(sceneDidEnterBackground), name: UIScene.didEnterBackgroundNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(sceneDidEnterBackground), name: UIScene.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.publisher(for: .timelinePositionChanged)
.filter { [unowned self] in
if let timelinePosition = $0.object as? TimelinePosition,
timelinePosition.accountID == self.mastodonController.accountInfo?.id,
timelinePosition.timeline == self.timeline {
return true
} else {
return false
}
}
.debounce(for: .milliseconds(500), scheduler: DispatchQueue.main)
.sink { [unowned self] _ in
_ = promptToSyncPositionIfNecessary()
}
.store(in: &cancellables)
} }
// separate method because InstanceTimelineViewController needs to be able to customize it // separate method because InstanceTimelineViewController needs to be able to customize it
@ -209,7 +225,11 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
} }
} }
} else { } else {
checkPresentIfEnoughTimeElapsed() if promptToSyncPositionIfNecessary() {
// no-op
} else {
checkPresentIfEnoughTimeElapsed()
}
} }
} }
@ -233,19 +253,29 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
saveState() saveState()
} }
private func saveState() { private func currentCenterVisibleIndexPath(snapshot: NSDiffableDataSourceSnapshot<Section, Item>?) -> IndexPath? {
guard isViewLoaded, let snapshot = snapshot ?? dataSource.snapshot()
persistsState else {
return
}
let visible = collectionView.indexPathsForVisibleItems.sorted() let visible = collectionView.indexPathsForVisibleItems.sorted()
let snapshot = dataSource.snapshot()
let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size) let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
let midPoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY) let midPoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
guard !visible.isEmpty, guard !visible.isEmpty,
let statusesSection = snapshot.sectionIdentifiers.firstIndex(of: .statuses), let statusesSection = snapshot.sectionIdentifiers.firstIndex(of: .statuses),
let rawCenterVisible = collectionView.indexPathForItem(at: midPoint), let rawCenterVisible = collectionView.indexPathForItem(at: midPoint),
let centerVisible = visible.first(where: { $0.section == statusesSection && $0 >= rawCenterVisible }) else { let centerVisible = visible.first(where: { $0.section == statusesSection && $0 >= rawCenterVisible }) else {
return nil
}
return centerVisible
}
private func saveState() {
guard isViewLoaded,
persistsState,
let accountInfo = mastodonController.accountInfo else {
return
}
let snapshot = dataSource.snapshot()
guard let centerVisible = currentCenterVisibleIndexPath(snapshot: snapshot) else {
return return
} }
let allItems = snapshot.itemIdentifiers(inSection: .statuses) let allItems = snapshot.itemIdentifiers(inSection: .statuses)
@ -288,10 +318,11 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
} }
stateRestorationLogger.debug("TimelineViewController: saving state to persistent store with with centerID \(centerVisibleID)") stateRestorationLogger.debug("TimelineViewController: saving state to persistent store with with centerID \(centerVisibleID)")
let state = mastodonController.persistentContainer.getTimelineState(timeline: timeline) ?? TimelineState(timeline: timeline, context: mastodonController.persistentContainer.viewContext) let context = mastodonController.persistentContainer.viewContext
state.setStatuses(ids) let position = mastodonController.persistentContainer.getTimelinePosition(timeline: timeline) ?? TimelinePosition(timeline: timeline, account: accountInfo, context: context)
state.centerStatusID = centerVisibleID position.statusIDs = ids
mastodonController.persistentContainer.save(context: mastodonController.persistentContainer.viewContext) position.centerStatusID = centerVisibleID
mastodonController.persistentContainer.save(context: context)
} }
func stateRestorationActivity() -> NSUserActivity? { func stateRestorationActivity() -> NSUserActivity? {
@ -304,45 +335,68 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
return activity return activity
} }
private func restoreState() -> Bool { func restoreState() -> Bool {
guard persistsState, guard persistsState,
Preferences.shared.timelineStateRestoration, Preferences.shared.timelineStateRestoration,
let state = mastodonController.persistentContainer.getTimelineState(timeline: timeline) else { let position = mastodonController.persistentContainer.getTimelinePosition(timeline: timeline) else {
return false return false
} }
let statusIDs = state.statusMOs.map(\.id)
loadViewIfNeeded() loadViewIfNeeded()
controller.restoreInitial { Task {
var snapshot = dataSource.snapshot() await controller.restoreInitial {
snapshot.appendSections([.statuses]) await loadStatusesToRestore(position: position)
let items = statusIDs.map { Item.status(id: $0, collapseState: .unknown, filterState: .unknown) } applyItemsToRestore(position: position)
snapshot.appendItems(items, toSection: .statuses)
dataSource.apply(snapshot, animatingDifferences: false) {
if let centerID = state.centerStatusID,
let index = statusIDs.firstIndex(of: centerID),
let indexPath = self.dataSource.indexPath(for: items[index]) {
// it sometimes takes multiple attempts to convert on the right scroll position
// since we're dealing with a bunch of unmeasured cells, so just try a few times in a loop
var count = 0
while count < 5 {
count += 1
let origOffset = self.collectionView.contentOffset
self.collectionView.layoutIfNeeded()
self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: false)
let newOffset = self.collectionView.contentOffset
if abs(origOffset.y - newOffset.y) <= 1 {
break
}
}
stateRestorationLogger.fault("TimelineViewController: restored statuses with center ID \(centerID)")
} else {
stateRestorationLogger.fault("TimelineViewController: restored statuses, but couldn't find center ID")
}
} }
} }
return true return true
} }
@MainActor
private func loadStatusesToRestore(position: TimelinePosition) async {
let unloaded = position.statusIDs.filter({ mastodonController.persistentContainer.status(for: $0) == nil })
guard !unloaded.isEmpty else {
return
}
await withTaskGroup(of: Void.self) { group in
for id in unloaded {
group.addTask { @MainActor in
if let (status, _) = try? await self.mastodonController.run(Client.getStatus(id: id)) {
self.mastodonController.persistentContainer.addOrUpdate(status: status)
}
}
}
}
}
private func applyItemsToRestore(position: TimelinePosition) {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.statuses])
let items = position.statusIDs.map { Item.status(id: $0, collapseState: .unknown, filterState: .unknown) }
snapshot.appendItems(items, toSection: .statuses)
dataSource.apply(snapshot, animatingDifferences: false) {
if let centerID = position.centerStatusID,
let index = position.statusIDs.firstIndex(of: centerID),
let indexPath = self.dataSource.indexPath(for: items[index]) {
// it sometimes takes multiple attempts to convert on the right scroll position
// since we're dealing with a bunch of unmeasured cells, so just try a few times in a loop
var count = 0
while count < 5 {
count += 1
let origOffset = self.collectionView.contentOffset
self.collectionView.layoutIfNeeded()
self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: false)
let newOffset = self.collectionView.contentOffset
if abs(origOffset.y - newOffset.y) <= 1 {
break
}
}
stateRestorationLogger.fault("TimelineViewController: restored statuses with center ID \(centerID)")
} else {
stateRestorationLogger.fault("TimelineViewController: restored statuses, but couldn't find center ID")
}
}
}
private func removeTimelineDescriptionCell() { private func removeTimelineDescriptionCell() {
var snapshot = dataSource.snapshot() var snapshot = dataSource.snapshot()
snapshot.deleteItems([.publicTimelineDescription]) snapshot.deleteItems([.publicTimelineDescription])
@ -394,7 +448,11 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
view.window?.windowScene == scene else { view.window?.windowScene == scene else {
return return
} }
checkPresentIfEnoughTimeElapsed() if promptToSyncPositionIfNecessary() {
// no-op
} else {
checkPresentIfEnoughTimeElapsed()
}
} }
@objc private func sceneDidEnterBackground(_ notification: Foundation.Notification) { @objc private func sceneDidEnterBackground(_ notification: Foundation.Notification) {
@ -407,6 +465,45 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
saveState() saveState()
} }
private func promptToSyncPositionIfNecessary() -> Bool {
guard persistsState,
let timelinePosition = mastodonController.persistentContainer.getTimelinePosition(timeline: timeline) else {
return false
}
let snapshot = dataSource.snapshot()
guard let centerVisible = currentCenterVisibleIndexPath(snapshot: snapshot),
snapshot.sectionIdentifiers.contains(.statuses) else {
return false
}
let statusesSection = snapshot.itemIdentifiers(inSection: .statuses)
let centerVisibleStatusID: String
switch statusesSection[centerVisible.row] {
case .gap:
guard case .status(let id, _, _) = statusesSection[centerVisible.row + 1] else {
fatalError()
}
centerVisibleStatusID = id
case .status(let id, _, _):
centerVisibleStatusID = id
default:
fatalError()
}
guard timelinePosition.centerStatusID != centerVisibleStatusID else {
return false
}
var config = ToastConfiguration(title: "Sync Position")
config.edge = .top
config.dismissAutomaticallyAfter = 5
config.systemImageName = "arrow.triangle.2.circlepath"
config.action = { [unowned self] toast in
toast.dismissToast(animated: true)
_ = self.restoreState()
}
showToast(configuration: config, animated: true)
UIAccessibility.post(notification: .announcement, argument: "Synced Position Updated")
return true
}
@objc func refresh() { @objc func refresh() {
Task { Task {
if case .notLoadedInitial = controller.state { if case .notLoadedInitial = controller.state {

View File

@ -53,7 +53,14 @@ class TimelinesPageViewController: SegmentedPageViewController<TimelinesPageView
await vc.checkPresent(jumpImmediately: true) await vc.checkPresent(jumpImmediately: true)
} }
return true return true
}) }),
UIAccessibilityCustomAction(name: "Jump to Sync Position", actionHandler: { [unowned self] _ in
guard let vc = currentViewController as? TimelineViewController else {
return false
}
_ = vc.restoreState()
return true
}),
] ]
pinnedTimelinesObservation = mastodonController.accountPreferences.observe(\.pinnedTimelinesData, changeHandler: { [unowned self] _, _ in pinnedTimelinesObservation = mastodonController.accountPreferences.observe(\.pinnedTimelinesData, changeHandler: { [unowned self] _, _ in

View File

@ -80,12 +80,12 @@ class TimelineLikeController<Item> {
} }
/// Used to indicate to the controller that the initial set of posts have been restored externally. /// Used to indicate to the controller that the initial set of posts have been restored externally.
func restoreInitial(doRestore: () -> Void) { func restoreInitial(doRestore: () async -> Void) async {
guard state == .notLoadedInitial else { guard state == .notLoadedInitial || state == .idle else {
return return
} }
state = .restoringInitial state = .restoringInitial
doRestore() await doRestore()
state = .idle state = .idle
} }
@ -234,7 +234,7 @@ class TimelineLikeController<Item> {
} }
case .idle: case .idle:
switch to { switch to {
case .loadingInitial(_, _), .loadingNewer(_), .loadingOlder(_, _), .loadingGap(_, _): case .restoringInitial, .loadingInitial(_, _), .loadingNewer(_), .loadingOlder(_, _), .loadingGap(_, _):
return true return true
default: default:
return false return false