Refine loading logic

This commit is contained in:
Alfonso Grillo 2023-01-20 12:25:52 +01:00
parent d670badbc5
commit 9625440508
4 changed files with 29 additions and 11 deletions

View file

@ -27,6 +27,7 @@ enum MockPollHistoryScreenState: MockScreenState, CaseIterable {
case past
case activeEmpty
case pastEmpty
case loading
/// The associated screen
var screenType: Any.Type {
@ -49,6 +50,9 @@ enum MockPollHistoryScreenState: MockScreenState, CaseIterable {
case .pastEmpty:
pollHistoryMode = .past
pollService.pastPollsData = []
case .loading:
pollHistoryMode = .active
pollService.fetchState = true
}
let viewModel = PollHistoryViewModel(mode: pollHistoryMode, pollService: pollService)

View file

@ -65,15 +65,15 @@ private extension PollHistoryViewModel {
pollService
.isFetching
.filter { $0 }
.first()
.sink { isFetching in
self.state.loadingState = .loading(firstLoad: true)
self.state.loadingState = isFetching ? .loading(firstLoad: true) : .idle
}
.store(in: &subcriptions)
pollService
.isFetching
.dropFirst()
.sink { isFetching in
self.state.loadingState = isFetching ? .loading(firstLoad: false) : .idle
}

View file

@ -32,9 +32,10 @@ final class MockPollHistoryService: PollHistoryServiceProtocol {
polls.send(poll)
}
}
var fetchState: Bool = false
var isFetching: AnyPublisher<Bool, Never> {
Just(false).eraseToAnyPublisher()
Just(fetchState).eraseToAnyPublisher()
}
var activePollsData: [TimelinePollDetails] = (1..<10)

View file

@ -33,6 +33,8 @@ struct PollHistory: View {
if viewModel.viewState.loadingState.isLoadingOnLanding {
loadingView
} else if viewModel.viewState.loadingState.isLoading == false, viewModel.viewState.polls.isEmpty {
noPollsView
} else {
pollListView
}
@ -57,19 +59,30 @@ struct PollHistory: View {
PollListItem(pollData: pollData)
}
.frame(maxWidth: .infinity, alignment: .leading)
Button {
#warning("handle action")
} label: {
Text("Load more polls")
}
.frame(maxWidth: .infinity, alignment: .leading)
loadMoreButton
}
.padding(.top, 32)
.padding(.horizontal, 16)
}
}
private var loadMoreButton: some View {
HStack(spacing: 8) {
if viewModel.viewState.loadingState.isLoading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
}
Button {
#warning("handle action")
} label: {
Text("Load more polls")
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}
private var noPollsView: some View {
Text(viewModel.mode == .active ? VectorL10n.pollHistoryNoActivePollText : VectorL10n.pollHistoryNoPastPollText)
.font(theme.fonts.body)