Skip to content

Commit

Permalink
Snapshotting settings through postprocessing and sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ptsochantaris committed Aug 21, 2023
1 parent 88090bf commit dc2cc8d
Show file tree
Hide file tree
Showing 34 changed files with 480 additions and 502 deletions.
2 changes: 1 addition & 1 deletion PocketTrailer/AdvancedSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ final class AdvancedSettingsViewController: UITableViewController, PickerViewCon
}

settingsChangedTimer = PopTimer(timeInterval: 1.0) { @MainActor in
await DataManager.postProcessAllItems(in: DataManager.main)
await DataManager.postProcessAllItems(in: DataManager.main, settings: Settings.cache)
}

importExport = ImportExport(parent: self)
Expand Down
2 changes: 1 addition & 1 deletion PocketTrailer/CommentBlacklistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ final class CommentBlacklistViewController: UITableViewController {
Settings.itemAuthorBlacklist = newList
}
Task {
await DataManager.postProcessAllItems(in: DataManager.main)
await DataManager.postProcessAllItems(in: DataManager.main, settings: Settings.cache)
}
}

Expand Down
55 changes: 30 additions & 25 deletions PocketTrailer/MasterViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ final class TabBarSet {
var tabItems: [UITabBarItem] {
let label = viewCriterion?.label
var items = [UITabBarItem]()

let settings = Settings.cache

let prf = ListableItem.requestForItems(of: PullRequest.self, withFilter: nil, sectionIndex: -1, criterion: viewCriterion)
let prf = ListableItem.requestForItems(of: PullRequest.self, withFilter: nil, sectionIndex: -1, criterion: viewCriterion, settings: settings)
if try! DataManager.main.count(for: prf) > 0 {
let i = UITabBarItem(title: label ?? "Pull Requests", image: UIImage(named: "prsTab"), selectedImage: nil)
let prUnreadCount = PullRequest.badgeCount(in: DataManager.main, criterion: viewCriterion)
let prUnreadCount = PullRequest.badgeCount(in: DataManager.main, criterion: viewCriterion, settings: settings)
i.badgeValue = prUnreadCount > 0 ? "\(prUnreadCount)" : nil
items.append(i)
prItem = i
}
let isf = ListableItem.requestForItems(of: Issue.self, withFilter: nil, sectionIndex: -1, criterion: viewCriterion)
let isf = ListableItem.requestForItems(of: Issue.self, withFilter: nil, sectionIndex: -1, criterion: viewCriterion, settings: settings)
if try! DataManager.main.count(for: isf) > 0 {
let i = UITabBarItem(title: label ?? "Issues", image: UIImage(named: "issuesTab"), selectedImage: nil)
let issuesUnreadCount = Issue.badgeCount(in: DataManager.main, criterion: viewCriterion)
let issuesUnreadCount = Issue.badgeCount(in: DataManager.main, criterion: viewCriterion, settings: settings)
i.badgeValue = issuesUnreadCount > 0 ? "\(issuesUnreadCount)" : nil
items.append(i)
issuesItem = i
Expand Down Expand Up @@ -74,7 +76,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
})
if (tabs.items?.count ?? 0) > 1 {
a.addAction(UIAlertAction(title: "On Other Tabs Too", style: .destructive) { _ in
app.markEverythingRead()
app.markEverythingRead(settings: Settings.cache)
})
}
present(a, animated: true)
Expand Down Expand Up @@ -132,8 +134,9 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
}

private func markAllAsRead() {
let settings = Settings.cache
for i in fetchedResultsController?.fetchedObjects ?? [] {
i.catchUpWithComments()
i.catchUpWithComments(settings: settings)
}
}

Expand Down Expand Up @@ -336,7 +339,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
}
if i.isSnoozing {
if canIssueKeyForIndexPath(action: .wake(date: i.snoozeUntil), indexPath: ip) {
i.wakeUp()
i.wakeUp(settings: Settings.cache)
}
} else {
let presets = SnoozePreset.allSnoozePresets(in: DataManager.main)
Expand Down Expand Up @@ -368,7 +371,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
if let ip = tableView.indexPathForSelectedRow, let i = fetchedResultsController?.object(at: ip) {
let isMuted = i.muted
if (!isMuted && canIssueKeyForIndexPath(action: .mute, indexPath: ip)) || (isMuted && canIssueKeyForIndexPath(action: .unmute, indexPath: ip)) {
i.setMute(to: !isMuted)
i.setMute(to: !isMuted, settings: Settings.cache)
}
}
}
Expand Down Expand Up @@ -530,7 +533,8 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
let r = Range(uncheckedBounds: (lower: 0, upper: fetchedResultsController?.sections?.count ?? 0))
let currentIndexes = IndexSet(integersIn: r)

