Show announcement count

This commit is contained in:
Justin Mazzocchi 2021-05-09 21:39:36 -07:00
parent 07dad77fd7
commit cb97e21cd2
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
5 changed files with 24 additions and 7 deletions

View File

@ -609,7 +609,7 @@ public extension ContentDatabase {
ValueObservation.tracking(Announcement.fetchCount)
.removeDuplicates()
.publisher(in: databaseWriter)
.combineLatest(ValueObservation.tracking(Announcement.fetchCount)
.combineLatest(ValueObservation.tracking(Announcement.filter(Announcement.Columns.read == false).fetchCount)
.removeDuplicates()
.publisher(in: databaseWriter))
.map { (total: $0, unread: $1) }

View File

@ -141,6 +141,8 @@ class TableViewController: UITableViewController {
if let loadMoreView = cell.contentView as? LoadMoreView {
visibleLoadMoreViews.insert(loadMoreView)
}
(cell.contentView as? AnnouncementView)?.dismissIfUnread()
}
override func tableView(_ tableView: UITableView,

View File

@ -41,7 +41,7 @@ final class TimelinesViewController: UIPageViewController {
image: UIImage(systemName: "newspaper"),
selectedImage: nil)
announcementsButton.primaryAction = UIAction(
let announcementsAction = UIAction(
title: NSLocalizedString("main-navigation.announcements", comment: ""),
image: UIImage(systemName: "megaphone")) { [weak self] _ in
guard let self = self else { return }
@ -52,8 +52,21 @@ final class TimelinesViewController: UIPageViewController {
self.navigationController?.pushViewController(announcementsViewController, animated: true)
}
announcementsButton.primaryAction = announcementsAction
viewModel.$announcementCount
.sink { [weak self] in
if $0.unread > 0 {
announcementsAction.image = UIImage(systemName: "\($0.unread).circle.fill")
?? UIImage(systemName: "megaphone.fill")
self?.announcementsButton.primaryAction = announcementsAction
self?.announcementsButton.tintColor = .systemRed
} else {
announcementsAction.image = UIImage(systemName: "megaphone")
self?.announcementsButton.primaryAction = announcementsAction
self?.announcementsButton.tintColor = nil
}
self?.navigationItem.rightBarButtonItem = $0.total > 0 ? self?.announcementsButton : nil
}
.store(in: &cancellables)

View File

@ -33,7 +33,9 @@ public extension AnnouncementViewModel {
.eraseToAnyPublisher())
}
func dismiss() {
func dismissIfUnread() {
guard !announcement.read else { return }
eventsSubject.send(
announcementService.dismiss()
.map { _ in .ignorableOutput }

View File

@ -47,6 +47,10 @@ extension AnnouncementView {
static func estimatedHeight(width: CGFloat, announcement: Announcement) -> CGFloat {
UITableView.automaticDimension
}
func dismissIfUnread() {
announcementConfiguration.viewModel.dismissIfUnread()
}
}
extension AnnouncementView: UIContentView {
@ -167,9 +171,5 @@ private extension AnnouncementView {
if snapshot.itemIdentifiers != dataSource.snapshot().itemIdentifiers {
dataSource.apply(snapshot, animatingDifferences: false) { viewModel.reload() }
}
if !viewModel.announcement.read {
viewModel.dismiss()
}
}
}