Эх сурвалжийг харах

Merge pull request #555 from deltachat/less-clicks

less clicks on save-message and start-chat
cyBerta 5 жил өмнө
parent
commit
ee6e6357b2

+ 12 - 11
deltachat-ios/Controller/ChatListController.swift

@@ -41,6 +41,11 @@ class ChatListController: UIViewController {
     override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)
         getChatList()
+
+        if RelayHelper.sharedInstance.isForwarding() {
+            chatTable.scrollToTop()
+        }
+
         updateTitle()
 
         let nc = NotificationCenter.default
@@ -109,19 +114,18 @@ class ChatListController: UIViewController {
     }
 
     @objc func cancelButtonPressed() {
+        // cancel forwarding
         RelayHelper.sharedInstance.cancel()
+        getChatList()
         updateTitle()
     }
 
-    private func getNumberOfArchivedChats() -> Int {
-        let chatList = dcContext.getChatlist(flags: DC_GCL_ARCHIVED_ONLY, queryString: nil, queryId: 0)
-        return chatList.length
-    }
-
     private func getChatList() {
         var gclFlags: Int32 = 0
         if showArchive {
             gclFlags |= DC_GCL_ARCHIVED_ONLY
+        } else if RelayHelper.sharedInstance.isForwarding() {
+            gclFlags |= DC_GCL_FOR_FORWARDING
         }
         chatList = dcContext.getChatlist(flags: gclFlags, queryString: nil, queryId: 0)
         chatTable.reloadData()
@@ -157,8 +161,9 @@ extension ChatListController: UITableViewDataSource, UITableViewDelegate {
         }
 
         let chatId = chatList.getChatId(index: row)
+        let chat = DcChat(id: chatId)
         if chatId == DC_CHAT_ID_ARCHIVED_LINK {
-            return getArchiveCell(tableView)
+            return getArchiveCell(tableView, title: chat.name)
         }
 
         let cell: ContactCell
@@ -170,7 +175,6 @@ extension ChatListController: UITableViewDataSource, UITableViewDelegate {
             cell = ContactCell(style: .default, reuseIdentifier: "ChatCell")
         }
 
-        let chat = DcChat(id: chatId)
         let summary = chatList.getSummary(index: row)
         let unreadMessages = dcContext.getUnreadMessages(chatId: chatId)
 
@@ -294,7 +298,7 @@ extension ChatListController: UITableViewDataSource, UITableViewDelegate {
         return deaddropCell
     }
 
-    func getArchiveCell(_ tableView: UITableView) -> UITableViewCell {
+    func getArchiveCell(_ tableView: UITableView, title: String) -> UITableViewCell {
         let archiveCell: UITableViewCell
         if let cell = tableView.dequeueReusableCell(withIdentifier: "ArchiveCell") {
             archiveCell = cell
@@ -302,9 +306,6 @@ extension ChatListController: UITableViewDataSource, UITableViewDelegate {
             archiveCell = UITableViewCell(style: .default, reuseIdentifier: "ArchiveCell")
         }
         archiveCell.textLabel?.textAlignment = .center
-        var title = String.localized("chat_archived_chats_title")
-        let count = getNumberOfArchivedChats()
-        title.append(" (\(count))")
         archiveCell.textLabel?.text = title
         archiveCell.textLabel?.textColor = .systemBlue
         return archiveCell

+ 18 - 8
deltachat-ios/Controller/ChatViewController.swift

@@ -603,13 +603,19 @@ class ChatViewController: MessagesViewController {
     }
 
     private func askToChatWith(email: String) {
-        confirmationAlert(title: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), email),
-                          actionTitle: String.localized("start_chat"),
-                          actionHandler: { _ in
-                            self.dismiss(animated: true, completion: nil)
-                            let contactId = self.dcContext.createContact(name: "", email: email)
-                            let chatId = self.dcContext.createChat(contactId: contactId)
-                            self.coordinator?.showChat(chatId: chatId)})
+        let contactId = self.dcContext.createContact(name: "", email: email)
+        if dcContext.getChatIdByContactId(contactId: contactId) != 0 {
+            self.dismiss(animated: true, completion: nil)
+            let chatId = self.dcContext.createChatByContactId(contactId: contactId)
+            self.coordinator?.showChat(chatId: chatId)
+        } else {
+            confirmationAlert(title: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), email),
+                              actionTitle: String.localized("start_chat"),
+                              actionHandler: { _ in
+                                self.dismiss(animated: true, completion: nil)
+                                let chatId = self.dcContext.createChatByContactId(contactId: contactId)
+                                self.coordinator?.showChat(chatId: chatId)})
+        }
     }
 
     private func askToDeleteMessage(id: Int) {
@@ -621,7 +627,10 @@ class ChatViewController: MessagesViewController {
 
     private func askToForwardMessage() {
         let chat = DcChat(id: self.chatId)
-        confirmationAlert(title: String.localizedStringWithFormat(String.localized("ask_forward"), chat.name),
+        if chat.isSelfTalk {
+            RelayHelper.sharedInstance.forward(to: self.chatId)
+        } else {
+            confirmationAlert(title: String.localizedStringWithFormat(String.localized("ask_forward"), chat.name),
                           actionTitle: String.localized("menu_forward"),
                           actionHandler: { _ in
                             RelayHelper.sharedInstance.forward(to: self.chatId)
@@ -629,6 +638,7 @@ class ChatViewController: MessagesViewController {
                           cancelHandler: { _ in
                             self.dismiss(animated: false, completion: nil)
                             self.coordinator?.navigateBack()})
+        }
     }
 }
 

+ 4 - 16
deltachat-ios/Controller/ContactDetailViewController.swift

@@ -147,7 +147,7 @@ class ContactDetailViewController: UITableViewController {
             handleCellAction(for: indexPath.row)
         case .startChat:
             let contactId = viewModel.contactId
-            askToChatWith(contactId: contactId)
+            chatWith(contactId: contactId)
         case .sharedChats:
             let chatId = viewModel.getSharedChatIdAt(indexPath: indexPath)
             coordinator?.showChat(chatId: chatId)
@@ -271,21 +271,9 @@ extension ContactDetailViewController {
         }
     }
 
-    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: .safeActionSheet)
-        alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
-            self.dismiss(animated: true, completion: nil)
-            let chatId = Int(dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId)))
-            self.coordinator?.showChat(chatId: chatId)
-        }))
-        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 chatWith(contactId: Int) {
+        let chatId = self.viewModel.context.createChatByContactId(contactId: contactId)
+        self.coordinator?.showChat(chatId: chatId)
     }
 
 }

