Sfoglia il codice sorgente

Merge pull request #172 from deltachat/avoid_force_cast

avoid force casts
björn petersen 6 anni fa
parent
commit
70f81152e1

+ 4 - 1
deltachat-ios/AppDelegate.swift

@@ -227,7 +227,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
     }
     }
 
 
     @objc private func reachabilityChanged(note: Notification) {
     @objc private func reachabilityChanged(note: Notification) {
-        let reachability = note.object as! Reachability
+        guard let reachability = note.object as? Reachability else {
+            logger.info("reachability object missing")
+            return
+        }
 
 
         switch reachability.connection {
         switch reachability.connection {
         case .wifi, .cellular:
         case .wifi, .cellular:

+ 0 - 2
deltachat-ios/Controller/AccountSetupController.swift

@@ -451,7 +451,6 @@ class AccountSetupController: UITableViewController {
             if let ui = notification.userInfo {
             if let ui = notification.userInfo {
                 if ui["error"] as! Bool {
                 if ui["error"] as! Bool {
                     self.updateProgressHud(error: ui["errorMessage"] as? String)
                     self.updateProgressHud(error: ui["errorMessage"] as? String)
-                    // self.hudHandler.setHudError(ui["errorMessage"] as? String)
                 } else if ui["done"] as! Bool {
                 } else if ui["done"] as! Bool {
                     self.updateProgressHudSuccess(callback: self.handleLoginSuccess)
                     self.updateProgressHudSuccess(callback: self.handleLoginSuccess)
                 } else {
                 } else {
@@ -468,7 +467,6 @@ class AccountSetupController: UITableViewController {
             if let ui = notification.userInfo {
             if let ui = notification.userInfo {
                 if ui["error"] as! Bool {
                 if ui["error"] as! Bool {
                     self.updateProgressHud(error: ui["errorMessage"] as? String)
                     self.updateProgressHud(error: ui["errorMessage"] as? String)
-                    // self.hudHandler.setHudError(ui["errorMessage"] as? String)
                 } else if ui["done"] as! Bool {
                 } else if ui["done"] as! Bool {
                     self.updateProgressHudSuccess(callback: self.handleLoginSuccess)
                     self.updateProgressHudSuccess(callback: self.handleLoginSuccess)
                 } else {
                 } else {

+ 5 - 4
deltachat-ios/Controller/ChatViewController.swift

@@ -127,10 +127,11 @@ class ChatViewController: MessagesViewController {
             object: nil, queue: OperationQueue.main
             object: nil, queue: OperationQueue.main
         ) { notification in
         ) { notification in
             if let ui = notification.userInfo {
             if let ui = notification.userInfo {
-                if self.chatId == ui["chat_id"] as! Int {
-                    let id = ui["message_id"] as! Int
-                    if id > 0 {
-                        self.insertMessage(DCMessage(id: id))
+                if self.chatId == ui["chat_id"] as? Int {
+                    if let id = ui["message_id"] as? Int {
+                        if id > 0 {
+                            self.insertMessage(DCMessage(id: id))
+                        }
                     }
                     }
                 }
                 }
             }
             }

+ 17 - 11
deltachat-ios/Controller/GroupChatDetailViewController.swift

@@ -150,22 +150,28 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
             return cell
             return cell
         } else if section == 1 {
         } else if section == 1 {
             if row == 0 {
             if row == 0 {
-                let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath) as! ActionCell
-                cell.actionTitle = String.localized("group_add_members")
+                let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath)
+                if let actionCell = cell as? ActionCell {
+                    actionCell.actionTitle = String.localized("group_add_members")
+                }
                 return cell
                 return cell
             } else {
             } else {
-                let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as! ContactCell
-                let contact = groupMembers[row - staticCellCountMemberSection]
-                cell.nameLabel.text = contact.name
-                cell.emailLabel.text = contact.email
-                cell.initialsLabel.text = Utils.getInitials(inputName: contact.name)
-                cell.setColor(contact.color)
+                let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
+                if let contactCell = cell as? ContactCell {
+                    let contact = groupMembers[row - staticCellCountMemberSection]
+                    contactCell.nameLabel.text = contact.name
+                    contactCell.emailLabel.text = contact.email
+                    contactCell.initialsLabel.text = Utils.getInitials(inputName: contact.name)
+                    contactCell.setColor(contact.color)
+                }
                 return cell
                 return cell
             }
             }
         } else if section == 2 {
         } else if section == 2 {
-            let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath) as! ActionCell
-            cell.actionTitle = String.localized("menu_leave_group")
-            cell.actionColor = UIColor.red
+            let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath)
+            if let actionCell = cell as? ActionCell {
+                actionCell.actionTitle = String.localized("menu_leave_group")
+                actionCell.actionColor = UIColor.red
+            }
             return cell
             return cell
         }
         }
 
 

+ 12 - 12
deltachat-ios/Controller/GroupNameController.swift

@@ -60,20 +60,20 @@ class GroupNameController: UITableViewController {
         let row = indexPath.row
         let row = indexPath.row
 
 
         if section == 0 {
         if section == 0 {
-            let cell = tableView.dequeueReusableCell(withIdentifier: "groupLabelCell", for: indexPath) as! GroupLabelCell
-            cell.onTextChanged = updateGroupName
-
+            let cell = tableView.dequeueReusableCell(withIdentifier: "groupLabelCell", for: indexPath)
+            if let groupLabelCell = cell as? GroupLabelCell {
+                groupLabelCell.onTextChanged = updateGroupName
+            }
             return cell
             return cell
-
         } else {
         } else {
-            let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath) as! ContactCell
-
-            let contact = DCContact(id: groupContactIds[row])
-            cell.nameLabel.text = contact.name
-            cell.emailLabel.text = contact.email
-            cell.initialsLabel.text = Utils.getInitials(inputName: contact.name)
-            cell.setColor(contact.color)
-
+            let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
+            if let contactCell = cell as? ContactCell {
+                let contact = DCContact(id: groupContactIds[row])
+                contactCell.nameLabel.text = contact.name
+                contactCell.emailLabel.text = contact.email
+                contactCell.initialsLabel.text = Utils.getInitials(inputName: contact.name)
+                contactCell.setColor(contact.color)
+            }
             return cell
             return cell
         }
         }
     }
     }

+ 3 - 3
deltachat-ios/Controller/NewChatViewController.swift

@@ -92,12 +92,12 @@ class NewChatViewController: UITableViewController {
         ) {
         ) {
             notification in
             notification in
             if let ui = notification.userInfo {
             if let ui = notification.userInfo {
-                if ui["error"] as! Bool {
+                if ui["error"] as? Bool ?? false {
                     self.hud?.error(ui["errorMessage"] as? String)
                     self.hud?.error(ui["errorMessage"] as? String)
-                } else if ui["done"] as! Bool {
+                } else if ui["done"] as? Bool ?? false {
                     self.hud?.done()
                     self.hud?.done()
                 } else {
                 } else {
-                    self.hud?.progress(ui["progress"] as! Int)
+                    self.hud?.progress(ui["progress"] as? Int ?? 0)
                 }
                 }
             }
             }
         }
         }

+ 6 - 5
deltachat-ios/Controller/NewProfileViewController.swift

@@ -171,11 +171,12 @@ class NewProfileViewController: UIViewController, QrCodeReaderDelegate {
         ) { notification in
         ) { notification in
             print("secure join: ", notification)
             print("secure join: ", notification)
             if let ui = notification.userInfo {
             if let ui = notification.userInfo {
-                if ui["progress"] as! Int == 400 {
-                    let contactId = ui["contact_id"] as! Int
-                    self.progressAlert.message = String.localizedStringWithFormat(
-                        String.localized("qrscan_x_verified_introduce_myself"),
-                        DCContact(id: contactId).nameNAddr)
+                if ui["progress"] as? Int == 400 {
+                    if let contactId = ui["contact_id"] as? Int {
+                        self.progressAlert.message = String.localizedStringWithFormat(
+                            String.localized("qrscan_x_verified_introduce_myself"),
+                            DCContact(id: contactId).nameNAddr)
+                    }
                 }
                 }
             }
             }
         }
         }

+ 5 - 5
deltachat-ios/Controller/QrCodeReaderController.swift

@@ -80,11 +80,11 @@ class QrCodeReaderController: UIViewController {
 extension QrCodeReaderController: AVCaptureMetadataOutputObjectsDelegate {
 extension QrCodeReaderController: AVCaptureMetadataOutputObjectsDelegate {
     func metadataOutput(_: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from _: AVCaptureConnection) {
     func metadataOutput(_: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from _: AVCaptureConnection) {
 
 
-        let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
-
-        if supportedCodeTypes.contains(metadataObj.type) {
-            if metadataObj.stringValue != nil {
-                self.delegate?.handleQrCode(metadataObj.stringValue!)
+        if let metadataObj = metadataObjects[0] as? AVMetadataMachineReadableCodeObject {
+            if supportedCodeTypes.contains(metadataObj.type) {
+                if metadataObj.stringValue != nil {
+                    self.delegate?.handleQrCode(metadataObj.stringValue!)
+                }
             }
             }
         }
         }
     }
     }

+ 6 - 6
deltachat-ios/Controller/SettingsController.swift

@@ -39,12 +39,12 @@ internal final class SettingsViewController: QuickTableViewController {
             queue: nil
             queue: nil
         ) { notification in
         ) { notification in
             if let ui = notification.userInfo {
             if let ui = notification.userInfo {
-                if ui["error"] as! Bool {
+                if ui["error"] as? Bool ?? false {
                     self.hudHandler.setHudError(ui["errorMessage"] as? String)
                     self.hudHandler.setHudError(ui["errorMessage"] as? String)
-                } else if ui["done"] as! Bool {
+                } else if ui["done"] as? Bool ?? false {
                     self.hudHandler.setHudDone(callback: nil)
                     self.hudHandler.setHudDone(callback: nil)
                 } else {
                 } else {
-                    self.hudHandler.setHudProgress(ui["progress"] as! Int)
+                    self.hudHandler.setHudProgress(ui["progress"] as? Int ?? 0)
                 }
                 }
             }
             }
         }
         }
@@ -54,12 +54,12 @@ internal final class SettingsViewController: QuickTableViewController {
             queue: nil
             queue: nil
         ) { notification in
         ) { notification in
             if let ui = notification.userInfo {
             if let ui = notification.userInfo {
-                if ui["error"] as! Bool {
+                if ui["error"] as? Bool ?? false {
                     self.hudHandler.setHudError(ui["errorMessage"] as? String)
                     self.hudHandler.setHudError(ui["errorMessage"] as? String)
-                } else if ui["done"] as! Bool {
+                } else if ui["done"] as? Bool ?? false {
                     self.hudHandler.setHudDone(callback: nil)
                     self.hudHandler.setHudDone(callback: nil)
                 } else {
                 } else {
-                    self.hudHandler.setHudProgress(ui["progress"] as! Int)
+                    self.hudHandler.setHudProgress(ui["progress"] as? Int ?? 0)
                 }
                 }
             }
             }
         }
         }

+ 7 - 3
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -100,7 +100,10 @@ class AppCoordinator: NSObject, Coordinator {
 
 
     func showChat(chatId: Int) {
     func showChat(chatId: Int) {
         showTab(index: 3)
         showTab(index: 3)
-        let navController = self.chatListController as! UINavigationController
+        guard let navController = self.chatListController as? UINavigationController else {
+            assertionFailure("huh? why no nav controller?")
+            return
+        }
         let chatVC = ChatViewController(dcContext: dcContext, chatId: chatId)
         let chatVC = ChatViewController(dcContext: dcContext, chatId: chatId)
         let coordinator = ChatViewCoordinator(dcContext: dcContext, navigationController: navController, chatId: chatId)
         let coordinator = ChatViewCoordinator(dcContext: dcContext, navigationController: navController, chatId: chatId)
         chatVC.coordinator = coordinator
         chatVC.coordinator = coordinator
@@ -195,8 +198,9 @@ class ProfileCoordinator: Coordinator {
     }
     }
 
 
     func showChat(chatId: Int) {
     func showChat(chatId: Int) {
-        let appDelegate = UIApplication.shared.delegate as! AppDelegate
-        appDelegate.appCoordinator.showChat(chatId: chatId)
+        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
+            appDelegate.appCoordinator.showChat(chatId: chatId)
+        }
     }
     }
 }
 }
 
 

+ 2 - 4
deltachat-ios/DC/events.swift

@@ -41,15 +41,13 @@ public func callbackSwift(event: CInt, data1: CUnsignedLong, data2: CUnsignedLon
                     "progress": Int(data1),
                     "progress": Int(data1),
                     "error": Int(data1) == 0,
                     "error": Int(data1) == 0,
                     "done": done,
                     "done": done,
-                    "errorMessage": AppDelegate.lastErrorDuringConfig,
+                    "errorMessage": AppDelegate.lastErrorDuringConfig as Any,
                 ]
                 ]
             )
             )
 
 
             if done {
             if done {
                 UserDefaults.standard.set(true, forKey: Constants.Keys.deltachatUserProvidedCredentialsKey)
                 UserDefaults.standard.set(true, forKey: Constants.Keys.deltachatUserProvidedCredentialsKey)
                 UserDefaults.standard.synchronize()
                 UserDefaults.standard.synchronize()
-                let appDelegate = UIApplication.shared.delegate as! AppDelegate
-                // appDelegate.appCoordinator?.showTab(index: 3)
                 AppDelegate.lastErrorDuringConfig = nil
                 AppDelegate.lastErrorDuringConfig = nil
             }
             }
         }
         }
@@ -135,7 +133,7 @@ public func callbackSwift(event: CInt, data1: CUnsignedLong, data2: CUnsignedLon
                     "progress": Int(data1),
                     "progress": Int(data1),
                     "error": Int(data1) == 0,
                     "error": Int(data1) == 0,
                     "done": Int(data1) == 1000,
                     "done": Int(data1) == 1000,
-                    "errorMessage": AppDelegate.lastErrorDuringConfig,
+                    "errorMessage": AppDelegate.lastErrorDuringConfig as Any,
                 ]
                 ]
             )
             )
         }
         }