diff --git a/Mastodon/Scene/Onboarding/ConfirmEmail/MastodonConfirmEmailViewController.swift b/Mastodon/Scene/Onboarding/ConfirmEmail/MastodonConfirmEmailViewController.swift index 2d69f0dd..dee1510c 100644 --- a/Mastodon/Scene/Onboarding/ConfirmEmail/MastodonConfirmEmailViewController.swift +++ b/Mastodon/Scene/Onboarding/ConfirmEmail/MastodonConfirmEmailViewController.swift @@ -111,7 +111,24 @@ extension MastodonConfirmEmailViewController { 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 + // 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 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) diff --git a/Mastodon/Scene/Onboarding/ConfirmEmail/MastodonConfirmEmailViewModel.swift b/Mastodon/Scene/Onboarding/ConfirmEmail/MastodonConfirmEmailViewModel.swift index aff25474..9fbd24ed 100644 --- a/Mastodon/Scene/Onboarding/ConfirmEmail/MastodonConfirmEmailViewModel.swift +++ b/Mastodon/Scene/Onboarding/ConfirmEmail/MastodonConfirmEmailViewModel.swift @@ -12,20 +12,29 @@ import MastodonSDK final class MastodonConfirmEmailViewModel { var disposeBag = Set() + // input let context: AppContext var email: String let authenticateInfo: AuthenticationViewModel.AuthenticateInfo let userToken: Mastodon.Entity.Token + let updateCredentialQuery: Mastodon.API.Account.UpdateCredentialQuery let timestampUpdatePublisher = Timer.publish(every: 4.0, on: .main, in: .common) .autoconnect() .share() .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.email = email self.authenticateInfo = authenticateInfo self.userToken = userToken + self.updateCredentialQuery = updateCredentialQuery } } diff --git a/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewController.swift b/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewController.swift index d1ef11c6..f078e9b8 100644 --- a/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewController.swift +++ b/Mastodon/Scene/Onboarding/Register/MastodonRegisterViewController.swift @@ -5,12 +5,12 @@ // Created by MainasuK Cirno on 2021-2-5. // +import AlamofireImage import Combine import MastodonSDK import os.log import PhotosUI import UIKit -import UITextField_Shake final class MastodonRegisterViewController: UIViewController, NeedsDependency, OnboardingViewControllerAppearance { var disposeBag = Set() @@ -623,10 +623,10 @@ extension MastodonRegisterViewController { username: username, email: email, password: password, - agreement: true, // TODO: - locale: "en" // TODO: + agreement: true, // user confirmed in the server rules scene + locale: Locale.current.languageCode ?? "en" ) - + // register without show server rules context.apiService.accountRegister( domain: viewModel.domain, @@ -646,7 +646,21 @@ extension MastodonRegisterViewController { } receiveValue: { [weak self] response in guard let self = self else { return } 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) } .store(in: &disposeBag)