pushdeer/ios/PushDeer-iOS/PushDeer/Service/AppState.swift

198 lines
7.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.

//
// AppState.swift
// PushDeer
//
// Created by HEXT on 2022/1/4.
//
import Foundation
import AuthenticationServices
class AppState: ObservableObject {
/// token
@Published var token : String = "" {
didSet {
getUserDefaults().set(token, forKey: "PushDeer_token")
}
}
///
@Published var devices: [DeviceItem] = []
/// key
@Published var keys: [KeyItem] = []
///
// @Published var messages: [MessageItem] = []
/// tab
@Published var tabSelectedIndex: Int = 2 {
didSet {
getUserDefaults().set(tabSelectedIndex, forKey: "PushDeer_tabSelectedIndex")
}
}
/// token
@Published var deviceToken: String = ""
///
@Published var userInfo: UserInfoContent?
/// UI
@Published var isShowTestPush: Bool = true {
didSet {
getUserDefaults().set(isShowTestPush, forKey: "PushDeer_isShowTestPush")
}
}
/// 使
@Published var isUseBuiltInBrowser: Bool = true {
didSet {
getUserDefaults().set(isUseBuiltInBrowser, forKey: "PushDeer_isUseBuiltInBrowser")
}
}
/// MarkDown BaseURL
@Published var markDownBaseURL: String? {
didSet {
getUserDefaults().set(markDownBaseURL, forKey: "PushDeer_markDownBaseURL")
}
}
/// API endpoint
@Published var api_endpoint : String = "" {
didSet {
getUserDefaults().set(api_endpoint, forKey: "PushDeer_api_endpoint")
}
}
var isAppClip: Bool {
#if APPCLIP
return true
#else
return false
#endif
}
static let shared = AppState()
private init() {
reloadUserDefaults()
moveOldUserDefaults()
}
func getUserDefaults() -> UserDefaults {
let ud = UserDefaults(suiteName: Env.appGroupId)
if let ud = ud {
return ud
} else {
return UserDefaults.standard
}
}
func reloadUserDefaults() -> Void {
let _token = getUserDefaults().string(forKey: "PushDeer_token")
let _tabSelectedIndex = getUserDefaults().integer(forKey: "PushDeer_tabSelectedIndex")
let _isShowTestPush = getUserDefaults().object(forKey: "PushDeer_isShowTestPush")
let _isUseBuiltInBrowser = getUserDefaults().object(forKey: "PushDeer_isUseBuiltInBrowser")
let _markDownBaseURL = getUserDefaults().string(forKey: "PushDeer_markDownBaseURL")
let _api_endpoint = getUserDefaults().string(forKey: "PushDeer_api_endpoint")
token = _token ?? ""
tabSelectedIndex = _tabSelectedIndex
isShowTestPush = _isShowTestPush as? Bool ?? true
isUseBuiltInBrowser = _isUseBuiltInBrowser as? Bool ?? true
markDownBaseURL = _markDownBaseURL
api_endpoint = _api_endpoint ?? ""
}
/// , ,
func moveOldUserDefaults() -> Void {
let oldUserDefaults = UserDefaults.standard
if let _token = oldUserDefaults.string(forKey: "PushDeer_token") {
oldUserDefaults.removeObject(forKey: "PushDeer_token")
token = _token
}
if let _tabSelectedIndex = oldUserDefaults.object(forKey: "PushDeer_tabSelectedIndex") as? Int {
oldUserDefaults.removeObject(forKey: "PushDeer_tabSelectedIndex")
tabSelectedIndex = _tabSelectedIndex
}
if let _isShowTestPush = oldUserDefaults.object(forKey: "PushDeer_isShowTestPush") as? Bool {
oldUserDefaults.removeObject(forKey: "PushDeer_isShowTestPush")
isShowTestPush = _isShowTestPush
}
if let _isUseBuiltInBrowser = oldUserDefaults.object(forKey: "PushDeer_isUseBuiltInBrowser") as? Bool {
oldUserDefaults.removeObject(forKey: "PushDeer_isUseBuiltInBrowser")
isUseBuiltInBrowser = _isUseBuiltInBrowser
}
if let _markDownBaseURL = oldUserDefaults.string(forKey: "PushDeer_markDownBaseURL") {
oldUserDefaults.removeObject(forKey: "PushDeer_markDownBaseURL")
markDownBaseURL = _markDownBaseURL
}
if let _api_endpoint = oldUserDefaults.string(forKey: "PushDeer_api_endpoint") {
oldUserDefaults.removeObject(forKey: "PushDeer_api_endpoint")
api_endpoint = _api_endpoint
}
}
func loginAfter() {
Task {
// UserInfo
self.userInfo = try await HttpRequest.getUserInfo()
var stoken = self.userInfo?.simple_token
if isEmpty(stoken) {
// UserInfo stoken ,
stoken = try await HttpRequest.stokenRegen().stoken
}
if isNotEmpty(stoken) {
// stoken ,
self.saveSTokenToLocal(stoken: stoken)
}
}
}
func saveSTokenToLocal(stoken: String?) -> Void {
let key = "stoken_\(self.api_endpoint)"
getUserDefaults().set(stoken, forKey: key)
}
func getLocalSToken() -> String? {
let key = "stoken_\(self.api_endpoint)"
return getUserDefaults().string(forKey: key)
}
func deleteLocalSToken() -> Void {
let key = "stoken_\(self.api_endpoint)"
getUserDefaults().removeObject(forKey: key)
}
func appleIdLogin(_ result: Result<ASAuthorization, Error>) async throws -> TokenContent {
switch result {
case let .success(authorization):
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
// IDAPP
print(appleIDCredential.user) // 000791.7a323f1326dd4674bc16d32fd6339875.1424
// emailfullName->Apple ID->->使AppleIDApp APP
print(appleIDCredential.email as Any) // easychen@qq.com
print(appleIDCredential.fullName as Any) // givenName: lijie familyName: chen
// JWTtoken.3, base64
let idToken = String(data:appleIDCredential.identityToken!, encoding: .utf8)
print(idToken as Any)
do {
//
let result = try await HttpRequest.login(idToken: idToken!)
print(result)
//
return result
} catch {
print(error)
//
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "\n\(error.localizedDescription)", code: -4, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-4)\n\(error.localizedDescription)"])
}
} else {
// Apple
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示"), code: -3, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-3)"])
}
case let .failure(error):
print(error)
if (error as NSError).code == 1001 {
// Apple
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "\n\(error.localizedDescription)", code: 1001, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("你已取消授权", comment: "")])
}
// Apple
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "\n\(error.localizedDescription)", code: -2, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-2)\n\(error.localizedDescription)"])
}
}
}