Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search to send-contact-list (#2209) #2218

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions deltachat-ios/Chat/Send Contact/SendContactViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,33 @@ class SendContactViewController: UIViewController {

private let context: DcContext
private let contactIds: [Int]
private var filteredContactIds: [Int]

let tableView: UITableView

var delegate: SendContactViewControllerDelegate?
let searchController: UISearchController
let emptySearchStateLabel: EmptyStateLabel
private var emptySearchStateLabelWidthConstraint: NSLayoutConstraint?

init(dcContext: DcContext) {
tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(ContactCell.self, forCellReuseIdentifier: ContactCell.reuseIdentifier)
tableView.keyboardDismissMode = .onDrag

context = dcContext
contactIds = dcContext.getContacts(flags: DC_GCL_ADD_SELF)
filteredContactIds = contactIds

searchController = UISearchController(searchResultsController: nil)
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = String.localized("search")
searchController.searchBar.showsCancelButton = false
searchController.hidesNavigationBarDuringPresentation = false

emptySearchStateLabel = EmptyStateLabel()
emptySearchStateLabel.isHidden = true

super.init(nibName: nil, bundle: nil)

Expand All @@ -33,6 +48,10 @@ class SendContactViewController: UIViewController {

let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(SendContactViewController.cancel(_:)))
navigationItem.rightBarButtonItem = cancelButton

searchController.searchResultsUpdater = self
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
}

required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
Expand All @@ -45,39 +64,84 @@ class SendContactViewController: UIViewController {
view.bottomAnchor.constraint(equalTo: tableView.bottomAnchor),
]

emptySearchStateLabelWidthConstraint = emptySearchStateLabel.widthAnchor.constraint(equalTo: tableView.widthAnchor)

NSLayoutConstraint.activate(constraints)
}

// MARK: - Actions

@objc func cancel(_ sender: Any) {
searchController.isActive = false
dismiss(animated: true)
}
}

// MARK: - UITableViewDelegate
extension SendContactViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)

let contactId = contactIds[indexPath.row]
let contactId = filteredContactIds[indexPath.row]
delegate?.contactSelected(self, contactId: contactId)

searchController.isActive = false
dismiss(animated: true)
}
}

// MARK: - UITableViewDataSource
extension SendContactViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contactIds.count
return filteredContactIds.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: ContactCell.reuseIdentifier, for: indexPath) as? ContactCell else { fatalError("Where's my ContactCell??") }

let contactId = contactIds[indexPath.row]
let contactId = filteredContactIds[indexPath.row]
let viewModel = ContactCellViewModel.make(contactId: contactId, dcContext: context)
cell.updateCell(cellViewModel: viewModel)

return cell
}
}

// MARK: - UISearchResultsUpdating
extension SendContactViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text else {
filteredContactIds = contactIds
return
}

filterContentForSearchText(searchText)
}

private func filterContentForSearchText(_ searchText: String) {
filteredContactIds = filterContactIds(queryString: searchText)
tableView.reloadData()
tableView.scrollToTop()

// handle empty searchstate
if searchController.isActive && filteredContactIds.isEmpty {
let text = String.localizedStringWithFormat(
String.localized("search_no_result_for_x"),
searchText
)
emptySearchStateLabel.text = text
emptySearchStateLabel.isHidden = false
tableView.tableHeaderView = emptySearchStateLabel
emptySearchStateLabelWidthConstraint?.isActive = true
} else {
emptySearchStateLabel.text = nil
emptySearchStateLabel.isHidden = true
emptySearchStateLabelWidthConstraint?.isActive = false
tableView.tableHeaderView = nil
}
}

private func filterContactIds(queryString: String) -> [Int] {
return context.getContacts(flags: DC_GCL_ADD_SELF, queryString: queryString)
}
}
Loading