fix: handle bad network error when loading servers in pick server scene

This commit is contained in:
CMK 2021-06-16 14:49:00 +08:00
parent 9f17d5dffd
commit 99af62658f
4 changed files with 32 additions and 26 deletions

View File

@ -237,7 +237,7 @@ extension MastodonPickServerViewController {
.store(in: &disposeBag) .store(in: &disposeBag)
viewModel.emptyStateViewState viewModel.emptyStateViewState
.receive(on: DispatchQueue.main) .receive(on: RunLoop.main)
.sink { [weak self] state in .sink { [weak self] state in
guard let self = self else { return } guard let self = self else { return }
switch state { switch state {

View File

@ -45,9 +45,10 @@ extension MastodonPickServerViewModel.LoadIndexedServerState {
viewModel.context.apiService.servers(language: nil, category: nil) viewModel.context.apiService.servers(language: nil, category: nil)
.sink { completion in .sink { completion in
switch completion { switch completion {
case .failure: case .failure(let error):
// TODO: handle error // TODO: handle error
stateMachine.enter(Fail.self) stateMachine.enter(Fail.self)
viewModel.loadingIndexedServersError.value = error
case .finished: case .finished:
break break
} }

View File

@ -61,6 +61,7 @@ class MastodonPickServerViewModel: NSObject {
let error = CurrentValueSubject<Error?, Never>(nil) let error = CurrentValueSubject<Error?, Never>(nil)
let isLoadingIndexedServers = CurrentValueSubject<Bool, Never>(false) let isLoadingIndexedServers = CurrentValueSubject<Bool, Never>(false)
let loadingIndexedServersError = CurrentValueSubject<Error?, Never>(nil)
let emptyStateViewState = CurrentValueSubject<EmptyStateViewState, Never>(.none) let emptyStateViewState = CurrentValueSubject<EmptyStateViewState, Never>(.none)
init(context: AppContext, mode: PickServerMode) { init(context: AppContext, mode: PickServerMode) {
@ -142,16 +143,23 @@ extension MastodonPickServerViewModel {
}) })
.store(in: &disposeBag) .store(in: &disposeBag)
isLoadingIndexedServers Publishers.CombineLatest(
.map { isLoadingIndexedServers -> EmptyStateViewState in isLoadingIndexedServers,
if isLoadingIndexedServers { loadingIndexedServersError
return .loading )
.map { isLoadingIndexedServers, loadingIndexedServersError -> EmptyStateViewState in
if isLoadingIndexedServers {
if loadingIndexedServersError != nil {
return .badNetwork
} else { } 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( Publishers.CombineLatest3(
indexedServers.eraseToAnyPublisher(), indexedServers.eraseToAnyPublisher(),

View File

@ -76,22 +76,19 @@ extension PickServerEmptyStateView {
]) ])
containerStackView.addArrangedSubview(networkIndicatorImageView) containerStackView.addArrangedSubview(networkIndicatorImageView)
let infoContainerView = UIView() let infoContainerStackView = UIStackView()
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false infoContainerStackView.axis = .horizontal
infoContainerView.addSubview(activityIndicatorView) infoContainerStackView.distribution = .fill
NSLayoutConstraint.activate([
activityIndicatorView.leadingAnchor.constraint(equalTo: infoContainerView.leadingAnchor), infoContainerStackView.addArrangedSubview(activityIndicatorView)
activityIndicatorView.centerYAnchor.constraint(equalTo: infoContainerView.centerYAnchor), infoContainerStackView.spacing = 4
activityIndicatorView.bottomAnchor.constraint(equalTo: infoContainerView.bottomAnchor), activityIndicatorView.setContentHuggingPriority(.required - 1, for: .horizontal)
])
infoLabel.translatesAutoresizingMaskIntoConstraints = false infoContainerStackView.addArrangedSubview(infoLabel)
infoContainerView.addSubview(infoLabel) infoLabel.setContentCompressionResistancePriority(.required - 1, for: .vertical)
NSLayoutConstraint.activate([ infoLabel.setContentCompressionResistancePriority(.required - 1, for: .horizontal)
infoLabel.leadingAnchor.constraint(equalTo: activityIndicatorView.trailingAnchor, constant: 4),
infoLabel.centerYAnchor.constraint(equalTo: infoContainerView.centerYAnchor), containerStackView.addArrangedSubview(infoContainerStackView)
infoLabel.trailingAnchor.constraint(equalTo: infoContainerView.trailingAnchor),
])
containerStackView.addArrangedSubview(infoContainerView)
let bottomPaddingView = UIView() let bottomPaddingView = UIView()
bottomPaddingView.translatesAutoresizingMaskIntoConstraints = false bottomPaddingView.translatesAutoresizingMaskIntoConstraints = false
@ -104,7 +101,7 @@ extension PickServerEmptyStateView {
]) ])
NSLayoutConstraint.activate([ 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 activityIndicatorView.hidesWhenStopped = true