jsdw_ios/QuickLocation/Manager/Account/AppContextManager.swift

226 lines
7.2 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.

//
// AppContextManager.swift
// SHECommunity
//
// Created by 林 on 2024/11/27.
//
import Foundation
import SwiftyUserDefaults
class AppContextManager: NSObject {
static let shared = AppContextManager()
private var loginAccount: UserConfigModel?
var systemConfig: SystemConfigModel?
var imToken: String?
var imTokenUserId: String?
var account: UserConfigModel? {
if let loginAccount = loginAccount {
return loginAccount
}
guard let jsonData = Defaults[\.userConfig] else { return nil }
guard let loginAccount = UserConfigModel(data: jsonData) else { return nil }
self.loginAccount = loginAccount
return loginAccount
}
/// 是否游客
var isGuest: Bool {
return account?.temp == true
}
/// token
var token: String {
account?.token ?? ""
}
/// 用户ID
var userId: String {
account?.uid ?? ""
}
/// 用户头像
var avaterIcon: UIImage {
guard let account = account else { return UIImage() }
return HeadPic.image(for: account.displayHeadPic)
}
var head_pic: String {
guard let account else { return "" }
let headPic = account.head_pic.trimmingCharacters(in: .whitespacesAndNewlines)
if !headPic.isEmpty { return headPic }
return account.avater?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
}
/// 用户名
var name: String {
account?.name ?? ""
}
/// 用户手机号
var userPhone: String {
return account?.phone ?? ""
}
/// 性别
var sex: Int {
account?.sex ?? -1
}
/// 状态:0、无状态 1、SOS 2、气泡等
var status: Int {
account?.status ?? 0
}
@discardableResult
public func saveAccount(_ account: UserConfigModel) -> Bool {
var tmpAccount = account
if let token = account.token {
Defaults[\.loginToken] = token
}
else if let loginToken = Defaults[\.loginToken] {
tmpAccount.token = loginToken
}
guard let jsonData = tmpAccount.toData() else {
return false
}
Defaults[\.userConfig] = jsonData
loginAccount = tmpAccount
AppUnlockCoordinator.shared.checkOfflineUnlockForCurrentUser()
return true
}
public func saveToken(_ token: String) {
Defaults[\.loginToken] = token
}
/// 状态:0、无状态 1、SOS 2、气泡等
public func updateAccountStatus(_ status: Int) {
guard var account = self.account else { return }
account.status = status
saveAccount(account)
}
/// 心情:0、无状态 1、开心 2、愤怒
public func updateAccountMood(_ mood: Int) {
guard var account = self.account else { return }
account.mood = mood
saveAccount(account)
}
/// 同步服务端返回的用户状态和心情。
public func updateAccountState(status: Int, mood: Int) {
guard var account = self.account else { return }
account.status = status
account.mood = mood
saveAccount(account)
}
var defaultGroupKey: String {
get { Defaults[\.defaultGroupKey] }
set { Defaults[\.defaultGroupKey] = newValue }
}
public func deleteAccount() {
AppUnlockCoordinator.shared.accountDidLogout(userId: userId)
Defaults[\.loginToken] = nil
Defaults[\.userConfig] = nil
Defaults[\.defaultGroupKey] = ""
loginAccount = nil
imToken = nil
imTokenUserId = nil
}
}
extension AppContextManager {
public func saveLocationHistory(data: [String: Any]) {
guard let latitude = data["latitude"] as? CGFloat,
let longitude = data["longitude"] as? CGFloat else { return }
guard let account = loginAccount, let userId = account.uid else {
DLToast.showError(text: "用户不存在")
return
}
if let searchHistory = Defaults[\.locationHistory] {
var searchList: [[String: Any]] = searchHistory[userId] ?? [[:]]
for historyData in searchList {
// 记录存在
if historyData["latitude"] as! CGFloat == latitude,
historyData["longitude"] as! CGFloat == longitude {
let sortList = searchList.sorted { (data1, data2) -> Bool in
if latitude == data1["latitude"] as! CGFloat,
longitude == data1["longitude"] as! CGFloat {
return true
}
return false
}
searchList = sortList
}
// 记录不存在
else {
searchList.insert(data, at: 0)
}
}
let history: [String: [[String: Any]]] = [userId: searchList]
Defaults[\.locationHistory] = history
}
else {
let history: [String: [[String: Any]]] = [userId: [data]]
Defaults[\.locationHistory] = history
}
}
public func cleanLocationHistory() {
if let account = loginAccount, let userId = account.uid {
let history: [String: [[String: Any]]] = [userId: []]
Defaults[\.locationHistory] = history
}
}
public func saveSearchHistory(searchText: String) {
if let account = loginAccount, let userId = account.uid {
if let searchHistory = Defaults[\.searchHistory] {
var searchList: [String] = searchHistory[userId] ?? []
if searchList.contains(searchText) {
let sortList = searchList.sorted { (text1, text2) -> Bool in
if searchText == text1 {
return true
}
return false
}
searchList = sortList
}
else {
searchList.insert(searchText, at: 0)
}
let history: [String: [String]] = [userId: searchList]
Defaults[\.searchHistory] = history
}
else {
let history: [String: [String]] = [userId: [searchText]]
Defaults[\.searchHistory] = history
}
}
}
public func deleteSearchHistory(searchText: String) {
// if let account = loginAccount, let userId = account.id {
// if let userSearchHistory = Defaults[\.userSearchHistory] {
// var searchList: [String] = userSearchHistory[userId] ?? []
// if searchList.contains(searchText) {
// searchList.removeObject(searchText)
// }
// let history: [String: [String]] = [userId: searchList]
// Defaults[\.userSearchHistory] = history
// }
// }
}
public func clearnSearchHistory() {
if let account = loginAccount, let userId = account.uid {
let history: [String: [String]] = [userId: []]
Defaults[\.searchHistory] = history
}
}
}