feat: add avatar and display name update logic after sign-up flow

This commit is contained in:
CMK 2021-03-08 19:05:15 +08:00
parent 091839c2e4
commit 1a66ba92c0
3 changed files with 47 additions and 7 deletions

View File

@ -111,7 +111,24 @@ extension MastodonConfirmEmailViewController {
case .failure(let error): 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) 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: case .finished:
break // upload avatar and set display name in the background
self.context.apiService.accountUpdateCredentials(
domain: self.viewModel.authenticateInfo.domain,
query: self.viewModel.updateCredentialQuery,
authorization: Mastodon.API.OAuth.Authorization(accessToken: self.viewModel.userToken.accessToken)
)
.retry(3)
.sink { completion in
switch completion {
case .failure(let error):
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: setup avatar & display name fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
case .finished:
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: setup avatar & display name success", ((#file as NSString).lastPathComponent), #line, #function)
}
} receiveValue: { _ in
// do nothing
}
.store(in: &self.context.disposeBag) // execute in the background
} }
} receiveValue: { response in } receiveValue: { response in
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: user %s's email confirmed", ((#file as NSString).lastPathComponent), #line, #function, response.value.username) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: user %s's email confirmed", ((#file as NSString).lastPathComponent), #line, #function, response.value.username)

View File

@ -12,20 +12,29 @@ import MastodonSDK
final class MastodonConfirmEmailViewModel { final class MastodonConfirmEmailViewModel {
var disposeBag = Set<AnyCancellable>() var disposeBag = Set<AnyCancellable>()
// input
let context: AppContext let context: AppContext
var email: String var email: String
let authenticateInfo: AuthenticationViewModel.AuthenticateInfo let authenticateInfo: AuthenticationViewModel.AuthenticateInfo
let userToken: Mastodon.Entity.Token let userToken: Mastodon.Entity.Token
let updateCredentialQuery: Mastodon.API.Account.UpdateCredentialQuery
let timestampUpdatePublisher = Timer.publish(every: 4.0, on: .main, in: .common) let timestampUpdatePublisher = Timer.publish(every: 4.0, on: .main, in: .common)
.autoconnect() .autoconnect()
.share() .share()
.eraseToAnyPublisher() .eraseToAnyPublisher()
init(context: AppContext, email: String, authenticateInfo: AuthenticationViewModel.AuthenticateInfo, userToken: Mastodon.Entity.Token) { init(
context: AppContext,
email: String,
authenticateInfo: AuthenticationViewModel.AuthenticateInfo,
userToken: Mastodon.Entity.Token,
updateCredentialQuery: Mastodon.API.Account.UpdateCredentialQuery
) {
self.context = context self.context = context
self.email = email self.email = email
self.authenticateInfo = authenticateInfo self.authenticateInfo = authenticateInfo
self.userToken = userToken self.userToken = userToken
self.updateCredentialQuery = updateCredentialQuery
} }
} }

View File

@ -5,12 +5,12 @@
// Created by MainasuK Cirno on 2021-2-5. // Created by MainasuK Cirno on 2021-2-5.
// //
import AlamofireImage
import Combine import Combine
import MastodonSDK import MastodonSDK
import os.log import os.log
import PhotosUI import PhotosUI
import UIKit import UIKit
import UITextField_Shake
final class MastodonRegisterViewController: UIViewController, NeedsDependency, OnboardingViewControllerAppearance { final class MastodonRegisterViewController: UIViewController, NeedsDependency, OnboardingViewControllerAppearance {
var disposeBag = Set<AnyCancellable>() var disposeBag = Set<AnyCancellable>()
@ -623,8 +623,8 @@ extension MastodonRegisterViewController {
username: username, username: username,
email: email, email: email,
password: password, password: password,
agreement: true, // TODO: agreement: true, // user confirmed in the server rules scene
locale: "en" // TODO: locale: Locale.current.languageCode ?? "en"
) )
// register without show server rules // register without show server rules
@ -646,7 +646,21 @@ extension MastodonRegisterViewController {
} receiveValue: { [weak self] response in } receiveValue: { [weak self] response in
guard let self = self else { return } guard let self = self else { return }
let userToken = response.value let userToken = response.value
let viewModel = MastodonConfirmEmailViewModel(context: self.context, email: email, authenticateInfo: self.viewModel.authenticateInfo, userToken: userToken) let updateCredentialQuery: Mastodon.API.Account.UpdateCredentialQuery = {
let displayName: String? = self.viewModel.displayName.value.isEmpty ? nil : self.viewModel.displayName.value
let avatar: Mastodon.Query.MediaAttachment? = {
guard let avatarImage = self.viewModel.avatarImage.value else { return nil }
guard avatarImage.size.width <= 400 else {
return .jpeg(avatarImage.af.imageScaled(to: CGSize(width: 400, height: 400)).jpegData(compressionQuality: 0.8))
}
return .jpeg(avatarImage.jpegData(compressionQuality: 0.8))
}()
return Mastodon.API.Account.UpdateCredentialQuery(
displayName: displayName,
avatar: avatar
)
}()
let viewModel = MastodonConfirmEmailViewModel(context: self.context, email: email, authenticateInfo: self.viewModel.authenticateInfo, userToken: userToken, updateCredentialQuery: updateCredentialQuery)
self.coordinator.present(scene: .mastodonConfirmEmail(viewModel: viewModel), from: self, transition: .show) self.coordinator.present(scene: .mastodonConfirmEmail(viewModel: viewModel), from: self, transition: .show)
} }
.store(in: &disposeBag) .store(in: &disposeBag)