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)
viewModel.emptyStateViewState
.receive(on: DispatchQueue.main)
.receive(on: RunLoop.main)
.sink { [weak self] state in
guard let self = self else { return }
switch state {

View File

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

View File

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

View File

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