Add my threads filtering

This commit is contained in:
ismailgulek
2021-11-19 03:36:51 +03:00
parent eac62e1811
commit d34f5d43f3
7 changed files with 89 additions and 8 deletions
@@ -21,6 +21,8 @@ import Foundation
/// ThreadListViewController view actions exposed to view model
enum ThreadListViewAction {
case loadData
case showFilterTypes
case selectFilterType(_ type: ThreadListFilterType)
case complete
case cancel
}
@@ -117,7 +117,7 @@ final class ThreadListViewController: UIViewController {
self.navigationItem.rightBarButtonItem = filterBarButtonItem
self.title = "Threads"
self.title = VectorL10n.threadsTitle
self.threadsTableView.tableFooterView = UIView()
self.threadsTableView.register(cellType: ThreadTableViewCell.self)
@@ -132,6 +132,8 @@ final class ThreadListViewController: UIViewController {
self.renderLoading()
case .loaded:
self.renderLoaded()
case .showingFilterTypes:
self.renderShowingFilterTypes()
case .error(let error):
self.render(error: error)
}
@@ -146,6 +148,40 @@ final class ThreadListViewController: UIViewController {
self.threadsTableView.reloadData()
}
private func renderShowingFilterTypes() {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let allThreadsAction = UIAlertAction(title: ThreadListFilterType.all.title,
style: .default,
handler: { [weak self] action in
guard let self = self else { return }
self.viewModel.process(viewAction: .selectFilterType(.all))
})
if self.viewModel.selectedFilterType == .all {
allThreadsAction.setValue(true, forKey: "checked")
}
alertController.addAction(allThreadsAction)
let myThreadsAction = UIAlertAction(title: ThreadListFilterType.myThreads.title,
style: .default,
handler: { [weak self] action in
guard let self = self else { return }
self.viewModel.process(viewAction: .selectFilterType(.myThreads))
})
if self.viewModel.selectedFilterType == .myThreads {
myThreadsAction.setValue(true, forKey: "checked")
}
alertController.addAction(myThreadsAction)
alertController.addAction(UIAlertAction(title: VectorL10n.cancel,
style: .cancel,
handler: nil))
alertController.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
self.present(alertController, animated: true, completion: nil)
}
private func render(error: Error) {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
self.errorPresenter.presentError(from: self, forError: error, animated: true, handler: nil)
@@ -155,7 +191,7 @@ final class ThreadListViewController: UIViewController {
@objc
private func filterButtonTapped(_ sender: UIBarButtonItem) {
self.viewModel.process(viewAction: .complete)
self.viewModel.process(viewAction: .showFilterTypes)
}
}
@@ -36,6 +36,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
weak var viewDelegate: ThreadListViewModelViewDelegate?
weak var coordinatorDelegate: ThreadListViewModelCoordinatorDelegate?
var selectedFilterType: ThreadListFilterType = .all
private(set) var viewState: ThreadListViewState = .idle {
didSet {
@@ -49,6 +50,7 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
roomId: String) {
self.session = session
self.roomId = roomId
session.threadingService.addDelegate(self)
}
deinit {
@@ -60,12 +62,17 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
func process(viewAction: ThreadListViewAction) {
switch viewAction {
case .loadData:
self.loadData()
loadData()
case .showFilterTypes:
viewState = .showingFilterTypes
case .selectFilterType(let type):
selectedFilterType = type
loadData()
case .complete:
self.coordinatorDelegate?.threadListViewModelDidLoadThreads(self)
coordinatorDelegate?.threadListViewModelDidLoadThreads(self)
case .cancel:
self.cancelOperations()
self.coordinatorDelegate?.threadListViewModelDidCancel(self)
cancelOperations()
coordinatorDelegate?.threadListViewModelDidCancel(self)
}
}
@@ -166,8 +173,13 @@ final class ThreadListViewModel: ThreadListViewModelProtocol {
viewState = .loading
threads = session.threadingService.threads(inRoom: roomId)
session.threadingService.addDelegate(self)
switch selectedFilterType {
case .all:
threads = session.threadingService.threads(inRoom: roomId)
case .myThreads:
threads = session.threadingService.participatedThreads(inRoom: roomId)
}
threadsLoaded()
}
@@ -37,6 +37,21 @@ protocol ThreadListViewModelProtocol {
var viewState: ThreadListViewState { get }
var selectedFilterType: ThreadListFilterType { get }
var numberOfThreads: Int { get }
func threadViewModel(at index: Int) -> ThreadViewModel?
}
enum ThreadListFilterType {
case all
case myThreads
var title: String {
switch self {
case .all:
return VectorL10n.threadsActionAllThreads
case .myThreads:
return VectorL10n.threadsActionMyThreads
}
}
}
@@ -23,5 +23,6 @@ enum ThreadListViewState {
case idle
case loading
case loaded
case showingFilterTypes
case error(Error)
}