Files
suixinkan_uikit/suixinkan/DataStore/AppStore.swift

65 lines
1.6 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.

//
// AppStore.swift
// suixinkan
//
import Foundation
/// Android `AppStoreDataSource`
/// Token
final class AppStore {
static let shared = AppStore()
private enum Key {
static let token = "key_in_token"
static let lastLoginUsername = "key_last_login_username"
static let privacyAgreementAccepted = "key_privacy_agreement_accepted"
}
private let defaults: UserDefaults
init(defaults: UserDefaults = .standard) {
self.defaults = defaults
}
/// Token
var token: String {
get { defaults.string(forKey: Key.token) ?? "" }
set { defaults.set(newValue, forKey: Key.token) }
}
///
var lastLoginUsername: String? {
get { defaults.string(forKey: Key.lastLoginUsername) }
set {
if let newValue {
defaults.set(newValue, forKey: Key.lastLoginUsername)
} else {
defaults.removeObject(forKey: Key.lastLoginUsername)
}
}
}
///
var privacyAgreementAccepted: Bool {
get { defaults.bool(forKey: Key.privacyAgreementAccepted) }
set { defaults.set(newValue, forKey: Key.privacyAgreementAccepted) }
}
/// Token
var isLoggedIn: Bool {
!token.isEmpty
}
/// Token
func saveToken(_ token: String) {
self.token = token
}
///
func logout() {
token = ""
}
}