updateQuery(newFetchRequest: itemFetchRequest)
let settings = Settings.cache
updateQuery(newFetchRequest: itemFetchRequest(settings: settings))

let r2 = Range(uncheckedBounds: (lower: 0, upper: fetchedResultsController?.sections?.count ?? 0))
let dataIndexes = IndexSet(integersIn: r2)
Expand Down Expand Up @@ -629,12 +633,13 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
viewingPrs = true
}

let settings = Settings.cache
let newFetchRequest = itemFetchRequest(settings: settings)
if fetchedResultsController == nil {
updateQuery(newFetchRequest: itemFetchRequest)
updateQuery(newFetchRequest: newFetchRequest)
tableView.reloadData()
} else {
let latestFetchRequest = fetchedResultsController?.fetchRequest
let newFetchRequest = itemFetchRequest
let newCount = tabs.items?.count ?? 0
if newCount != lastTabCount || latestFetchRequest != newFetchRequest {
updateQuery(newFetchRequest: newFetchRequest)
Expand Down Expand Up @@ -812,7 +817,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl

private func showDetail(url: URL, objectId: NSManagedObjectID) {
if let item = try? DataManager.main.existingObject(with: objectId) as? ListableItem {
item.catchUpWithComments()
item.catchUpWithComments(settings: Settings.cache)
}
UIApplication.shared.open(url, options: [:])
}
Expand Down Expand Up @@ -872,12 +877,12 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl

case .mute:
return UIAction(title: action.title, image: UIImage(systemName: "speaker.slash")) { _ in
item.setMute(to: true)
item.setMute(to: true, settings: Settings.cache)
}

case .unmute:
return UIAction(title: action.title, image: UIImage(systemName: "speaker.2")) { _ in
item.setMute(to: false)
item.setMute(to: false, settings: Settings.cache)
}

case .openRepo:
Expand All @@ -894,7 +899,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
case let .snooze(presets):
var presetItems = presets.map { preset -> UIAction in
UIAction(title: preset.listDescription) { _ in
item.snooze(using: preset)
item.snooze(using: preset, settings: Settings.cache)
}
}
presetItems.append(UIAction(title: "Configure...", image: UIImage(systemName: "gear"), identifier: nil) { _ in
Expand All @@ -904,7 +909,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl

case .wake:
return UIAction(title: action.title, image: UIImage(systemName: "sun.max")) { _ in
item.wakeUp()
item.wakeUp(settings: Settings.cache)
}
}
}
Expand Down Expand Up @@ -933,7 +938,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
animator.preferredCommitStyle = .dismiss
animator.addCompletion {
if let id = configuration.identifier as? NSManagedObjectID, let item = try? DataManager.main.existingObject(with: id) as? ListableItem, let urlString = item.urlForOpening, let url = URL(string: urlString) {
item.catchUpWithComments()
item.catchUpWithComments(settings: Settings.cache)
UIApplication.shared.open(url, options: [:])
}
}
Expand All @@ -944,7 +949,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
i = itemUri,
let oid = DataManager.id(for: i),
let o = try? DataManager.main.existingObject(with: oid) as? ListableItem {
o.catchUpWithComments()
o.catchUpWithComments(settings: Settings.cache)
}
}

Expand All @@ -954,7 +959,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
let oid = DataManager.id(for: i),
let o = try? DataManager.main.existingObject(with: oid) as? ListableItem {
o.latestReadCommentDate = .distantPast
o.postProcess()
o.postProcess(settings: Settings.cache)
}
}

Expand All @@ -966,17 +971,17 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
preferredStyle: .alert)
for preset in snoozePresets {
a.addAction(UIAlertAction(title: preset.listDescription, style: .default) { _ in
i.snooze(using: preset)
i.snooze(using: preset, settings: Settings.cache)
})
}
a.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(a, animated: true)
}

