From 8a9f1d2a789074ce5fc1118df3438eb9e63ff00c Mon Sep 17 00:00:00 2001 From: Gil Eluard Date: Mon, 19 Dec 2022 14:56:06 +0100 Subject: [PATCH] Load the thread list using server-side sorting and pagination --- .../ThreadList/ThreadListViewController.swift | 6 +++++- .../ThreadList/ThreadListViewModel.swift | 19 ++++++++++++++----- changelog.d/6059.feature | 1 + 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 changelog.d/6059.feature diff --git a/Riot/Modules/Threads/ThreadList/ThreadListViewController.swift b/Riot/Modules/Threads/ThreadList/ThreadListViewController.swift index f4075a0cf9..7c532be5ad 100644 --- a/Riot/Modules/Threads/ThreadList/ThreadListViewController.swift +++ b/Riot/Modules/Threads/ThreadList/ThreadListViewController.swift @@ -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) } @@ -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) { diff --git a/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift b/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift index 29ae93b020..16620a6214 100644 --- a/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift +++ b/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift @@ -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? @@ -71,6 +71,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol { viewState = .showingFilterTypes case .selectFilterType(let type): selectedFilterType = type + resetData() loadData() case .selectThread(let index): selectThread(index) @@ -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 @@ -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) diff --git a/changelog.d/6059.feature b/changelog.d/6059.feature new file mode 100644 index 0000000000..c4a9e72215 --- /dev/null +++ b/changelog.d/6059.feature @@ -0,0 +1 @@ +Threads: Load the thread list using server-side sorting and pagination \ No newline at end of file