feat: 接入消息未读数、全部已读与首页红点角标同步。
固定消息中心为首页入口,并在推送到达、已读后刷新桌面角标。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@ -13,6 +13,7 @@ final class HomeViewController: BaseViewController {
|
||||
|
||||
private let viewModel = HomeViewModel()
|
||||
private let homeAPI = NetworkServices.shared.homeAPI
|
||||
private let messageCenterAPI = NetworkServices.shared.messageCenterAPI
|
||||
|
||||
private let scenicHeaderView = HomeScenicHeaderView()
|
||||
private var collectionView: UICollectionView!
|
||||
@ -74,6 +75,7 @@ final class HomeViewController: BaseViewController {
|
||||
Task { @MainActor in self?.showToast(message) }
|
||||
}
|
||||
scenicHeaderView.onTap = { [weak self] in self?.presentScenicSelection() }
|
||||
scenicHeaderView.onMessageTap = { [weak self] in self?.openMessageCenter() }
|
||||
collectionView.delegate = self
|
||||
|
||||
NotificationCenter.default.addObserver(
|
||||
@ -88,11 +90,26 @@ final class HomeViewController: BaseViewController {
|
||||
name: NotificationName.scenicDidChange,
|
||||
object: nil
|
||||
)
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(handleUnreadMessageCountDidChange(_:)),
|
||||
name: NotificationName.unreadMessageCountDidChange,
|
||||
object: nil
|
||||
)
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(handleUnreadMessageCountDidChange(_:)),
|
||||
name: UIApplication.didBecomeActiveNotification,
|
||||
object: nil
|
||||
)
|
||||
}
|
||||
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
navigationController?.setNavigationBarHidden(true, animated: animated)
|
||||
if hasInitialized {
|
||||
Task { await refreshUnreadMessageStatus() }
|
||||
}
|
||||
}
|
||||
|
||||
override func viewDidAppear(_ animated: Bool) {
|
||||
@ -272,6 +289,7 @@ final class HomeViewController: BaseViewController {
|
||||
private func initializeHome() async {
|
||||
showLoading()
|
||||
await viewModel.initialize(api: homeAPI)
|
||||
await refreshUnreadMessageStatus()
|
||||
hideLoading()
|
||||
|
||||
applyViewModel()
|
||||
@ -287,7 +305,10 @@ final class HomeViewController: BaseViewController {
|
||||
|
||||
@MainActor
|
||||
private func applyViewModel() {
|
||||
scenicHeaderView.apply(scenicName: viewModel.currentScenicName)
|
||||
scenicHeaderView.apply(
|
||||
scenicName: viewModel.currentScenicName,
|
||||
hasUnreadMessages: viewModel.hasUnreadMessages
|
||||
)
|
||||
|
||||
let showWorkBlock = !viewModel.isMinimalTopRole
|
||||
let showStore = viewModel.currentAppRole == .storeAdmin && viewModel.storeItem != nil
|
||||
@ -499,6 +520,10 @@ final class HomeViewController: BaseViewController {
|
||||
pushScenicSelection()
|
||||
}
|
||||
|
||||
private func openMessageCenter() {
|
||||
navigationController?.pushViewController(MessageCenterViewController(), animated: true)
|
||||
}
|
||||
|
||||
private func pushScenicSelection() {
|
||||
navigationController?.setNavigationBarHidden(false, animated: true)
|
||||
let controller = ScenicSelectionViewController()
|
||||
@ -529,6 +554,7 @@ final class HomeViewController: BaseViewController {
|
||||
viewModel.markNeedsPermissionReload()
|
||||
Task {
|
||||
await viewModel.reloadIfNeeded(api: homeAPI)
|
||||
await refreshUnreadMessageStatus()
|
||||
applyViewModel()
|
||||
await evaluateDialogsWithDelay()
|
||||
}
|
||||
@ -541,6 +567,21 @@ final class HomeViewController: BaseViewController {
|
||||
applyViewModel()
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func handleUnreadMessageCountDidChange(_ notification: Notification) {
|
||||
guard hasInitialized else { return }
|
||||
if let count = notification.userInfo?[NotificationUserInfoKey.unreadCount] as? Int {
|
||||
viewModel.updateUnreadMessageCount(count)
|
||||
Task { await PushNotificationManager.shared.updateApplicationIconBadgeCount(count) }
|
||||
} else {
|
||||
Task { await refreshUnreadMessageStatus() }
|
||||
}
|
||||
}
|
||||
|
||||
private func refreshUnreadMessageStatus() async {
|
||||
guard await viewModel.refreshUnreadMessageStatus(api: messageCenterAPI) else { return }
|
||||
await PushNotificationManager.shared.updateApplicationIconBadgeCount(viewModel.unreadMessageCount)
|
||||
}
|
||||
}
|
||||
|
||||
extension HomeViewController: UICollectionViewDelegate {
|
||||
|
||||
Reference in New Issue
Block a user