private var itemFetchRequest: NSFetchRequest<ListableItem> {
private func itemFetchRequest(settings: Settings.Cache) -> NSFetchRequest<ListableItem> {
let type: ListableItem.Type = viewingPrs ? PullRequest.self : Issue.self
let text = navigationItem.searchController?.searchBar.text
return ListableItem.requestForItems(of: type, withFilter: text, sectionIndex: -1, criterion: currentTabBarSet?.viewCriterion)
return ListableItem.requestForItems(of: type, withFilter: text, sectionIndex: -1, criterion: currentTabBarSet?.viewCriterion, settings: settings)
}

private var animatedUpdates = false
Expand Down Expand Up @@ -1049,9 +1054,9 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
private func configureCell(cell: UITableViewCell, withObject: ListableItem) {
guard let c = cell as? PRCell else { return }
if let o = withObject as? PullRequest {
c.setPullRequest(pullRequest: o)
c.setPullRequest(pullRequest: o, settings: Settings.cache)
} else if let o = withObject as? Issue {
c.setIssue(issue: o)
c.setIssue(issue: o, settings: Settings.cache)
}
}

Expand Down Expand Up @@ -1128,7 +1133,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl

func resetView(becauseOfChanges: Bool) async {
await safeScrollToTop()
updateQuery(newFetchRequest: itemFetchRequest)
updateQuery(newFetchRequest: itemFetchRequest(settings: Settings.cache))
updateStatus(becauseOfChanges: becauseOfChanges)
tableView.reloadData()
}
Expand Down
7 changes: 4 additions & 3 deletions PocketTrailer/NotificationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ final class NotificationManager: NSObject {
return
}

let settings = Settings.cache
switch action {
case "mute":
relatedItem.setMute(to: true)
relatedItem.setMute(to: true, settings: settings)
case "read":
relatedItem.catchUpWithComments()
relatedItem.catchUpWithComments(settings: settings)
default:
relatedItem.catchUpWithComments()
relatedItem.catchUpWithComments(settings: settings)

if let urlToOpen = userInfo[NOTIFICATION_URL_KEY] as? String ?? relatedComment?.webUrl ?? relatedItem.webUrl,
let u = URL(string: urlToOpen) {
Expand Down
40 changes: 20 additions & 20 deletions PocketTrailer/PRCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,20 @@ final class PRCell: UITableViewCell {

private weak var item: ListableItem?

func setPullRequest(pullRequest: PullRequest) {
func setPullRequest(pullRequest: PullRequest, settings: Settings.Cache) {
item = pullRequest

let separator = traitCollection.containsTraits(in: compactTraits) ? "\n" : " "

let detailFont = _description.font!
_title.attributedText = pullRequest.title(with: _title.font, labelFont: detailFont, titleColor: UIColor.label, numberColor: UIColor.secondaryLabel)
_title.attributedText = pullRequest.title(with: _title.font, labelFont: detailFont, titleColor: UIColor.label, numberColor: UIColor.secondaryLabel, settings: settings)

let l = pullRequest.labelsAttributedString(labelFont: _labels.font)
let l = pullRequest.labelsAttributedString(labelFont: _labels.font, settings: settings)
_labels.attributedText = l
_labels.isHidden = (l?.length ?? 0) == 0

let sub = pullRequest.subtitle(with: detailFont, lightColor: UIColor.secondaryLabel, darkColor: UIColor.label, separator: separator)
let r = pullRequest.reviewsAttributedString(labelFont: detailFont)
let sub = pullRequest.subtitle(with: detailFont, lightColor: UIColor.secondaryLabel, darkColor: UIColor.label, separator: separator, settings: settings)
let r = pullRequest.reviewsAttributedString(labelFont: detailFont, settings: settings)
if let r, r.length > 0 {
let s = NSMutableAttributedString(attributedString: r)
s.append(NSAttributedString(string: "\n"))
Expand All @@ -109,12 +109,12 @@ final class PRCell: UITableViewCell {
}

let muted = pullRequest.muted
setCountsImageAndFade(item: pullRequest, muted: muted)
setCountsImageAndFade(item: pullRequest, muted: muted, settings: settings)

var statusText: NSMutableAttributedString?
var totalStatuses = 0
if pullRequest.section.shouldListStatuses {
let statusItems = pullRequest.displayedStatusLines
if pullRequest.section.shouldListStatuses(settings: settings) {
let statusItems = pullRequest.displayedStatusLines(settings: settings)
var statusCount = statusItems.count
totalStatuses = statusCount
var lineAttributes = statusAttributes
Expand All @@ -133,51 +133,51 @@ final class PRCell: UITableViewCell {
_statuses.isHidden = totalStatuses == 0

if totalStatuses > 0, let statusString = statusText?.string {
var title = pullRequest.accessibleTitle
var title = pullRequest.accessibleTitle(settings: settings)
if muted {
title = "(Muted) - \(title)"
}
accessibilityLabel = "\(title), \(unreadCount.text.orEmpty) unread comments, \(readCount.text.orEmpty) total comments, \(pullRequest.accessibleSubtitle). \(totalStatuses) statuses: \(statusString)"
accessibilityLabel = "\(title), \(unreadCount.text.orEmpty) unread comments, \(readCount.text.orEmpty) total comments, \(pullRequest.accessibleSubtitle(settings: settings)). \(totalStatuses) statuses: \(statusString)"
} else {
accessibilityLabel = "\(pullRequest.accessibleTitle), \(unreadCount.text.orEmpty) unread comments, \(readCount.text.orEmpty) total comments, \(pullRequest.accessibleSubtitle)"
accessibilityLabel = "\(pullRequest.accessibleTitle(settings: settings)), \(unreadCount.text.orEmpty) unread comments, \(readCount.text.orEmpty) total comments, \(pullRequest.accessibleSubtitle(settings: settings))"
}
}

func setIssue(issue: Issue) {
func setIssue(issue: Issue, settings: Settings.Cache) {
item = issue

let separator = traitCollection.containsTraits(in: compactTraits) ? "\n" : " "

let detailFont = _description.font!
_title.attributedText = issue.title(with: _title.font, labelFont: detailFont, titleColor: UIColor.label, numberColor: UIColor.secondaryLabel)
_title.attributedText = issue.title(with: _title.font, labelFont: detailFont, titleColor: UIColor.label, numberColor: UIColor.secondaryLabel, settings: settings)

let l = issue.labelsAttributedString(labelFont: _labels.font)
let l = issue.labelsAttributedString(labelFont: _labels.font, settings: settings)
_labels.attributedText = l
_labels.isHidden = (l?.length ?? 0) == 0

_description.attributedText = issue.subtitle(with: detailFont, lightColor: UIColor.secondaryLabel, darkColor: UIColor.label, separator: separator)
_description.attributedText = issue.subtitle(with: detailFont, lightColor: UIColor.secondaryLabel, darkColor: UIColor.label, separator: separator, settings: settings)

_statuses.attributedText = nil
_statuses.isHidden = true

let muted = issue.muted
setCountsImageAndFade(item: issue, muted: muted)
var title = issue.accessibleTitle
setCountsImageAndFade(item: issue, muted: muted, settings: settings)
var title = issue.accessibleTitle(settings: settings)
if muted {
title = "(Muted) - \(title)"
}
accessibilityLabel = "\(title), \(unreadCount.text.orEmpty) unread comments, \(readCount.text.orEmpty) total comments, \(issue.accessibleSubtitle)"
accessibilityLabel = "\(title), \(unreadCount.text.orEmpty) unread comments, \(readCount.text.orEmpty) total comments, \(issue.accessibleSubtitle(settings: settings))"
}

private func setCountsImageAndFade(item: ListableItem, muted: Bool) {
private func setCountsImageAndFade(item: ListableItem, muted: Bool, settings: Settings.Cache) {
let _commentsTotal = Int(item.totalComments)
let _commentsNew = Int(item.unreadComments)
let fade = muted || item.isSnoozing

readCount.text = numberFormatter.string(for: _commentsTotal)
readCount.isHidden = _commentsTotal == 0

if let p = item as? PullRequest, Settings.cache.markPrsAsUnreadOnNewCommits, p.hasNewCommits {
if let p = item as? PullRequest, settings.markPrsAsUnreadOnNewCommits, p.hasNewCommits {
unreadCount.isHidden = false
unreadCount.text = _commentsNew == 0 ? "!" : numberFormatter.string(for: _commentsNew)
} else {
Expand Down
4 changes: 2 additions & 2 deletions PocketTrailer/RepoSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class RepoSettingsViewController: UITableViewController, UITextFieldDelega
}

settingsChangedTimer = PopTimer(timeInterval: 1.0) { @MainActor in
await DataManager.postProcessAllItems(in: DataManager.main)
await DataManager.postProcessAllItems(in: DataManager.main, settings: Settings.cache)
}

tableView.tableHeaderView = header
Expand Down Expand Up @@ -175,7 +175,7 @@ final class RepoSettingsViewController: UITableViewController, UITextFieldDelega
if settingsChangedTimer.isPushed {
settingsChangedTimer.abort()
Task {
await DataManager.postProcessAllItems(in: DataManager.main)
await DataManager.postProcessAllItems(in: DataManager.main, settings: Settings.cache)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion PocketTrailer/ServersViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ final class ServersViewController: UITableViewController {
}
Task {
await DataManager.saveDB()
await DataManager.postProcessAllItems(in: DataManager.main)
await DataManager.postProcessAllItems(in: DataManager.main, settings: Settings.cache)
_ = await app.startRefresh()
}
}
Expand Down
2 changes: 1 addition & 1 deletion PocketTrailer/SnoozingEditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class SnoozingEditorViewController: UIViewController, UITableViewDelegate,

a.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
a.addAction(UIAlertAction(title: "Wake Them Up", style: .destructive) { _ in
snoozeItem.wakeUpAllAssociatedItems()
snoozeItem.wakeUpAllAssociatedItems(settings: Settings.cache)
self.deletePreset()
})
a.addAction(UIAlertAction(title: "Keep Them Snoozed", style: .destructive) { _ in
Expand Down
Loading

0 comments on commit dc2cc8d

Please sign in to comment.