IOS-97 more view model in emailListViewController
authorXavier Algarra <xavier@pep-project.org>
Thu, 29 Jun 2017 12:02:12 +0200
changeset 2363e969a926d09b
parent 2362 9aeaabed0d07
child 2364 78f0a37c4eb2
IOS-97 more view model in emailListViewController
pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
pEpForiOS/UI/EmailDisplay/EmailListViewModel.swift
     1.1 --- a/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Wed Jun 28 18:51:01 2017 +0200
     1.2 +++ b/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Thu Jun 29 12:02:12 2017 +0200
     1.3 @@ -29,7 +29,7 @@
     1.4      var viewModel: EmailListViewModel?
     1.5      var state = UIState()
     1.6      let searchController = UISearchController(searchResultsController: nil)
     1.7 -    let cellsInUse = NSCache<NSString, EmailListViewCell>()
     1.8 +    //let cellsInUse = NSCache<NSString, EmailListViewCell>()
     1.9  
    1.10      /**
    1.11       After trustwords have been invoked, this will be the partner identity that
    1.12 @@ -40,7 +40,7 @@
    1.13      @IBOutlet weak var enableFilterButton: UIBarButtonItem!
    1.14      @IBOutlet weak var textFilterButton: UIBarButtonItem!
    1.15  
    1.16 -    private var filterEnabled = false
    1.17 +    //private var filterEnabled = false
    1.18  
    1.19      override func viewDidLoad() {
    1.20          super.viewDidLoad()
    1.21 @@ -57,7 +57,11 @@
    1.22              return
    1.23          }
    1.24  
    1.25 -        self.textFilterButton.isEnabled = filterEnabled
    1.26 +        if let vm = viewModel {
    1.27 +            self.textFilterButton.isEnabled = vm.filterEnabled
    1.28 +        } else {
    1.29 +            self.textFilterButton.isEnabled = false
    1.30 +        }
    1.31  
    1.32          setDefaultColors()
    1.33          initialConfig()
    1.34 @@ -109,20 +113,23 @@
    1.35  
    1.36  
    1.37      @IBAction func showUnreadButtonTapped(_ sender: UIBarButtonItem) {
    1.38 -        if filterEnabled {
    1.39 -            filterEnabled = false
    1.40 -            textFilterButton.title = ""
    1.41 -            enableFilterButton.image = UIImage(named: "unread-icon")
    1.42 -            updateFilter(filter: Filter.unified())
    1.43 -        } else {
    1.44 -            filterEnabled = true
    1.45 -            textFilterButton.title = "Filter by: unread"
    1.46 -            enableFilterButton.image = UIImage(named: "unread-icon-active")
    1.47 -            if config != nil {
    1.48 -                updateFilter(filter: Filter.unread())
    1.49 +        if let vm = viewModel {
    1.50 +            if vm.filterEnabled {
    1.51 +                vm.filterEnabled = false
    1.52 +                textFilterButton.title = ""
    1.53 +                enableFilterButton.image = UIImage(named: "unread-icon")
    1.54 +                updateFilter(filter: Filter.unified())
    1.55 +            } else {
    1.56 +                vm.filterEnabled = true
    1.57 +                textFilterButton.title = "Filter by: unread"
    1.58 +                enableFilterButton.image = UIImage(named: "unread-icon-active")
    1.59 +                if config != nil {
    1.60 +                    updateFilter(filter: Filter.unread())
    1.61 +                }
    1.62              }
    1.63 +            self.textFilterButton.isEnabled = vm.filterEnabled
    1.64          }
    1.65 -        self.textFilterButton.isEnabled = filterEnabled
    1.66 +
    1.67  
    1.68      }
    1.69  
    1.70 @@ -331,7 +338,7 @@
    1.71          } else {
    1.72              if let text = searchText, text != "" {
    1.73                  let f = Filter.search(subject: text)
    1.74 -                if filterEnabled {
    1.75 +                if let vm = viewModel, vm.filterEnabled {
    1.76                      f.and(filter: Filter.unread())
    1.77                      updateFilter(filter: f)
    1.78                  }
    1.79 @@ -447,7 +454,7 @@
    1.80                      tableView.reloadData()
    1.81                  }
    1.82              } else if message.isGhost {
    1.83 -                if let cell = cellFor(message: message), let ip = tableView.indexPath(for: cell) {
    1.84 +                if let vm = viewModel, let cell = vm.cellFor(message: message), let ip = tableView.indexPath(for: cell) {
    1.85                      Log.info(
    1.86                          component: #function,
    1.87                          content: "delete message at \(index), \(folder.messageCount()) messages")
    1.88 @@ -457,7 +464,7 @@
    1.89                  }
    1.90              } else {
    1.91                  // other flags than delete must have been changed
    1.92 -                if let cell = cellFor(message: message) {
    1.93 +                if let vm = viewModel, let cell = vm.cellFor(message: message) {
    1.94                      cell.updateFlags(message: message)
    1.95                  } else {
    1.96                      tableView.reloadData()
    1.97 @@ -466,20 +473,6 @@
    1.98          }
    1.99      }
   1.100  
   1.101 -    // MARK: - Message -> Cell association
   1.102 -
   1.103 -    func keyFor(message: Message) -> NSString {
   1.104 -        let parentName = message.parent?.name ?? "unknown"
   1.105 -        return "\(message.uuid) \(parentName) \(message.uuid)" as NSString
   1.106 -    }
   1.107 -
   1.108 -    func associate(message: Message, toCell: EmailListViewCell) {
   1.109 -        cellsInUse.setObject(toCell, forKey: keyFor(message: message))
   1.110 -    }
   1.111 -
   1.112 -    func cellFor(message: Message) -> EmailListViewCell? {
   1.113 -        return cellsInUse.object(forKey: keyFor(message: message))
   1.114 -    }
   1.115  }
   1.116  
   1.117  // MARK: - MessageFolderDelegate
     2.1 --- a/pEpForiOS/UI/EmailDisplay/EmailListViewModel.swift	Wed Jun 28 18:51:01 2017 +0200
     2.2 +++ b/pEpForiOS/UI/EmailDisplay/EmailListViewModel.swift	Thu Jun 29 12:02:12 2017 +0200
     2.3 @@ -15,8 +15,10 @@
     2.4      var folderToShow: Folder?
     2.5      let cellsInUse = NSCache<NSString, EmailListViewCell>()
     2.6  
     2.7 +    var filterEnabled = false
     2.8 +
     2.9      init(config: EmailListConfig?) {
    2.10 -        MessageModelConfig.messageFolderDelegate = self
    2.11 +        //MessageModelConfig.messageFolderDelegate = self
    2.12          folderToShow = config?.folder
    2.13      }
    2.14  
    2.15 @@ -36,11 +38,16 @@
    2.16          }
    2.17      }
    2.18  
    2.19 +    // MARK: - Message -> Cell association
    2.20 +
    2.21      func associate(cell: EmailListViewCell, position: Int) {
    2.22          if let message = self[position] {
    2.23              cellsInUse.setObject(cell, forKey: keyFor(message: message))
    2.24          }
    2.25 +    }
    2.26  
    2.27 +    func cellFor(message: Message) -> EmailListViewCell? {
    2.28 +        return cellsInUse.object(forKey: keyFor(message: message))
    2.29      }
    2.30  
    2.31      func keyFor(message: Message) -> NSString {
    2.32 @@ -48,8 +55,6 @@
    2.33          return "\(message.uuid) \(parentName) \(message.uuid)" as NSString
    2.34      }
    2.35  
    2.36 -    
    2.37 -
    2.38  }
    2.39  
    2.40