Browse Source

fix app crash if saved messages chat gets deleted

cyberta 4 years ago
parent
commit
ac98fadd07

+ 3 - 2
deltachat-ios/Controller/ChatListController.swift

@@ -458,8 +458,9 @@ class ChatListController: UITableViewController {
             return
             return
         }
         }
 
 
-        let row = viewModel.deleteChat(chatId: chatId)
-        tableView.deleteRows(at: [IndexPath(row: row, section: 0)], with: .fade)
+        if let row = viewModel.deleteChat(chatId: chatId) {
+            tableView.deleteRows(at: [IndexPath(row: row, section: 0)], with: .fade)
+        }
     }
     }
 
 
     // MARK: - coordinator
     // MARK: - coordinator

+ 8 - 3
deltachat-ios/ViewModel/ChatListViewModel.swift

@@ -23,7 +23,7 @@ protocol ChatListViewModelProtocol: class, UISearchResultsUpdating {
     var emptySearchText: String? { get }
     var emptySearchText: String? { get }
 
 
     /// returns ROW of table
     /// returns ROW of table
-    func deleteChat(chatId: Int) -> Int
+    func deleteChat(chatId: Int) -> Int?
     func archiveChatToggle(chatId: Int)
     func archiveChatToggle(chatId: Int)
     func pinChatToggle(chatId: Int)
     func pinChatToggle(chatId: Int)
     func refreshData()
     func refreshData()
@@ -201,13 +201,18 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
         return nil
         return nil
     }
     }
 
 
-    func deleteChat(chatId: Int) -> Int {
+    func deleteChat(chatId: Int) -> Int? {
         // find index of chatId
         // find index of chatId
         let indexToDelete = Array(0..<chatList.length).filter { chatList.getChatId(index: $0) == chatId }.first
         let indexToDelete = Array(0..<chatList.length).filter { chatList.getChatId(index: $0) == chatId }.first
+        let chat = dcContext.getChat(chatId: chatId)
         dcContext.deleteChat(chatId: chatId)
         dcContext.deleteChat(chatId: chatId)
         updateChatList(notifyListener: false)
         updateChatList(notifyListener: false)
         safe_assert(indexToDelete != nil)
         safe_assert(indexToDelete != nil)
-        return indexToDelete ?? -1
+        // Do not return the index of the tableview cell for the selfTalk chat:
+        // Immediately after deleting the chat, a message to the device talk chat gets added.
+        // If the device talk chat was not existing it would be created and added to the table data source.
+        // That case invalidates the index.
+        return chat.isSelfTalk ? nil : indexToDelete
     }
     }
 
 
     func archiveChatToggle(chatId: Int) {
     func archiveChatToggle(chatId: Int) {