193 lines
5.9 KiB
Swift
193 lines
5.9 KiB
Swift
//
|
||
// 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 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 UIImage(named: "UserIcon/\(account.head_pic)") ?? UIImage()
|
||
}
|
||
var head_pic: String {
|
||
account?.head_pic ?? ""
|
||
}
|
||
/// 用户名
|
||
var name: String {
|
||
account?.name ?? ""
|
||
}
|
||
/// 用户手机号
|
||
var userPhone: String {
|
||
return account?.phone ?? ""
|
||
}
|
||
/// 性别
|
||
var sex: Int {
|
||
account?.sex ?? -1
|
||
}
|
||
/// VIP
|
||
var vip: Int {
|
||
account?.vip ?? 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
|
||
|
||
return true
|
||
}
|
||
|
||
public func saveToken(_ token: String) {
|
||
Defaults[\.loginToken] = token
|
||
}
|
||
|
||
public func deleteAccount() {
|
||
Defaults[\.loginToken] = nil
|
||
Defaults[\.userConfig] = nil
|
||
loginAccount = 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
|
||
}
|
||
}
|
||
}
|