Files
suixinkan_uikit/suixinkan/Features/Home/Services/HomeCommonMenuStore.swift
汉秋 c083f1d4b3 升级 AppStore 缓存 Key 并在覆盖安装时强制重新登录。
统一账号作用域 Key 对齐 Android,新增缓存 schema 迁移清理旧数据,并同步更新相关模块与单元测试。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-14 09:35:55 +08:00

66 lines
2.5 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.

//
// HomeCommonMenuStore.swift
// suixinkan
//
import Foundation
/// URI
final class HomeCommonMenuStore {
private let defaults: UserDefaults
init(defaults: UserDefaults = .standard) {
self.defaults = defaults
}
/// URI
func savedCommonURIs(accountScope: String, roleCode: String) -> [String] {
guard let key = storageKey(accountScope: accountScope, roleCode: roleCode) else { return [] }
return defaults.stringArray(forKey: key) ?? []
}
/// URI
func saveCommonURIs(_ uris: [String], accountScope: String, roleCode: String) {
guard let key = storageKey(accountScope: accountScope, roleCode: roleCode) else { return }
defaults.set(uris, forKey: key)
}
/// URI 4
static func defaultCommonURIs(from permissions: [HomePermissionItem]) -> [String] {
let uris = permissions.map(\.uri)
return uris.count > 4 ? Array(uris.prefix(4)) : uris
}
///
func commonMenus(
from allMenus: [HomeMenuItem],
permissions: [HomePermissionItem],
accountScope: String,
roleCode: String
) -> [HomeMenuItem] {
let saved = savedCommonURIs(accountScope: accountScope, roleCode: roleCode)
let defaultURIs = Self.defaultCommonURIs(from: permissions)
let commonURIs = saved.isEmpty ? defaultURIs : saved
return Self.splitMenus(from: allMenus, commonURIs: commonURIs).common
}
/// URI
static func splitMenus(
from allMenus: [HomeMenuItem],
commonURIs: [String]
) -> (common: [HomeMenuItem], more: [HomeMenuItem]) {
let commonSet = Set(commonURIs)
let common = allMenus.filter { commonSet.contains($0.uri) }
let more = allMenus.filter { !commonSet.contains($0.uri) }
return (common, more)
}
private func storageKey(accountScope: String, roleCode: String) -> String? {
let normalizedScope = accountScope.trimmingCharacters(in: .whitespacesAndNewlines)
let normalizedRoleCode = roleCode.trimmingCharacters(in: .whitespacesAndNewlines)
guard !normalizedScope.isEmpty, !normalizedRoleCode.isEmpty else { return nil }
return "\(normalizedScope)_role_\(normalizedRoleCode)_common_uris"
}
}