Update pinned timelines when changed remotely

This commit is contained in:
Shadowfacts 2022-12-24 12:20:13 -05:00
parent 60b182ac18
commit 3cba0bce34
2 changed files with 24 additions and 11 deletions

View File

@ -507,24 +507,28 @@ class MastodonCachePersistentStore: NSPersistentCloudKitContainer {
self.backgroundContext.performAndWait {
if let result = try? self.backgroundContext.execute(req) as? NSPersistentHistoryResult,
let transactions = result.result as? [NSPersistentHistoryTransaction] {
var changes: (hashtags: Bool, instances: Bool) = (false, false)
var changedHashtags = false
var changedInstances = false
var changedTimelinePositions: [NSManagedObjectID] = []
var changedAccountPrefs = false
outer: for transaction in transactions {
for change in transaction.changes ?? [] {
if change.changedObjectID.entity.name == "SavedHashtag" {
changes.hashtags = true
changedHashtags = true
} else if change.changedObjectID.entity.name == "SavedInstance" {
changes.instances = true
changedInstances = true
} else if change.changedObjectID.entity.name == "TimelinePosition" {
changedTimelinePositions.append(change.changedObjectID)
} else if change.changedObjectID.entity.name == "AccountPreferences" {
changedAccountPrefs = true
}
}
}
DispatchQueue.main.async {
if changes.hashtags {
if changedHashtags {
NotificationCenter.default.post(name: .savedHashtagsChanged, object: nil)
}
if changes.instances {
if changedInstances {
NotificationCenter.default.post(name: .savedInstancesChanged, object: nil)
}
for id in changedTimelinePositions {
@ -533,6 +537,9 @@ class MastodonCachePersistentStore: NSPersistentCloudKitContainer {
}
NotificationCenter.default.post(name: .timelinePositionChanged, object: timelinePosition)
}
if changedAccountPrefs {
NotificationCenter.default.post(name: .accountPreferencesChangedRemotely, object: nil)
}
}
}
}
@ -543,4 +550,5 @@ class MastodonCachePersistentStore: NSPersistentCloudKitContainer {
extension Foundation.Notification.Name {
static let timelinePositionChanged = Notification.Name("timelinePositionChanged")
static let accountPreferencesChangedRemotely = Notification.Name("accountPreferencesChangedRemotely")
}

View File

@ -9,6 +9,7 @@
import UIKit
import SwiftUI
import Pachyderm
import Combine
class TimelinesPageViewController: SegmentedPageViewController<TimelinesPageViewController.Page> {
@ -18,7 +19,7 @@ class TimelinesPageViewController: SegmentedPageViewController<TimelinesPageView
weak var mastodonController: MastodonController!
private var pinnedTimelinesObservation: NSKeyValueObservation?
private var cancellables = Set<AnyCancellable>()
init(mastodonController: MastodonController) {
self.mastodonController = mastodonController
@ -63,12 +64,16 @@ class TimelinesPageViewController: SegmentedPageViewController<TimelinesPageView
}),
]
pinnedTimelinesObservation = mastodonController.accountPreferences.observe(\.pinnedTimelinesData, changeHandler: { [unowned self] _, _ in
let pages = self.mastodonController.accountPreferences.pinnedTimelines.map {
Page(mastodonController: self.mastodonController, timeline: $0)
mastodonController.accountPreferences.publisher(for: \.pinnedTimelinesData)
.map { _ in () }
.merge(with: NotificationCenter.default.publisher(for: .accountPreferencesChangedRemotely).map { _ in () })
.sink { _ in
let pages = self.mastodonController.accountPreferences.pinnedTimelines.map {
Page(mastodonController: self.mastodonController, timeline: $0)
}
self.setPages(pages, animated: false)
}
self.setPages(pages, animated: false)
})
.store(in: &cancellables)
}
required init?(coder: NSCoder) {