feat: 接入消息未读数、全部已读与首页红点角标同步。

固定消息中心为首页入口,并在推送到达、已读后刷新桌面角标。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-21 18:02:43 +08:00
parent e7f1d777dd
commit caeeb9a1cf
18 changed files with 748 additions and 37 deletions

View File

@ -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 {

View File

@ -9,10 +9,16 @@ import UIKit
///
final class HomeScenicHeaderView: UIView {
///
var onTap: (() -> Void)?
///
var onMessageTap: (() -> Void)?
private let titleLabel = UILabel()
private let arrowView = UIImageView(image: UIImage(systemName: "chevron.down"))
private let messageButton = UIButton(type: .system)
private let unreadDotView = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
@ -25,8 +31,24 @@ final class HomeScenicHeaderView: UIView {
arrowView.contentMode = .scaleAspectFit
arrowView.setContentCompressionResistancePriority(.required, for: .horizontal)
var messageConfiguration = UIButton.Configuration.plain()
messageConfiguration.image = UIImage(systemName: "bell")
messageConfiguration.baseForegroundColor = AppColor.textPrimary
messageConfiguration.contentInsets = .zero
messageButton.configuration = messageConfiguration
messageButton.accessibilityIdentifier = "home_message_button"
messageButton.addTarget(self, action: #selector(handleMessageTap), for: .touchUpInside)
unreadDotView.backgroundColor = AppColor.danger
unreadDotView.layer.cornerRadius = 4
unreadDotView.isUserInteractionEnabled = false
unreadDotView.isHidden = true
unreadDotView.accessibilityIdentifier = "home_message_unread_dot"
addSubview(titleLabel)
addSubview(arrowView)
addSubview(messageButton)
messageButton.addSubview(unreadDotView)
titleLabel.snp.makeConstraints { make in
make.leading.equalTo(safeAreaLayoutGuide).offset(AppSpacing.screenHorizontalInset)
@ -34,12 +56,23 @@ final class HomeScenicHeaderView: UIView {
}
arrowView.snp.makeConstraints { make in
make.leading.equalTo(titleLabel.snp.trailing).offset(AppSpacing.xs)
make.trailing.lessThanOrEqualTo(safeAreaLayoutGuide).offset(-AppSpacing.screenHorizontalInset)
make.trailing.lessThanOrEqualTo(messageButton.snp.leading).offset(-AppSpacing.xs)
make.centerY.equalTo(safeAreaLayoutGuide)
make.width.height.equalTo(16)
}
messageButton.snp.makeConstraints { make in
make.trailing.equalTo(safeAreaLayoutGuide).offset(-AppSpacing.screenHorizontalInset)
make.centerY.equalTo(safeAreaLayoutGuide)
make.width.height.equalTo(44)
}
unreadDotView.snp.makeConstraints { make in
make.centerX.equalTo(messageButton.snp.centerX).offset(9)
make.centerY.equalTo(messageButton.snp.centerY).offset(-9)
make.width.height.equalTo(8)
}
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
tap.delegate = self
addGestureRecognizer(tap)
}
@ -48,12 +81,26 @@ final class HomeScenicHeaderView: UIView {
fatalError("init(coder:) has not been implemented")
}
func apply(scenicName: String) {
///
func apply(scenicName: String, hasUnreadMessages: Bool) {
let trimmed = scenicName.trimmingCharacters(in: .whitespacesAndNewlines)
titleLabel.text = trimmed.isEmpty ? "请选择景区" : trimmed
unreadDotView.isHidden = !hasUnreadMessages
messageButton.accessibilityLabel = hasUnreadMessages ? "消息中心,有未读消息" : "消息中心"
}
@objc private func handleTap() {
onTap?()
}
@objc private func handleMessageTap() {
onMessageTap?()
}
}
extension HomeScenicHeaderView: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
guard let touchedView = touch.view else { return true }
return touchedView !== messageButton && !touchedView.isDescendant(of: messageButton)
}
}

View File

@ -19,6 +19,12 @@ final class MessageCenterViewController: BaseViewController, UITableViewDelegate
private let loadingIndicator = UIActivityIndicatorView(style: .medium)
private let footerSpinner = UIActivityIndicatorView(style: .medium)
private let emptyLabel = UILabel()
private lazy var markAllReadButton = UIBarButtonItem(
title: "全部已读",
style: .plain,
target: self,
action: #selector(markAllReadTapped)
)
private var dataSource: UITableViewDiffableDataSource<Section, MessageItem>!
@ -46,6 +52,7 @@ final class MessageCenterViewController: BaseViewController, UITableViewDelegate
override func setupNavigationBar() {
title = "消息中心"
navigationItem.rightBarButtonItem = markAllReadButton
}
override func setupUI() {
@ -79,7 +86,8 @@ final class MessageCenterViewController: BaseViewController, UITableViewDelegate
override func setupConstraints() {
tableView.snp.makeConstraints { make in
make.edges.equalTo(view.safeAreaLayoutGuide)
make.top.equalTo(view.safeAreaLayoutGuide)
make.leading.trailing.bottom.equalToSuperview()
}
loadingIndicator.snp.makeConstraints { make in
make.center.equalToSuperview()
@ -103,6 +111,11 @@ final class MessageCenterViewController: BaseViewController, UITableViewDelegate
self?.openDetail(message)
}
}
viewModel.onUnreadMessageCountChange = { count in
Task { @MainActor in
await PushNotificationManager.shared.updateApplicationIconBadgeCount(count)
}
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
@ -125,6 +138,12 @@ final class MessageCenterViewController: BaseViewController, UITableViewDelegate
}
}
@objc private func markAllReadTapped() {
Task { @MainActor in
await viewModel.markAllAsRead(api: api)
}
}
private func configureDataSource() {
dataSource = UITableViewDiffableDataSource<Section, MessageItem>(tableView: tableView) { tableView, indexPath, item in
let cell = tableView.dequeueReusableCell(
@ -154,6 +173,7 @@ final class MessageCenterViewController: BaseViewController, UITableViewDelegate
}
tableView.tableFooterView = footerView()
markAllReadButton.isEnabled = !viewModel.isLoading && !viewModel.isMarkingAllAsRead
}
private func applySnapshot(animated: Bool) {