模块化 AppStore 并完善素材管理与个人空间设置。

将会话、权限、位置、收款与排队存储拆分为独立模块,同步更新各 ViewModel 与单元测试,并补充素材管理 UI 与个人空间设置交互。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-13 11:01:08 +08:00
parent ceca780ab3
commit 005349f8e6
128 changed files with 2953 additions and 1123 deletions

View File

@ -5,403 +5,43 @@
import Foundation
/// Android `AppStoreDataSource`
/// 退
final class AppStore {
///
static let shared = AppStore()
private enum Key {
static let token = "key_in_token"
static let lastLoginUsername = "key_last_login_username"
static let privacyAgreementAccepted = "key_privacy_agreement_accepted"
static let userId = "key_in_user_id"
static let userName = "key_in_user_name"
static let realName = "key_in_real_name"
static let avatar = "key_in_avatar"
static let phone = "key_in_phone"
static let accountType = "key_in_account_type"
static let accountDisplayName = "key_in_account_display_name"
static let roleCode = "key_in_role_code"
static let roleName = "key_in_role_name"
static let currentScenicId = "key_in_current_scenic_id"
static let currentScenicName = "key_in_current_scenic_name"
static let currentStoreId = "key_in_current_store_id"
static let legacyRoleId = "key_in_role_id"
static let rolePermissionList = "key_role_permission_list"
static let permissionItems = "key_in_permission"
static let roleScenicList = "key_current_role_scenic_list"
static let onlineStatus = "key_online_status"
static let lastLocationReportTime = "key_last_location_report_time"
static let locationReminderMinutes = "key_location_reminder_minutes"
static let isOpenReceiveVoice = "key_is_open_receive_voice"
static let scenicQueuePunchSpotId = "_scenic_queue_punch_spot_id"
static let scenicQueuePunchSpotName = "_scenic_queue_punch_spot_name"
static let scenicQueueRemark = "_scenic_queue_remark"
static let scenicQueueSettingsSnapshot = "_scenic_queue_settings_snapshot"
static let scenicQueueOfflineTTS = "_scenic_queue_offline_tts_v1"
static let scenicQueuePresetVoices = "_scenic_queue_preset_voices_v1"
}
///
let session: AppSessionStore
private let defaults: UserDefaults
///
let permissions: AppPermissionStore
///
let location: AppLocationStore
///
let payment: AppPaymentStore
///
let scenicQueue: AppScenicQueueStore
/// 使 UserDefaults
init(defaults: UserDefaults = .standard) {
self.defaults = defaults
let session = AppSessionStore(defaults: defaults)
self.session = session
permissions = AppPermissionStore(defaults: defaults, session: session)
location = AppLocationStore(defaults: defaults, session: session)
payment = AppPaymentStore(defaults: defaults, session: session)
scenicQueue = AppScenicQueueStore(defaults: defaults, session: session)
}
/// Token
var token: String {
get { defaults.string(forKey: Key.token) ?? "" }
set { defaults.set(newValue, forKey: Key.token) }
}
///
var lastLoginUsername: String? {
get { defaults.string(forKey: Key.lastLoginUsername) }
set {
if let newValue {
defaults.set(newValue, forKey: Key.lastLoginUsername)
} else {
defaults.removeObject(forKey: Key.lastLoginUsername)
}
}
}
///
var privacyAgreementAccepted: Bool {
get { defaults.bool(forKey: Key.privacyAgreementAccepted) }
set { defaults.set(newValue, forKey: Key.privacyAgreementAccepted) }
}
var userId: String {
get { defaults.string(forKey: Key.userId) ?? "" }
set { defaults.set(newValue, forKey: Key.userId) }
}
var userName: String {
get { defaults.string(forKey: Key.userName) ?? "" }
set { defaults.set(newValue, forKey: Key.userName) }
}
var realName: String {
get { defaults.string(forKey: Key.realName) ?? "" }
set { defaults.set(newValue, forKey: Key.realName) }
}
var avatar: String {
get { defaults.string(forKey: Key.avatar) ?? "" }
set { defaults.set(newValue, forKey: Key.avatar) }
}
var phone: String {
get { defaults.string(forKey: Key.phone) ?? "" }
set { defaults.set(newValue, forKey: Key.phone) }
}
var accountType: String {
get { defaults.string(forKey: Key.accountType) ?? "" }
set { defaults.set(newValue, forKey: Key.accountType) }
}
var accountDisplayName: String {
get { defaults.string(forKey: Key.accountDisplayName) ?? "" }
set { defaults.set(newValue, forKey: Key.accountDisplayName) }
}
var roleCode: String {
get { defaults.string(forKey: Key.roleCode) ?? "" }
set { defaults.set(newValue, forKey: Key.roleCode) }
}
var roleName: String {
get { defaults.string(forKey: Key.roleName) ?? "" }
set { defaults.set(newValue, forKey: Key.roleName) }
}
var currentScenicId: Int {
get {
let value = defaults.string(forKey: Key.currentScenicId) ?? ""
return Int(value) ?? 0
}
set {
defaults.set(newValue > 0 ? String(newValue) : "", forKey: Key.currentScenicId)
}
}
var currentScenicName: String {
get { defaults.string(forKey: Key.currentScenicName) ?? "" }
set { defaults.set(newValue, forKey: Key.currentScenicName) }
}
var currentStoreId: Int {
get { defaults.integer(forKey: Key.currentStoreId) }
set { defaults.set(newValue, forKey: Key.currentStoreId) }
}
/// role_id
var legacyRoleId: Int {
get { defaults.integer(forKey: accountScopedKey(Key.legacyRoleId)) }
set { defaults.set(newValue, forKey: accountScopedKey(Key.legacyRoleId)) }
}
/// 线
var onlineStatus: Bool {
get { defaults.bool(forKey: accountScopedKey(Key.onlineStatus)) }
set { defaults.set(newValue, forKey: accountScopedKey(Key.onlineStatus)) }
}
///
var lastLocationReportTime: Int64 {
get { Int64(defaults.integer(forKey: accountScopedKey(Key.lastLocationReportTime))) }
set { defaults.set(Int(newValue), forKey: accountScopedKey(Key.lastLocationReportTime)) }
}
/// 0
var locationReminderMinutes: Int {
get { defaults.integer(forKey: accountScopedKey(Key.locationReminderMinutes)) }
set { defaults.set(newValue, forKey: accountScopedKey(Key.locationReminderMinutes)) }
}
/// Android `getIsOpenReceiveVoice`
var isOpenReceiveVoice: Bool {
get { defaults.bool(forKey: accountScopedKey(Key.isOpenReceiveVoice)) }
set { defaults.set(newValue, forKey: accountScopedKey(Key.isOpenReceiveVoice)) }
}
/// ID
var scenicQueuePunchSpotId: String {
get { defaults.string(forKey: accountScopedKey(Key.scenicQueuePunchSpotId)) ?? "" }
set { defaults.set(newValue, forKey: accountScopedKey(Key.scenicQueuePunchSpotId)) }
}
///
var scenicQueuePunchSpotName: String {
get { defaults.string(forKey: accountScopedKey(Key.scenicQueuePunchSpotName)) ?? "" }
set { defaults.set(newValue, forKey: accountScopedKey(Key.scenicQueuePunchSpotName)) }
}
///
var scenicQueueRemark: String {
get { defaults.string(forKey: accountScopedKey(Key.scenicQueueRemark)) ?? "" }
set { defaults.set(newValue, forKey: accountScopedKey(Key.scenicQueueRemark)) }
}
/// 使线 TTS
var scenicQueueUseOfflineTTS: Bool {
get { defaults.bool(forKey: accountScopedKey(Key.scenicQueueOfflineTTS)) }
set { defaults.set(newValue, forKey: accountScopedKey(Key.scenicQueueOfflineTTS)) }
}
/// Android `getAccountCachePrefix`
var accountCachePrefix: String {
let uid = userId.trimmingCharacters(in: .whitespacesAndNewlines)
let type = accountType.trimmingCharacters(in: .whitespacesAndNewlines)
if uid.isEmpty { return "guest" }
if type.isEmpty { return uid }
return "\(uid)_\(type)"
}
///
var currentAppRole: AppRoleCode? {
AppRoleCode.fromCode(roleCode) ?? AppRoleCode.fromRoleName(roleName)
}
///
var isPhotographerRole: Bool {
if let role = currentAppRole {
return role.isPhotographer
}
let name = roleName.trimmingCharacters(in: .whitespacesAndNewlines)
return name == "摄影师"
}
/// Token
var isLoggedIn: Bool {
!token.isEmpty
}
/// Token
func saveToken(_ token: String) {
self.token = token
}
///
func applyAccount(_ account: AccountSwitchAccount) {
userId = String(account.businessUserId)
accountType = account.accountType
accountDisplayName = account.title
userName = account.subtitle
phone = account.phone
if !account.avatar.isEmpty {
avatar = account.avatar
}
currentScenicName = account.scenicName
currentScenicId = account.scenicId ?? 0
currentStoreId = account.storeId ?? 0
}
///
func applyUserInfo(_ info: UserInfoResponse) {
if !info.nickname.isEmpty {
userName = info.nickname
}
if !info.realName.isEmpty {
realName = info.realName
}
if !info.avatar.isEmpty {
avatar = info.avatar
}
if !info.phone.isEmpty {
phone = info.phone
}
if !info.roleName.isEmpty, roleName.isEmpty {
roleName = info.roleName
}
}
///
///
func logout() {
clearPermissionSnapshot()
token = ""
userId = ""
userName = ""
realName = ""
avatar = ""
phone = ""
accountType = ""
accountDisplayName = ""
roleCode = ""
roleName = ""
currentScenicId = 0
currentScenicName = ""
currentStoreId = 0
}
/// role-permission
func saveRolePermissionList(_ list: [RolePermissionResponse]) {
guard let data = try? JSONEncoder().encode(list) else { return }
defaults.set(data, forKey: accountScopedKey(Key.rolePermissionList))
}
/// role-permission
func rolePermissionList() -> [RolePermissionResponse] {
guard let data = defaults.data(forKey: accountScopedKey(Key.rolePermissionList)),
let list = try? JSONDecoder().decode([RolePermissionResponse].self, from: data) else {
return []
}
return list
}
///
func savePermissionItems(_ items: [HomePermissionItem]) {
guard let data = try? JSONEncoder().encode(items) else { return }
defaults.set(data, forKey: accountScopedKey(Key.permissionItems))
}
///
func permissionItems() -> [HomePermissionItem] {
guard let data = defaults.data(forKey: accountScopedKey(Key.permissionItems)),
let items = try? JSONDecoder().decode([HomePermissionItem].self, from: data) else {
return []
}
return items
}
///
func saveRoleScenicList(_ scenicList: [ScenicInfo]) {
guard let data = try? JSONEncoder().encode(scenicList) else { return }
defaults.set(data, forKey: accountScopedKey(Key.roleScenicList))
}
///
func roleScenicList() -> [ScenicInfo] {
guard let data = defaults.data(forKey: accountScopedKey(Key.roleScenicList)),
let list = try? JSONDecoder().decode([ScenicInfo].self, from: data) else {
return []
}
return list
}
/// role-permission
func matchRolePermissionItem(in list: [RolePermissionResponse]) -> RolePermissionResponse? {
RolePermissionMatcher.match(
in: list,
roleCode: roleCode,
roleName: roleName,
legacyRoleId: legacyRoleId
)
}
///
func clearLastLocationReportTime() {
defaults.removeObject(forKey: accountScopedKey(Key.lastLocationReportTime))
}
///
func hasScenicQueuePunchSpotSaved() -> Bool {
let id = scenicQueuePunchSpotId.trimmingCharacters(in: .whitespacesAndNewlines)
return Int64(id).map { $0 > 0 } ?? false
}
///
func saveScenicQueueSettingsSnapshot(_ snapshot: ScenicQueueSettingsSnapshot) {
guard let data = try? JSONEncoder().encode(snapshot) else { return }
defaults.set(data, forKey: accountScopedKey(Key.scenicQueueSettingsSnapshot))
}
///
func scenicQueueSettingsSnapshot() -> ScenicQueueSettingsSnapshot? {
guard let data = defaults.data(forKey: accountScopedKey(Key.scenicQueueSettingsSnapshot)) else { return nil }
return try? JSONDecoder().decode(ScenicQueueSettingsSnapshot.self, from: data)
}
/// 5
func saveScenicQueuePresetVoices(_ voices: [String]) {
let normalized = voices
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
.prefix(5)
defaults.set(Array(normalized), forKey: accountScopedKey(Key.scenicQueuePresetVoices))
}
///
func scenicQueuePresetVoices() -> [String] {
defaults.stringArray(forKey: accountScopedKey(Key.scenicQueuePresetVoices)) ?? []
}
///
func clearScenicQueueSnapshot() {
[
Key.scenicQueuePunchSpotId,
Key.scenicQueuePunchSpotName,
Key.scenicQueueRemark,
Key.scenicQueueSettingsSnapshot,
Key.scenicQueueOfflineTTS,
Key.scenicQueuePresetVoices,
].forEach { defaults.removeObject(forKey: accountScopedKey($0)) }
}
///
func clearPermissionSnapshot() {
let prefix = accountScopedKey("")
let keys = [
Key.rolePermissionList,
Key.permissionItems,
Key.roleScenicList,
Key.onlineStatus,
Key.lastLocationReportTime,
Key.locationReminderMinutes,
Key.isOpenReceiveVoice,
Key.scenicQueuePunchSpotId,
Key.scenicQueuePunchSpotName,
Key.scenicQueueRemark,
Key.scenicQueueSettingsSnapshot,
Key.scenicQueueOfflineTTS,
Key.scenicQueuePresetVoices,
]
keys.forEach { defaults.removeObject(forKey: prefix + $0) }
}
private func accountScopedKey(_ key: String) -> String {
"\(accountCachePrefix)_\(key)"
permissions.clear()
location.clear()
payment.clear()
scenicQueue.clear()
session.clearAuthenticatedSession()
}
}