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) ValueObservation.tracking(Announcement.fetchCount)
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseWriter) .publisher(in: databaseWriter)
.combineLatest(ValueObservation.tracking(Announcement.fetchCount) .combineLatest(ValueObservation.tracking(Announcement.filter(Announcement.Columns.read == false).fetchCount)
.removeDuplicates() .removeDuplicates()
.publisher(in: databaseWriter)) .publisher(in: databaseWriter))
.map { (total: $0, unread: $1) } .map { (total: $0, unread: $1) }

View file

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

View file

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

View file

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

View file

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