40 lines
1.3 KiB
Swift
40 lines
1.3 KiB
Swift
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
|
||
}
|
||
}
|