122 lines
4.3 KiB
Swift
122 lines
4.3 KiB
Swift
//
|
|
// CommonControllerAutocomplete.swift
|
|
// Wallet
|
|
//
|
|
// Created by Igor on 10.02.2021.
|
|
//
|
|
|
|
import UIKit
|
|
import Combine
|
|
import WalletUIComponents
|
|
|
|
class CommonControllerAutocomplete: UIViewController {
|
|
|
|
@IBOutlet private weak var tableView: UITableView!
|
|
@IBOutlet private weak var searchTextFieldContainer: UIView!
|
|
@IBOutlet private weak var height: NSLayoutConstraint!
|
|
|
|
private let viewModel: TextFieldViewModel
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
private let completion: (Common.Model.Menu) -> Void
|
|
private let service: CommonMenuAutocompleteService
|
|
private var collection = [Common.Model.Menu]()
|
|
private var isLoading = false
|
|
private var selected: Common.Model.Menu?
|
|
|
|
init(
|
|
image: UIImage? = nil,
|
|
title: String? = nil,
|
|
text: String? = nil,
|
|
placeholder: String? = nil,
|
|
service: CommonMenuAutocompleteService,
|
|
selected: Common.Model.Menu? = nil,
|
|
_ completion: @escaping (Common.Model.Menu) -> Void
|
|
) {
|
|
self.service = service
|
|
self.selected = selected
|
|
self.completion = completion
|
|
self.viewModel = TextFieldViewModel(placeholder: placeholder ?? "")
|
|
|
|
super.init(nibName: "CommonControllerAutocomplete", bundle: nil)
|
|
|
|
self.title = title
|
|
self.text = text
|
|
self.image = image
|
|
_ = view
|
|
self.height.constant = 2*UIScreen.main.bounds.height/3
|
|
self.view.layoutIfNeeded()
|
|
|
|
self.tableView.register(cell: CommonCellAutocomplete.self)
|
|
self.service.didUpdate = {
|
|
Loader.hide(in: self.tableView)
|
|
self.collection = service.collection
|
|
self.isLoading = false
|
|
self.tableView.reloadData()
|
|
}
|
|
self.reload()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
self.setupTextFieldSearch()
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
|
|
self.viewModel.objectWillChange
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { [weak self] in
|
|
guard let self else { return }
|
|
self.textFieldDidChange(self.viewModel.text)
|
|
}
|
|
.store(in: &self.cancellables)
|
|
}
|
|
|
|
private func reload() {
|
|
self.isLoading = true
|
|
self.tableView.reloadData()
|
|
if !Loader.isLoading(in: self.tableView) {
|
|
Loader.show(in: self.tableView)
|
|
}
|
|
self.service.apply(filter: self.viewModel.text)
|
|
}
|
|
|
|
private func setupTextFieldSearch() {
|
|
let textField = CommonTextFieldSearch(viewModel: self.viewModel)
|
|
let hostingTextFieldView = UIHostingView(rootView: textField, autoresizingMask: false)
|
|
self.searchTextFieldContainer.addSubview(hostingTextFieldView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
hostingTextFieldView.topAnchor.constraint(equalTo: self.searchTextFieldContainer.topAnchor),
|
|
hostingTextFieldView.leadingAnchor.constraint(equalTo: self.searchTextFieldContainer.leadingAnchor),
|
|
hostingTextFieldView.trailingAnchor.constraint(equalTo: self.searchTextFieldContainer.trailingAnchor),
|
|
hostingTextFieldView.bottomAnchor.constraint(equalTo: self.searchTextFieldContainer.bottomAnchor)
|
|
])
|
|
}
|
|
|
|
private func textFieldDidChange(_ text: String?) {
|
|
self.reload()
|
|
}
|
|
}
|
|
|
|
extension CommonControllerAutocomplete: UITableViewDataSource, UITableViewDelegate {
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { self.isLoading ? 0 : self.collection.count }
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
let cell = tableView.dequeueReusableCell(CommonCellAutocomplete.self, indexPath: indexPath)
|
|
cell.adjust(to: self.collection[indexPath.row], isSelected: self.selected == self.collection[indexPath.row])
|
|
return cell
|
|
}
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
self.selected = collection[indexPath.row]
|
|
tableView.reloadData()
|
|
self.dismiss(animated: true) { self.completion(self.collection[indexPath.row]) }
|
|
}
|
|
}
|