IOS-744 filter adapted to Filter and viewModel changes IOS-700-sluggish-ui
authorbuff <andreas@pep-project.org>
Wed, 04 Oct 2017 18:40:07 +0200
branchIOS-700-sluggish-ui
changeset 31634ca07e477ed1
parent 3162 e2ac8a85eaca
child 3164 72b7dc8b3e77
IOS-744 filter adapted to Filter and viewModel changes
pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
pEpForiOS/UI/EmailDisplay/EmailListViewModel.swift
     1.1 --- a/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Wed Oct 04 17:13:12 2017 +0200
     1.2 +++ b/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Wed Oct 04 18:40:07 2017 +0200
     1.3 @@ -65,6 +65,7 @@
     1.4          super.viewDidLoad()
     1.5          title = NSLocalizedString("Inbox", comment: "General name for (unified) inbox")
     1.6          UIHelper.emailListTableHeight(self.tableView)
     1.7 +        self.textFilterButton.isEnabled = false
     1.8          addSearchBar()
     1.9      }
    1.10  
    1.11 @@ -77,7 +78,7 @@
    1.12  
    1.13          //BUFF: TODO
    1.14          if let vm = model {
    1.15 -            self.textFilterButton.isEnabled = vm.filterEnabled
    1.16 +            self.textFilterButton.isEnabled = vm.isFilterEnabled
    1.17              updateFilterText()
    1.18          } else {
    1.19              self.textFilterButton.isEnabled = false
    1.20 @@ -145,26 +146,10 @@
    1.21  
    1.22      // MARK: - Other
    1.23  
    1.24 -    func updateFilterText() {
    1.25 -        if let vm = model, let txt = vm.enabledFilter?.text {
    1.26 -            textFilterButton.title = "Filter by: " + txt
    1.27 -        }
    1.28 -    }
    1.29 -
    1.30      private func realNameOfFolderToShow() -> String? {
    1.31          return model?.folderToShow?.realName
    1.32      }
    1.33  
    1.34 -    func handleButtonFilter(enabled: Bool) {
    1.35 -        if enabled == false {
    1.36 -            textFilterButton.title = ""
    1.37 -            enableFilterButton.image = UIImage(named: "unread-icon")
    1.38 -        } else {
    1.39 -            enableFilterButton.image = UIImage(named: "unread-icon-active")
    1.40 -            updateFilterText()
    1.41 -        }
    1.42 -    }
    1.43 -
    1.44      private func configure(cell: EmailListViewCell, for indexPath: IndexPath) {
    1.45          // Configure lightweight stuff on main thread ...
    1.46          guard let saveModel = model else {
    1.47 @@ -221,10 +206,20 @@
    1.48      // MARK: - Actions
    1.49  
    1.50      @IBAction func filterButtonHasBeenPressed(_ sender: UIBarButtonItem) {
    1.51 -        handlefilter()
    1.52 +        guard let vm = model else {
    1.53 +            Log.shared.errorAndCrash(component: #function, errorString: "We should have a model here")
    1.54 +            return
    1.55 +        }
    1.56 +        vm.isFilterEnabled = !vm.isFilterEnabled
    1.57 +        upadteFilterButtonView()
    1.58      }
    1.59  
    1.60 -    func handlefilter() {
    1.61 +    func upadteFilterButtonView() {
    1.62 +        guard let vm = model else {
    1.63 +            Log.shared.errorAndCrash(component: #function, errorString: "We should have a model here")
    1.64 +            return
    1.65 +        }
    1.66 +
    1.67          //BUFF: TODO
    1.68          //        if let vm = viewModel {
    1.69          //            if vm.filterEnabled {
    1.70 @@ -242,6 +237,21 @@
    1.71          //            }
    1.72          //            self.textFilterButton.isEnabled = vm.filterEnabled
    1.73          //        }
    1.74 +
    1.75 +        textFilterButton.isEnabled = vm.isFilterEnabled
    1.76 +        if textFilterButton.isEnabled {
    1.77 +            enableFilterButton.image = UIImage(named: "unread-icon-active")
    1.78 +            updateFilterText()
    1.79 +        } else {
    1.80 +            textFilterButton.title = ""
    1.81 +            enableFilterButton.image = UIImage(named: "unread-icon")
    1.82 +        }
    1.83 +    }
    1.84 +
    1.85 +    func updateFilterText() {
    1.86 +        if let vm = model, let txt = vm.activeFilter?.text {
    1.87 +            textFilterButton.title = "Filter by: " + txt
    1.88 +        }
    1.89      }
    1.90  
    1.91      // MARK: - UITableViewDataSource
    1.92 @@ -318,10 +328,7 @@
    1.93          guard let vm = model, let searchText = searchController.searchBar.text else {
    1.94              return
    1.95          }
    1.96 -        vm.addSearchFilter(forSearchText: searchText)
    1.97 -        //        if let vm = model {
    1.98 -        //            vm.filterContentForSearchText(searchText: searchController.searchBar.text!, clear: false) //BUFF:
    1.99 -        //        }
   1.100 +        vm.setSearchFilter(forSearchText: searchText)
   1.101      }
   1.102  
   1.103      func didDismissSearchController(_ searchController: UISearchController) {
   1.104 @@ -355,10 +362,10 @@
   1.105  
   1.106      func updateView() {
   1.107          //BUFF: uncomment
   1.108 -        if let m = model, let filter = model?.folderToShow?.filter, filter.isDefault() {
   1.109 -            m.filterEnabled = false //BUFF: remove enabled. Folder knows if no filter is set
   1.110 -            handleButtonFilter(enabled: false)
   1.111 -        }
   1.112 +//        if let m = model, let filter = model?.folderToShow?.filter, filter.isDefault() {
   1.113 +////            m.isFilterEnabled = false //BUFF: remove enabled. Fixit
   1.114 +////            handleButtonFilter(enabled: m.isFilterEnabled)
   1.115 +//        }
   1.116          self.tableView.reloadData()
   1.117      }
   1.118  }
   1.119 @@ -553,7 +560,7 @@
   1.120                  return
   1.121              }
   1.122              destiny.appConfig = appConfig
   1.123 -            //            destiny.filterDelegate = model //BUFF: adjust FilterTableViewController
   1.124 +            destiny.filterDelegate = model
   1.125              destiny.inFolder = false
   1.126              destiny.filterEnabled = model?.folderToShow?.filter
   1.127              destiny.hidesBottomBarWhenPushed = true
     2.1 --- a/pEpForiOS/UI/EmailDisplay/EmailListViewModel.swift	Wed Oct 04 17:13:12 2017 +0200
     2.2 +++ b/pEpForiOS/UI/EmailDisplay/EmailListViewModel.swift	Wed Oct 04 18:40:07 2017 +0200
     2.3 @@ -20,20 +20,12 @@
     2.4  
     2.5  extension EmailListViewModel: FilterUpdateProtocol {
     2.6      public func addFilter(_ filter: Filter) {
     2.7 -        //BUFF: TODO
     2.8 -        //        if let updatee = folderToShow?.filter {
     2.9 -        //            updatee.and(filter: filter)
    2.10 -        //            folderToShow?.filter = updatee
    2.11 -        //            enabledFilter = updatee
    2.12 -        //        } else {
    2.13 -        //            folderToShow?.filter = filter
    2.14 -        //            enabledFilter = filter
    2.15 -        //        }
    2.16 -        //
    2.17 -        //        resetViewModel() //BUFF:
    2.18 +        setFilterViewFilter(filter: filter)
    2.19      }
    2.20  }
    2.21  
    2.22 +// MARK: - EmailListViewModel
    2.23 +
    2.24  class EmailListViewModel {
    2.25      let contactImageTool = IdentityImageTool()
    2.26      class Row {
    2.27 @@ -242,38 +234,62 @@
    2.28      
    2.29      // MARK: Filter
    2.30      
    2.31 -    public var isFilterEnabled = false //BUFF: public?
    2.32 -        public private(set) var enabledFilter : Filter? = nil //BUFF: get from folder
    2.33 +    public var isFilterEnabled = false {
    2.34 +        didSet {
    2.35 +            handleFilterEnabledSwitch()
    2.36 +        }
    2.37 +    }
    2.38 +    public var activeFilter : Filter? {
    2.39 +        get {
    2.40 +            guard let folder = folderToShow else {
    2.41 +                return nil
    2.42 +            }
    2.43 +            return folder.filter
    2.44 +        }
    2.45 +    }
    2.46 +    //BUFF: get from folder
    2.47      //    private var lastFilterEnabled: Filter?
    2.48      //    private var lastSearchFilter: Filter?
    2.49      //    private var searchFilter: Filter?
    2.50 -    private var filterViewSetFilter: Filter?
    2.51 -    
    2.52 -    //    public func enableFilter() {
    2.53 -    //        if let lastFilter = lastFilterEnabled {
    2.54 -    //            addFilter(lastFilter)
    2.55 -    //        } else {
    2.56 -    //            addFilter(Filter.unread())
    2.57 -    //        }
    2.58 -    //    }
    2.59 +    static let defaultFilterViewFilter = Filter.unread()
    2.60 +    private var _filterViewFilter: Filter = defaultFilterViewFilter
    2.61 +    private var filterViewFilter: Filter {
    2.62 +        get {
    2.63 +            if _filterViewFilter.isEmpty() {
    2.64 +                _filterViewFilter = EmailListViewModel.defaultFilterViewFilter
    2.65 +            }
    2.66 +            return _filterViewFilter
    2.67 +        }
    2.68 +        set {
    2.69 +            _filterViewFilter = newValue
    2.70 +        }
    2.71 +    }
    2.72 +
    2.73 +    private func setFilterViewFilter(filter:Filter) {
    2.74 +        if isFilterEnabled {
    2.75 +            let folderFilter = assuredFilterOfFolderToShow()
    2.76 +            folderFilter.without(filter: filterViewFilter)
    2.77 +            folderFilter.and(filter: filter)
    2.78 +            resetViewModel()
    2.79 +        }
    2.80 +        filterViewFilter = filter
    2.81 +    }
    2.82 +
    2.83 +    private func handleFilterEnabledSwitch() {
    2.84 +        let folderFilter = assuredFilterOfFolderToShow()
    2.85 +        if isFilterEnabled {
    2.86 +            folderFilter.and(filter: filterViewFilter)
    2.87 +        } else {
    2.88 +            folderFilter.without(filter: filterViewFilter)
    2.89 +        }
    2.90 +        resetViewModel()
    2.91 +    }
    2.92      
    2.93      public func setSearchFilter(forSearchText txt: String = "") { //BUFF: here
    2.94          if txt == "" {
    2.95              return
    2.96          }
    2.97 -        guard let folder = folderToShow else {
    2.98 -            Log.shared.errorAndCrash(component: #function, errorString: "No folder.")
    2.99 -            return
   2.100 -        }
   2.101 -        if folder.filter == nil{
   2.102 -            folder.resetFilter()
   2.103 -        }
   2.104 -
   2.105 -        guard let folderFilter = folder.filter else {
   2.106 -            Log.shared.errorAndCrash(component: #function,
   2.107 -                                     errorString: "We just set the filter but do not have one?")
   2.108 -            return
   2.109 -        }
   2.110 +        let folderFilter = assuredFilterOfFolderToShow()
   2.111          folderFilter.removeSearchFilter()
   2.112          let searchFilter = Filter.search(subject: txt)
   2.113          folderFilter.and(filter: searchFilter)
   2.114 @@ -288,6 +304,23 @@
   2.115          filter.removeSearchFilter()
   2.116          resetViewModel()
   2.117      }
   2.118 +
   2.119 +    private func assuredFilterOfFolderToShow() -> Filter {
   2.120 +        guard let folder = folderToShow else {
   2.121 +            Log.shared.errorAndCrash(component: #function, errorString: "No folder.")
   2.122 +            return Filter.unified()
   2.123 +        }
   2.124 +        if folder.filter == nil{
   2.125 +            folder.resetFilter()
   2.126 +        }
   2.127 +
   2.128 +        guard let folderFilter = folder.filter else {
   2.129 +            Log.shared.errorAndCrash(component: #function,
   2.130 +                                     errorString: "We just set the filter but do not have one?")
   2.131 +            return Filter.unified()
   2.132 +        }
   2.133 +        return folderFilter
   2.134 +    }
   2.135      
   2.136      //    public func resetFilters() {
   2.137      //        lastFilterEnabled = folderToShow?.filter