From 7d1c8e5be9de480772a45a0967f2d1e8fb08de08 Mon Sep 17 00:00:00 2001 From: CMK Date: Fri, 5 Feb 2021 17:53:00 +0800 Subject: [PATCH] feat: [WIP] implement sign up scene --- Mastodon.xcodeproj/project.pbxproj | 18 ++ Mastodon/Coordinator/SceneCoordinator.swift | 5 + .../AuthenticationViewController.swift | 62 +++-- .../AuthenticationViewModel.swift | 10 + .../MastodonRegisterViewController.swift | 227 ++++++++++++++++++ .../Register/MastodonRegisterViewModel.swift | 29 +++ .../PublicTimelineViewController.swift | 2 +- .../PublicTimelineViewModel.swift | 7 - .../TimelineBottomLoaderTableViewCell.swift | 3 +- .../APIService/APIService+Account.swift | 13 + .../APIService/APIService+HomeTimeline.swift | 6 +- .../APIService+PublicTimeline.swift | 6 +- .../CoreData/APIService+CoreData+Toot.swift | 4 +- .../Persist/APIService+Persist+Timeline.swift | 2 +- .../API/Mastodon+API+Account.swift | 49 ++++ .../API/Mastodon+API+Timeline.swift | 36 +-- .../Entity/Mastodon+Entity+Status.swift | 4 +- 17 files changed, 430 insertions(+), 53 deletions(-) create mode 100644 Mastodon/Scene/Authentication/Register/MastodonRegisterViewController.swift create mode 100644 Mastodon/Scene/Authentication/Register/MastodonRegisterViewModel.swift diff --git a/Mastodon.xcodeproj/project.pbxproj b/Mastodon.xcodeproj/project.pbxproj index 2ef2884b7..63bc2deb2 100644 --- a/Mastodon.xcodeproj/project.pbxproj +++ b/Mastodon.xcodeproj/project.pbxproj @@ -104,6 +104,8 @@ DB98338825C945ED00AD9700 /* Assets.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB98338625C945ED00AD9700 /* Assets.swift */; }; DB98339C25C96DE600AD9700 /* APIService+Account.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB98339B25C96DE600AD9700 /* APIService+Account.swift */; }; DBD4ED1125CC0FEB0041B741 /* HomeViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBD4ED1025CC0FEB0041B741 /* HomeViewModel.swift */; }; + DBE0821525CD382600FD6BBD /* MastodonRegisterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBE0821425CD382600FD6BBD /* MastodonRegisterViewController.swift */; }; + DBE0822425CD3F1E00FD6BBD /* MastodonRegisterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBE0822325CD3F1E00FD6BBD /* MastodonRegisterViewModel.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -265,6 +267,8 @@ DB98338625C945ED00AD9700 /* Assets.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Assets.swift; sourceTree = ""; }; DB98339B25C96DE600AD9700 /* APIService+Account.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "APIService+Account.swift"; sourceTree = ""; }; DBD4ED1025CC0FEB0041B741 /* HomeViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewModel.swift; sourceTree = ""; }; + DBE0821425CD382600FD6BBD /* MastodonRegisterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MastodonRegisterViewController.swift; sourceTree = ""; }; + DBE0822325CD3F1E00FD6BBD /* MastodonRegisterViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MastodonRegisterViewModel.swift; sourceTree = ""; }; DBF53F5F25C14E88008AAC7B /* Mastodon.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = Mastodon.xctestplan; path = Mastodon/Mastodon.xctestplan; sourceTree = ""; }; DBF53F6025C14E9D008AAC7B /* MastodonSDK.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = MastodonSDK.xctestplan; sourceTree = ""; }; EC6E707B68A67DB08EC288FA /* Pods-MastodonTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MastodonTests.debug.xcconfig"; path = "Target Support Files/Pods-MastodonTests/Pods-MastodonTests.debug.xcconfig"; sourceTree = ""; }; @@ -465,6 +469,7 @@ isa = PBXGroup; children = ( DB0140A625C40C0900F9F3CF /* PinBased */, + DBE0821A25CD382900FD6BBD /* Register */, DB01409525C40B6700F9F3CF /* AuthenticationViewController.swift */, DB98334625C8056600AD9700 /* AuthenticationViewModel.swift */, ); @@ -756,6 +761,15 @@ path = HomeTimeline; sourceTree = ""; }; + DBE0821A25CD382900FD6BBD /* Register */ = { + isa = PBXGroup; + children = ( + DBE0821425CD382600FD6BBD /* MastodonRegisterViewController.swift */, + DBE0822325CD3F1E00FD6BBD /* MastodonRegisterViewModel.swift */, + ); + path = Register; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -1122,6 +1136,7 @@ DB8AF52F25C13561002E6C99 /* DocumentStore.swift in Sources */, DB98337F25C9452D00AD9700 /* APIService+APIError.swift in Sources */, 2DF123A725C3B0210020F248 /* ActiveLabel.swift in Sources */, + DBE0822425CD3F1E00FD6BBD /* MastodonRegisterViewModel.swift in Sources */, DB5086A525CC0B7000C2C187 /* AvatarBarButtonItem.swift in Sources */, 2D76316B25C14D4C00929FB9 /* PublicTimelineViewModel.swift in Sources */, 2D46975E25C2A54100CF4AA9 /* NSLayoutConstraint.swift in Sources */, @@ -1143,6 +1158,7 @@ DB427DD625BAA00100D1B89D /* AppDelegate.swift in Sources */, DB98338725C945ED00AD9700 /* Strings.swift in Sources */, DB45FAB625CA5485005A8AC7 /* UIAlertController.swift in Sources */, + DBE0821525CD382600FD6BBD /* MastodonRegisterViewController.swift in Sources */, DB98336B25C9420100AD9700 /* APIService+App.swift in Sources */, DB8AF54425C13647002E6C99 /* SceneCoordinator.swift in Sources */, DB45FAF925CA80A2005A8AC7 /* APIService+CoreData+MastodonAuthentication.swift in Sources */, @@ -1405,6 +1421,7 @@ MARKETING_VERSION = 1.0.0; PRODUCT_BUNDLE_IDENTIFIER = org.joinmastodon.Mastodon; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; @@ -1429,6 +1446,7 @@ MARKETING_VERSION = 1.0.0; PRODUCT_BUNDLE_IDENTIFIER = org.joinmastodon.Mastodon; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; diff --git a/Mastodon/Coordinator/SceneCoordinator.swift b/Mastodon/Coordinator/SceneCoordinator.swift index 9418b6383..6bdf135af 100644 --- a/Mastodon/Coordinator/SceneCoordinator.swift +++ b/Mastodon/Coordinator/SceneCoordinator.swift @@ -39,6 +39,7 @@ extension SceneCoordinator { enum Scene { case authentication(viewModel: AuthenticationViewModel) case mastodonPinBasedAuthentication(viewModel: MastodonPinBasedAuthenticationViewModel) + case mastodonRegister(viewModel: MastodonRegisterViewModel) case alertController(alertController: UIAlertController) } @@ -120,6 +121,10 @@ private extension SceneCoordinator { let _viewController = MastodonPinBasedAuthenticationViewController() _viewController.viewModel = viewModel viewController = _viewController + case .mastodonRegister(let viewModel): + let _viewController = MastodonRegisterViewController() + _viewController.viewModel = viewModel + viewController = _viewController case .alertController(let alertController): if let popoverPresentationController = alertController.popoverPresentationController { assert( diff --git a/Mastodon/Scene/Authentication/AuthenticationViewController.swift b/Mastodon/Scene/Authentication/AuthenticationViewController.swift index 3b4a8bfc2..e0a561910 100644 --- a/Mastodon/Scene/Authentication/AuthenticationViewController.swift +++ b/Mastodon/Scene/Authentication/AuthenticationViewController.swift @@ -59,7 +59,13 @@ final class AuthenticationViewController: UIViewController, NeedsDependency { return button }() - let activityIndicatorView: UIActivityIndicatorView = { + let signInActivityIndicatorView: UIActivityIndicatorView = { + let activityIndicatorView = UIActivityIndicatorView(style: .medium) + activityIndicatorView.hidesWhenStopped = true + return activityIndicatorView + }() + + let signUpActivityIndicatorView: UIActivityIndicatorView = { let activityIndicatorView = UIActivityIndicatorView(style: .medium) activityIndicatorView.hidesWhenStopped = true return activityIndicatorView @@ -99,11 +105,11 @@ extension AuthenticationViewController { signInButton.heightAnchor.constraint(equalToConstant: 44).priority(.defaultHigh), ]) - activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false - view.addSubview(activityIndicatorView) + signInActivityIndicatorView.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(signInActivityIndicatorView) NSLayoutConstraint.activate([ - activityIndicatorView.centerXAnchor.constraint(equalTo: signInButton.centerXAnchor), - activityIndicatorView.centerYAnchor.constraint(equalTo: signInButton.centerYAnchor), + signInActivityIndicatorView.centerXAnchor.constraint(equalTo: signInButton.centerXAnchor), + signInActivityIndicatorView.centerYAnchor.constraint(equalTo: signInButton.centerYAnchor), ]) signUpButton.translatesAutoresizingMaskIntoConstraints = false @@ -115,6 +121,13 @@ extension AuthenticationViewController { signUpButton.heightAnchor.constraint(equalToConstant: 44).priority(.defaultHigh), ]) + signUpActivityIndicatorView.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(signUpActivityIndicatorView) + NSLayoutConstraint.activate([ + signUpActivityIndicatorView.centerXAnchor.constraint(equalTo: signUpButton.centerXAnchor), + signUpActivityIndicatorView.centerYAnchor.constraint(equalTo: signUpButton.centerYAnchor), + ]) + NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: domainTextField) .compactMap { notification in guard let textField = notification.object as? UITextField? else { return nil } @@ -127,13 +140,30 @@ extension AuthenticationViewController { .receive(on: DispatchQueue.main) .sink { [weak self] isAuthenticating in guard let self = self else { return } - isAuthenticating ? self.activityIndicatorView.startAnimating() : self.activityIndicatorView.stopAnimating() + isAuthenticating ? self.signInActivityIndicatorView.startAnimating() : self.signInActivityIndicatorView.stopAnimating() self.signInButton.setTitle(isAuthenticating ? "" : "Sign in", for: .normal) - self.signInButton.isEnabled = !isAuthenticating - self.signUpButton.isEnabled = !isAuthenticating } .store(in: &disposeBag) + viewModel.isRegistering + .receive(on: DispatchQueue.main) + .sink { [weak self] isRegistering in + guard let self = self else { return } + isRegistering ? self.signUpActivityIndicatorView.startAnimating() : self.signUpActivityIndicatorView.stopAnimating() + self.signUpButton.setTitle(isRegistering ? "" : "Sign up", for: .normal) + } + .store(in: &disposeBag) + + viewModel.isIdle + .receive(on: DispatchQueue.main) + .sink { [weak self] isIdle in + guard let self = self else { return } + self.signInButton.isEnabled = isIdle + self.signUpButton.isEnabled = isIdle + } + .store(in: &disposeBag) + + viewModel.authenticated .receive(on: DispatchQueue.main) .sink { [weak self] domain, user in @@ -196,7 +226,7 @@ extension AuthenticationViewController { domainTextField.shake() return } - guard !viewModel.isAuthenticating.value else { return } + guard viewModel.isIdle.value else { return } viewModel.isAuthenticating.value = true context.apiService.createApplication(domain: domain) .tryMap { response -> AuthenticationViewModel.AuthenticateInfo in @@ -241,8 +271,8 @@ extension AuthenticationViewController { domainTextField.shake() return } - guard !viewModel.isAuthenticating.value else { return } - + guard viewModel.isIdle.value else { return } + viewModel.isRegistering.value = true context.apiService.instance(domain: domain) .compactMap { [weak self] response -> AnyPublisher, Error>? in guard let self = self else { return nil } @@ -265,16 +295,20 @@ extension AuthenticationViewController { } .switchToLatest() .receive(on: DispatchQueue.main) - .sink { completion in + .sink { [weak self] completion in + guard let self = self else { return } + self.viewModel.isRegistering.value = false + switch completion { case .failure(let error): - break + self.viewModel.error.send(error) case .finished: break } } receiveValue: { [weak self] response in guard let self = self else { return } - print(response) + let mastodonRegisterViewModel = MastodonRegisterViewModel(domain: domain, applicationToken: response.value) + self.coordinator.present(scene: .mastodonRegister(viewModel: mastodonRegisterViewModel), from: self, transition: .show) } .store(in: &disposeBag) } diff --git a/Mastodon/Scene/Authentication/AuthenticationViewModel.swift b/Mastodon/Scene/Authentication/AuthenticationViewModel.swift index c213e73d0..cb197dc0a 100644 --- a/Mastodon/Scene/Authentication/AuthenticationViewModel.swift +++ b/Mastodon/Scene/Authentication/AuthenticationViewModel.swift @@ -27,6 +27,8 @@ final class AuthenticationViewModel { let domain = CurrentValueSubject(nil) let isDomainValid = CurrentValueSubject(false) let isAuthenticating = CurrentValueSubject(false) + let isRegistering = CurrentValueSubject(false) + let isIdle = CurrentValueSubject(true) let authenticated = PassthroughSubject<(domain: String, account: Mastodon.Entity.Account), Never>() let error = CurrentValueSubject(nil) @@ -59,6 +61,14 @@ final class AuthenticationViewModel { .assign(to: \.value, on: domain) .store(in: &disposeBag) + Publishers.CombineLatest( + isAuthenticating.eraseToAnyPublisher(), + isRegistering.eraseToAnyPublisher() + ) + .map { !$0 && !$1 } + .assign(to: \.value, on: self.isIdle) + .store(in: &disposeBag) + domain .map { $0 != nil } .assign(to: \.value, on: isDomainValid) diff --git a/Mastodon/Scene/Authentication/Register/MastodonRegisterViewController.swift b/Mastodon/Scene/Authentication/Register/MastodonRegisterViewController.swift new file mode 100644 index 000000000..b25686815 --- /dev/null +++ b/Mastodon/Scene/Authentication/Register/MastodonRegisterViewController.swift @@ -0,0 +1,227 @@ +// +// MastodonRegisterViewController.swift +// Mastodon +// +// Created by MainasuK Cirno on 2021-2-5. +// + +import os.log +import UIKit +import Combine +import MastodonSDK +import UITextField_Shake + +final class MastodonRegisterViewController: UIViewController, NeedsDependency { + + var disposeBag = Set() + + weak var context: AppContext! { willSet { precondition(!isViewLoaded) } } + weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } } + + var viewModel: MastodonRegisterViewModel! + + let usernameLabel: UILabel = { + let label = UILabel() + label.font = .preferredFont(forTextStyle: .headline) + label.textColor = Asset.Colors.Label.primary.color + label.text = "Username:" + return label + }() + + let usernameTextField: UITextField = { + let textField = UITextField() + textField.placeholder = "Username" + textField.autocapitalizationType = .none + textField.autocorrectionType = .no + return textField + }() + + let emailLabel: UILabel = { + let label = UILabel() + label.font = .preferredFont(forTextStyle: .headline) + label.textColor = Asset.Colors.Label.primary.color + label.text = "Email:" + return label + }() + + let emailTextField: UITextField = { + let textField = UITextField() + textField.placeholder = "example@gmail.com" + textField.autocapitalizationType = .none + textField.autocorrectionType = .no + textField.keyboardType = .emailAddress + return textField + }() + + let passwordLabel: UILabel = { + let label = UILabel() + label.font = .preferredFont(forTextStyle: .headline) + label.textColor = Asset.Colors.Label.primary.color + label.text = "Password:" + return label + }() + + let passwordTextField: UITextField = { + let textField = UITextField() + textField.placeholder = "Password" + textField.autocapitalizationType = .none + textField.autocorrectionType = .no + textField.keyboardType = .asciiCapable + textField.isSecureTextEntry = true + return textField + }() + + let signUpButton: UIButton = { + let button = UIButton(type: .system) + button.titleLabel?.font = .preferredFont(forTextStyle: .headline) + button.setBackgroundImage(UIImage.placeholder(color: Asset.Colors.Background.secondarySystemBackground.color), for: .normal) + button.setBackgroundImage(UIImage.placeholder(color: Asset.Colors.Background.secondarySystemBackground.color.withAlphaComponent(0.8)), for: .disabled) + button.setTitleColor(Asset.Colors.Label.primary.color, for: .normal) + button.setTitle("Sign up", for: .normal) + button.layer.masksToBounds = true + button.layer.cornerRadius = 8 + button.layer.cornerCurve = .continuous + return button + }() + + let signUpActivityIndicatorView: UIActivityIndicatorView = { + let activityIndicatorView = UIActivityIndicatorView(style: .medium) + activityIndicatorView.hidesWhenStopped = true + return activityIndicatorView + }() + +} + +extension MastodonRegisterViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + title = "Sign Up" + view.backgroundColor = Asset.Colors.Background.systemBackground.color + + let stackView = UIStackView() + stackView.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(stackView) + NSLayoutConstraint.activate([ + stackView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 16), + stackView.leadingAnchor.constraint(equalTo: view.readableContentGuide.leadingAnchor), + stackView.trailingAnchor.constraint(equalTo: view.readableContentGuide.trailingAnchor), + ]) + + stackView.axis = .vertical + stackView.spacing = 8 + + stackView.addArrangedSubview(usernameLabel) + stackView.addArrangedSubview(usernameTextField) + stackView.addArrangedSubview(emailLabel) + stackView.addArrangedSubview(emailTextField) + stackView.addArrangedSubview(passwordLabel) + stackView.addArrangedSubview(passwordTextField) + + signUpButton.translatesAutoresizingMaskIntoConstraints = false + stackView.addArrangedSubview(signUpButton) + NSLayoutConstraint.activate([ + signUpButton.heightAnchor.constraint(equalToConstant: 44).priority(.defaultHigh), + ]) + + signUpActivityIndicatorView.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(signUpActivityIndicatorView) + NSLayoutConstraint.activate([ + signUpActivityIndicatorView.centerXAnchor.constraint(equalTo: signUpButton.centerXAnchor), + signUpActivityIndicatorView.centerYAnchor.constraint(equalTo: signUpButton.centerYAnchor), + ]) + + viewModel.isRegistering + .receive(on: DispatchQueue.main) + .sink { [weak self] isRegistering in + guard let self = self else { return } + isRegistering ? self.signUpActivityIndicatorView.startAnimating() : self.signUpActivityIndicatorView.stopAnimating() + self.signUpButton.setTitle(isRegistering ? "" : "Sign up", for: .normal) + self.signUpButton.isEnabled = !isRegistering + } + .store(in: &disposeBag) + + viewModel.error + .compactMap { $0 } + .receive(on: DispatchQueue.main) + .sink { [weak self] error in + guard let self = self else { return } + let alertController = UIAlertController(error, preferredStyle: .alert) + let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) + alertController.addAction(okAction) + self.coordinator.present( + scene: .alertController(alertController: alertController), + from: nil, + transition: .alertController(animated: true, completion: nil) + ) + } + .store(in: &disposeBag) + + signUpButton.addTarget(self, action: #selector(MastodonRegisterViewController.signUpButtonPressed(_:)), for: .touchUpInside) + } + +} + +extension MastodonRegisterViewController { + + @objc private func signUpButtonPressed(_ sender: UIButton) { + os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function) + guard let username = usernameTextField.text else { + usernameTextField.shake() + return + } + + guard let email = emailTextField.text else { + emailTextField.shake() + return + } + + guard let password = passwordTextField.text else { + passwordTextField.shake() + return + } + + guard !viewModel.isRegistering.value else { return } + viewModel.isRegistering.value = true + + let query = Mastodon.API.Account.RegisterQuery( + username: username, + email: email, + password: password, + agreement: true, // TODO: + locale: "en" // TODO: + ) + + context.apiService.accountRegister( + domain: viewModel.domain, + query: query, + authorization: viewModel.applicationAuthorization + ) + .receive(on: DispatchQueue.main) + .sink { [weak self] completion in + guard let self = self else { return } + self.viewModel.isRegistering.value = false + switch completion { + case .failure(let error): + self.viewModel.error.send(error) + case .finished: + break + } + } receiveValue: { [weak self] response in + guard let self = self else { return } + let _ = response.value + // TODO: + let alertController = UIAlertController(title: "Success", message: "Regsiter request sent. Please check your email.\n(Auto sign in not implement yet.)", preferredStyle: .alert) + let okAction = UIAlertAction(title: "OK", style: .default) { [weak self] _ in + guard let self = self else { return } + self.navigationController?.popViewController(animated: true) + } + alertController.addAction(okAction) + self.coordinator.present(scene: .alertController(alertController: alertController), from: self, transition: .alertController(animated: true, completion: nil)) + } + .store(in: &disposeBag) + + } + +} diff --git a/Mastodon/Scene/Authentication/Register/MastodonRegisterViewModel.swift b/Mastodon/Scene/Authentication/Register/MastodonRegisterViewModel.swift new file mode 100644 index 000000000..f2404bb0a --- /dev/null +++ b/Mastodon/Scene/Authentication/Register/MastodonRegisterViewModel.swift @@ -0,0 +1,29 @@ +// +// MastodonRegisterViewModel.swift +// Mastodon +// +// Created by MainasuK Cirno on 2021-2-5. +// + +import Foundation +import Combine +import MastodonSDK + +final class MastodonRegisterViewModel { + + // input + let domain: String + let applicationToken: Mastodon.Entity.Token + let isRegistering = CurrentValueSubject(false) + + // output + let applicationAuthorization: Mastodon.API.OAuth.Authorization + let error = CurrentValueSubject(nil) + + init(domain: String, applicationToken: Mastodon.Entity.Token) { + self.domain = domain + self.applicationToken = applicationToken + self.applicationAuthorization = Mastodon.API.OAuth.Authorization(accessToken: applicationToken.accessToken) + } + +} diff --git a/Mastodon/Scene/PublicTimeline/PublicTimelineViewController.swift b/Mastodon/Scene/PublicTimeline/PublicTimelineViewController.swift index 3046dc9eb..a15f6a3fc 100644 --- a/Mastodon/Scene/PublicTimeline/PublicTimelineViewController.swift +++ b/Mastodon/Scene/PublicTimeline/PublicTimelineViewController.swift @@ -27,7 +27,7 @@ final class PublicTimelineViewController: UIViewController, NeedsDependency, Tim tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self)) tableView.rowHeight = UITableView.automaticDimension tableView.separatorStyle = .none - tableView.backgroundColor = Asset.Colors.Background.secondarySystemBackground.color + tableView.backgroundColor = .clear return tableView }() diff --git a/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel.swift b/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel.swift index fda4a2409..af7e70980 100644 --- a/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel.swift +++ b/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel.swift @@ -104,10 +104,3 @@ class PublicTimelineViewModel: NSObject { os_log("%{public}s[%{public}ld], %{public}s", (#file as NSString).lastPathComponent, #line, #function) } } - -extension PublicTimelineViewModel { - - func loadMore() -> AnyPublisher, Error> { - return context.apiService.publicTimeline(domain: "mstdn.jp") - } -} diff --git a/Mastodon/Scene/Share/View/TableviewCell/TimelineBottomLoaderTableViewCell.swift b/Mastodon/Scene/Share/View/TableviewCell/TimelineBottomLoaderTableViewCell.swift index f2892a841..2dfbd6257 100644 --- a/Mastodon/Scene/Share/View/TableviewCell/TimelineBottomLoaderTableViewCell.swift +++ b/Mastodon/Scene/Share/View/TableviewCell/TimelineBottomLoaderTableViewCell.swift @@ -11,7 +11,8 @@ import Combine final class TimelineBottomLoaderTableViewCell: TimelineLoaderTableViewCell { override func _init() { super._init() - + backgroundColor = .clear + activityIndicatorView.isHidden = false activityIndicatorView.startAnimating() } diff --git a/Mastodon/Service/APIService/APIService+Account.swift b/Mastodon/Service/APIService/APIService+Account.swift index 1fcf212b6..6e26dbf83 100644 --- a/Mastodon/Service/APIService/APIService+Account.swift +++ b/Mastodon/Service/APIService/APIService+Account.swift @@ -43,4 +43,17 @@ extension APIService { .eraseToAnyPublisher() } + func accountRegister( + domain: String, + query: Mastodon.API.Account.RegisterQuery, + authorization: Mastodon.API.OAuth.Authorization + ) -> AnyPublisher, Error> { + return Mastodon.API.Account.register( + session: session, + domain: domain, + query: query, + authorization: authorization + ) + } + } diff --git a/Mastodon/Service/APIService/APIService+HomeTimeline.swift b/Mastodon/Service/APIService/APIService+HomeTimeline.swift index b4160fad4..06157a147 100644 --- a/Mastodon/Service/APIService/APIService+HomeTimeline.swift +++ b/Mastodon/Service/APIService/APIService+HomeTimeline.swift @@ -22,7 +22,7 @@ extension APIService { limit: Int = 100, local: Bool? = nil, authorizationBox: AuthenticationService.MastodonAuthenticationBox - ) -> AnyPublisher, Error> { + ) -> AnyPublisher, Error> { let authorization = authorizationBox.userAuthorization let requestMastodonUserID = authorizationBox.userID let query = Mastodon.API.Timeline.HomeTimelineQuery( @@ -39,7 +39,7 @@ extension APIService { query: query, authorization: authorization ) - .flatMap { response -> AnyPublisher, Error> in + .flatMap { response -> AnyPublisher, Error> in return APIService.Persist.persistTimeline( managedObjectContext: self.backgroundManagedObjectContext, domain: domain, @@ -50,7 +50,7 @@ extension APIService { log: OSLog.api ) .setFailureType(to: Error.self) - .tryMap { result -> Mastodon.Response.Content<[Mastodon.Entity.Toot]> in + .tryMap { result -> Mastodon.Response.Content<[Mastodon.Entity.Status]> in switch result { case .success: return response diff --git a/Mastodon/Service/APIService/APIService+PublicTimeline.swift b/Mastodon/Service/APIService/APIService+PublicTimeline.swift index aa45dbb92..26fd91f56 100644 --- a/Mastodon/Service/APIService/APIService+PublicTimeline.swift +++ b/Mastodon/Service/APIService/APIService+PublicTimeline.swift @@ -22,7 +22,7 @@ extension APIService { sinceID: Mastodon.Entity.Status.ID? = nil, maxID: Mastodon.Entity.Status.ID? = nil, limit: Int = 100 - ) -> AnyPublisher, Error> { + ) -> AnyPublisher, Error> { let query = Mastodon.API.Timeline.PublicTimelineQuery( local: nil, remote: nil, @@ -38,7 +38,7 @@ extension APIService { domain: domain, query: query ) - .flatMap { response -> AnyPublisher, Error> in + .flatMap { response -> AnyPublisher, Error> in return APIService.Persist.persistTimeline( managedObjectContext: self.backgroundManagedObjectContext, domain: domain, @@ -49,7 +49,7 @@ extension APIService { log: OSLog.api ) .setFailureType(to: Error.self) - .tryMap { result -> Mastodon.Response.Content<[Mastodon.Entity.Toot]> in + .tryMap { result -> Mastodon.Response.Content<[Mastodon.Entity.Status]> in switch result { case .success: return response diff --git a/Mastodon/Service/APIService/CoreData/APIService+CoreData+Toot.swift b/Mastodon/Service/APIService/CoreData/APIService+CoreData+Toot.swift index c9f5e325c..e69cf9c31 100644 --- a/Mastodon/Service/APIService/CoreData/APIService+CoreData+Toot.swift +++ b/Mastodon/Service/APIService/CoreData/APIService+CoreData+Toot.swift @@ -16,7 +16,7 @@ extension APIService.CoreData { static func createOrMergeToot( into managedObjectContext: NSManagedObjectContext, for requestMastodonUser: MastodonUser?, - entity: Mastodon.Entity.Toot, + entity: Mastodon.Entity.Status, domain: String, networkDate: Date, log: OSLog @@ -83,7 +83,7 @@ extension APIService.CoreData { } } - static func mergeToot(for requestMastodonUser: MastodonUser?, old toot: Toot,in domain: String, entity: Mastodon.Entity.Toot, networkDate: Date) { + static func mergeToot(for requestMastodonUser: MastodonUser?, old toot: Toot,in domain: String, entity: Mastodon.Entity.Status, networkDate: Date) { guard networkDate > toot.updatedAt else { return } // merge diff --git a/Mastodon/Service/APIService/Persist/APIService+Persist+Timeline.swift b/Mastodon/Service/APIService/Persist/APIService+Persist+Timeline.swift index 61ff91de0..259c796a6 100644 --- a/Mastodon/Service/APIService/Persist/APIService+Persist+Timeline.swift +++ b/Mastodon/Service/APIService/Persist/APIService+Persist+Timeline.swift @@ -24,7 +24,7 @@ extension APIService.Persist { managedObjectContext: NSManagedObjectContext, domain: String, query: Mastodon.API.Timeline.TimelineQuery, - response: Mastodon.Response.Content<[Mastodon.Entity.Toot]>, + response: Mastodon.Response.Content<[Mastodon.Entity.Status]>, persistType: PersistTimelineType, requestMastodonUserID: MastodonUser.ID?, // could be nil when response from public endpoint log: OSLog diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account.swift index 81fe9cd00..246cec89e 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account.swift @@ -13,6 +13,9 @@ extension Mastodon.API.Account { static func verifyCredentialsEndpointURL(domain: String) -> URL { return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("accounts/verify_credentials") } + static func registerEndpointURL(domain: String) -> URL { + return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("accounts") + } public static func verifyCredentials( session: URLSession, @@ -31,4 +34,50 @@ extension Mastodon.API.Account { } .eraseToAnyPublisher() } + + public static func register( + session: URLSession, + domain: String, + query: RegisterQuery, + authorization: Mastodon.API.OAuth.Authorization + ) -> AnyPublisher, Error> { + let request = Mastodon.API.post( + url: registerEndpointURL(domain: domain), + query: query, + authorization: authorization + ) + return session.dataTaskPublisher(for: request) + .tryMap { data, response in + let value = try Mastodon.API.decode(type: Mastodon.Entity.Token.self, from: data, response: response) + return Mastodon.Response.Content(value: value, response: response) + } + .eraseToAnyPublisher() + } + +} + +extension Mastodon.API.Account { + + public struct RegisterQuery: Codable, PostQuery { + public let reason: String? + public let username: String + public let email: String + public let password: String + public let agreement: Bool + public let locale: String + + public init(reason: String? = nil, username: String, email: String, password: String, agreement: Bool, locale: String) { + self.reason = reason + self.username = username + self.email = email + self.password = password + self.agreement = agreement + self.locale = locale + } + + var body: Data? { + return try? Mastodon.API.encoder.encode(self) + } + } + } diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Timeline.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Timeline.swift index 66d7d8060..80e30701a 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Timeline.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Timeline.swift @@ -21,7 +21,7 @@ extension Mastodon.API.Timeline { session: URLSession, domain: String, query: PublicTimelineQuery - ) -> AnyPublisher, Error> { + ) -> AnyPublisher, Error> { let request = Mastodon.API.get( url: publicTimelineEndpointURL(domain: domain), query: query, @@ -29,7 +29,7 @@ extension Mastodon.API.Timeline { ) return session.dataTaskPublisher(for: request) .tryMap { data, response in - let value = try Mastodon.API.decode(type: [Mastodon.Entity.Toot].self, from: data, response: response) + let value = try Mastodon.API.decode(type: [Mastodon.Entity.Status].self, from: data, response: response) return Mastodon.Response.Content(value: value, response: response) } .eraseToAnyPublisher() @@ -40,7 +40,7 @@ extension Mastodon.API.Timeline { domain: String, query: HomeTimelineQuery, authorization: Mastodon.API.OAuth.Authorization - ) -> AnyPublisher, Error> { + ) -> AnyPublisher, Error> { let request = Mastodon.API.get( url: homeTimelineEndpointURL(domain: domain), query: query, @@ -48,7 +48,7 @@ extension Mastodon.API.Timeline { ) return session.dataTaskPublisher(for: request) .tryMap { data, response in - let value = try Mastodon.API.decode(type: [Mastodon.Entity.Toot].self, from: data, response: response) + let value = try Mastodon.API.decode(type: [Mastodon.Entity.Status].self, from: data, response: response) return Mastodon.Response.Content(value: value, response: response) } .eraseToAnyPublisher() @@ -57,8 +57,8 @@ extension Mastodon.API.Timeline { } public protocol TimelineQueryType { - var maxID: Mastodon.Entity.Toot.ID? { get } - var sinceID: Mastodon.Entity.Toot.ID? { get } + var maxID: Mastodon.Entity.Status.ID? { get } + var sinceID: Mastodon.Entity.Status.ID? { get } } extension Mastodon.API.Timeline { @@ -70,18 +70,18 @@ extension Mastodon.API.Timeline { public let local: Bool? public let remote: Bool? public let onlyMedia: Bool? - public let maxID: Mastodon.Entity.Toot.ID? - public let sinceID: Mastodon.Entity.Toot.ID? - public let minID: Mastodon.Entity.Toot.ID? + public let maxID: Mastodon.Entity.Status.ID? + public let sinceID: Mastodon.Entity.Status.ID? + public let minID: Mastodon.Entity.Status.ID? public let limit: Int? public init( local: Bool? = nil, remote: Bool? = nil, onlyMedia: Bool? = nil, - maxID: Mastodon.Entity.Toot.ID? = nil, - sinceID: Mastodon.Entity.Toot.ID? = nil, - minID: Mastodon.Entity.Toot.ID? = nil, + maxID: Mastodon.Entity.Status.ID? = nil, + sinceID: Mastodon.Entity.Status.ID? = nil, + minID: Mastodon.Entity.Status.ID? = nil, limit: Int? = nil ) { self.local = local @@ -108,16 +108,16 @@ extension Mastodon.API.Timeline { } public struct HomeTimelineQuery: Codable, TimelineQuery, GetQuery { - public let maxID: Mastodon.Entity.Toot.ID? - public let sinceID: Mastodon.Entity.Toot.ID? - public let minID: Mastodon.Entity.Toot.ID? + public let maxID: Mastodon.Entity.Status.ID? + public let sinceID: Mastodon.Entity.Status.ID? + public let minID: Mastodon.Entity.Status.ID? public let limit: Int? public let local: Bool? public init( - maxID: Mastodon.Entity.Toot.ID? = nil, - sinceID: Mastodon.Entity.Toot.ID? = nil, - minID: Mastodon.Entity.Toot.ID? = nil, + maxID: Mastodon.Entity.Status.ID? = nil, + sinceID: Mastodon.Entity.Status.ID? = nil, + minID: Mastodon.Entity.Status.ID? = nil, limit: Int? = nil, local: Bool? = nil ) { diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Status.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Status.swift index a8843666f..4b820b235 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Status.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Status.swift @@ -8,9 +8,7 @@ import Foundation extension Mastodon.Entity { - - public typealias Toot = Status - + /// Status /// /// - Since: 0.1.0