feat: search contacts in create closed group page

pull/689/head
Ryan Zhao 2 years ago
parent 67a9047df7
commit 6e3c80c16b

@ -21,15 +21,21 @@ private final class TableView: UITableView {
final class NewClosedGroupVC: BaseVC, UITableViewDataSource, UITableViewDelegate, TableViewTouchDelegate, UITextFieldDelegate, UIScrollViewDelegate { final class NewClosedGroupVC: BaseVC, UITableViewDataSource, UITableViewDelegate, TableViewTouchDelegate, UITextFieldDelegate, UIScrollViewDelegate {
private let contactProfiles: [Profile] = Profile.fetchAllContactProfiles(excludeCurrentUser: true) private let contactProfiles: [Profile] = Profile.fetchAllContactProfiles(excludeCurrentUser: true)
private var searchResults: [Profile] {
return searchText.isEmpty ? contactProfiles : contactProfiles.filter { $0.displayName().contains(searchText) }
}
private var selectedContacts: Set<String> = [] private var selectedContacts: Set<String> = []
private var searchText: String = ""
// MARK: - Components // MARK: - Components
private lazy var nameTextField: TextField = { private lazy var nameTextField: TextField = {
let result = TextField( let result = TextField(
placeholder: "vc_create_closed_group_text_field_hint".localized(), placeholder: "vc_create_closed_group_text_field_hint".localized(),
customHeight: 48 usesDefaultHeight: false,
customHeight: 50
) )
result.set(.height, to: 50)
result.layer.borderColor = Colors.border.withAlphaComponent(0.5).cgColor result.layer.borderColor = Colors.border.withAlphaComponent(0.5).cgColor
result.layer.cornerRadius = 13 result.layer.cornerRadius = 13
return result return result
@ -62,6 +68,7 @@ final class NewClosedGroupVC: BaseVC, UITableViewDataSource, UITableViewDelegate
result.setTitle(NSLocalizedString("CREATE_GROUP_BUTTON_TITLE", comment: ""), for: .normal) result.setTitle(NSLocalizedString("CREATE_GROUP_BUTTON_TITLE", comment: ""), for: .normal)
result.addTarget(self, action: #selector(createClosedGroup), for: .touchUpInside) result.addTarget(self, action: #selector(createClosedGroup), for: .touchUpInside)
result.set(.width, to: 160) result.set(.width, to: 160)
result.backgroundColor = Colors.navigationBarBackground
return result return result
}() }()
@ -144,22 +151,22 @@ final class NewClosedGroupVC: BaseVC, UITableViewDataSource, UITableViewDelegate
view.addSubview(createGroupButton) view.addSubview(createGroupButton)
createGroupButton.center(.horizontal, in: view) createGroupButton.center(.horizontal, in: view)
createGroupButton.pin(.bottom, to: .bottom, of: view, withInset: -Values.largeSpacing) createGroupButton.pin(.bottom, to: .bottom, of: view, withInset: -Values.veryLargeSpacing)
} }
// MARK: - Table View Data Source // MARK: - Table View Data Source
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contactProfiles.count return searchResults.count
} }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UserCell = tableView.dequeue(type: UserCell.self, for: indexPath) let cell: UserCell = tableView.dequeue(type: UserCell.self, for: indexPath)
cell.update( cell.update(
with: contactProfiles[indexPath.row].id, with: searchResults[indexPath.row].id,
profile: contactProfiles[indexPath.row], profile: searchResults[indexPath.row],
isZombie: false, isZombie: false,
accessory: .o(isSelected: selectedContacts.contains(contactProfiles[indexPath.row].id)) accessory: .o(isSelected: selectedContacts.contains(searchResults[indexPath.row].id))
) )
return cell return cell
@ -187,11 +194,11 @@ final class NewClosedGroupVC: BaseVC, UITableViewDataSource, UITableViewDelegate
} }
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if !selectedContacts.contains(contactProfiles[indexPath.row].id) { if !selectedContacts.contains(searchResults[indexPath.row].id) {
selectedContacts.insert(contactProfiles[indexPath.row].id) selectedContacts.insert(searchResults[indexPath.row].id)
} }
else { else {
selectedContacts.remove(contactProfiles[indexPath.row].id) selectedContacts.remove(searchResults[indexPath.row].id)
} }
tableView.deselectRow(at: indexPath, animated: true) tableView.deselectRow(at: indexPath, animated: true)
@ -257,7 +264,8 @@ final class NewClosedGroupVC: BaseVC, UITableViewDataSource, UITableViewDelegate
extension NewClosedGroupVC: UISearchBarDelegate { extension NewClosedGroupVC: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// searchText = searchText self.searchText = searchText
self.tableView.reloadData()
} }
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {

@ -56,7 +56,7 @@ public extension UISearchBar {
let searchTextField: UITextField = self.searchTextField let searchTextField: UITextField = self.searchTextField
searchTextField.borderStyle = .none searchTextField.borderStyle = .none
searchTextField.layer.cornerRadius = 18 searchTextField.layer.cornerRadius = 18
searchTextField.backgroundColor = .white // The search bar background color searchTextField.backgroundColor = isLightMode ? .white : .black // The search bar background color
searchTextField.textColor = Colors.text searchTextField.textColor = Colors.text
searchTextField.attributedPlaceholder = NSAttributedString(string: "Search Contacts", attributes: [ .foregroundColor : Colors.searchBarPlaceholder ]) searchTextField.attributedPlaceholder = NSAttributedString(string: "Search Contacts", attributes: [ .foregroundColor : Colors.searchBarPlaceholder ])
setPositionAdjustment(UIOffset(horizontal: 4, vertical: 0), for: UISearchBar.Icon.search) setPositionAdjustment(UIOffset(horizontal: 4, vertical: 0), for: UISearchBar.Icon.search)

Loading…
Cancel
Save