Przeglądaj źródła

Merge branch 'master' into qr_code_group_join_scan

björn petersen 5 lat temu
rodzic
commit
a6039565aa

+ 1 - 0
deltachat-ios/Controller/ChatListController.swift

@@ -150,6 +150,7 @@ extension ChatListController: UITableViewDataSource, UITableViewDelegate {
 
         cell.nameLabel.text = chat.name
         if let img = chat.profileImage {
+            cell.resetBackupImage()
             cell.setImage(img)
         } else {
             cell.setBackupImage(name: chat.name, color: chat.color)

+ 1 - 1
deltachat-ios/Controller/ContactListController.swift

@@ -67,7 +67,7 @@ class ContactListController: UITableViewController {
         return searchController.searchBar.text?.isEmpty ?? true
     }
 
-    private func filterContentForSearchText(_ searchText: String, scope _: String = "All") {
+    private func filterContentForSearchText(_ searchText: String, scope _: String = String.localized("pref_show_emails_all")) {
         let contactsWithHighlights: [ContactWithSearchResults] = contacts.map { contact in
             let indexes = contact.contact.contains(searchText: searchText)
             return ContactWithSearchResults(contact: contact.contact, indexesToHighlight: indexes)

+ 66 - 44
deltachat-ios/Controller/NewChatViewController.swift

@@ -5,6 +5,12 @@ import UIKit
 class NewChatViewController: UITableViewController {
     weak var coordinator: NewChatCoordinator?
 
+    private let sectionNew = 0
+    private let sectionImportedContacts = 1
+    private let sectionNewRowNewGroup = 0
+    private let sectionNewRowScanQrCode = 1
+    private let sectionNewRowNewContact = 2
+
     private lazy var searchController: UISearchController = {
         let searchController = UISearchController(searchResultsController: nil)
         searchController.searchResultsUpdater = self
@@ -102,12 +108,6 @@ class NewChatViewController: UITableViewController {
         }
     }
 
-    override func viewWillDisappear(_: Bool) {
-        title = String.localized("pref_chats")
-        /* hack: when navigating to chatView (removing this viewController),
-        there was a delayed backButton update (showing 'New Chat' for a moment) */
-    }
-
     override func viewDidDisappear(_ animated: Bool) {
         super.viewDidDisappear(animated)
 
@@ -133,9 +133,9 @@ class NewChatViewController: UITableViewController {
     }
 
     override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
-        if section == 0 {
+        if section == sectionNew {
             return 3
-        } else if section == 1 {
+        } else if section == sectionImportedContacts {
             if deviceContactAccessGranted {
                 return isFiltering() ? filteredContacts.count : contacts.count
             } else {
@@ -150,8 +150,8 @@ class NewChatViewController: UITableViewController {
         let section = indexPath.section
         let row = indexPath.row
 
-        if section == 0 {
-            if row == 0 {
+        if section == sectionNew {
+            if row == sectionNewRowNewGroup {
                 // new group row
                 let cell: UITableViewCell
                 if let c = tableView.dequeueReusableCell(withIdentifier: "newContactCell") {
@@ -164,8 +164,8 @@ class NewChatViewController: UITableViewController {
 
                 return cell
             }
-            if row == 1 {
-                // new contact row
+            if row == sectionNewRowScanQrCode {
+                // scan QR code row
                 let cell: UITableViewCell
                 if let c = tableView.dequeueReusableCell(withIdentifier: "scanGroupCell") {
                     cell = c
@@ -178,7 +178,7 @@ class NewChatViewController: UITableViewController {
                 return cell
             }
 
-            if row == 2 {
+            if row == sectionNewRowNewContact {
                 // new contact row
                 let cell: UITableViewCell
                 if let c = tableView.dequeueReusableCell(withIdentifier: "newContactCell") {
@@ -191,7 +191,8 @@ class NewChatViewController: UITableViewController {
 
                 return cell
             }
-        } else if section == 1 {
+        } else if section == sectionImportedContacts {
+            // import device contacts section
             if deviceContactAccessGranted {
                 let cell: ContactCell
                 if let c = tableView.dequeueReusableCell(withIdentifier: "contactCell") as? ContactCell {
@@ -213,7 +214,7 @@ class NewChatViewController: UITableViewController {
                 return cell
             }
         } else {
-            // section 2
+            // section contact list if device contacts are not imported
             let cell: ContactCell
             if let c = tableView.dequeueReusableCell(withIdentifier: "contactCell") as? ContactCell {
                 cell = c
@@ -233,11 +234,11 @@ class NewChatViewController: UITableViewController {
         let row = indexPath.row
         let section = indexPath.section
 
-        if section == 0 {
-            if row == 0 {
+        if section == sectionNew {
+            if row == sectionNewRowNewGroup {
                 coordinator?.showNewGroupController()
             }
-            if row == 1 {
+            if row == sectionNewRowScanQrCode {
                 if UIImagePickerController.isSourceTypeAvailable(.camera) {
                     coordinator?.showQRCodeController()
                 } else {
@@ -248,28 +249,45 @@ class NewChatViewController: UITableViewController {
                     present(alert, animated: true, completion: nil)
                 }
             }
-            if row == 2 {
+            if row == sectionNewRowNewContact {
                 coordinator?.showNewContactController()
             }
-        } else if section == 1 {
+        } else if section == sectionImportedContacts {
             if deviceContactAccessGranted {
-                if searchController.isActive {
-                    // edge case: when searchController is active but searchBar is empty -> filteredContacts is empty, so we fallback to contactIds
-                    let contactId = isFiltering() ? filteredContacts[row].contact.id : contactIds[row]
-                    searchController.dismiss(animated: false, completion: {
-                        self.coordinator?.showNewChat(contactId: contactId)
-                    })
-                } else {
-                    let contactId = contactIds[row]
-                    coordinator?.showNewChat(contactId: contactId)
-                }
+                showChatAt(row: row)
             } else {
                 showSettingsAlert()
             }
         } else {
-            let contactIndex = row
-            let contactId = contactIds[contactIndex]
-            coordinator?.showNewChat(contactId: contactId)
+            showChatAt(row: row)
+        }
+    }
+
+    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: .alert)
+        alert.addAction(UIAlertAction(title: String.localized("ok"), 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 = isFiltering() ? filteredContacts[row].contact.id : contactIds[row]
+            searchController.dismiss(animated: false, completion: {
+                self.askToChatWith(contactId: contactId)
+            })
+        } else {
+            let contactId = contactIds[row]
+            self.askToChatWith(contactId: contactId)
         }
     }
 
@@ -277,20 +295,24 @@ class NewChatViewController: UITableViewController {
         let contact = contactWithHighlight.contact
         let displayName = contact.displayName
 
-        if let nameHighlightedIndexes = contactWithHighlight.indexesToHighlight.filter({ $0.contactDetail == .NAME }).first,
-            let emailHighlightedIndexes = contactWithHighlight.indexesToHighlight.filter({ $0.contactDetail == .EMAIL }).first {
+        let emailLabelFontSize = cell.emailLabel.font.pointSize
+        let nameLabelFontSize = cell.nameLabel.font.pointSize
+
+        cell.initialsLabel.text = Utils.getInitials(inputName: displayName)
+        cell.setColor(contact.color)
+
+        if let emailHighlightedIndexes = contactWithHighlight.indexesToHighlight.filter({ $0.contactDetail == .EMAIL }).first {
             // gets here when contact is a result of current search -> highlights relevant indexes
-            let nameLabelFontSize = cell.nameLabel.font.pointSize
-            let emailLabelFontSize = cell.emailLabel.font.pointSize
+            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)
-            cell.emailLabel.attributedText = contact.email.boldAt(indexes: emailHighlightedIndexes.indexes, fontSize: emailLabelFontSize)
         } else {
-            cell.nameLabel.text = displayName
-            cell.emailLabel.text = contact.email
+            cell.nameLabel.attributedText = displayName.boldAt(indexes: [], fontSize: nameLabelFontSize)
         }
-        cell.initialsLabel.text = Utils.getInitials(inputName: displayName)
-        cell.setColor(contact.color)
     }
 
     private func searchBarIsEmpty() -> Bool {
@@ -299,7 +321,7 @@ class NewChatViewController: UITableViewController {
 
     private func filterContentForSearchText(_ searchText: String, scope _: String = String.localized("pref_show_emails_all")) {
         let contactsWithHighlights: [ContactWithSearchResults] = contacts.map { contact in
-            let indexes = contact.contact.contains(searchText: searchText)
+            let indexes = contact.contact.containsExact(searchText: searchText)
             return ContactWithSearchResults(contact: contact.contact, indexesToHighlight: indexes)
         }
 
@@ -329,7 +351,7 @@ extension NewChatViewController: QrCodeReaderDelegate {
             }
         } else {
             let alert = UIAlertController(title: String.localized("invalid_qr_code"), message: code, preferredStyle: .alert)
-            alert.addAction(UIAlertAction(title: String.localized("OK"), style: .cancel, handler: { _ in
+            alert.addAction(UIAlertAction(title: String.localized("OK"), style: .default, handler: { _ in
                 self.dismiss(animated: true, completion: nil)
             }))
             present(alert, animated: true, completion: nil)

+ 1 - 0
deltachat-ios/Controller/NewProfileViewController.swift

@@ -250,6 +250,7 @@ class NewProfileViewController: UIViewController, QrCodeReaderDelegate {
         label.font = UIFont.systemFont(ofSize: 14)
         return label
     }
+
     private func createQRCodeScannerButton() -> UIView {
         let btn = UIButton.init(type: UIButton.ButtonType.system)
         btn.translatesAutoresizingMaskIntoConstraints = false

+ 26 - 0
deltachat-ios/Helper/Extensions.swift

@@ -145,6 +145,32 @@ extension DcContact {
             return []
         }
     }
+
+    func containsExact(searchText text: String) -> [ContactHighlights] {
+        var contactHighlights = [ContactHighlights]()
+
+        let nameString = name + ""
+        let emailString = email + ""
+        if let nameRange = nameString.range(of: text, options: .caseInsensitive) {
+            let index: Int = nameString.distance(from: nameString.startIndex, to: nameRange.lowerBound)
+            var nameIndexes = [Int]()
+            for i in index..<(index + text.count) {
+                nameIndexes.append(i)
+            }
+            contactHighlights.append(ContactHighlights(contactDetail: .NAME, indexes: nameIndexes))
+        }
+
+        if let emailRange = emailString.range(of: text, options: .caseInsensitive) {
+            let index: Int = emailString.distance(from: emailString.startIndex, to: emailRange.lowerBound)
+            var emailIndexes = [Int]()
+            for i in index..<(index + text.count) {
+                emailIndexes.append(i)
+            }
+            contactHighlights.append(ContactHighlights(contactDetail: .EMAIL, indexes: emailIndexes))
+        }
+
+        return contactHighlights
+    }
 }
 
 extension UIImage {

+ 5 - 0
deltachat-ios/View/ContactCell.swift

@@ -172,6 +172,11 @@ class ContactCell: UITableViewCell {
         }
     }
 
+    func resetBackupImage() {
+        initialsLabel.text = ""
+        setColor(UIColor.clear)
+    }
+
     func setBackupImage(name: String, color: UIColor) {
         let text = Utils.getInitials(inputName: name)
 

+ 1 - 1
deltachat-ios/libraries/deltachat-core-rust

@@ -1 +1 @@
-Subproject commit f3e53a05a6f8c4b8451f54841f4e38b71146bd1d
+Subproject commit 275aa853f55b3a1403e5159ec3b27ecbec7878be