Files
suixinkan_ios_uikit/suixinkan_ios/Features/MessageCenter/ViewModels/MessageCenterViewModel.swift
汉秋 d99a5b1bf8 Advance UIKit rewrite with AMap integration and core UI modules.
Integrate高德 SDK with simulator-safe build flags, add map views for operating area and punch points, refactor main tabs and key feature screens to UIKit with Diffable lists, and document Swift concurrency defaults in AGENTS.md.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-26 15:16:12 +08:00

172 lines
5.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MessageCenterViewModel.swift
// suixinkan
//
// Created by Codex on 2026/6/25.
//
import Foundation
@MainActor
/// ViewModel
final class MessageCenterViewModel {
var onChange: (() -> Void)?
var messages: [MessageItem] = [] { didSet { onChange?() } }
var selectedFilter: MessageFilter = .all { didSet { onChange?() } }
var isLoading = false { didSet { onChange?() } }
var isLoadingMore = false { didSet { onChange?() } }
var loadFailed = false { didSet { onChange?() } }
var loadFailureReason: String? { didSet { onChange?() } }
var message: String? { didSet { onChange?() } }
private(set) var hasMoreMessages = false { didSet { onChange?() } }
private(set) var lastId = 0 { didSet { onChange?() } }
private let pageSize = 20
///
var unreadCount: Int {
messages.filter { !$0.isRead }.count
}
///
var filterDescription: String {
selectedFilter == .all ? "显示全部消息" : "仅显示未读消息"
}
///
func selectFilter(_ filter: MessageFilter, api: any MessageCenterServing) async {
guard selectedFilter != filter else { return }
selectedFilter = filter
await reloadFirstPage(api: api)
}
///
func reloadFirstPage(api: any MessageCenterServing) async {
isLoading = true
loadFailed = false
loadFailureReason = nil
message = nil
defer { isLoading = false }
do {
let response = try await api.messageList(lastId: 0, limit: pageSize, unread: selectedFilter.rawValue)
messages = mapMessages(response.items)
hasMoreMessages = response.hasMore
lastId = response.lastId
} catch {
clearMessages()
loadFailed = true
loadFailureReason = error.localizedDescription
message = error.localizedDescription
}
}
///
func loadMore(api: any MessageCenterServing) async {
guard hasMoreMessages, !isLoadingMore, !isLoading else { return }
isLoadingMore = true
message = nil
defer { isLoadingMore = false }
do {
let response = try await api.messageList(lastId: lastId, limit: pageSize, unread: selectedFilter.rawValue)
messages.append(contentsOf: mapMessages(response.items))
messages = deduplicatedAndSorted(messages)
hasMoreMessages = response.hasMore
lastId = response.lastId
} catch {
message = error.localizedDescription
}
}
///
func markAsRead(api: any MessageCenterServing, item: MessageItem) async throws {
guard !item.isRead, let msgId = item.msgId else { return }
try await api.messageRead(id: msgId)
guard let index = messages.firstIndex(where: { $0.id == item.id }) else { return }
messages[index].isRead = true
}
///
func markAllAsRead(api: any MessageCenterServing) async throws {
let original = messages
do {
for item in messages where !item.isRead {
if let msgId = item.msgId {
try await api.messageRead(id: msgId)
}
}
messages = messages.map { item in
var next = item
next.isRead = true
return next
}
} catch {
messages = original
throw error
}
}
///
func deleteMessage(api: any MessageCenterServing, item: MessageItem) async throws {
if let msgId = item.msgId {
try await api.messageDelete(id: msgId)
}
messages.removeAll { $0.id == item.id }
}
///
func mapMessages(_ source: [MessageEntity]) -> [MessageItem] {
source.map { item in
MessageItem(
id: "msg_\(item.id)",
msgId: item.id,
title: item.title.trimmingCharacters(in: .whitespacesAndNewlines).nonEmpty
?? item.typeName.trimmingCharacters(in: .whitespacesAndNewlines).nonEmpty
?? "系统通知",
detail: item.content,
time: item.pushAt.trimmingCharacters(in: .whitespacesAndNewlines).nonEmpty
?? item.createdAt.trimmingCharacters(in: .whitespacesAndNewlines).nonEmpty
?? "0000-00-00 00:00",
isRead: item.isRead,
type: Self.messageType(from: item.type)
)
}
}
///
static func messageType(from value: Int) -> MessageType {
switch value {
case 1:
return .order
case 2:
return .writeOff
default:
return .system
}
}
/// deduplicated
private func deduplicatedAndSorted(_ source: [MessageItem]) -> [MessageItem] {
var unique: [String: MessageItem] = [:]
for item in source {
unique[item.id] = item
}
return unique.values.sorted { $0.time > $1.time }
}
/// Messages
private func clearMessages() {
messages = []
hasMoreMessages = false
lastId = 0
isLoadingMore = false
}
}
private extension String {
var nonEmpty: String? {
isEmpty ? nil : self
}
}