Skip to content

Commit

Permalink
Merge pull request #7185 from vector-im/gil/6059-Load_thread_list_usi…
Browse files Browse the repository at this point in the history
…ng_server-side_sorting_and_pagination

Load the thread list using server-side sorting and pagination
  • Loading branch information
gileluard authored Dec 28, 2022
2 parents ece07c4 + 8a9f1d2 commit c4d66d6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ final class ThreadListViewController: UIViewController {

private func renderLoading() {
emptyView.isHidden = true
threadsTableView.isHidden = true
threadsTableView.isHidden = viewModel.numberOfThreads == 0
self.activityPresenter.presentActivityIndicator(on: self.view, animated: true)
}

Expand Down Expand Up @@ -352,6 +352,10 @@ extension ThreadListViewController: UITableViewDelegate {
cell.backgroundColor = theme.backgroundColor
cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = theme.selectedBackgroundColor

if indexPath.row == viewModel.numberOfThreads - 1 {
viewModel.process(viewAction: .loadData)
}
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Expand Down
19 changes: 14 additions & 5 deletions Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
private var threads: [MXThreadProtocol] = []
private var eventFormatter: MXKEventFormatter?
private var roomState: MXRoomState?

private var nextBatch: String?
private var currentOperation: MXHTTPOperation?
private var longPressedThread: MXThreadProtocol?

Expand Down Expand Up @@ -71,6 +71,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
viewState = .showingFilterTypes
case .selectFilterType(let type):
selectedFilterType = type
resetData()
loadData()
case .selectThread(let index):
selectThread(index)
Expand Down Expand Up @@ -230,7 +231,15 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
)
}

private func resetData() {
nextBatch = nil
threads = []
}

private func loadData(showLoading: Bool = true) {
guard threads.isEmpty || nextBatch != nil else {
return
}

if showLoading {
viewState = .loading
Expand All @@ -245,12 +254,12 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
onlyParticipated = true
}

session.threadingService.allThreads(inRoom: roomId,
onlyParticipated: onlyParticipated) { [weak self] response in
session.threadingService.allThreads(inRoom: roomId, from: nextBatch, onlyParticipated: onlyParticipated) { [weak self] response in
guard let self = self else { return }
switch response {
case .success(let threads):
self.threads = threads
case .success(let value):
self.threads = self.threads + value.threads
self.nextBatch = value.nextBatch
self.threadsLoaded()
case .failure(let error):
MXLog.error("[ThreadListViewModel] loadData", context: error)
Expand Down
1 change: 1 addition & 0 deletions changelog.d/6059.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Threads: Load the thread list using server-side sorting and pagination

0 comments on commit c4d66d6

Please sign in to comment.