From 08302cf2fab64d0fc47b1108960506703adcc6b6 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Fri, 11 Nov 2022 18:44:01 +0100 Subject: [PATCH] Move some code around (#540) and remove an obsolete property --- .../Login/MastodonLoginViewController.swift | 46 ++++++++++++++++ .../MastodonPickServerViewController.swift | 54 ------------------- .../MastodonPickServerViewModel.swift | 15 ++---- .../Welcome/WelcomeViewController.swift | 2 +- 4 files changed, 50 insertions(+), 67 deletions(-) diff --git a/Mastodon/Scene/Onboarding/Login/MastodonLoginViewController.swift b/Mastodon/Scene/Onboarding/Login/MastodonLoginViewController.swift index f0c47885f..5797c2ae2 100644 --- a/Mastodon/Scene/Onboarding/Login/MastodonLoginViewController.swift +++ b/Mastodon/Scene/Onboarding/Login/MastodonLoginViewController.swift @@ -56,6 +56,52 @@ class MastodonLoginViewController: UIViewController { @objc func nextButtonPressed(_ sender: Any) { delegate?.nextButtonPressed(self) } + + @objc func login(_ sender: Any) { +// guard let server = viewModel.selectedServer.value else { return } +// authenticationViewModel.isAuthenticating.send(true) +// context.apiService.createApplication(domain: server.domain) +// .tryMap { response -> AuthenticationViewModel.AuthenticateInfo in +// let application = response.value +// guard let info = AuthenticationViewModel.AuthenticateInfo( +// domain: server.domain, +// application: application, +// redirectURI: response.value.redirectURI ?? APIService.oauthCallbackURL +// ) else { +// throw APIService.APIError.explicit(.badResponse) +// } +// return info +// } +// .receive(on: DispatchQueue.main) +// .sink { [weak self] completion in +// guard let self = self else { return } +// self.authenticationViewModel.isAuthenticating.send(false) +// +// switch completion { +// case .failure(let error): +// os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: sign in fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription) +// self.viewModel.error.send(error) +// case .finished: +// break +// } +// } receiveValue: { [weak self] info in +// guard let self = self else { return } +// let authenticationController = MastodonAuthenticationController( +// context: self.context, +// authenticateURL: info.authorizeURL +// ) +// +// self.mastodonAuthenticationController = authenticationController +// authenticationController.authenticationSession?.presentationContextProvider = self +// authenticationController.authenticationSession?.start() +// +// self.authenticationViewModel.authenticate( +// info: info, +// pinCodePublisher: authenticationController.pinCodePublisher +// ) +// } +// .store(in: &disposeBag) + } } // MARK: - OnboardingViewControllerAppearance diff --git a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift index 7ec24a246..2fa46cac9 100644 --- a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift +++ b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift @@ -270,60 +270,6 @@ extension MastodonPickServerViewController { } @objc private func nextButtonDidPressed(_ sender: UIButton) { - switch viewModel.mode { - case .signIn: doSignIn() - case .signUp: doSignUp() - } - } - - //TODO: @zeitschlag Move to MastodonLoginViewController - private func doSignIn() { - guard let server = viewModel.selectedServer.value else { return } - authenticationViewModel.isAuthenticating.send(true) - context.apiService.createApplication(domain: server.domain) - .tryMap { response -> AuthenticationViewModel.AuthenticateInfo in - let application = response.value - guard let info = AuthenticationViewModel.AuthenticateInfo( - domain: server.domain, - application: application, - redirectURI: response.value.redirectURI ?? APIService.oauthCallbackURL - ) else { - throw APIService.APIError.explicit(.badResponse) - } - return info - } - .receive(on: DispatchQueue.main) - .sink { [weak self] completion in - guard let self = self else { return } - self.authenticationViewModel.isAuthenticating.send(false) - - switch completion { - case .failure(let error): - os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: sign in fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription) - self.viewModel.error.send(error) - case .finished: - break - } - } receiveValue: { [weak self] info in - guard let self = self else { return } - let authenticationController = MastodonAuthenticationController( - context: self.context, - authenticateURL: info.authorizeURL - ) - - self.mastodonAuthenticationController = authenticationController - authenticationController.authenticationSession?.presentationContextProvider = self - authenticationController.authenticationSession?.start() - - self.authenticationViewModel.authenticate( - info: info, - pinCodePublisher: authenticationController.pinCodePublisher - ) - } - .store(in: &disposeBag) - } - - private func doSignUp() { os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function) guard let server = viewModel.selectedServer.value else { return } authenticationViewModel.isAuthenticating.send(true) diff --git a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift index 719de4cb4..ff6806b71 100644 --- a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift +++ b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift @@ -17,12 +17,7 @@ import MastodonCore import MastodonUI class MastodonPickServerViewModel: NSObject { - - enum PickServerMode { - case signUp - case signIn - } - + enum EmptyStateViewState { case none case loading @@ -34,7 +29,6 @@ class MastodonPickServerViewModel: NSObject { let serverSectionHeaderView = PickServerServerSectionTableHeaderView() // input - let mode: PickServerMode let context: AppContext var categoryPickerItems: [CategoryPickerItem] = { var items: [CategoryPickerItem] = [] @@ -72,9 +66,8 @@ class MastodonPickServerViewModel: NSObject { let loadingIndexedServersError = CurrentValueSubject(nil) let emptyStateViewState = CurrentValueSubject(.none) - init(context: AppContext, mode: PickServerMode) { + init(context: AppContext) { self.context = context - self.mode = mode super.init() configure() @@ -115,9 +108,7 @@ extension MastodonPickServerViewModel { .map { indexedServers, selectCategoryItem, searchText -> [Mastodon.Entity.Server] in // ignore approval required servers when sign-up var indexedServers = indexedServers - if self.mode == .signUp { - indexedServers = indexedServers.filter { !$0.approvalRequired } - } + indexedServers = indexedServers.filter { !$0.approvalRequired } // Note: // sort by calculate last week users count // and make medium size (~800) server to top diff --git a/Mastodon/Scene/Onboarding/Welcome/WelcomeViewController.swift b/Mastodon/Scene/Onboarding/Welcome/WelcomeViewController.swift index 36f9bf7e4..5a9af3dd9 100644 --- a/Mastodon/Scene/Onboarding/Welcome/WelcomeViewController.swift +++ b/Mastodon/Scene/Onboarding/Welcome/WelcomeViewController.swift @@ -317,7 +317,7 @@ extension WelcomeViewController { extension WelcomeViewController { @objc private func signUpButtonDidClicked(_ sender: UIButton) { - _ = coordinator.present(scene: .mastodonPickServer(viewMode: MastodonPickServerViewModel(context: context, mode: .signUp)), from: self, transition: .show) + _ = coordinator.present(scene: .mastodonPickServer(viewMode: MastodonPickServerViewModel(context: context)), from: self, transition: .show) } @objc