Files
2026-06-26 14:33:31 +08:00

47 lines
1.1 KiB
Swift
Raw Permalink 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.

//
// AppSession.swift
// suixinkan
//
// Created by Codex on 2026/6/18.
//
import Foundation
///
enum AuthPhase: Equatable {
case loggedOut
case restoring
case loggedIn
}
@MainActor
/// token
final class AppSession {
var onChange: (() -> Void)?
private(set) var phase: AuthPhase = .loggedOut { didSet { onChange?() } }
private(set) var token: String? { didSet { onChange?() } }
var isLoggedIn: Bool {
phase == .loggedIn
}
///
func beginRestoring(token: String? = nil) {
self.token = token?.trimmingCharacters(in: .whitespacesAndNewlines)
phase = .restoring
}
/// token
func markLoggedIn(token: String? = nil) {
self.token = token?.trimmingCharacters(in: .whitespacesAndNewlines)
phase = .loggedIn
}
/// token
func logout() {
token = nil
phase = .loggedOut
}
}