Display thread when row tapped

This commit is contained in:
ismailgulek 2021-11-19 17:51:40 +03:00
parent 40236a8e0b
commit 08f43e5bcb
No known key found for this signature in database
GPG key ID: E96336D42D9470A9
8 changed files with 88 additions and 7 deletions

View file

@ -136,9 +136,6 @@ final class RoomCoordinator: NSObject, RoomCoordinatorProtocol {
} }
func toPresentable() -> UIViewController { func toPresentable() -> UIViewController {
if parameters.threadId != nil {
return self.navigationRouter?.toPresentable() ?? self.roomViewController
}
return self.roomViewController return self.roomViewController
} }

View file

@ -65,6 +65,10 @@ extension ThreadListCoordinator: ThreadListViewModelCoordinatorDelegate {
self.delegate?.threadListCoordinatorDidLoadThreads(self) self.delegate?.threadListCoordinatorDidLoadThreads(self)
} }
func threadListViewModelDidSelectThread(_ viewModel: ThreadListViewModelProtocol, thread: MXThread) {
self.delegate?.threadListCoordinatorDidSelectThread(self, thread: thread)
}
func threadListViewModelDidCancel(_ viewModel: ThreadListViewModelProtocol) { func threadListViewModelDidCancel(_ viewModel: ThreadListViewModelProtocol) {
self.delegate?.threadListCoordinatorDidCancel(self) self.delegate?.threadListCoordinatorDidCancel(self)
} }

View file

@ -20,6 +20,7 @@ import Foundation
protocol ThreadListCoordinatorDelegate: AnyObject { protocol ThreadListCoordinatorDelegate: AnyObject {
func threadListCoordinatorDidLoadThreads(_ coordinator: ThreadListCoordinatorProtocol) func threadListCoordinatorDidLoadThreads(_ coordinator: ThreadListCoordinatorProtocol)
func threadListCoordinatorDidSelectThread(_ coordinator: ThreadListCoordinatorProtocol, thread: MXThread)
func threadListCoordinatorDidCancel(_ coordinator: ThreadListCoordinatorProtocol) func threadListCoordinatorDidCancel(_ coordinator: ThreadListCoordinatorProtocol)
} }

View file

@ -21,8 +21,9 @@ import Foundation
/// ThreadListViewController view actions exposed to view model /// ThreadListViewController view actions exposed to view model
enum ThreadListViewAction { enum ThreadListViewAction {
case loadData case loadData
case complete
case showFilterTypes case showFilterTypes
case selectFilterType(_ type: ThreadListFilterType) case selectFilterType(_ type: ThreadListFilterType)
case complete case selectThread(_ index: Int)
case cancel case cancel
} }

View file

@ -114,7 +114,9 @@ final class ThreadListViewController: UIViewController {
titleView.mode = .allThreads titleView.mode = .allThreads
titleView.viewDelegate = self titleView.viewDelegate = self
titleView.configure(withViewModel: viewModel.titleViewModel) titleView.configure(withViewModel: viewModel.titleViewModel)
navigationItem.titleView = titleView navigationItem.leftItemsSupplementBackButton = true
navigationItem.backBarButtonItem = UIBarButtonItem(title: nil, style: .plain, target: nil, action: nil)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: titleView)
self.threadsTableView.tableFooterView = UIView() self.threadsTableView.tableFooterView = UIView()
self.threadsTableView.register(cellType: ThreadTableViewCell.self) self.threadsTableView.register(cellType: ThreadTableViewCell.self)
@ -231,6 +233,8 @@ extension ThreadListViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true) tableView.deselectRow(at: indexPath, animated: true)
viewModel.process(viewAction: .selectThread(indexPath.row))
} }
} }

View file

@ -63,13 +63,15 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
switch viewAction { switch viewAction {
case .loadData: case .loadData:
loadData() loadData()
case .complete:
coordinatorDelegate?.threadListViewModelDidLoadThreads(self)
case .showFilterTypes: case .showFilterTypes:
viewState = .showingFilterTypes viewState = .showingFilterTypes
case .selectFilterType(let type): case .selectFilterType(let type):
selectedFilterType = type selectedFilterType = type
loadData() loadData()
case .complete: case .selectThread(let index):
coordinatorDelegate?.threadListViewModelDidLoadThreads(self) selectThread(index)
case .cancel: case .cancel:
cancelOperations() cancelOperations()
coordinatorDelegate?.threadListViewModelDidCancel(self) coordinatorDelegate?.threadListViewModelDidCancel(self)
@ -226,6 +228,14 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
} }
} }
private func selectThread(_ index: Int) {
guard index < threads.count else {
return
}
let thread = threads[index]
coordinatorDelegate?.threadListViewModelDidSelectThread(self, thread: thread)
}
private func cancelOperations() { private func cancelOperations() {
self.currentOperation?.cancel() self.currentOperation?.cancel()
} }

View file

