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

replace potentially harmful 'unowned' by 'weak'

B. Petersen 5 жил өмнө
parent
commit
79597ce2f3

+ 3 - 3
DcCore/DcCore/DC/Wrapper.swift

@@ -675,7 +675,7 @@ public class DcChat {
         return DcUtils.copyAndFreeArray(inputArray: dc_get_chat_contacts(DcContext.shared.contextPointer, UInt32(id)))
         return DcUtils.copyAndFreeArray(inputArray: dc_get_chat_contacts(DcContext.shared.contextPointer, UInt32(id)))
     }
     }
 
 
-    public lazy var profileImage: UIImage? = { [unowned self] in
+    public lazy var profileImage: UIImage? = { [weak self] in
         guard let cString = dc_chat_get_profile_image(chatPointer) else { return nil }
         guard let cString = dc_chat_get_profile_image(chatPointer) else { return nil }
         let filename = String(cString: cString)
         let filename = String(cString: cString)
         dc_str_unref(cString)
         dc_str_unref(cString)
@@ -830,7 +830,7 @@ public class DcMsg {
         return nil
         return nil
     }
     }
 
 
-    public lazy var image: UIImage? = { [unowned self] in
+    public lazy var image: UIImage? = { [weak self] in
         let filetype = dc_msg_get_viewtype(messagePointer)
         let filetype = dc_msg_get_viewtype(messagePointer)
         if let path = fileURL, filetype == DC_MSG_IMAGE {
         if let path = fileURL, filetype == DC_MSG_IMAGE {
             if path.isFileURL {
             if path.isFileURL {
@@ -1019,7 +1019,7 @@ public class DcContact {
         return dc_contact_is_blocked(contactPointer) == 1
         return dc_contact_is_blocked(contactPointer) == 1
     }
     }
 
 
-    public lazy var profileImage: UIImage? = { [unowned self] in
+    public lazy var profileImage: UIImage? = { [weak self] in
         guard let cString = dc_contact_get_profile_image(contactPointer) else { return nil }
         guard let cString = dc_contact_get_profile_image(contactPointer) else { return nil }
         let filename = String(cString: cString)
         let filename = String(cString: cString)
         dc_str_unref(cString)
         dc_str_unref(cString)

+ 5 - 4
Pods/ReachabilitySwift/Sources/Reachability.swift

@@ -223,14 +223,15 @@ public extension Reachability {
 fileprivate extension Reachability {
 fileprivate extension Reachability {
 
 
     func setReachabilityFlags() throws {
     func setReachabilityFlags() throws {
-        try reachabilitySerialQueue.sync { [unowned self] in
+        try reachabilitySerialQueue.sync { [weak self] in
+            guard let strongSelf = self else { return }
             var flags = SCNetworkReachabilityFlags()
             var flags = SCNetworkReachabilityFlags()
-            if !SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags) {
-                self.stopNotifier()
+            if !SCNetworkReachabilityGetFlags(strongSelf.reachabilityRef, &flags) {
+                strongSelf.stopNotifier()
                 throw ReachabilityError.UnableToGetInitialFlags
                 throw ReachabilityError.UnableToGetInitialFlags
             }
             }
             
             
-            self.flags = flags
+            strongSelf.flags = flags
         }
         }
     }
     }
     
     

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

@@ -97,8 +97,8 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
     private lazy var providerInfoCell: ProviderInfoCell = {
     private lazy var providerInfoCell: ProviderInfoCell = {
         let cell = ProviderInfoCell()
         let cell = ProviderInfoCell()
         cell.onInfoButtonPressed = {
         cell.onInfoButtonPressed = {
-            [unowned self] in
-            self.handleProviderInfoButton()
+            [weak self] in
+            self?.handleProviderInfoButton()
         }
         }
         return cell
         return cell
     }()
     }()
@@ -612,7 +612,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
 
 
              let oAuthAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
              let oAuthAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
              let confirm = UIAlertAction(title: "Confirm", style: .default, handler: {
              let confirm = UIAlertAction(title: "Confirm", style: .default, handler: {
-                 [unowned self] _ in
+                 [weak self] _ in // TODO: refactor usages of `self` to `self?` when this code is used again
                  let nc = NotificationCenter.default
                  let nc = NotificationCenter.default
                  self.oauth2Observer = nc.addObserver(self, selector: #selector(self.oauthLoginApproved), name: NSNotification.Name("oauthLoginApproved"), object: nil)
                  self.oauth2Observer = nc.addObserver(self, selector: #selector(self.oauthLoginApproved), name: NSNotification.Name("oauthLoginApproved"), object: nil)
                  self.launchOAuthBrowserWindow(url: url)
                  self.launchOAuthBrowserWindow(url: url)

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

@@ -232,20 +232,20 @@ class ChatListController: UITableViewController {
         }
         }
         let archiveActionTitle: String = String.localized(viewModel.isArchive ? "unarchive" : "archive")
         let archiveActionTitle: String = String.localized(viewModel.isArchive ? "unarchive" : "archive")
 
 
-        let archiveAction = UITableViewRowAction(style: .destructive, title: archiveActionTitle) { [unowned self] _, _ in
-            self.viewModel.archiveChatToggle(chatId: chatId)
+        let archiveAction = UITableViewRowAction(style: .destructive, title: archiveActionTitle) { [weak self] _, _ in
+            self?.viewModel.archiveChatToggle(chatId: chatId)
         }
         }
         archiveAction.backgroundColor = UIColor.lightGray
         archiveAction.backgroundColor = UIColor.lightGray
 
 
         let chat = dcContext.getChat(chatId: chatId)
         let chat = dcContext.getChat(chatId: chatId)
         let pinned = chat.visibility==DC_CHAT_VISIBILITY_PINNED
         let pinned = chat.visibility==DC_CHAT_VISIBILITY_PINNED
-        let pinAction = UITableViewRowAction(style: .destructive, title: String.localized(pinned ? "unpin" : "pin")) { [unowned self] _, _ in
-            self.viewModel.pinChatToggle(chatId: chat.id)
+        let pinAction = UITableViewRowAction(style: .destructive, title: String.localized(pinned ? "unpin" : "pin")) { [weak self] _, _ in
+            self?.viewModel.pinChatToggle(chatId: chat.id)
         }
         }
         pinAction.backgroundColor = UIColor.systemGreen
         pinAction.backgroundColor = UIColor.systemGreen
 
 
-        let deleteAction = UITableViewRowAction(style: .normal, title: String.localized("delete")) { [unowned self] _, _ in
-            self.showDeleteChatConfirmationAlert(chatId: chatId)
+        let deleteAction = UITableViewRowAction(style: .normal, title: String.localized("delete")) { [weak self] _, _ in
+            self?.showDeleteChatConfirmationAlert(chatId: chatId)
         }
         }
         deleteAction.backgroundColor = UIColor.systemRed
         deleteAction.backgroundColor = UIColor.systemRed
 
 

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

@@ -62,8 +62,8 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     override func viewDidLoad() {
     override func viewDidLoad() {
         super.viewDidLoad()
         super.viewDidLoad()
         title = String.localized("pref_profile_info_headline")
         title = String.localized("pref_profile_info_headline")
-        avatarSelectionCell.onAvatarTapped = { [unowned self] in
-            self.onAvatarTapped()
+        avatarSelectionCell.onAvatarTapped = { [weak self] in
+            self?.onAvatarTapped()
         }
         }
     }
     }
 
 

+ 7 - 8
deltachat-ios/Controller/GroupChatDetailViewController.swift

@@ -401,21 +401,20 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
             !isMemberManagementRow(row: row) &&
             !isMemberManagementRow(row: row) &&
             getGroupMemberIdFor(row) != currentUser.id {
             getGroupMemberIdFor(row) != currentUser.id {
             // action set for members except for current user
             // action set for members except for current user
-            let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [unowned self] _, indexPath in
-
-                let contact = self.getGroupMember(at: row)
+            let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [weak self] _, indexPath in
+                guard let strongSelf = self else { return }
+                let contact = strongSelf.getGroupMember(at: row)
                 let title = String.localizedStringWithFormat(String.localized("ask_remove_members"), contact.nameNAddr)
                 let title = String.localizedStringWithFormat(String.localized("ask_remove_members"), contact.nameNAddr)
                 let alert = UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)
                 let alert = UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)
                 alert.addAction(UIAlertAction(title: String.localized("remove_desktop"), style: .destructive, handler: { _ in
                 alert.addAction(UIAlertAction(title: String.localized("remove_desktop"), style: .destructive, handler: { _ in
-                    let success = self.dcContext.removeContactFromChat(chatId: self.chat.id, contactId: contact.id)
+                    let success = strongSelf.dcContext.removeContactFromChat(chatId: strongSelf.chat.id, contactId: contact.id)
                     if success {
                     if success {
-                        self.removeGroupMemberFromTableAt(indexPath)
+                        strongSelf.removeGroupMemberFromTableAt(indexPath)
                     }
                     }
                 }))
                 }))
                 alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
                 alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
-                self.present(alert, animated: true, completion: nil)
-
- }
+                strongSelf.present(alert, animated: true, completion: nil)
+            }
             delete.backgroundColor = UIColor.red
             delete.backgroundColor = UIColor.red
             return [delete]
             return [delete]
         }
         }

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

@@ -40,10 +40,10 @@ class HelpViewController: UIViewController, WKNavigationDelegate {
     }
     }
 
 
     override func viewWillAppear(_ animated: Bool) {
     override func viewWillAppear(_ animated: Bool) {
-        loadHtmlContent { [unowned self] url in
+        loadHtmlContent { [weak self] url in
             // return to main thread
             // return to main thread
             DispatchQueue.main.async {
             DispatchQueue.main.async {
-                self.webView.loadFileURL(url, allowingReadAccessTo: url)
+                self?.webView.loadFileURL(url, allowingReadAccessTo: url)
             }
             }
         }
         }
     }
     }

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

@@ -228,19 +228,21 @@ class NewChatViewController: UITableViewController {
         if indexPath.section == sectionContacts {
         if indexPath.section == sectionContacts {
             let contactId = contactIdByRow(indexPath.row)
             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.showContactDetail(contactId: contactId)
+            let edit = UITableViewRowAction(style: .normal, title: String.localized("info")) { [weak self] _, _ in
+                guard let strongSelf = self else { return }
+                if strongSelf.searchController.isActive {
+                    strongSelf.searchController.dismiss(animated: false) {
+                        strongSelf.showContactDetail(contactId: contactId)
                     }
                     }
                 } else {
                 } else {
-                    self.showContactDetail(contactId: contactId)
+                    strongSelf.showContactDetail(contactId: contactId)
                 }
                 }
             }
             }
 
 
-            let delete = UITableViewRowAction(style: .destructive, title: String.localized("delete")) { [unowned self] _, _ in
-                let contactId = self.contactIdByRow(indexPath.row)
-                self.askToDeleteContact(contactId: contactId, context: self.dcContext)
+            let delete = UITableViewRowAction(style: .destructive, title: String.localized("delete")) { [weak self] _, _ in
+                guard let strongSelf = self else { return }
+                let contactId = strongSelf.contactIdByRow(indexPath.row)
+                strongSelf.askToDeleteContact(contactId: contactId, context: strongSelf.dcContext)
             }
             }
 
 
             edit.backgroundColor = DcColors.primary
             edit.backgroundColor = DcColors.primary

+ 12 - 10
deltachat-ios/Controller/NewGroupController.swift

@@ -254,14 +254,16 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
 
 
         //swipe by delete
         //swipe by delete
         if section == sectionGroupMembers, groupContactIds[row] != DC_CONTACT_ID_SELF {
         if section == sectionGroupMembers, groupContactIds[row] != DC_CONTACT_ID_SELF {
-            let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [unowned self] _, indexPath in
-                if self.groupChatId != 0, self.dcContext.getChat(chatId: self.groupChatId).contactIds.contains(self.groupContactIds[row]) {
-                    let success = self.dcContext.removeContactFromChat(chatId: self.groupChatId, contactId: self.groupContactIds[row])
+            let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [weak self] _, indexPath in
+                guard let strongSelf = self else { return }
+                if strongSelf.groupChatId != 0,
+                    strongSelf.dcContext.getChat(chatId: strongSelf.groupChatId).contactIds.contains(strongSelf.groupContactIds[row]) {
+                    let success = strongSelf.dcContext.removeContactFromChat(chatId: strongSelf.groupChatId, contactId: strongSelf.groupContactIds[row])
                     if success {
                     if success {
-                        self.removeGroupContactFromList(at: indexPath)
+                        strongSelf.removeGroupContactFromList(at: indexPath)
                     }
                     }
                 } else {
                 } else {
-                    self.removeGroupContactFromList(at: indexPath)
+                    strongSelf.removeGroupContactFromList(at: indexPath)
                 }
                 }
             }
             }
             delete.backgroundColor = UIColor.red
             delete.backgroundColor = UIColor.red
@@ -356,8 +358,8 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
 
 
     private func showQrCodeInvite(chatId: Int) {
     private func showQrCodeInvite(chatId: Int) {
         let qrInviteCodeController = QrInviteViewController(dcContext: dcContext, chatId: chatId)
         let qrInviteCodeController = QrInviteViewController(dcContext: dcContext, chatId: chatId)
-        qrInviteCodeController.onDismissed = { [unowned self] in
-            self.updateGroupContactIdsOnQRCodeInvite()
+        qrInviteCodeController.onDismissed = { [weak self] in
+            self?.updateGroupContactIdsOnQRCodeInvite()
         }
         }
         navigationController?.pushViewController(qrInviteCodeController, animated: true)
         navigationController?.pushViewController(qrInviteCodeController, animated: true)
     }
     }
@@ -365,9 +367,9 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     private func showAddMembers(preselectedMembers: Set<Int>, isVerified: Bool) {
     private func showAddMembers(preselectedMembers: Set<Int>, isVerified: Bool) {
         let newGroupController = NewGroupAddMembersViewController(preselected: preselectedMembers,
         let newGroupController = NewGroupAddMembersViewController(preselected: preselectedMembers,
                                                                   isVerified: isVerified)
                                                                   isVerified: isVerified)
-        newGroupController.onMembersSelected = { [unowned self] (memberIds: Set<Int>) -> Void in
-            self.updateGroupContactIdsOnListSelection(memberIds)
-            self.navigationController?.popViewController(animated: true)
+        newGroupController.onMembersSelected = { [weak self] (memberIds: Set<Int>) -> Void in
+            self?.updateGroupContactIdsOnListSelection(memberIds)
+            self?.navigationController?.popViewController(animated: true)
         }
         }
         navigationController?.pushViewController(newGroupController, animated: true)
         navigationController?.pushViewController(newGroupController, animated: true)
     }
     }

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

@@ -35,8 +35,8 @@ class ProfileInfoViewController: UITableViewController {
         let cell =  TextFieldCell.makeNameCell()
         let cell =  TextFieldCell.makeNameCell()
         cell.placeholder = String.localized("pref_your_name")
         cell.placeholder = String.localized("pref_your_name")
         cell.setText(text: dcContext.displayname)
         cell.setText(text: dcContext.displayname)
-        cell.onTextFieldChange = {[unowned self] textField in
-            self.displayName = textField.text
+        cell.onTextFieldChange = {[weak self] textField in
+            self?.displayName = textField.text
         }
         }
         return cell
         return cell
     }()
     }()

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

@@ -410,8 +410,8 @@ internal final class SettingsViewController: UITableViewController {
             UserDefaults.standard.set(!locationStreaming, forKey: "location_streaming")
             UserDefaults.standard.set(!locationStreaming, forKey: "location_streaming")
         }))
         }))
 
 
-        let logAction = UIAlertAction(title: String.localized("pref_view_log"), style: .default, handler: { [unowned self] _ in
-            self.showDebugToolkit()
+        let logAction = UIAlertAction(title: String.localized("pref_view_log"), style: .default, handler: { [weak self] _ in
+            self?.showDebugToolkit()
         })
         })
         alert.addAction(logAction)
         alert.addAction(logAction)
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))

+ 17 - 16
deltachat-ios/Controller/WelcomeViewController.swift

@@ -14,21 +14,22 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
 
 
     private lazy var welcomeView: WelcomeContentView = {
     private lazy var welcomeView: WelcomeContentView = {
         let view = WelcomeContentView()
         let view = WelcomeContentView()
-        view.onLogin = { [unowned self] in
-            let accountSetupController = AccountSetupController(dcContext: self.dcContext, editView: false)
+        view.onLogin = { [weak self] in
+            guard let strongSelf = self else { return }
+            let accountSetupController = AccountSetupController(dcContext: strongSelf.dcContext, editView: false)
             accountSetupController.onLoginSuccess = {
             accountSetupController.onLoginSuccess = {
-                [unowned self] in
+                [weak self] in
                 if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
                 if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
                     appDelegate.appCoordinator.presentTabBarController()
                     appDelegate.appCoordinator.presentTabBarController()
                 }
                 }
             }
             }
-            self.navigationController?.pushViewController(accountSetupController, animated: true)
+            strongSelf.navigationController?.pushViewController(accountSetupController, animated: true)
         }
         }
-        view.onScanQRCode  = { [unowned self] in
+        view.onScanQRCode  = { [weak self] in
             let qrReader = QrCodeReaderController()
             let qrReader = QrCodeReaderController()
             qrReader.delegate = self
             qrReader.delegate = self
-            self.qrCodeReader = qrReader
-            self.navigationController?.pushViewController(qrReader, animated: true)
+            self?.qrCodeReader = qrReader
+            self?.navigationController?.pushViewController(qrReader, animated: true)
         }
         }
         view.translatesAutoresizingMaskIntoConstraints = false
         view.translatesAutoresizingMaskIntoConstraints = false
         return view
         return view
@@ -41,14 +42,14 @@ class WelcomeViewController: UIViewController, ProgressAlertHandler {
         self.dcContext = dcContext
         self.dcContext = dcContext
         super.init(nibName: nil, bundle: nil)
         super.init(nibName: nil, bundle: nil)
         self.navigationItem.title = String.localized("welcome_desktop")
         self.navigationItem.title = String.localized("welcome_desktop")
-        onProgressSuccess = { [unowned self] in
+        onProgressSuccess = { [weak self] in
             let profileInfoController = ProfileInfoViewController(context: dcContext)
             let profileInfoController = ProfileInfoViewController(context: dcContext)
             profileInfoController.onClose = {
             profileInfoController.onClose = {
                 if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
                 if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
                     appDelegate.appCoordinator.presentTabBarController()
                     appDelegate.appCoordinator.presentTabBarController()
                 }
                 }
             }
             }
-            self.navigationController?.setViewControllers([profileInfoController], animated: true)
+            self?.navigationController?.setViewControllers([profileInfoController], animated: true)
         }
         }
     }
     }
 
 
