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,8 +58,10 @@ class AppSplitViewController: UISplitViewController {
let read = nav.topViewController as? ReadViewController else {
return
}
read.item.read = !read.item.read
updateImage(toolbarItem: item)
Task {
await fervorController.markItem(read.item, read: !read.item.read)
updateImage(toolbarItem: item)
}
}
private func updateImage(toolbarItem: NSToolbarItem) {

View File

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

View File

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

View File

@ -189,7 +189,9 @@ extension ReadViewController: StretchyMenuInteractionDelegate {
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
item.read = !item.read
Task {
await self.fervorController.markItem(item, read: !item.read)
}
}),
]
}