feat: [WIP] implement sign up scene

This commit is contained in:
CMK 2021-02-05 17:53:00 +08:00
parent 87c734c734
commit 7d1c8e5be9
17 changed files with 430 additions and 53 deletions

View File

@ -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 = "<group>"; };
DB98339B25C96DE600AD9700 /* APIService+Account.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "APIService+Account.swift"; sourceTree = "<group>"; };
DBD4ED1025CC0FEB0041B741 /* HomeViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewModel.swift; sourceTree = "<group>"; };
DBE0821425CD382600FD6BBD /* MastodonRegisterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MastodonRegisterViewController.swift; sourceTree = "<group>"; };
DBE0822325CD3F1E00FD6BBD /* MastodonRegisterViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MastodonRegisterViewModel.swift; sourceTree = "<group>"; };
DBF53F5F25C14E88008AAC7B /* Mastodon.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = Mastodon.xctestplan; path = Mastodon/Mastodon.xctestplan; sourceTree = "<group>"; };
DBF53F6025C14E9D008AAC7B /* MastodonSDK.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = MastodonSDK.xctestplan; sourceTree = "<group>"; };
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 = "<group>"; };
@ -465,6 +469,7 @@
isa = PBXGroup;
children = (
DB0140A625C40C0900F9F3CF /* PinBased */,
DBE0821A25CD382900FD6BBD /* Register */,
DB01409525C40B6700F9F3CF /* AuthenticationViewController.swift */,
DB98334625C8056600AD9700 /* AuthenticationViewModel.swift */,
);
@ -756,6 +761,15 @@
path = HomeTimeline;
sourceTree = "<group>";
};
DBE0821A25CD382900FD6BBD /* Register */ = {
isa = PBXGroup;
children = (
DBE0821425CD382600FD6BBD /* MastodonRegisterViewController.swift */,
DBE0822325CD3F1E00FD6BBD /* MastodonRegisterViewModel.swift */,
);
path = Register;
sourceTree = "<group>";
};
/* 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";
};

View File

@ -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(

View File

@ -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<Mastodon.Response.Content<Mastodon.Entity.Application>, 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)
}

View File

@ -27,6 +27,8 @@ final class AuthenticationViewModel {
let domain = CurrentValueSubject<String?, Never>(nil)
let isDomainValid = CurrentValueSubject<Bool, Never>(false)
let isAuthenticating = CurrentValueSubject<Bool, Never>(false)
let isRegistering = CurrentValueSubject<Bool, Never>(false)
let isIdle = CurrentValueSubject<Bool, Never>(true)
let authenticated = PassthroughSubject<(domain: String, account: Mastodon.Entity.Account), Never>()
let error = CurrentValueSubject<Error?, Never>(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)

View File

@ -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<AnyCancellable>()
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)
}
}

View File

@ -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<Bool, Never>(false)
// output
let applicationAuthorization: Mastodon.API.OAuth.Authorization
let error = CurrentValueSubject<Error?, Never>(nil)
init(domain: String, applicationToken: Mastodon.Entity.Token) {
self.domain = domain
self.applicationToken = applicationToken
self.applicationAuthorization = Mastodon.API.OAuth.Authorization(accessToken: applicationToken.accessToken)
}
}

View File

@ -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
}()

View File

@ -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<Mastodon.Response.Content<[Mastodon.Entity.Toot]>, Error> {
return context.apiService.publicTimeline(domain: "mstdn.jp")
}
}

View File

@ -11,7 +11,8 @@ import Combine
final class TimelineBottomLoaderTableViewCell: TimelineLoaderTableViewCell {
override func _init() {
super._init()
backgroundColor = .clear
activityIndicatorView.isHidden = false
activityIndicatorView.startAnimating()
}

View File

@ -43,4 +43,17 @@ extension APIService {
.eraseToAnyPublisher()
}
func accountRegister(
domain: String,
query: Mastodon.API.Account.RegisterQuery,
authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Token>, Error> {
return Mastodon.API.Account.register(
session: session,
domain: domain,
query: query,
authorization: authorization
)
}
}

View File

@ -22,7 +22,7 @@ extension APIService {
limit: Int = 100,
local: Bool? = nil,
authorizationBox: AuthenticationService.MastodonAuthenticationBox
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Toot]>, Error> {
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Status]>, 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<Mastodon.Response.Content<[Mastodon.Entity.Toot]>, Error> in
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Status]>, 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

View File

@ -22,7 +22,7 @@ extension APIService {
sinceID: Mastodon.Entity.Status.ID? = nil,
maxID: Mastodon.Entity.Status.ID? = nil,
limit: Int = 100
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Toot]>, Error> {
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Status]>, Error> {
let query = Mastodon.API.Timeline.PublicTimelineQuery(
local: nil,
remote: nil,
@ -38,7 +38,7 @@ extension APIService {
domain: domain,
query: query
)
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Toot]>, Error> in
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Status]>, 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

View File

@ -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

View File

@ -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

View File

@ -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<Mastodon.Response.Content<Mastodon.Entity.Token>, 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)
}
}
}

View File

@ -21,7 +21,7 @@ extension Mastodon.API.Timeline {
session: URLSession,
domain: String,
query: PublicTimelineQuery
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Toot]>, Error> {
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Status]>, 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<Mastodon.Response.Content<[Mastodon.Entity.Toot]>, Error> {
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Status]>, 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
) {

View File

@ -8,9 +8,7 @@
import Foundation
extension Mastodon.Entity {
public typealias Toot = Status
/// Status
///
/// - Since: 0.1.0