IOS-1521 adapts MM changes. Reduces MessageModelUtil usage, uses AddressBook as Singleton IOS-1521+IOS-1495
authorbuff <andreas@pep-project.org>
Tue, 14 May 2019 18:30:47 +0200
branchIOS-1521+IOS-1495
changeset 8612961366028e40
parent 8611 14e23a7f54fa
child 8613 463170dfc0b8
IOS-1521 adapts MM changes. Reduces MessageModelUtil usage, uses AddressBook as Singleton
pEpForiOS/AppDelegate.swift
pEpForiOS/UI/EmailDisplayList/EmailListViewModel.swift
pEpForiOS/UI/EmailDisplayList/MessageViewModel.swift
pEpForiOS/UI/Folder/ViewModel/FolderSectionViewModel.swift
pEpForiOS/UI/Folder/ViewModel/FolderViewModel.swift
pEpForiOS/Util/IdentityImageTool.swift
pEpForiOSTests/ImapFlagsTests.swift
     1.1 --- a/pEpForiOS/AppDelegate.swift	Tue May 14 16:45:34 2019 +0200
     1.2 +++ b/pEpForiOS/AppDelegate.swift	Tue May 14 18:30:47 2019 +0200
     1.3 @@ -198,21 +198,33 @@
     1.4          messageModelService?.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 -                MessageModelUtil.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 @@ -239,7 +251,7 @@
    1.43          Log.shared.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 @@ -260,11 +272,7 @@
    1.52      }
    1.53  
    1.54      func applicationWillEnterForeground(_ application: UIApplication) {
    1.55 -        DispatchQueue.global(qos: .userInitiated).async {
    1.56 -            MessageModelUtil.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/EmailDisplayList/EmailListViewModel.swift	Tue May 14 16:45:34 2019 +0200
     2.2 +++ b/pEpForiOS/UI/EmailDisplayList/EmailListViewModel.swift	Tue May 14 18:30:47 2019 +0200
     2.3 @@ -303,15 +303,6 @@
     2.4          return messageQueryResults[indexPath.row]
     2.5      }
     2.6  
     2.7 -    //
     2.8 -//    internal func requestEmailViewIfNeeded(for message:Message) {
     2.9 -//        MessageModelUtil.performAndWait {
    2.10 -//            DispatchQueue.main.async {
    2.11 -//                self.screenComposer?.emailListViewModel(self, requestsShowEmailViewFor: message)
    2.12 -//            }
    2.13 -//        }
    2.14 -//    }
    2.15 -
    2.16      func freeMemory() {
    2.17          contactImageTool.clearCache()
    2.18      }
     3.1 --- a/pEpForiOS/UI/EmailDisplayList/MessageViewModel.swift	Tue May 14 16:45:34 2019 +0200
     3.2 +++ b/pEpForiOS/UI/EmailDisplayList/MessageViewModel.swift	Tue May 14 18:30:47 2019 +0200
     3.3 @@ -326,36 +326,10 @@
     3.4      }
     3.5  }
     3.6  
     3.7 -//PRAGMA MARK: Message View Model + Operations
     3.8 +// MARK: Operations
     3.9  
    3.10  extension MessageViewModel {
    3.11  
    3.12 -    // Message threading is not supported. Let's keep it for now. It might be helpful for
    3.13 -    // reimplementing.
    3.14 -//    private func getMessageCountOperation(completion: @escaping (Int)->()) -> SelfReferencingOperation {
    3.15 -//
    3.16 -//        let getMessageCountOperation = SelfReferencingOperation {  [weak self] operation in
    3.17 -//            guard let me = self else {
    3.18 -//                return
    3.19 -//            }
    3.20 -//            MessageModelUtil.performAndWait {
    3.21 -//                guard
    3.22 -//                    let operation = operation,
    3.23 -//                    !operation.isCancelled else {
    3.24 -//                        return
    3.25 -//                }
    3.26 -//                let messageCount = 0 // no threading
    3.27 -//                me.internalMessageCount = messageCount
    3.28 -//                if (!operation.isCancelled){
    3.29 -//                    DispatchQueue.main.async {
    3.30 -//                        completion(messageCount)
    3.31 -//                    }
    3.32 -//                }
    3.33 -//            }
    3.34 -//        }
    3.35 -//        return getMessageCountOperation
    3.36 -//    }
    3.37 -
    3.38      private func getBodyPeekOperation(for message: Message, completion: @escaping (String)->()) -> SelfReferencingOperation {
    3.39  
    3.40          let getBodyPeekOperation = SelfReferencingOperation {operation in
     4.1 --- a/pEpForiOS/UI/Folder/ViewModel/FolderSectionViewModel.swift	Tue May 14 16:45:34 2019 +0200
     4.2 +++ b/pEpForiOS/UI/Folder/ViewModel/FolderSectionViewModel.swift	Tue May 14 18:30:47 2019 +0200
     4.3 @@ -59,7 +59,7 @@
     4.4          if let cachedContactImage = contactImageTool.cachedIdentityImage(for: userKey) {
     4.5              callback(cachedContactImage)
     4.6          } else {
     4.7 -            DispatchQueue.global().async {
     4.8 +            DispatchQueue.global(qos: .userInitiated) .async {
     4.9                  let contactImage = self.contactImageTool.identityImage(for: userKey)
    4.10                  DispatchQueue.main.async {
    4.11                      callback(contactImage)
     5.1 --- a/pEpForiOS/UI/Folder/ViewModel/FolderViewModel.swift	Tue May 14 16:45:34 2019 +0200
     5.2 +++ b/pEpForiOS/UI/Folder/ViewModel/FolderViewModel.swift	Tue May 14 18:30:47 2019 +0200
     5.3 @@ -51,7 +51,7 @@
     5.4      func refreshFolderList() {
     5.5          DispatchQueue.global(qos: .userInitiated).async {
     5.6              MessageModelUtil.perform {
     5.7 -                self.folderSyncService.requestFolders(inAccounts: Account.all())
     5.8 +                self.folderSyncService.requestFolders(inAccounts: Account.all()) //!!!: must not be in UI. According to fetchOlder()
     5.9              }
    5.10          }
    5.11      }
     6.1 --- a/pEpForiOS/Util/IdentityImageTool.swift	Tue May 14 16:45:34 2019 +0200
     6.2 +++ b/pEpForiOS/Util/IdentityImageTool.swift	Tue May 14 18:30:47 2019 +0200
     6.3 @@ -157,8 +157,7 @@
     6.4  
     6.5          if let addressBookID = identityKey.addressBookId {
     6.6              // Get image from system AddressBook if any
     6.7 -            let ab = AddressBook()
     6.8 -            if let contact = ab.contactBy(addressBookID: addressBookID),
     6.9 +            if let contact = AddressBook.shared.contactBy(addressBookID: addressBookID),
    6.10                  let imgData = contact.thumbnailImageData {
    6.11                  image = UIImage(data: imgData)
    6.12              }
     7.1 --- a/pEpForiOSTests/ImapFlagsTests.swift	Tue May 14 16:45:34 2019 +0200
     7.2 +++ b/pEpForiOSTests/ImapFlagsTests.swift	Tue May 14 18:30:47 2019 +0200
     7.3 @@ -9,7 +9,7 @@
     7.4  import XCTest
     7.5  
     7.6  import pEpForiOS
     7.7 -import MessageModel
     7.8 +@testable import MessageModel
     7.9  import PantomimeFramework
    7.10  
    7.11  class ImapFlagsTests: XCTestCase {