@ -24,6 +24,7 @@ protocol ThreadListViewModelViewDelegate: AnyObject {
protocol ThreadListViewModelCoordinatorDelegate: AnyObject { protocol ThreadListViewModelCoordinatorDelegate: AnyObject {
func threadListViewModelDidLoadThreads(_ viewModel: ThreadListViewModelProtocol) func threadListViewModelDidLoadThreads(_ viewModel: ThreadListViewModelProtocol)
func threadListViewModelDidSelectThread(_ viewModel: ThreadListViewModelProtocol, thread: MXThread)
func threadListViewModelDidCancel(_ viewModel: ThreadListViewModelProtocol) func threadListViewModelDidCancel(_ viewModel: ThreadListViewModelProtocol)
} }

View file

@ -26,6 +26,7 @@ final class ThreadsCoordinator: NSObject, ThreadsCoordinatorProtocol {
// MARK: Private // MARK: Private
private let parameters: ThreadsCoordinatorParameters private let parameters: ThreadsCoordinatorParameters
private var selectedThreadCoordinator: RoomCoordinator?
private var navigationRouter: NavigationRouterType { private var navigationRouter: NavigationRouterType {
return self.parameters.navigationRouter return self.parameters.navigationRouter
@ -42,6 +43,11 @@ final class ThreadsCoordinator: NSObject, ThreadsCoordinatorProtocol {
init(parameters: ThreadsCoordinatorParameters) { init(parameters: ThreadsCoordinatorParameters) {
self.parameters = parameters self.parameters = parameters
super.init()
NotificationCenter.default.addObserver(self,
selector: #selector(didPopModule(_:)),
name: NavigationRouter.didPopModule,
object: nil)
} }
// MARK: - Public // MARK: - Public
@ -74,6 +80,21 @@ final class ThreadsCoordinator: NSObject, ThreadsCoordinatorProtocol {
// MARK: - Private // MARK: - Private
@objc
private func didPopModule(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let module = userInfo[NavigationRouter.NotificationUserInfoKey.module] as? Presentable,
let selectedThreadCoordinator = selectedThreadCoordinator else {
return
}
if module.toPresentable() == selectedThreadCoordinator.toPresentable() {
selectedThreadCoordinator.delegate = nil
remove(childCoordinator: selectedThreadCoordinator)
self.selectedThreadCoordinator = nil
}
}
private func createThreadListCoordinator() -> ThreadListCoordinator { private func createThreadListCoordinator() -> ThreadListCoordinator {
let coordinatorParameters = ThreadListCoordinatorParameters(session: self.parameters.session, let coordinatorParameters = ThreadListCoordinatorParameters(session: self.parameters.session,
roomId: self.parameters.roomId) roomId: self.parameters.roomId)
@ -81,6 +102,19 @@ final class ThreadsCoordinator: NSObject, ThreadsCoordinatorProtocol {
coordinator.delegate = self coordinator.delegate = self
return coordinator return coordinator
} }
private func createThreadCoordinator(forThread thread: MXThread) -> RoomCoordinator {
let parameters = RoomCoordinatorParameters(navigationRouter: navigationRouter,
navigationRouterStore: nil,
session: parameters.session,
roomId: parameters.roomId,
eventId: nil,
threadId: thread.identifier,
displayConfiguration: .forThreads)
let coordinator = RoomCoordinator(parameters: parameters)
coordinator.delegate = self
return coordinator
}
} }
// MARK: - UIAdaptivePresentationControllerDelegate // MARK: - UIAdaptivePresentationControllerDelegate
@ -97,7 +131,36 @@ extension ThreadsCoordinator: ThreadListCoordinatorDelegate {
self.delegate?.threadsCoordinatorDidComplete(self) self.delegate?.threadsCoordinatorDidComplete(self)
} }
func threadListCoordinatorDidSelectThread(_ coordinator: ThreadListCoordinatorProtocol, thread: MXThread) {
let roomCoordinator = createThreadCoordinator(forThread: thread)
selectedThreadCoordinator = roomCoordinator
roomCoordinator.start()
self.add(childCoordinator: roomCoordinator)
}
func threadListCoordinatorDidCancel(_ coordinator: ThreadListCoordinatorProtocol) { func threadListCoordinatorDidCancel(_ coordinator: ThreadListCoordinatorProtocol) {
self.delegate?.threadsCoordinatorDidComplete(self) self.delegate?.threadsCoordinatorDidComplete(self)
} }
} }
// MARK: - RoomCoordinatorDelegate
extension ThreadsCoordinator: RoomCoordinatorDelegate {
func roomCoordinatorDidLeaveRoom(_ coordinator: RoomCoordinatorProtocol) {
}
func roomCoordinatorDidCancelRoomPreview(_ coordinator: RoomCoordinatorProtocol) {
}
func roomCoordinator(_ coordinator: RoomCoordinatorProtocol, didSelectRoomWithId roomId: String, eventId: String?) {
}
func roomCoordinatorDidDismissInteractively(_ coordinator: RoomCoordinatorProtocol) {
}
}