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 {
if parameters.threadId != nil {
return self.navigationRouter?.toPresentable() ?? self.roomViewController
}
return self.roomViewController
}

View file

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

View file

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

View file

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

View file

@ -114,7 +114,9 @@ final class ThreadListViewController: UIViewController {
titleView.mode = .allThreads
titleView.viewDelegate = self
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.register(cellType: ThreadTableViewCell.self)
@ -231,6 +233,8 @@ extension ThreadListViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
viewModel.process(viewAction: .selectThread(indexPath.row))
}
}

View file

@ -63,13 +63,15 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
switch viewAction {
case .loadData:
loadData()
case .complete:
coordinatorDelegate?.threadListViewModelDidLoadThreads(self)
case .showFilterTypes:
viewState = .showingFilterTypes
case .selectFilterType(let type):
selectedFilterType = type
loadData()
case .complete:
coordinatorDelegate?.threadListViewModelDidLoadThreads(self)
case .selectThread(let index):
selectThread(index)
case .cancel:
cancelOperations()
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() {
self.currentOperation?.cancel()
}

View file

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

View file

@ -26,6 +26,7 @@ final class ThreadsCoordinator: NSObject, ThreadsCoordinatorProtocol {
// MARK: Private
private let parameters: ThreadsCoordinatorParameters
private var selectedThreadCoordinator: RoomCoordinator?
private var navigationRouter: NavigationRouterType {
return self.parameters.navigationRouter
@ -42,6 +43,11 @@ final class ThreadsCoordinator: NSObject, ThreadsCoordinatorProtocol {
init(parameters: ThreadsCoordinatorParameters) {
self.parameters = parameters
super.init()
NotificationCenter.default.addObserver(self,
selector: #selector(didPopModule(_:)),
name: NavigationRouter.didPopModule,
object: nil)
}
// MARK: - Public
@ -73,6 +79,21 @@ final class ThreadsCoordinator: NSObject, ThreadsCoordinatorProtocol {
}
// 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 {
let coordinatorParameters = ThreadListCoordinatorParameters(session: self.parameters.session,
@ -81,6 +102,19 @@ final class ThreadsCoordinator: NSObject, ThreadsCoordinatorProtocol {
coordinator.delegate = self
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
@ -97,7 +131,36 @@ extension ThreadsCoordinator: ThreadListCoordinatorDelegate {
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) {
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) {
}
}