From 1fa8da3f348e60c22d062a53f3a67c3a68de2879 Mon Sep 17 00:00:00 2001 From: ismailgulek Date: Thu, 10 Feb 2022 16:48:27 +0300 Subject: [PATCH 1/4] Use thread protocol when possible --- .../Threads/ThreadList/ThreadListCoordinator.swift | 4 ++-- .../ThreadList/ThreadListCoordinatorProtocol.swift | 2 +- .../Threads/ThreadList/ThreadListViewModel.swift | 12 ++++++------ .../ThreadList/ThreadListViewModelProtocol.swift | 4 ++-- .../Threads/ThreadList/Views/Cell/ThreadModel.swift | 2 +- Riot/Modules/Threads/ThreadsCoordinator.swift | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Riot/Modules/Threads/ThreadList/ThreadListCoordinator.swift b/Riot/Modules/Threads/ThreadList/ThreadListCoordinator.swift index a870d8808b..618d967c53 100644 --- a/Riot/Modules/Threads/ThreadList/ThreadListCoordinator.swift +++ b/Riot/Modules/Threads/ThreadList/ThreadListCoordinator.swift @@ -65,11 +65,11 @@ extension ThreadListCoordinator: ThreadListViewModelCoordinatorDelegate { self.delegate?.threadListCoordinatorDidLoadThreads(self) } - func threadListViewModelDidSelectThread(_ viewModel: ThreadListViewModelProtocol, thread: MXThread) { + func threadListViewModelDidSelectThread(_ viewModel: ThreadListViewModelProtocol, thread: MXThreadProtocol) { self.delegate?.threadListCoordinatorDidSelectThread(self, thread: thread) } - func threadListViewModelDidSelectThreadViewInRoom(_ viewModel: ThreadListViewModelProtocol, thread: MXThread) { + func threadListViewModelDidSelectThreadViewInRoom(_ viewModel: ThreadListViewModelProtocol, thread: MXThreadProtocol) { self.delegate?.threadListCoordinatorDidSelectRoom(self, roomId: thread.roomId, eventId: thread.id) } diff --git a/Riot/Modules/Threads/ThreadList/ThreadListCoordinatorProtocol.swift b/Riot/Modules/Threads/ThreadList/ThreadListCoordinatorProtocol.swift index e235054808..fb9b02bee0 100644 --- a/Riot/Modules/Threads/ThreadList/ThreadListCoordinatorProtocol.swift +++ b/Riot/Modules/Threads/ThreadList/ThreadListCoordinatorProtocol.swift @@ -20,7 +20,7 @@ import Foundation protocol ThreadListCoordinatorDelegate: AnyObject { func threadListCoordinatorDidLoadThreads(_ coordinator: ThreadListCoordinatorProtocol) - func threadListCoordinatorDidSelectThread(_ coordinator: ThreadListCoordinatorProtocol, thread: MXThread) + func threadListCoordinatorDidSelectThread(_ coordinator: ThreadListCoordinatorProtocol, thread: MXThreadProtocol) func threadListCoordinatorDidSelectRoom(_ coordinator: ThreadListCoordinatorProtocol, roomId: String, eventId: String) func threadListCoordinatorDidCancel(_ coordinator: ThreadListCoordinatorProtocol) } diff --git a/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift b/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift index 381c19f52a..01d42bf474 100644 --- a/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift +++ b/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift @@ -26,12 +26,12 @@ final class ThreadListViewModel: ThreadListViewModelProtocol { private let session: MXSession private let roomId: String - private var threads: [MXThread] = [] + private var threads: [MXThreadProtocol] = [] private var eventFormatter: MXKEventFormatter? private var roomState: MXRoomState? private var currentOperation: MXHTTPOperation? - private var longPressedThread: MXThread? + private var longPressedThread: MXThreadProtocol? // MARK: Public @@ -144,7 +144,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol { // MARK: - Private - private func model(forThread thread: MXThread) -> ThreadModel { + private func model(forThread thread: MXThreadProtocol) -> ThreadModel { let rootAvatarViewData: AvatarViewData? let rootMessageSender: MXUser? let lastAvatarViewData: AvatarViewData? @@ -199,7 +199,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol { notificationStatus: notificationStatus) } - private func rootMessageText(forThread thread: MXThread) -> NSAttributedString? { + private func rootMessageText(forThread thread: MXThreadProtocol) -> NSAttributedString? { guard let eventFormatter = eventFormatter else { return nil } @@ -229,7 +229,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol { error: formatterError) } - private func lastMessageTextAndTime(forThread thread: MXThread) -> (NSAttributedString?, String?) { + private func lastMessageTextAndTime(forThread thread: MXThreadProtocol) -> (NSAttributedString?, String?) { guard let eventFormatter = eventFormatter else { return (nil, nil) } @@ -323,7 +323,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol { private func actionShare() { guard let thread = longPressedThread, - let index = threads.firstIndex(of: thread) else { + let index = threads.firstIndex(where: { thread.id == $0.id }) else { return } if let permalink = MXTools.permalink(toEvent: thread.id, inRoom: thread.roomId), diff --git a/Riot/Modules/Threads/ThreadList/ThreadListViewModelProtocol.swift b/Riot/Modules/Threads/ThreadList/ThreadListViewModelProtocol.swift index 3720f0c35f..09db9be206 100644 --- a/Riot/Modules/Threads/ThreadList/ThreadListViewModelProtocol.swift +++ b/Riot/Modules/Threads/ThreadList/ThreadListViewModelProtocol.swift @@ -24,8 +24,8 @@ protocol ThreadListViewModelViewDelegate: AnyObject { protocol ThreadListViewModelCoordinatorDelegate: AnyObject { func threadListViewModelDidLoadThreads(_ viewModel: ThreadListViewModelProtocol) - func threadListViewModelDidSelectThread(_ viewModel: ThreadListViewModelProtocol, thread: MXThread) - func threadListViewModelDidSelectThreadViewInRoom(_ viewModel: ThreadListViewModelProtocol, thread: MXThread) + func threadListViewModelDidSelectThread(_ viewModel: ThreadListViewModelProtocol, thread: MXThreadProtocol) + func threadListViewModelDidSelectThreadViewInRoom(_ viewModel: ThreadListViewModelProtocol, thread: MXThreadProtocol) func threadListViewModelDidCancel(_ viewModel: ThreadListViewModelProtocol) } diff --git a/Riot/Modules/Threads/ThreadList/Views/Cell/ThreadModel.swift b/Riot/Modules/Threads/ThreadList/Views/Cell/ThreadModel.swift index 1692b9f6cd..6667d5bfc5 100644 --- a/Riot/Modules/Threads/ThreadList/Views/Cell/ThreadModel.swift +++ b/Riot/Modules/Threads/ThreadList/Views/Cell/ThreadModel.swift @@ -32,7 +32,7 @@ enum ThreadNotificationStatus { case notified case highlighted - init(withThread thread: MXThread) { + init(withThread thread: MXThreadProtocol) { if thread.highlightCount > 0 { self = .highlighted } else if thread.isParticipated && thread.notificationCount > 0 { diff --git a/Riot/Modules/Threads/ThreadsCoordinator.swift b/Riot/Modules/Threads/ThreadsCoordinator.swift index f5857dcc97..d645decd36 100644 --- a/Riot/Modules/Threads/ThreadsCoordinator.swift +++ b/Riot/Modules/Threads/ThreadsCoordinator.swift @@ -153,7 +153,7 @@ extension ThreadsCoordinator: ThreadListCoordinatorDelegate { } - func threadListCoordinatorDidSelectThread(_ coordinator: ThreadListCoordinatorProtocol, thread: MXThread) { + func threadListCoordinatorDidSelectThread(_ coordinator: ThreadListCoordinatorProtocol, thread: MXThreadProtocol) { let roomCoordinator = createThreadCoordinator(forThreadId: thread.id) selectedThreadCoordinator = roomCoordinator roomCoordinator.start() From ee2b4e40587d038bcc0315273ec6a1df03b79ea9 Mon Sep 17 00:00:00 2001 From: ismailgulek Date: Thu, 10 Feb 2022 17:35:18 +0300 Subject: [PATCH 2/4] Use new api to fetch thread list --- .../ThreadList/ThreadListViewModel.swift | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift b/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift index 01d42bf474..2242250ba5 100644 --- a/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift +++ b/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift @@ -250,23 +250,35 @@ final class ThreadListViewModel: ThreadListViewModelProtocol { if showLoading { viewState = .loading } + + let onlyParticipated: Bool switch selectedFilterType { case .all: - threads = session.threadingService.threads(inRoom: roomId) + onlyParticipated = false case .myThreads: - threads = session.threadingService.participatedThreads(inRoom: roomId) + onlyParticipated = true } - if threads.isEmpty { - viewState = .empty(emptyViewModel) - return + session.threadingService.allThreads(inRoom: roomId, + onlyParticipated: onlyParticipated) { [weak self] response in + guard let self = self else { return } + switch response { + case .success(let threads): + self.threads = threads + self.threadsLoaded() + case .failure(let error): + MXLog.error("[ThreadListViewModel] loadData: error: \(error)") + } } - - threadsLoaded() } private func threadsLoaded() { + if threads.isEmpty { + viewState = .empty(emptyViewModel) + return + } + guard let eventFormatter = session.roomSummaryUpdateDelegate as? MXKEventFormatter, let room = session.room(withRoomId: roomId) else { // go into loaded state From b51c717bdd4bc188d9e5f11f47ae1a8da58b5f4f Mon Sep 17 00:00:00 2001 From: ismailgulek Date: Thu, 10 Feb 2022 17:35:54 +0300 Subject: [PATCH 3/4] Add changelog --- changelog.d/5540.change | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/5540.change diff --git a/changelog.d/5540.change b/changelog.d/5540.change new file mode 100644 index 0000000000..f885fdd8cf --- /dev/null +++ b/changelog.d/5540.change @@ -0,0 +1 @@ +ThreadListViewModel: Use new apis to fetch threads. From 5967e7fbb693d06d59e03a570ab9caf0a39b1e7d Mon Sep 17 00:00:00 2001 From: ismailgulek Date: Tue, 15 Feb 2022 15:46:22 +0300 Subject: [PATCH 4/4] Display thread list api errors --- Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift b/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift index 2242250ba5..e63a637030 100644 --- a/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift +++ b/Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift @@ -269,6 +269,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol { self.threadsLoaded() case .failure(let error): MXLog.error("[ThreadListViewModel] loadData: error: \(error)") + self.viewState = .error(error) } } }