Files
ios-client/IosClientKit/Sources/CoreRepository/FakeSessionRepository.swift

40 lines
1.3 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.
import Combine
import CoreDomain
import Foundation
/// Fake 会话仓库(登录态在进程内共享,默认未登录)。
public final class FakeSessionRepository: SessionRepository, @unchecked Sendable {
private let isLoggedIn = CurrentValueSubject<Bool, Never>(false)
public init() {}
public func observeIsLoggedIn() -> AsyncThrowingStream<Bool, Error> {
AsyncThrowingStream { continuation in
let cancellable = self.isLoggedIn.sink { continuation.yield($0) }
continuation.onTermination = { _ in cancellable.cancel() }
}
}
public func requestSmsCode(phone: String) async throws {
try await Task.sleep(for: .milliseconds(500))
guard phone.range(of: #"^1\d{10}$"#, options: .regularExpression) != nil else {
throw SessionError.invalidPhone
}
}
public func login(phone: String, code: String) async throws {
try await Task.sleep(for: .milliseconds(500))
guard code == "123456" else {
throw SessionError.wrongCode
}
guard phone == "13812341234" else {
throw SessionError.phoneNotMatched
}
isLoggedIn.value = true
}
public func logout() async throws {
isLoggedIn.value = false
}
}