Skip to content

Commit

Permalink
Settings cache improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ptsochantaris committed Aug 20, 2023
1 parent 9692507 commit 3798a75
Show file tree
Hide file tree
Showing 25 changed files with 340 additions and 312 deletions.
18 changes: 9 additions & 9 deletions PocketTrailer/AdvancedSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ final class AdvancedSettingsViewController: UITableViewController, PickerViewCon
}

private func showOptionalReviewWarning(previousSync: Bool) {
if !previousSync, API.shouldSyncReviews || API.shouldSyncReviewAssignments {
if !previousSync, Settings.cache.requiresReviewApis {
for p in PullRequest.allItems(in: DataManager.main) {
p.resetSyncState()
}
Expand Down Expand Up @@ -684,12 +684,12 @@ final class AdvancedSettingsViewController: UITableViewController, PickerViewCon
} else if section == .Reviews {
switch originalIndex {
case 0:
let previousShouldSync = (API.shouldSyncReviews || API.shouldSyncReviewAssignments)
let previousShouldSync = Settings.cache.requiresReviewApis
Settings.displayReviewsOnItems = !Settings.displayReviewsOnItems
showOptionalReviewWarning(previousSync: previousShouldSync)

case 1:
let previousShouldSync = (API.shouldSyncReviews || API.shouldSyncReviewAssignments)
let previousShouldSync = Settings.cache.requiresReviewApis
Settings.showRequestedTeamReviews = !Settings.showRequestedTeamReviews
showOptionalReviewWarning(previousSync: previousShouldSync)

Expand All @@ -702,31 +702,31 @@ final class AdvancedSettingsViewController: UITableViewController, PickerViewCon
performSegue(withIdentifier: "showPicker", sender: v)

case 4:
let previousShouldSync = (API.shouldSyncReviews || API.shouldSyncReviewAssignments)
let previousShouldSync = Settings.cache.requiresReviewApis
Settings.notifyOnReviewChangeRequests = !Settings.notifyOnReviewChangeRequests
showOptionalReviewWarning(previousSync: previousShouldSync)

case 5:
Settings.notifyOnAllReviewChangeRequests = !Settings.notifyOnAllReviewChangeRequests

case 6:
let previousShouldSync = (API.shouldSyncReviews || API.shouldSyncReviewAssignments)
let previousShouldSync = Settings.cache.requiresReviewApis
Settings.notifyOnReviewAcceptances = !Settings.notifyOnReviewAcceptances
showOptionalReviewWarning(previousSync: previousShouldSync)

case 7:
Settings.notifyOnAllReviewAcceptances = !Settings.notifyOnAllReviewAcceptances

case 8:
let previousShouldSync = (API.shouldSyncReviews || API.shouldSyncReviewAssignments)
let previousShouldSync = Settings.cache.requiresReviewApis
Settings.notifyOnReviewDismissals = !Settings.notifyOnReviewDismissals
showOptionalReviewWarning(previousSync: previousShouldSync)

case 9:
Settings.notifyOnAllReviewDismissals = !Settings.notifyOnAllReviewDismissals

case 10:
let previousShouldSync = (API.shouldSyncReviews || API.shouldSyncReviewAssignments)
let previousShouldSync = Settings.cache.requiresReviewApis
Settings.notifyOnReviewAssignments = !Settings.notifyOnReviewAssignments
showOptionalReviewWarning(previousSync: previousShouldSync)

Expand Down Expand Up @@ -956,7 +956,7 @@ final class AdvancedSettingsViewController: UITableViewController, PickerViewCon
settingsChangedTimer.push()

} else if sip.section == SettingsSection.Reviews.rawValue {
let previous = (API.shouldSyncReviews || API.shouldSyncReviewAssignments)
let previous = Settings.cache.requiresReviewApis
if sip.row == 2 {
Settings.assignedDirectReviewHandlingPolicy = Section(assignmentPolicyMenuIndex: didSelectIndexPath.row)
} else if sip.row == 3 {
Expand All @@ -965,7 +965,7 @@ final class AdvancedSettingsViewController: UITableViewController, PickerViewCon
showOptionalReviewWarning(previousSync: previous)

} else if sip.section == SettingsSection.Reactions.rawValue {
let previous = API.shouldSyncReactions
let previous = Settings.cache.shouldSyncReactions
Settings.reactionScanningBatchSize = didSelectIndexPath.row + 1
showOptionalReviewWarning(previousSync: previous)
}
Expand Down
2 changes: 1 addition & 1 deletion PocketTrailer/ApiSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ final class ApiSettingsViewController: UIViewController, UITextFieldDelegate {
}

private func updateUI() {
let profile = Settings.syncProfile
let profile = Settings.cache.syncProfile
highToggle.isOn = profile == .high
moderateToggle.isOn = profile == .moderate
defaultToggle.isOn = profile == .cautious
Expand Down
24 changes: 4 additions & 20 deletions PocketTrailer/MasterViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
private var currentTabBarSet: TabBarSet?

private var searchTimer: PopTimer!
private var context: SettingsCache?

private var pluralNameForItems: String {
viewingPrs ? "pull requests" : "issues"
Expand Down Expand Up @@ -556,8 +555,6 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
}

private func updateQuery(newFetchRequest: NSFetchRequest<ListableItem>) {
context = nil

if fetchedResultsController == nil || fetchedResultsController?.fetchRequest.entityName != newFetchRequest.entityName {
let c = NSFetchedResultsController(fetchRequest: newFetchRequest, managedObjectContext: DataManager.main, sectionNameKeyPath: "sectionName", cacheName: nil)
fetchedResultsController = c
Expand Down Expand Up @@ -782,7 +779,6 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
context = nil
tableView.reloadData()
}

Expand All @@ -797,7 +793,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
if let o = fetchedResultsController?.object(at: indexPath) {
configureCell(cell: cell, withObject: o, context: usedContext)
configureCell(cell: cell, withObject: o)
}
return cell
}
Expand Down Expand Up @@ -986,7 +982,6 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
private var animatedUpdates = false

func controllerWillChangeContent(_: NSFetchedResultsController<NSFetchRequestResult>) {
context = nil
animatedUpdates = UIApplication.shared.applicationState != .background
sectionsChanged = false
if animatedUpdates {
Expand All @@ -1013,15 +1008,6 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
sectionsChanged = true
}

private var usedContext: SettingsCache {
if let context {
return context
}
let used = SettingsCache()
context = used
return used
}

func controller(_: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
guard animatedUpdates else { return }

Expand All @@ -1036,7 +1022,7 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
}
case .update:
if let indexPath, let object = anObject as? ListableItem, let cell = tableView.cellForRow(at: indexPath) {
configureCell(cell: cell, withObject: object, context: usedContext)
configureCell(cell: cell, withObject: object)
}
case .move:
if let indexPath, let newIndexPath {
Expand All @@ -1056,15 +1042,14 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
if animatedUpdates {
tableView.endUpdates()
} else {
context = nil
tableView.reloadData()
}
}

private func configureCell(cell: UITableViewCell, withObject: ListableItem, context: SettingsCache) {
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, context: context)
c.setPullRequest(pullRequest: o)
} else if let o = withObject as? Issue {
c.setIssue(issue: o)
}
Expand Down Expand Up @@ -1145,7 +1130,6 @@ final class MasterViewController: UITableViewController, NSFetchedResultsControl
await safeScrollToTop()
updateQuery(newFetchRequest: itemFetchRequest)
updateStatus(becauseOfChanges: becauseOfChanges)
context = nil
tableView.reloadData()
}

Expand Down
6 changes: 3 additions & 3 deletions PocketTrailer/PRCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ final class PRCell: UITableViewCell {

private weak var item: ListableItem?

func setPullRequest(pullRequest: PullRequest, context: SettingsCache) {
func setPullRequest(pullRequest: PullRequest) {
item = pullRequest

let separator = traitCollection.containsTraits(in: compactTraits) ? "\n" : " "
Expand Down Expand Up @@ -114,7 +114,7 @@ final class PRCell: UITableViewCell {
var statusText: NSMutableAttributedString?
var totalStatuses = 0
if pullRequest.section.shouldListStatuses {
let statusItems = pullRequest.displayedStatusLines(context: context)
let statusItems = pullRequest.displayedStatusLines
var statusCount = statusItems.count
totalStatuses = statusCount
var lineAttributes = statusAttributes
Expand Down Expand Up @@ -177,7 +177,7 @@ final class PRCell: UITableViewCell {
readCount.text = numberFormatter.string(for: _commentsTotal)
readCount.isHidden = _commentsTotal == 0

if let p = item as? PullRequest, Settings.markPrsAsUnreadOnNewCommits, p.hasNewCommits {
if let p = item as? PullRequest, Settings.cache.markPrsAsUnreadOnNewCommits, p.hasNewCommits {
unreadCount.isHidden = false
unreadCount.text = _commentsNew == 0 ? "!" : numberFormatter.string(for: _commentsNew)
} else {
Expand Down
22 changes: 10 additions & 12 deletions PocketTrailer/WatchManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ final class WatchManager: NSObject, WCSessionDelegate {
@MainActor
private func processList(message: JSON) async -> JSON {
var result = JSON()
let context = SettingsCache()

switch (message["list"] as? String).orEmpty {
case "overview":
Expand All @@ -128,12 +127,11 @@ final class WatchManager: NSObject, WCSessionDelegate {
apiServerUri: message["apiUri"] as! String,
group: message["group"] as! String,
count: message["count"] as! Int,
onlyUnread: message["onlyUnread"] as! Bool,
context: context
onlyUnread: message["onlyUnread"] as! Bool
)

case "item_detail":
if let lid = message["localId"] as? String, let details = buildItemDetail(localId: lid, context: context) {
if let lid = message["localId"] as? String, let details = buildItemDetail(localId: lid) {
result["result"] = details
return reportSuccess(result: result)
} else {
Expand Down Expand Up @@ -161,7 +159,7 @@ final class WatchManager: NSObject, WCSessionDelegate {
////////////////////////////

@MainActor
private func buildItemList(type: String, sectionIndex: Int, from: Int, apiServerUri: String, group: String, count: Int, onlyUnread: Bool, context: SettingsCache) async -> JSON {
private func buildItemList(type: String, sectionIndex: Int, from: Int, apiServerUri: String, group: String, count: Int, onlyUnread: Bool) async -> JSON {
let showLabels = Settings.showLabels
let entity: ListableItem.Type
if type == "prs" {
Expand All @@ -184,13 +182,13 @@ final class WatchManager: NSObject, WCSessionDelegate {
f.fetchOffset = from
f.fetchLimit = count

let items = try! DataManager.main.fetch(f).map { self.baseDataForItem(item: $0, showLabels: showLabels, context: context) }
let items = try! DataManager.main.fetch(f).map { self.baseDataForItem(item: $0, showLabels: showLabels) }
let compressedData = (try? NSKeyedArchiver.archivedData(withRootObject: items, requiringSecureCoding: false).data(operation: .compress)) ?? Data()
return ["result": compressedData]
}

@MainActor
private func baseDataForItem(item: ListableItem, showLabels: Bool, context: SettingsCache) -> JSON {
private func baseDataForItem(item: ListableItem, showLabels: Bool) -> JSON {
let font = UIFont.systemFont(ofSize: UIFont.systemFontSize)
let smallFont = UIFont.systemFont(ofSize: UIFont.systemFontSize - 4)

Expand All @@ -208,7 +206,7 @@ final class WatchManager: NSObject, WCSessionDelegate {
itemData["labels"] = labelsForItem(item: item)
}
if let item = item as? PullRequest, item.section.shouldListStatuses {
itemData["statuses"] = statusLinesForPr(pr: item, context: context)
itemData["statuses"] = statusLinesForPr(pr: item)
}
return itemData
}
Expand All @@ -226,9 +224,9 @@ final class WatchManager: NSObject, WCSessionDelegate {
}

@MainActor
private func statusLinesForPr(pr: PullRequest, context: SettingsCache) -> [JSON] {
private func statusLinesForPr(pr: PullRequest) -> [JSON] {
var statusLines = [JSON]()
for status in pr.displayedStatusLines(context: context) {
for status in pr.displayedStatusLines {
statusLines.append([
"color": status.colorForDisplay,
"text": status.descriptionText.orEmpty
Expand All @@ -240,9 +238,9 @@ final class WatchManager: NSObject, WCSessionDelegate {
/////////////////////////////

@MainActor
private func buildItemDetail(localId: String, context: SettingsCache) -> Data? {
private func buildItemDetail(localId: String) -> Data? {
if let oid = DataManager.id(for: localId), let item = try? DataManager.main.existingObject(with: oid) as? ListableItem {
var result = baseDataForItem(item: item, showLabels: Settings.showLabels, context: context)
var result = baseDataForItem(item: item, showLabels: Settings.showLabels)
result["description"] = item.body
result["comments"] = commentsForItem(item: item)

Expand Down
16 changes: 0 additions & 16 deletions Shared/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,6 @@ enum API {
}
}

nonisolated static var shouldSyncReactions: Bool {
Settings.notifyOnItemReactions || Settings.notifyOnCommentReactions
}

nonisolated static var shouldSyncReviews: Bool {
Settings.displayReviewsOnItems || Settings.notifyOnReviewDismissals || Settings.notifyOnReviewAcceptances || Settings.notifyOnReviewChangeRequests || Settings.autoHidePrsIApproved || Settings.autoHidePrsIRejected
}

nonisolated static var shouldSyncReviewAssignments: Bool {
Settings.displayReviewsOnItems
|| Settings.showRequestedTeamReviews
|| Settings.notifyOnReviewAssignments
|| (Settings.assignedDirectReviewHandlingPolicy.visible)
|| (Settings.assignedTeamReviewHandlingPolicy.visible)
}

////////////////////////////////////// API interface

@MainActor
Expand Down
Loading

0 comments on commit 3798a75

Please sign in to comment.