IOS-1422 use AddressBook singleton approach IOS-1422
authorbuff <andreas@pep-project.org>
Wed, 15 May 2019 16:40:08 +0200
branchIOS-1422
changeset 8652cf93eccfd15b
parent 8651 e63e91c1cc0a
child 13152 ce1c9b4c3eb1
IOS-1422 use AddressBook singleton approach
pEpForiOS/AppDelegate.swift
pEpForiOS/UI/Folder/ViewModel/FolderSectionViewModel.swift
pEpForiOS/UI/Handshake/CellsAndSections/HandshakePartnerTableViewCell.swift
pEpForiOS/Util/IdentityImageTool.swift
     1.1 --- a/pEpForiOS/AppDelegate.swift	Wed May 15 14:53:42 2019 +0200
     1.2 +++ b/pEpForiOS/AppDelegate.swift	Wed May 15 16:40:08 2019 +0200
     1.3 @@ -220,21 +220,33 @@
     1.4          networkService?.cancel()
     1.5      }
     1.6  
     1.7 -    private func prepareUserNotifications() {
     1.8 +    private func askUserForPermissions() {
     1.9          UserNotificationTool.resetApplicationIconBadgeNumber()
    1.10 -        UserNotificationTool.askForPermissions() { granted in
    1.11 +        UserNotificationTool.askForPermissions() { [weak self] _ in
    1.12              // We do not care about whether or not the user granted permissions to
    1.13              // post notifications here (e.g. we ignore granted)
    1.14              // The calls are nested to avoid simultaniously showing permissions alert for notifications
    1.15              // and contact access.
    1.16 -            DispatchQueue.global(qos: .userInitiated).async {
    1.17 -                MessageModel.perform {
    1.18 -                    AddressBook.checkAndTransfer()
    1.19 +            self?.askForContactAccessPermissionsAndImportContacts()
    1.20 +        }
    1.21 +    }
    1.22 +
    1.23 +    private func askForContactAccessPermissionsAndImportContacts() {
    1.24 +        DispatchQueue.global(qos: .userInitiated).async { [weak self] in
    1.25 +            if AddressBook.shared.isAuthorized() {
    1.26 +                DispatchQueue.main.async {
    1.27 +                    self?.importContacts()
    1.28                  }
    1.29              }
    1.30          }
    1.31      }
    1.32  
    1.33 +    private func importContacts() {
    1.34 +        DispatchQueue.global(qos: .background).async { //!!!: Must become background task. Or stoped when going to background imo.
    1.35 +            AddressBook.shared.transferContacts()
    1.36 +        }
    1.37 +    }
    1.38 +
    1.39      // MARK: - UIApplicationDelegate
    1.40  
    1.41      func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
    1.42 @@ -261,7 +273,7 @@
    1.43          Logger.appDelegateLogger.log("Library url: %{public}@", String(describing: applicationDirectory()))
    1.44          deleteAllFolders(pEpReInitialized: pEpReInitialized)
    1.45  
    1.46 -        prepareUserNotifications()
    1.47 +        askUserForPermissions()
    1.48  
    1.49          let result = setupInitialViewController()
    1.50  
    1.51 @@ -282,11 +294,7 @@
    1.52      }
    1.53  
    1.54      func applicationWillEnterForeground(_ application: UIApplication) {
    1.55 -        DispatchQueue.global(qos: .userInitiated).async {
    1.56 -            MessageModel.perform {
    1.57 -                AddressBook.checkAndTransfer()
    1.58 -            }
    1.59 -        }
    1.60 +        importContacts()
    1.61          // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    1.62      }
    1.63  
     2.1 --- a/pEpForiOS/UI/Folder/ViewModel/FolderSectionViewModel.swift	Wed May 15 14:53:42 2019 +0200
     2.2 +++ b/pEpForiOS/UI/Folder/ViewModel/FolderSectionViewModel.swift	Wed May 15 16:40:08 2019 +0200
     2.3 @@ -58,7 +58,7 @@
     2.4          if let cachedContactImage = contactImageTool.cachedIdentityImage(for: ac.user) {
     2.5              callback(cachedContactImage)
     2.6          } else {
     2.7 -            DispatchQueue.global().async {
     2.8 +            DispatchQueue.global(qos: .userInitiated).async {
     2.9                  let contactImage = self.contactImageTool.identityImage(for: ac.user)
    2.10                  DispatchQueue.main.async {
    2.11                      callback(contactImage)
     3.1 --- a/pEpForiOS/UI/Handshake/CellsAndSections/HandshakePartnerTableViewCell.swift	Wed May 15 14:53:42 2019 +0200
     3.2 +++ b/pEpForiOS/UI/Handshake/CellsAndSections/HandshakePartnerTableViewCell.swift	Wed May 15 16:40:08 2019 +0200
     3.3 @@ -60,7 +60,9 @@
     3.4          didSet {
     3.5              updateView()
     3.6              viewModel?.partnerImage.observe() { [weak self] img in
     3.7 -                self?.partnerImageView.image = img
     3.8 +                DispatchQueue.main.async {
     3.9 +                    self?.partnerImageView.image = img
    3.10 +                }
    3.11              }
    3.12          }
    3.13      }
     4.1 --- a/pEpForiOS/Util/IdentityImageTool.swift	Wed May 15 14:53:42 2019 +0200
     4.2 +++ b/pEpForiOS/Util/IdentityImageTool.swift	Wed May 15 16:40:08 2019 +0200
     4.3 @@ -57,8 +57,7 @@
     4.4          }
     4.5  
     4.6          if let addressBookID = identity.addressBookID {
     4.7 -            let ab = AddressBook()
     4.8 -            if let contact = ab.contactBy(addressBookID: addressBookID),
     4.9 +            if let contact = AddressBook.shared.contactBy(addressBookID: addressBookID),
    4.10                  let imgData = contact.thumbnailImageData {
    4.11                  image = UIImage(data: imgData)
    4.12              }