@@ -147,17 +148,17 @@ extension WelcomeViewController: QrCodeReaderDelegate {
         let okAction = UIAlertAction(
         let okAction = UIAlertAction(
             title: String.localized("ok"),
             title: String.localized("ok"),
             style: .default,
             style: .default,
-            handler: { [unowned self] _ in
-                self.dismissQRReader()
-                self.createAccountFromQRCode(qrCode: qrCode)
+            handler: { [weak self] _ in
+                self?.dismissQRReader()
+                self?.createAccountFromQRCode(qrCode: qrCode)
             }
             }
         )
         )
 
 
         let qrCancelAction = UIAlertAction(
         let qrCancelAction = UIAlertAction(
             title: String.localized("cancel"),
             title: String.localized("cancel"),
             style: .cancel,
             style: .cancel,
-            handler: { [unowned self] _ in
-                self.dismissQRReader()
+            handler: { [weak self] _ in
+                self?.dismissQRReader()
             }
             }
         )
         )
 
 
@@ -172,8 +173,8 @@ extension WelcomeViewController: QrCodeReaderDelegate {
         let okAction = UIAlertAction(
         let okAction = UIAlertAction(
             title: String.localized("ok"),
             title: String.localized("ok"),
             style: .default,
             style: .default,
-            handler: { [unowned self] _ in
-                self.qrCodeReader?.startSession()
+            handler: { [weak self] _ in
+                self?.qrCodeReader?.startSession()
             }
             }
         )
         )
         alert.addAction(okAction)
         alert.addAction(okAction)

+ 4 - 4
deltachat-ios/Handler/DeviceContactsHandler.swift

@@ -85,15 +85,15 @@ class DeviceContactsHandler {
         case .denied:
         case .denied:
             contactListDelegate?.accessDenied()
             contactListDelegate?.accessDenied()
         case .restricted, .notDetermined:
         case .restricted, .notDetermined:
-            store.requestAccess(for: .contacts) { [unowned self] granted, _ in
+            store.requestAccess(for: .contacts) { [weak self] granted, _ in
                 if granted {
                 if granted {
                     DispatchQueue.main.async {
                     DispatchQueue.main.async {
-                        self.addContactsToCore()
-                        self.contactListDelegate?.accessGranted()
+                        self?.addContactsToCore()
+                        self?.contactListDelegate?.accessGranted()
                     }
                     }
                 } else {
                 } else {
                     DispatchQueue.main.async {
                     DispatchQueue.main.async {
-                        self.contactListDelegate?.accessDenied()
+                        self?.contactListDelegate?.accessDenied()
                     }
                     }
                 }
                 }
             }
             }

+ 4 - 2
deltachat-ios/Handler/HudHandler.swift

@@ -3,7 +3,7 @@ import UIKit
 
 
 class HudHandler {
 class HudHandler {
     var backupHud: JGProgressHUD?
     var backupHud: JGProgressHUD?
-    unowned var view: UIView
+    weak var view: UIView?
 
 
     init(parentView: UIView) {
     init(parentView: UIView) {
         view = parentView
         view = parentView
@@ -23,7 +23,9 @@ class HudHandler {
             hud.indicatorView = JGProgressHUDPieIndicatorView()
             hud.indicatorView = JGProgressHUDPieIndicatorView()
             hud.detailTextLabel.text = "0%"
             hud.detailTextLabel.text = "0%"
             hud.textLabel.text = text
             hud.textLabel.text = text
-            hud.show(in: self.view)
+            if let view = self.view {
+                hud.show(in: view)
+            }
             self.backupHud = hud
             self.backupHud = hud
         }
         }
     }
     }