Mark items read on the server immediately instead of waiting for sync

This commit is contained in:
Shadowfacts 2022-01-15 14:22:13 -05:00
parent 0be678063b
commit 1d2e666c00
5 changed files with 48 additions and 14 deletions

View File

@ -110,4 +110,25 @@ class FervorController {
} }
} }
@MainActor
func markItem(_ item: Item, read: Bool) async {
item.read = read
do {
let f = item.read ? client.read(item:) : client.unread(item:)
_ = try await f(item.id!)
item.needsReadStateSync = false
} catch {
logger.error("Failed to mark item (un)read: \(error.localizedDescription, privacy: .public)")
item.needsReadStateSync = true
}
if persistentContainer.viewContext.hasChanges {
do {
try persistentContainer.viewContext.save()
} catch {
logger.error("Failed to save view context: \(error.localizedDescription, privacy: .public)")
}
}
}
} }

View File

@ -58,9 +58,11 @@ class AppSplitViewController: UISplitViewController {
let read = nav.topViewController as? ReadViewController else { let read = nav.topViewController as? ReadViewController else {
return return
} }
read.item.read = !read.item.read Task {
await fervorController.markItem(read.item, read: !read.item.read)
updateImage(toolbarItem: item) updateImage(toolbarItem: item)
} }
}
private func updateImage(toolbarItem: NSToolbarItem) { private func updateImage(toolbarItem: NSToolbarItem) {
if let nav = viewController(for: .secondary) as? UINavigationController, if let nav = viewController(for: .secondary) as? UINavigationController,

View File

@ -8,6 +8,8 @@
import UIKit import UIKit
protocol ItemCollectionViewCellDelegate: AnyObject { protocol ItemCollectionViewCellDelegate: AnyObject {
var fervorController: FervorController { get }
func itemCellSelected(cell: ItemCollectionViewCell, item: Item) func itemCellSelected(cell: ItemCollectionViewCell, item: Item)
} }
@ -86,7 +88,9 @@ class ItemCollectionViewCell: UICollectionViewListCell {
func setRead(_ read: Bool, animated: Bool) { func setRead(_ read: Bool, animated: Bool) {
guard self.item.read != read else { return } guard self.item.read != read else { return }
self.item.read = read Task {
await self.delegate?.fervorController.markItem(self.item, read: read)
if animated { if animated {
// i don't know why .transition works but .animate doesn't // i don't know why .transition works but .animate doesn't
UIView.transition(with: self, duration: 0.2, options: .transitionCrossDissolve) { UIView.transition(with: self, duration: 0.2, options: .transitionCrossDissolve) {
@ -96,6 +100,7 @@ class ItemCollectionViewCell: UICollectionViewListCell {
updateColors() updateColors()
} }
} }
}
private func updateColors() { private func updateColors() {
if item.read { if item.read {

View File

@ -139,12 +139,16 @@ extension ItemsViewController: UICollectionViewDelegate {
})) }))
} }
if item.read { if item.read {
children.append(UIAction(title: "Mark as Unread", image: UIImage(systemName: "checkmark.circle"), handler: { _ in children.append(UIAction(title: "Mark as Unread", image: UIImage(systemName: "checkmark.circle"), handler: { [unowned self] _ in
item.read = false Task {
await self.fervorController.markItem(item, read: false)
}
})) }))
} else { } else {
children.append(UIAction(title: "Mark as Read", image: UIImage(systemName: "checkmark.circle.fill"), handler: { _ in children.append(UIAction(title: "Mark as Read", image: UIImage(systemName: "checkmark.circle.fill"), handler: { [unowned self] _ in
item.read = true Task {
await self.fervorController.markItem(item, read: true)
}
})) }))
} }
return UIMenu(children: children) return UIMenu(children: children)

View File

@ -189,7 +189,9 @@ extension ReadViewController: StretchyMenuInteractionDelegate {
self.present(UIActivityViewController(activityItems: [url], applicationActivities: nil), animated: true) self.present(UIActivityViewController(activityItems: [url], applicationActivities: nil), animated: true)
}), }),
StretchyMenuItem(title: item.read ? "Mark as Unread" : "Mark as Read", subtitle: nil, action: { [unowned self] in StretchyMenuItem(title: item.read ? "Mark as Unread" : "Mark as Read", subtitle: nil, action: { [unowned self] in
item.read = !item.read Task {
await self.fervorController.markItem(item, read: !item.read)
}
}), }),
] ]
} }