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

92 lines
2.9 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 {
UserDefaults.standard.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] = []
///
2022-01-04 22:28:18 +08:00
@Published var messages: [MessageItem] = []
/// tab
@Published var tabSelectedIndex: Int {
didSet {
UserDefaults.standard.set(tabSelectedIndex, forKey: "PushDeer_tabSelectedIndex")
}
}
/// token
@Published var deviceToken: String = ""
///
@Published var userInfo: UserInfoContent?
/// UI
@Published var isShowTestPush: Bool {
didSet {
UserDefaults.standard.set(isShowTestPush, forKey: "PushDeer_isShowTestPush")
}
}
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() {
let _token = UserDefaults.standard.string(forKey: "PushDeer_token")
let _tabSelectedIndex = UserDefaults.standard.integer(forKey: "PushDeer_tabSelectedIndex")
let _isShowTestPush = UserDefaults.standard.object(forKey: "PushDeer_isShowTestPush")
token = _token ?? ""
tabSelectedIndex = _tabSelectedIndex
isShowTestPush = _isShowTestPush as? Bool ?? true
}
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)
}
}
case let .failure(error):
print(error)
}
//
throw NSError(domain: "登录失败", code: -1, userInfo: nil)
}
2022-01-04 22:28:18 +08:00
}