Randomly select a server for user (#690)

This commit is contained in:
Nathan Mattes 2022-12-30 17:59:09 +01:00
parent efafefac48
commit 600e91eb4d
3 changed files with 27 additions and 14 deletions

View File

@ -123,12 +123,6 @@ extension MastodonPickServerViewController {
) )
.store(in: &disposeBag) .store(in: &disposeBag)
viewModel
.selectedServer
.map { $0 != nil }
.assign(to: \.isEnabled, on: onboardingNextView.nextButton)
.store(in: &disposeBag)
Publishers.Merge( Publishers.Merge(
viewModel.error, viewModel.error,
authenticationViewModel.error authenticationViewModel.error
@ -211,7 +205,7 @@ extension MastodonPickServerViewController {
} }
.store(in: &disposeBag) .store(in: &disposeBag)
onboardingNextView.nextButton.addTarget(self, action: #selector(MastodonPickServerViewController.nextButtonDidPressed(_:)), for: .touchUpInside) onboardingNextView.nextButton.addTarget(self, action: #selector(MastodonPickServerViewController.next(_:)), for: .touchUpInside)
title = L10n.Scene.ServerPicker.title title = L10n.Scene.ServerPicker.title
@ -241,9 +235,18 @@ extension MastodonPickServerViewController {
extension MastodonPickServerViewController { extension MastodonPickServerViewController {
@objc private func nextButtonDidPressed(_ sender: UIButton) { @objc private func next(_ sender: UIButton) {
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 } let server: Mastodon.Entity.Server
if let selectedServer = viewModel.selectedServer.value {
server = selectedServer
} else if let randomServer = viewModel.chooseRandomServer() {
server = randomServer
} else {
return
}
authenticationViewModel.isAuthenticating.send(true) authenticationViewModel.isAuthenticating.send(true)
context.apiService.instance(domain: server.domain) context.apiService.instance(domain: server.domain)

View File

@ -228,6 +228,19 @@ extension MastodonPickServerViewModel {
.store(in: &disposeBag) .store(in: &disposeBag)
} }
func chooseRandomServer() -> Mastodon.Entity.Server? {
guard let language = Locale.preferredLanguages.first?.lowercased() else { return nil }
let servers = indexedServers.value
guard servers.isNotEmpty else { return nil }
let randomServer = servers.filter {
$0.language == language
}.randomElement()
return randomServer
}
} }
extension MastodonPickServerViewModel { extension MastodonPickServerViewModel {

View File

@ -13,9 +13,7 @@ import MastodonLocalization
final class OnboardingNextView: UIView { final class OnboardingNextView: UIView {
static let buttonHeight: CGFloat = 50 static let buttonHeight: CGFloat = 50
private var observations = Set<NSKeyValueObservation>()
private let container: UIStackView = { private let container: UIStackView = {
let stackView = UIStackView() let stackView = UIStackView()
stackView.axis = .vertical stackView.axis = .vertical
@ -24,7 +22,6 @@ final class OnboardingNextView: UIView {
return stackView return stackView
}() }()
//TODO: Show loading
let nextButton: UIButton = { let nextButton: UIButton = {
let button = UIButton() let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false button.translatesAutoresizingMaskIntoConstraints = false