jsdw_ios/QuickLocation/Manager/Account/AppContextManager.swift

193 lines
5.9 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 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 1SOS 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
}
}
}