123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- import ALCameraViewController
- import Contacts
- import UIKit
- class NewChatViewController: UITableViewController {
- weak var coordinator: NewChatCoordinator?
- private let sectionNew = 0
- private let sectionNewRowNewGroup = 0
- private let sectionNewRowNewContact = 1
- private let sectionNewRowCount = 2
- private let sectionImportedContacts = 1
- private var sectionContacts: Int { return deviceContactAccessGranted ? 1 : 2 }
- private var sectionsCount: Int { return deviceContactAccessGranted ? 2 : 3 }
- private lazy var searchController: UISearchController = {
- let searchController = UISearchController(searchResultsController: nil)
- searchController.searchResultsUpdater = self
- searchController.obscuresBackgroundDuringPresentation = false
- searchController.searchBar.placeholder = String.localized("search")
- return searchController
- }()
- var contactIds: [Int] = Utils.getContactIds() {
- didSet {
- tableView.reloadData()
- }
- }
- // contactWithSearchResults.indexesToHightLight empty by default
- var contacts: [ContactWithSearchResults] {
- return contactIds.map { ContactWithSearchResults(contact: DcContact(id: $0), indexesToHighlight: []) }
- }
- // used when seachbar is active
- var filteredContacts: [ContactWithSearchResults] = []
- // searchBar active?
- func isFiltering() -> Bool {
- return searchController.isActive && !searchBarIsEmpty()
- }
- // weak var chatDisplayer: ChatDisplayer?
- var syncObserver: Any?
- var hud: ProgressHud?
- lazy var deviceContactHandler: DeviceContactsHandler = {
- let handler = DeviceContactsHandler()
- handler.contactListDelegate = self
- return handler
- }()
- var deviceContactAccessGranted: Bool = false {
- didSet {
- tableView.reloadData()
- }
- }
- init() {
- super.init(style: .grouped)
- hidesBottomBarWhenPushed = true
- }
- required init?(coder _: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- title = String.localized("menu_new_chat")
- deviceContactHandler.importDeviceContacts()
- navigationItem.searchController = searchController
- definesPresentationContext = true // to make sure searchbar will only be shown in this viewController
- if #available(iOS 11.0, *) {
- navigationItem.hidesSearchBarWhenScrolling = false
- }
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- deviceContactAccessGranted = CNContactStore.authorizationStatus(for: .contacts) == .authorized
- contactIds = Utils.getContactIds()
- }
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- let nc = NotificationCenter.default
- syncObserver = nc.addObserver(
- forName: dcNotificationSecureJoinerProgress,
- object: nil,
- queue: nil
- ) { notification in
- if let ui = notification.userInfo {
- if ui["error"] as? Bool ?? false {
- self.hud?.error(ui["errorMessage"] as? String)
- } else if ui["done"] as? Bool ?? false {
- self.hud?.done()
- } else {
- self.hud?.progress(ui["progress"] as? Int ?? 0)
- }
- }
- }
- }
- override func viewDidDisappear(_ animated: Bool) {
- super.viewDidDisappear(animated)
- let nc = NotificationCenter.default
- if let syncObserver = self.syncObserver {
- nc.removeObserver(syncObserver)
- }
- }
- @objc func cancelButtonPressed() {
- dismiss(animated: true, completion: nil)
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- // MARK: - Table view data source
- override func numberOfSections(in _: UITableView) -> Int {
- return sectionsCount
- }
- override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
- if section == sectionNew {
- return sectionNewRowCount
- } else if section == sectionImportedContacts {
- if deviceContactAccessGranted {
- return isFiltering() ? filteredContacts.count : contacts.count
- } else {
- return 1
- }
- } else {
- return isFiltering() ? filteredContacts.count : contacts.count
- }
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let section = indexPath.section
- let row = indexPath.row
- if section == sectionNew {
- if row == sectionNewRowNewGroup {
- // new group row
- let cell: UITableViewCell
- if let c = tableView.dequeueReusableCell(withIdentifier: "newContactCell") {
- cell = c
- } else {
- cell = UITableViewCell(style: .default, reuseIdentifier: "newContactCell")
- }
- cell.textLabel?.text = String.localized("menu_new_group")
- cell.textLabel?.textColor = view.tintColor
- return cell
- }
- if row == sectionNewRowNewContact {
- // new contact row
- let cell: UITableViewCell
- if let c = tableView.dequeueReusableCell(withIdentifier: "newContactCell") {
- cell = c
- } else {
- cell = UITableViewCell(style: .default, reuseIdentifier: "newContactCell")
- }
- cell.textLabel?.text = String.localized("menu_new_contact")
- cell.textLabel?.textColor = view.tintColor
- return cell
- }
- } else if section == sectionImportedContacts {
- // import device contacts section
- if deviceContactAccessGranted {
- let cell: ContactCell
- if let c = tableView.dequeueReusableCell(withIdentifier: "contactCell") as? ContactCell {
- cell = c
- } else {
- cell = ContactCell(style: .default, reuseIdentifier: "contactCell")
- }
- let contact: ContactWithSearchResults = contactSearchResultByRow(row)
- updateContactCell(cell: cell, contactWithHighlight: contact)
- return cell
- } else {
- let cell: ActionCell
- if let c = tableView.dequeueReusableCell(withIdentifier: "actionCell") as? ActionCell {
- cell = c
- } else {
- cell = ActionCell(style: .default, reuseIdentifier: "actionCell")
- }
- cell.actionTitle = String.localized("import_contacts")
- return cell
- }
- } else {
- // section contact list if device contacts are not imported
- let cell: ContactCell
- if let c = tableView.dequeueReusableCell(withIdentifier: "contactCell") as? ContactCell {
- cell = c
- } else {
- cell = ContactCell(style: .default, reuseIdentifier: "contactCell")
- }
- let contact: ContactWithSearchResults = contactSearchResultByRow(row)
- updateContactCell(cell: cell, contactWithHighlight: contact)
- return cell
- }
- // will actually never get here but compiler not happy
- return UITableViewCell(style: .default, reuseIdentifier: "cell")
- }
- override func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
- let row = indexPath.row
- let section = indexPath.section
- if section == sectionNew {
- if row == sectionNewRowNewGroup {
- coordinator?.showNewGroupController()
- }
- if row == sectionNewRowNewContact {
- coordinator?.showNewContactController()
- }
- } else if section == sectionImportedContacts {
- if deviceContactAccessGranted {
- showChatAt(row: row)
- } else {
- showSettingsAlert()
- }
- } else {
- showChatAt(row: row)
- }
- }
- override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
- if indexPath.section == sectionContacts {
- let contactId = contactIdByRow(indexPath.row)
- let edit = UITableViewRowAction(style: .normal, title: String.localized("info")) { [unowned self] _, _ in
- if self.searchController.isActive {
- self.searchController.dismiss(animated: false) {
- self.coordinator?.showContactDetail(contactId: contactId)
- }
- } else {
- self.coordinator?.showContactDetail(contactId: contactId)
- }
- }
- let delete = UITableViewRowAction(style: .destructive, title: String.localized("delete")) { [unowned self] _, _ in
- //handle delete
- if let dcContext = self.coordinator?.dcContext {
- let contactId = self.contactIdByRow(indexPath.row)
- self.askToDeleteContact(contactId: contactId, context: dcContext)
- }
- }
- edit.backgroundColor = DcColors.primary
- return [edit, delete]
- } else {
- return []
- }
- }
- override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
- return true
- }
- private func contactIdByRow(_ row: Int) -> Int {
- return isFiltering() ? filteredContacts[row].contact.id : contactIds[row]
- }
- private func contactSearchResultByRow(_ row: Int) -> ContactWithSearchResults {
- return isFiltering() ? filteredContacts[row] : contacts[row]
- }
- private func askToDeleteContact(contactId: Int, context: DcContext) {
- let contact = DcContact(id: contactId)
- let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("ask_delete_contact"), contact.nameNAddr),
- message: nil,
- preferredStyle: .actionSheet)
- alert.addAction(UIAlertAction(title: String.localized("delete"), style: .destructive, handler: { _ in
- self.dismiss(animated: true, completion: nil)
- if context.deleteContact(contactId: contactId) {
- self.contactIds = Utils.getContactIds()
- self.tableView.reloadData()
- }
- }))
- alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: { _ in
- self.dismiss(animated: true, completion: nil)
- }))
- present(alert, animated: true, completion: nil)
- }
- private func askToChatWith(contactId: Int) {
- let dcContact = DcContact(id: contactId)
- let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), dcContact.nameNAddr),
- message: nil,
- preferredStyle: .actionSheet)
- alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
- self.dismiss(animated: true, completion: nil)
- self.coordinator?.showNewChat(contactId: contactId)
- }))
- alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: { _ in
- self.dismiss(animated: true, completion: nil)
- }))
- present(alert, animated: true, completion: nil)
- }
- private func showChatAt(row: Int) {
- if searchController.isActive {
- // edge case: when searchController is active but searchBar is empty -> filteredContacts is empty, so we fallback to contactIds
- let contactId = contactIdByRow(row)
- searchController.dismiss(animated: false, completion: {
- self.askToChatWith(contactId: contactId)
- })
- } else {
- let contactId = contactIds[row]
- self.askToChatWith(contactId: contactId)
- }
- }
- private func updateContactCell(cell: ContactCell, contactWithHighlight: ContactWithSearchResults) {
- let contact = contactWithHighlight.contact
- let displayName = contact.displayName
- let emailLabelFontSize = cell.emailLabel.font.pointSize
- let nameLabelFontSize = cell.nameLabel.font.pointSize
- cell.initialsLabel.text = Utils.getInitials(inputName: displayName)
- cell.setColor(contact.color)
- cell.setVerified(isVerified: contact.isVerified)
- if let emailHighlightedIndexes = contactWithHighlight.indexesToHighlight.filter({ $0.contactDetail == .EMAIL }).first {
- // gets here when contact is a result of current search -> highlights relevant indexes
- cell.emailLabel.attributedText = contact.email.boldAt(indexes: emailHighlightedIndexes.indexes, fontSize: emailLabelFontSize)
- } else {
- cell.emailLabel.attributedText = contact.email.boldAt(indexes: [], fontSize: emailLabelFontSize)
- }
- if let nameHighlightedIndexes = contactWithHighlight.indexesToHighlight.filter({ $0.contactDetail == .NAME }).first {
- cell.nameLabel.attributedText = displayName.boldAt(indexes: nameHighlightedIndexes.indexes, fontSize: nameLabelFontSize)
- } else {
- cell.nameLabel.attributedText = displayName.boldAt(indexes: [], fontSize: nameLabelFontSize)
- }
- }
- private func searchBarIsEmpty() -> Bool {
- return searchController.searchBar.text?.isEmpty ?? true
- }
- private func filterContentForSearchText(_ searchText: String, scope _: String = String.localized("pref_show_emails_all")) {
- let contactsWithHighlights: [ContactWithSearchResults] = contacts.map { contact in
- let indexes = contact.contact.containsExact(searchText: searchText)
- return ContactWithSearchResults(contact: contact.contact, indexesToHighlight: indexes)
- }
- filteredContacts = contactsWithHighlights.filter { !$0.indexesToHighlight.isEmpty }
- tableView.reloadData()
- }
- }
- extension NewChatViewController: ContactListDelegate {
- func deviceContactsImported() {
- contactIds = Utils.getContactIds()
- // tableView.reloadData()
- }
- func accessGranted() {
- deviceContactAccessGranted = true
- }
- func accessDenied() {
- deviceContactAccessGranted = false
- }
- private func showSettingsAlert() {
- let alert = UIAlertController(
- title: String.localized("import_contacts"),
- message: String.localized("import_contacts_message"),
- preferredStyle: .alert
- )
- alert.addAction(UIAlertAction(title: String.localized("menu_settings"), style: .default) { _ in
- UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
- })
- alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel) { _ in
- })
- present(alert, animated: true)
- }
- }
- extension NewChatViewController: UISearchResultsUpdating {
- func updateSearchResults(for searchController: UISearchController) {
- if let searchText = searchController.searchBar.text {
- filterContentForSearchText(searchText)
- }
- }
- }
- struct ContactHighlights {
- let contactDetail: ContactDetail
- let indexes: [Int]
- }
- enum ContactDetail {
- case NAME
- case EMAIL
- }
- struct ContactWithSearchResults {
- let contact: DcContact
- let indexesToHighlight: [ContactHighlights]
- }
|