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

169 lines
6.7 KiB
Swift
Raw Normal View History

2022-01-04 22:28:18 +08:00
//
// AppState.swift
// PushDeer
//
// Created by HEXT on 2022/1/4.
//
import Foundation
import AuthenticationServices
2022-01-04 22:28:18 +08:00
class AppState: ObservableObject {
/// token
@Published var token : String = "" {
didSet {
getUserDefaults().set(token, forKey: "PushDeer_token")
}
}
///
2022-01-04 22:28:18 +08:00
@Published var devices: [DeviceItem] = []
/// key
2022-01-04 22:28:18 +08:00
@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
}
2022-01-04 22:28:18 +08:00
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 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)"])
}
}
2022-01-04 22:28:18 +08:00
}