Skip to content

Commit

Permalink
Merge pull request #5561 from vector-im/ismail/5540_thread_list
Browse files Browse the repository at this point in the history
Thread List API Implementation
  • Loading branch information
ismailgulek committed Feb 15, 2022
2 parents c576932 + 5967e7f commit 05e64f3
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Riot/Modules/Threads/ThreadList/ThreadListCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
39 changes: 26 additions & 13 deletions Riot/Modules/Threads/ThreadList/ThreadListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -250,23 +250,36 @@ 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)")
self.viewState = .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
Expand Down Expand Up @@ -323,7 +336,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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Riot/Modules/Threads/ThreadsCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions changelog.d/5540.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ThreadListViewModel: Use new apis to fetch threads.

0 comments on commit 05e64f3

Please sign in to comment.