Filter servers (#540)

This commit is contained in:
Nathan Mattes 2022-11-13 14:02:28 +01:00
parent f9f14cff3c
commit a910d67876
2 changed files with 22 additions and 10 deletions

View File

@ -63,11 +63,11 @@ class MastodonLoginViewController: UIViewController {
let dataSource = UITableViewDiffableDataSource<MastodonLoginViewSection, Mastodon.Entity.Server>(tableView: contentView.tableView) { [weak self] tableView, indexPath, itemIdentifier in let dataSource = UITableViewDiffableDataSource<MastodonLoginViewSection, Mastodon.Entity.Server>(tableView: contentView.tableView) { [weak self] tableView, indexPath, itemIdentifier in
guard let cell = tableView.dequeueReusableCell(withIdentifier: MastodonLoginServerTableViewCell.reuseIdentifier, for: indexPath) as? MastodonLoginServerTableViewCell, guard let cell = tableView.dequeueReusableCell(withIdentifier: MastodonLoginServerTableViewCell.reuseIdentifier, for: indexPath) as? MastodonLoginServerTableViewCell,
let self = self else { let self = self else {
fatalError("Wrong cell") fatalError("Wrong cell")
} }
let server = self.viewModel.serverList[indexPath.row] let server = self.viewModel.filteredServers[indexPath.row]
var configuration = cell.defaultContentConfiguration() var configuration = cell.defaultContentConfiguration()
configuration.text = server.domain configuration.text = server.domain
@ -83,7 +83,7 @@ class MastodonLoginViewController: UIViewController {
cell.backgroundColor = .systemBackground cell.backgroundColor = .systemBackground
} }
if self.viewModel.serverList.last == server { if self.viewModel.filteredServers.last == server {
cell.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner] cell.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
cell.layer.cornerRadius = 10 cell.layer.cornerRadius = 10
cell.layer.masksToBounds = true cell.layer.masksToBounds = true
@ -166,9 +166,7 @@ class MastodonLoginViewController: UIViewController {
} }
@objc func textfieldDidChange(_ textField: UITextField) { @objc func textfieldDidChange(_ textField: UITextField) {
print(textField.text ?? "---") viewModel.filterServers(withText: textField.text)
//TODO: @zeitschlag filter server-list, update tableview
contentView.navigationActionView.nextButton.isEnabled = false contentView.navigationActionView.nextButton.isEnabled = false
} }
} }
@ -179,7 +177,7 @@ extension MastodonLoginViewController: OnboardingViewControllerAppearance { }
//MARK: - UITableViewDelegate //MARK: - UITableViewDelegate
extension MastodonLoginViewController: UITableViewDelegate { extension MastodonLoginViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let server = viewModel.serverList[indexPath.row] let server = viewModel.filteredServers[indexPath.row]
viewModel.selectedServer = server viewModel.selectedServer = server
contentView.searchTextField.text = server.domain contentView.searchTextField.text = server.domain
@ -195,12 +193,12 @@ extension MastodonLoginViewController: MastodonLoginViewModelDelegate {
var snapshot = NSDiffableDataSourceSnapshot<MastodonLoginViewSection, Mastodon.Entity.Server>() var snapshot = NSDiffableDataSourceSnapshot<MastodonLoginViewSection, Mastodon.Entity.Server>()
snapshot.appendSections([MastodonLoginViewSection.servers]) snapshot.appendSections([MastodonLoginViewSection.servers])
snapshot.appendItems(viewModel.serverList) snapshot.appendItems(viewModel.filteredServers)
dataSource?.applySnapshot(snapshot, animated: false) dataSource?.applySnapshot(snapshot, animated: false)
OperationQueue.main.addOperation { OperationQueue.main.addOperation {
let numberOfResults = viewModel.serverList.count let numberOfResults = viewModel.filteredServers.count
self.contentView.updateCorners(numberOfResults: numberOfResults) self.contentView.updateCorners(numberOfResults: numberOfResults)
} }
} }

View File

@ -16,8 +16,9 @@ protocol MastodonLoginViewModelDelegate: AnyObject {
class MastodonLoginViewModel { class MastodonLoginViewModel {
var serverList: [Mastodon.Entity.Server] = [] private var serverList: [Mastodon.Entity.Server] = []
var selectedServer: Mastodon.Entity.Server? var selectedServer: Mastodon.Entity.Server?
var filteredServers: [Mastodon.Entity.Server] = []
weak var appContext: AppContext? weak var appContext: AppContext?
weak var delegate: MastodonLoginViewModelDelegate? weak var delegate: MastodonLoginViewModelDelegate?
@ -42,4 +43,17 @@ class MastodonLoginViewModel {
self.serverList = servers self.serverList = servers
}).store(in: &disposeBag) }).store(in: &disposeBag)
} }
func filterServers(withText query: String?) {
guard let query else {
filteredServers = serverList
delegate?.serversUpdated(self)
return
}
filteredServers = serverList.filter { $0.domain.contains(query) }.sorted {$0.totalUsers > $1.totalUsers }
delegate?.serversUpdated(self)
// AuthenticationViewModel.parseDomain(from: query)
}
} }