Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add badge in spaces button (PSG-944) #7088

Merged
merged 8 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions Riot/Modules/Home/AllChats/AllChatsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class AllChatsViewController: HomeViewController {
private var isOnboardingCoordinatorPreparing: Bool = false

private var allChatsOnboardingCoordinatorBridgePresenter: AllChatsOnboardingCoordinatorBridgePresenter?

private var theme: Theme {
ThemeService.shared().theme
}

@IBOutlet private var toolbar: UIToolbar!
private var isToolbarHidden: Bool = false {
Expand Down Expand Up @@ -137,12 +141,13 @@ class AllChatsViewController: HomeViewController {
searchController.delegate = self

NotificationCenter.default.addObserver(self, selector: #selector(self.setupEditOptions), name: AllChatsLayoutSettingsManager.didUpdateSettings, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.updateBadgeButton), name: MXSpaceNotificationCounter.didUpdateNotificationCount, object: nil)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

self.toolbar.tintColor = ThemeService.shared().theme.colors.accent
self.toolbar.tintColor = theme.colors.accent
if self.navigationItem.searchController == nil {
self.navigationItem.searchController = searchController
}
Expand Down Expand Up @@ -460,7 +465,7 @@ class AllChatsViewController: HomeViewController {
return
}

self.update(with: ThemeService.shared().theme)
self.update(with: theme)
}

private func update(with theme: Theme) {
Expand Down Expand Up @@ -500,22 +505,42 @@ class AllChatsViewController: HomeViewController {
self?.updateToolbar(with: menu)
}))
updateEmptyView()
updateBadgeButton()
}

private func updateRightNavigationItem(with menu: UIMenu) {
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "ellipsis.circle"), menu: menu)
}

private lazy var spacesButton: BadgedBarButtonItem = {
let innerButton = UIButton(type: .system)
innerButton.accessibilityLabel = VectorL10n.spaceSelectorTitle
innerButton.addTarget(self, action: #selector(self.showSpaceSelectorAction(sender:)), for: .touchUpInside)
innerButton.setImage(Asset.Images.allChatsSpacesIcon.image, for: .normal)
return BadgedBarButtonItem(withBaseButton: innerButton, theme: theme)
}()

@objc private func updateBadgeButton() {
guard isViewLoaded, let session = mainSession else {
return
}

let notificationCount = session.spaceService.missedNotificationsCount
let hasSpaceInvite = session.spaceService.hasSpaceInvite
let isBadgeHighlighed = session.spaceService.hasHighlightNotification || hasSpaceInvite
let badgeValue = (notificationCount == 0 && hasSpaceInvite) ? "!" : String(notificationCount)

spacesButton.badgeText = badgeValue
spacesButton.badgeBackgroundColor = isBadgeHighlighed ? theme.noticeColor : theme.noticeSecondaryColor
}

private func updateToolbar(with menu: UIMenu) {
guard isViewLoaded else {
return
}

self.isToolbarHidden = false
self.update(with: ThemeService.shared().theme)

let spacesButton = UIBarButtonItem(image: Asset.Images.allChatsSpacesIcon.image, style: .done, target: self, action: #selector(self.showSpaceSelectorAction(sender: )))
spacesButton.accessibilityLabel = VectorL10n.spaceSelectorTitle
self.update(with: theme)

self.toolbar.items = [
spacesButton,
Expand Down Expand Up @@ -1076,3 +1101,22 @@ extension AllChatsViewController: SplitViewMasterViewControllerProtocol {
self.refreshCurrentSelectedCell(false)
}
}

private extension MXSpaceService {
var hasSpaceInvite: Bool {
spaceSummaries.contains(where: { $0.isJoined == false })
}

var missedNotificationsCount: UInt {
let notificationState = notificationCounter.homeNotificationState
let groupNotifications = notificationState.groupMissedDiscussionsCount
let directNotifications = notificationState.directMissedDiscussionsCount

// `notificationState.allCount` returns twice the messages for favourite rooms. Fixing it here.
return groupNotifications + directNotifications
}

var hasHighlightNotification: Bool {
notificationCounter.homeNotificationState.allHighlightCount > 0
}
}
1 change: 1 addition & 0 deletions changelog.d/pr-7088.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add badge for messages in spaces button.