Visual_Novel_iOS/crush/Crush/Src/Utils/AppDictManager.swift

209 lines
6.0 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.

//
// AppDictManager.swift
// Crush
//
// Created by Leon on 2025/7/29.
//
import Foundation
class AppDictManager{
static let shared = AppDictManager()
//
private enum CacheKey: String {
case aiDict = "aiDict"
case giftDict = "giftDict"
}
var aiDict: AIDictInfo?
var aiDictApiLoading: Bool = false
var gifts: [GiftDictModel]?
var giftDictApiLoading: Bool = false
var chatModels : [AIChatModel]?
init() {
//
loadAIDict(block: nil)
loadGiftDict(block: nil)
loadChatModelDict(block: nil)
// 🔥
loadCachedData()
NotificationCenter.default.addObserver(self, selector: #selector(receiveNetStatusChanged(_:)), name: AppNotificationName.networkChanged.notificationName, object: nil)
}
func loadRequiredDict(){
// init() called load
}
// MARK: - Public
func getGiftModelBy(giftId: Int, completion: ((GiftDictModel?) -> Void)?){
if let giftsObjs = gifts, giftsObjs.count > 0 {
// return where giftId = GiftDictModel.id
for gift in giftsObjs {
if gift.id == giftId {
completion?(gift)
return
}
}
}
loadGiftDict { ok in
if ok {
self.getGiftModelBy(giftId: giftId, completion: completion)
}else{
completion?(nil)
}
}
}
// MARK: -
///
private func loadCachedData() {
// AI
if let cachedAIDict = AppCache.fetchCache(key: CacheKey.aiDict.rawValue, type: AIDictInfo.self) {
aiDict = cachedAIDict
dlog("✅ 从缓存加载AI字典成功")
}
//
if let cachedGiftDict = AppCache.fetchCache(key: CacheKey.giftDict.rawValue, type: [GiftDictModel].self) {
gifts = cachedGiftDict
dlog("✅ 从缓存加载礼物字典成功")
}
}
/// AI
private func cacheAIDict(_ dict: AIDictInfo) {
AppCache.cache(key: CacheKey.aiDict.rawValue, value: dict)
}
///
private func cacheGiftDict(_ dict: [GiftDictModel]) {
AppCache.cache(key: CacheKey.giftDict.rawValue, value: dict)
}
// MARK: -
func loadAIDict(block: ((_ ok: Bool) -> Void)?){
if aiDictApiLoading{
block?(false)
return
}
//
if aiDict != nil {
block?(true)
return
}
aiDictApiLoading = true
CommonProvider.request(.getAIDict, modelType: AIDictInfo.self) {[weak self] result in
self?.aiDictApiLoading = false
switch result {
case .success(let success):
self?.aiDict = success
//
if let dict = success {
self?.cacheAIDict(dict)
}
if APIConfig.environment == .test {
dlog("AI Dict: \(CodableHelper.encode(success ?? AIDictInfo()) ?? Data())")
}
block?(true)
case .failure(_):
block?(false)
}
}
}
func loadGiftDict(block: ((_ ok: Bool) -> Void)?){
if giftDictApiLoading {
block?(false)
return
}
//
if gifts != nil {
block?(true)
return
}
giftDictApiLoading = true
var params = [String:Any]()
var pages = [String:Any]()
pages.updateValue(1, forKey: "pn")
pages.updateValue(200, forKey: "ps")
params.updateValue(pages, forKey: "page")
CommonProvider.request(.getGiftDict(params: params), modelType: ResponseContentPageData<GiftDictModel>.self) {[weak self] result in
self?.giftDictApiLoading = false
switch result {
case .success(let model):
if let datas = model?.datas{
// dlog("gift: \(datas)")
self?.gifts = datas
//
self?.cacheGiftDict(datas)
}
block?(true)
case .failure:
block?(false)
}
}
}
func loadChatModelDict(block: ((_ ok: Bool) -> Void)?){
CommonProvider.request(.getChatModelDict, modelType: [AIChatModel].self) {[weak self] result in
switch result {
case .success(let model):
self?.chatModels = model
block?(true)
case .failure:
block?(false)
}
}
}
// MARK: - Noti
@objc private func receiveNetStatusChanged(_ notification: Notification) {
let status = LTNetworkManage.ltManage.status
if status != .notReachable {
if aiDict == nil {
loadAIDict(block: nil)
}
if (gifts ?? []).count == 0 {
loadGiftDict(block: nil)
}
if (chatModels ?? []).count == 0 {
loadChatModelDict(block: nil)
}
}
}
// MARK: -
///
func clearCache() {
AppCache.removeCache(key: CacheKey.aiDict.rawValue)
AppCache.removeCache(key: CacheKey.giftDict.rawValue)
aiDict = nil
gifts = nil
dlog("✅ 清除字典缓存成功")
}
///
func refreshAllDicts(completion: @escaping (Bool) -> Void) {
clearCache()
loadAIDict { aiSuccess in
self.loadGiftDict { giftSuccess in
completion(aiSuccess && giftSuccess)
}
}
}
}