+ 2 - 2
deltachat-ios/Controller/EditContactController.swift

@@ -5,8 +5,8 @@ class EditContactController: NewContactController {
     // for editing existing contacts (only
     // the name may be edited, therefore disable
     // the email field)
-    init(contactIdForUpdate: Int) {
-        super.init()
+    init(dcContext: DcContext, contactIdForUpdate: Int) {
+        super.init(dcContext: dcContext)
         title = String.localized("edit_contact")
 
         let contact = DcContact(id: contactIdForUpdate)

+ 15 - 10
deltachat-ios/Controller/NewChatViewController.swift

@@ -291,18 +291,23 @@ class NewChatViewController: UITableViewController {
     }
 
     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: .safeActionSheet)
-        alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
+        if dcContext.getChatIdByContactId(contactId: contactId) != 0 {
             self.dismiss(animated: true, completion: nil)
             self.coordinator?.showNewChat(contactId: contactId)
-        }))
-        alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: { _ in
-            self.reactivateSearchBarIfNeeded()
-        }))
-        present(alert, animated: true, completion: nil)
+        } else {
+            let dcContact = DcContact(id: contactId)
+            let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), dcContact.nameNAddr),
+                                          message: nil,
+                                          preferredStyle: .safeActionSheet)
+            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.reactivateSearchBarIfNeeded()
+            }))
+            present(alert, animated: true, completion: nil)
+        }
     }
 
     private func reactivateSearchBarIfNeeded() {

+ 5 - 3
deltachat-ios/Controller/NewContactController.swift

@@ -2,6 +2,7 @@ import UIKit
 
 class NewContactController: UITableViewController {
 
+    private let dcContext: DcContext
     weak var coordinator: EditContactCoordinatorProtocol?
     var openChatOnSave = true
 
@@ -27,7 +28,8 @@ class NewContactController: UITableViewController {
     let cells: [UITableViewCell]
 
     // for creating a new contact
-    init() {
+    init(dcContext: DcContext) {
+        self.dcContext = dcContext
         cells = [emailCell, nameCell]
         super.init(style: .grouped)
         emailCell.textField.delegate = self
@@ -74,8 +76,8 @@ class NewContactController: UITableViewController {
     }
 
     @objc func saveContactButtonPressed() {
-        let contactId = dc_create_contact(mailboxPointer, model.name, model.email)
-        let chatId = Int(dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId)))
+        let contactId = dcContext.createContact(name: model.name, email: model.email)
+        let chatId = dcContext.createChatByContactId(contactId: contactId)
         if openChatOnSave {
             coordinator?.showChat(chatId: chatId)
         } else {

+ 2 - 2
deltachat-ios/Controller/QrViewController.swift

@@ -169,7 +169,7 @@ class QrViewController: UITableViewController, QrCodeReaderDelegate {
             let msg = String.localizedStringWithFormat(String.localized(state==DC_QR_ADDR ? "ask_start_chat_with" : "qrshow_x_verified"), nameAndAddress)
             let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
             alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
-                let chatId = self.dcContext.createChat(contactId: qrParsed.id)
+                let chatId = self.dcContext.createChatByContactId(contactId: qrParsed.id)
                 self.coordinator?.showChat(chatId: chatId)
             }))
             alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
@@ -311,7 +311,7 @@ class QrViewController: UITableViewController, QrCodeReaderDelegate {
     }
 
     func displayNewChat(contactId: Int) {
-        let chatId = dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId))
+        let chatId = dcContext.createChatByContactId(contactId: contactId)
         let chatVC = ChatViewController(dcContext: dcContext, chatId: Int(chatId))
 
         chatVC.hidesBottomBarWhenPushed = true

+ 5 - 5
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -318,7 +318,7 @@ class NewChatCoordinator: Coordinator {
     }
 
     func showNewContactController() {
-        let newContactController = NewContactController()
+        let newContactController = NewContactController(dcContext: dcContext)
         let coordinator = EditContactCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
         newContactController.coordinator = coordinator
@@ -326,7 +326,7 @@ class NewChatCoordinator: Coordinator {
     }
 
     func showNewChat(contactId: Int) {
-        let chatId = dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId))
+        let chatId = dcContext.createChatByContactId(contactId: contactId)
         showChat(chatId: Int(chatId))
     }
 
@@ -366,7 +366,7 @@ class GroupChatDetailCoordinator: Coordinator {
     }
 
     func showSingleChatEdit(contactId: Int) {
-        let editContactController = EditContactController(contactIdForUpdate: contactId)
+        let editContactController = EditContactController(dcContext: dcContext, contactIdForUpdate: contactId)
         let coordinator = EditContactCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
         editContactController.coordinator = coordinator
@@ -564,7 +564,7 @@ class AddGroupMembersCoordinator: Coordinator {
     }
 
     func showNewContactController() {
-        let newContactController = NewContactController()
+        let newContactController = NewContactController(dcContext: dcContext)
         newContactController.openChatOnSave = false
         let coordinator = EditContactCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
@@ -658,7 +658,7 @@ class ContactDetailCoordinator: Coordinator, ContactDetailCoordinatorProtocol {
     }
 
     func showEditContact(contactId: Int) {
-        let editContactController = EditContactController(contactIdForUpdate: contactId)
+        let editContactController = EditContactController(dcContext: dcContext, contactIdForUpdate: contactId)
         let coordinator = EditContactCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
         editContactController.coordinator = coordinator

+ 5 - 1
deltachat-ios/DC/Wrapper.swift

@@ -55,10 +55,14 @@ class DcContext {
     }
 
     @discardableResult
-    func createChat(contactId: Int) -> Int {
+    func createChatByContactId(contactId: Int) -> Int {
         return Int(dc_create_chat_by_contact_id(contextPointer, UInt32(contactId)))
     }
 
+    func getChatIdByContactId(contactId: Int) -> Int {
+        return Int(dc_get_chat_id_by_contact_id(contextPointer, UInt32(contactId)))
+    }
+
     func createGroupChat(verified: Bool, name: String) -> Int {
         return Int(dc_create_group_chat(contextPointer, verified ? 1 : 0, name))
     }

+ 13 - 0
deltachat-ios/Extensions/Extensions.swift

@@ -71,3 +71,16 @@ extension UIAlertController.Style {
         }
     }
 }
+
+extension UITableView {
+    func scrollToTop() {
+        let numberOfSections = self.numberOfSections
+        if numberOfSections > 0 {
+            let numberOfRows = self.numberOfRows(inSection: 0)
+            if numberOfRows > 0 {
+                let indexPath = IndexPath(row: 0, section: 0)
+                self.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.top, animated: false)
+            }
+        }
+    }
+}

+ 1 - 0
deltachat-ios/ViewModel/ContactDetailViewModel.swift

@@ -1,6 +1,7 @@
 import UIKit
 
 protocol ContactDetailViewModelProtocol {
+    var context: DcContext { get }
     var contactId: Int { get }
     var contact: DcContact { get }
     var numberOfSections: Int { get }