2021-02-23 08:38:05 +01:00
|
|
|
//
|
|
|
|
// MastodonConfirmEmailViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/2/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
2021-02-24 13:06:28 +01:00
|
|
|
import Foundation
|
|
|
|
import MastodonSDK
|
|
|
|
import os.log
|
2021-02-23 08:38:05 +01:00
|
|
|
|
|
|
|
final class MastodonConfirmEmailViewModel {
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-02-24 13:06:28 +01:00
|
|
|
|
2021-02-23 08:38:05 +01:00
|
|
|
let context: AppContext
|
2021-02-24 13:06:28 +01:00
|
|
|
let coordinator: SceneCoordinator
|
2021-02-23 08:38:05 +01:00
|
|
|
var email: String
|
2021-02-24 13:06:28 +01:00
|
|
|
let authenticateInfo: AuthenticationViewModel.AuthenticateInfo
|
|
|
|
let userToken: Mastodon.Entity.Token
|
|
|
|
|
|
|
|
let timestampUpdatePublisher = Timer.publish(every: 4.0, on: .main, in: .common)
|
|
|
|
.autoconnect()
|
|
|
|
.share()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
|
|
|
init(context: AppContext,coordinator: SceneCoordinator, email: String, authenticateInfo: AuthenticationViewModel.AuthenticateInfo, userToken: Mastodon.Entity.Token) {
|
2021-02-23 08:38:05 +01:00
|
|
|
self.context = context
|
2021-02-24 13:06:28 +01:00
|
|
|
self.coordinator = coordinator
|
2021-02-23 08:38:05 +01:00
|
|
|
self.email = email
|
2021-02-24 13:06:28 +01:00
|
|
|
self.authenticateInfo = authenticateInfo
|
|
|
|
self.userToken = userToken
|
|
|
|
timestampUpdatePublisher
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
AuthenticationViewModel.verifyAndSaveAuthentication(context: self.context, info: authenticateInfo, userToken: userToken)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: swap user access token swap fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
|
|
|
case .finished:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} receiveValue: { _ in
|
|
|
|
self.coordinator.setup()
|
|
|
|
}
|
|
|
|
.store(in: &self.disposeBag)
|
|
|
|
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-02-23 08:38:05 +01:00
|
|
|
}
|
|
|
|
}
|