IOS-92 search working without vm
authorXavier Algarra <xavier@pep-project.org>
Tue, 27 Jun 2017 15:13:24 +0200
changeset 2344544503156670
parent 2343 904a4ced118a
child 2345 4dd2c835d36f
IOS-92 search working without vm
pEpForiOS.xcodeproj/project.pbxproj
pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
pEpForiOS/UI/EmailDisplay/EmailListViewModel.swift
pEpForiOS/UI/Filter/ViewModel/FilterCellViewModel.swift
     1.1 --- a/pEpForiOS.xcodeproj/project.pbxproj	Tue Jun 27 09:54:24 2017 +0200
     1.2 +++ b/pEpForiOS.xcodeproj/project.pbxproj	Tue Jun 27 15:13:24 2017 +0200
     1.3 @@ -1046,6 +1046,7 @@
     1.4  		43ED53621CC77F95006AB156 /* EmailDisplay */ = {
     1.5  			isa = PBXGroup;
     1.6  			children = (
     1.7 +				B74D08BF1F013F15003D092C /* ViewModel */,
     1.8  				430D73601E9CBD0600EA6FA9 /* Background */,
     1.9  				4323FE871E83E502006785E1 /* Util */,
    1.10  				220DCE281E0AB4DE002FE716 /* Cells */,
    1.11 @@ -1150,6 +1151,14 @@
    1.12  			name = MimeTypes;
    1.13  			sourceTree = "<group>";
    1.14  		};
    1.15 +		B74D08BF1F013F15003D092C /* ViewModel */ = {
    1.16 +			isa = PBXGroup;
    1.17 +			children = (
    1.18 +				B75FF00A1EFD420F00C57289 /* EmailListViewModel.swift */,
    1.19 +			);
    1.20 +			name = ViewModel;
    1.21 +			sourceTree = "<group>";
    1.22 +		};
    1.23  		B74F81031EB0E20700519FCC /* AccountsViewModel */ = {
    1.24  			isa = PBXGroup;
    1.25  			children = (
     2.1 --- a/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Tue Jun 27 09:54:24 2017 +0200
     2.2 +++ b/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Tue Jun 27 15:13:24 2017 +0200
     2.3 @@ -25,6 +25,7 @@
     2.4          var isSynching: Bool = false
     2.5      }
     2.6  
     2.7 +    var viewModel = EmailListViewModel()
     2.8      var config: EmailListConfig?
     2.9      var state = UIState()
    2.10      let searchController = UISearchController(searchResultsController: nil)
    2.11 @@ -158,6 +159,7 @@
    2.12                              cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    2.13          let cell = tableView.dequeueReusableCell(
    2.14              withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
    2.15 +        //mantener el configure cell para tal de no generar un vm para celdas
    2.16          if let message = cell.configureCell(config: config, indexPath: indexPath) {
    2.17              associate(message: message, toCell: cell)
    2.18          }
    2.19 @@ -310,7 +312,6 @@
    2.20  
    2.21      func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
    2.22          return UIAlertAction(title: "Forward", style: .default) { (action) in
    2.23 -            //self.performSegue(withIdentifier: self.segueCompose, sender: cell)
    2.24              self.performSegue(withIdentifier: .segueForward, sender: cell)
    2.25          }
    2.26      }
    2.27 @@ -322,18 +323,31 @@
    2.28  
    2.29      // MARK: - Content Search
    2.30  
    2.31 -    func filterContentForSearchText(searchText: String) {
    2.32 -
    2.33 +    func filterContentForSearchText(searchText: String? = nil, clear: Bool) {
    2.34 +        if clear {
    2.35 +            updateFilter(filter: Filter.unified())
    2.36 +        } else {
    2.37 +            if let text = searchText, text != "" {
    2.38 +                let f = Filter.search(subject: text)
    2.39 +                if filterEnabled {
    2.40 +                    f.and(filter: Filter.unread())
    2.41 +                    updateFilter(filter: f)
    2.42 +                }
    2.43 +                if config != nil {
    2.44 +                    updateFilter(filter: f)
    2.45 +                }
    2.46 +            }
    2.47 +        }
    2.48      }
    2.49 -
    2.50  }
    2.51  
    2.52  extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
    2.53      public func updateSearchResults(for searchController: UISearchController) {
    2.54 -        filterContentForSearchText(searchText: searchController.searchBar.text!)
    2.55 +        filterContentForSearchText(searchText: searchController.searchBar.text!, clear: false)
    2.56      }
    2.57  
    2.58      func didDismissSearchController(_ searchController: UISearchController) {
    2.59 +        filterContentForSearchText(clear: true)
    2.60      }
    2.61  }
    2.62  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/pEpForiOS/UI/EmailDisplay/EmailListViewModel.swift	Tue Jun 27 15:13:24 2017 +0200
     3.3 @@ -0,0 +1,60 @@
     3.4 +//
     3.5 +//  EmailListViewModel.swift
     3.6 +//  pEpForiOS
     3.7 +//
     3.8 +//  Created by Xavier Algarra on 23/06/2017.
     3.9 +//  Copyright © 2017 p≡p Security S.A. All rights reserved.
    3.10 +//
    3.11 +
    3.12 +import Foundation
    3.13 +import MessageModel
    3.14 +
    3.15 +public class EmailListViewModel {
    3.16 +
    3.17 +    var folderToShow: Folder?
    3.18 +    let cellsInUse = NSCache<NSString, EmailListViewCell>()
    3.19 +
    3.20 +    public init() {
    3.21 +        MessageModelConfig.messageFolderDelegate = self
    3.22 +    }
    3.23 +
    3.24 +    var count : Int {
    3.25 +        if let folder = folderToShow {
    3.26 +            return folder.messageCount()
    3.27 +        }
    3.28 +        return 0
    3.29 +    }
    3.30 +
    3.31 +    subscript(index: Int) -> Message? {
    3.32 +        get {
    3.33 +            if let folder = folderToShow, folder.messageCount() >= index {
    3.34 +                return self.folderToShow?.messageAt(index: index)
    3.35 +            }
    3.36 +            return nil
    3.37 +        }
    3.38 +    }
    3.39 +
    3.40 +    func associate(cell: EmailListViewCell, position: Int) {
    3.41 +        if let message = self[position] {
    3.42 +            cellsInUse.setObject(cell, forKey: keyFor(message: message))
    3.43 +        }
    3.44 +
    3.45 +    }
    3.46 +
    3.47 +    func keyFor(message: Message) -> NSString {
    3.48 +        let parentName = message.parent?.name ?? "unknown"
    3.49 +        return "\(message.uuid) \(parentName) \(message.uuid)" as NSString
    3.50 +    }
    3.51 +
    3.52 +    
    3.53 +
    3.54 +}
    3.55 +
    3.56 +
    3.57 +extension EmailListViewModel: MessageFolderDelegate {
    3.58 +    public func didChange(messageFolder: MessageFolder) {
    3.59 +        //GCD.onMainWait {
    3.60 +        //    self.didChangeInternal(messageFolder: messageFolder)
    3.61 +        //}
    3.62 +    }
    3.63 +}
     4.1 --- a/pEpForiOS/UI/Filter/ViewModel/FilterCellViewModel.swift	Tue Jun 27 09:54:24 2017 +0200
     4.2 +++ b/pEpForiOS/UI/Filter/ViewModel/FilterCellViewModel.swift	Tue Jun 27 15:13:24 2017 +0200
     4.3 @@ -61,7 +61,7 @@
     4.4              }
     4.5              icon = image
     4.6              title = NSLocalizedString("For me", comment: "title unread filter cell")
     4.7 -            //filter = Filter.toAddress(address: <#T##String#>)
     4.8 +            //filter = Filter.toAddress(address: String)
     4.9              //enabled = filter?.ofType(type: .unread) ?? false
    4.10  
    4.11          case .forMeCc:
    4.12 @@ -72,7 +72,7 @@
    4.13              }
    4.14              icon = image
    4.15              title = NSLocalizedString("For me in copy", comment: "title unread filter cell")
    4.16 -            //filter = Filter.toAddress(address: <#T##String#>)
    4.17 +            //filter = Filter.toAddress(address: String)
    4.18              //enabled = filter?.ofType(type: .unread) ?? false
    4.19  
    4.20          case .attachment:
    4.21 @@ -80,6 +80,11 @@
    4.22              self.title = NSLocalizedString("Attachments", comment: "title attachments filter cell")
    4.23              filter = Filter.attachment()
    4.24              enabled = filter?.ofType(type: .attachment) ?? false
    4.25 +
    4.26 +        case .search:
    4.27 +            self.icon = FlagImages.create(imageSize: squareSize).notSeenImage
    4.28 +            self.title = ""
    4.29 +
    4.30          }
    4.31      }
    4.32