From 99af62658f483a8285f3de7fac82f59907b05770 Mon Sep 17 00:00:00 2001 From: CMK Date: Wed, 16 Jun 2021 14:49:00 +0800 Subject: [PATCH] fix: handle bad network error when loading servers in pick server scene --- .../MastodonPickServerViewController.swift | 2 +- ...rverViewModel+LoadIndexedServerState.swift | 3 +- .../MastodonPickServerViewModel.swift | 22 ++++++++----- .../View/PickServerEmptyStateView.swift | 31 +++++++++---------- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift index 7c2a83ee7..46c5e234d 100644 --- a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift +++ b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewController.swift @@ -237,7 +237,7 @@ extension MastodonPickServerViewController { .store(in: &disposeBag) viewModel.emptyStateViewState - .receive(on: DispatchQueue.main) + .receive(on: RunLoop.main) .sink { [weak self] state in guard let self = self else { return } switch state { diff --git a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel+LoadIndexedServerState.swift b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel+LoadIndexedServerState.swift index 69f6a82fb..0c4910d17 100644 --- a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel+LoadIndexedServerState.swift +++ b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel+LoadIndexedServerState.swift @@ -45,9 +45,10 @@ extension MastodonPickServerViewModel.LoadIndexedServerState { viewModel.context.apiService.servers(language: nil, category: nil) .sink { completion in switch completion { - case .failure: + case .failure(let error): // TODO: handle error stateMachine.enter(Fail.self) + viewModel.loadingIndexedServersError.value = error case .finished: break } diff --git a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift index 8348f8843..95fc2be04 100644 --- a/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift +++ b/Mastodon/Scene/Onboarding/PickServer/MastodonPickServerViewModel.swift @@ -61,6 +61,7 @@ class MastodonPickServerViewModel: NSObject { let error = CurrentValueSubject(nil) let isLoadingIndexedServers = CurrentValueSubject(false) + let loadingIndexedServersError = CurrentValueSubject(nil) let emptyStateViewState = CurrentValueSubject(.none) init(context: AppContext, mode: PickServerMode) { @@ -142,16 +143,23 @@ extension MastodonPickServerViewModel { }) .store(in: &disposeBag) - isLoadingIndexedServers - .map { isLoadingIndexedServers -> EmptyStateViewState in - if isLoadingIndexedServers { - return .loading + Publishers.CombineLatest( + isLoadingIndexedServers, + loadingIndexedServersError + ) + .map { isLoadingIndexedServers, loadingIndexedServersError -> EmptyStateViewState in + if isLoadingIndexedServers { + if loadingIndexedServersError != nil { + return .badNetwork } else { - return .none + return .loading } + } else { + return .none } - .assign(to: \.value, on: emptyStateViewState) - .store(in: &disposeBag) + } + .assign(to: \.value, on: emptyStateViewState) + .store(in: &disposeBag) Publishers.CombineLatest3( indexedServers.eraseToAnyPublisher(), diff --git a/Mastodon/Scene/Onboarding/PickServer/View/PickServerEmptyStateView.swift b/Mastodon/Scene/Onboarding/PickServer/View/PickServerEmptyStateView.swift index aa356af77..6a30751a9 100644 --- a/Mastodon/Scene/Onboarding/PickServer/View/PickServerEmptyStateView.swift +++ b/Mastodon/Scene/Onboarding/PickServer/View/PickServerEmptyStateView.swift @@ -76,22 +76,19 @@ extension PickServerEmptyStateView { ]) containerStackView.addArrangedSubview(networkIndicatorImageView) - let infoContainerView = UIView() - activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false - infoContainerView.addSubview(activityIndicatorView) - NSLayoutConstraint.activate([ - activityIndicatorView.leadingAnchor.constraint(equalTo: infoContainerView.leadingAnchor), - activityIndicatorView.centerYAnchor.constraint(equalTo: infoContainerView.centerYAnchor), - activityIndicatorView.bottomAnchor.constraint(equalTo: infoContainerView.bottomAnchor), - ]) - infoLabel.translatesAutoresizingMaskIntoConstraints = false - infoContainerView.addSubview(infoLabel) - NSLayoutConstraint.activate([ - infoLabel.leadingAnchor.constraint(equalTo: activityIndicatorView.trailingAnchor, constant: 4), - infoLabel.centerYAnchor.constraint(equalTo: infoContainerView.centerYAnchor), - infoLabel.trailingAnchor.constraint(equalTo: infoContainerView.trailingAnchor), - ]) - containerStackView.addArrangedSubview(infoContainerView) + let infoContainerStackView = UIStackView() + infoContainerStackView.axis = .horizontal + infoContainerStackView.distribution = .fill + + infoContainerStackView.addArrangedSubview(activityIndicatorView) + infoContainerStackView.spacing = 4 + activityIndicatorView.setContentHuggingPriority(.required - 1, for: .horizontal) + + infoContainerStackView.addArrangedSubview(infoLabel) + infoLabel.setContentCompressionResistancePriority(.required - 1, for: .vertical) + infoLabel.setContentCompressionResistancePriority(.required - 1, for: .horizontal) + + containerStackView.addArrangedSubview(infoContainerStackView) let bottomPaddingView = UIView() bottomPaddingView.translatesAutoresizingMaskIntoConstraints = false @@ -104,7 +101,7 @@ extension PickServerEmptyStateView { ]) NSLayoutConstraint.activate([ - bottomPaddingView.heightAnchor.constraint(equalTo: topPaddingView.heightAnchor, multiplier: 1.0), + bottomPaddingView.heightAnchor.constraint(equalTo: topPaddingView.heightAnchor, multiplier: 1.0).priority(.defaultHigh), ]) activityIndicatorView.hidesWhenStopped = true