pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author igor <igor@pep-project.org>
Fri, 13 Jan 2017 10:44:06 +0100
changeset 1414 63f17f38ca24
parent 1408 d4f3ec47c2c7
child 1438 a95b5815208d
permissions -rw-r--r--
IOS-382 Fixed
     1 //
     2 //  EmailListViewController.swift
     3 //  pEpForiOS
     4 //
     5 //  Created by Dirk Zimmermann on 16/04/16.
     6 //  Copyright © 2016 p≡p Security S.A. All rights reserved.
     7 //
     8 
     9 import Foundation
    10 import UIKit
    11 import CoreData
    12 import MessageModel
    13 
    14 struct EmailListConfig {
    15     var appConfig: AppConfig?
    16 
    17     /** The folder to display, if it exists */
    18     var folder: Folder?
    19 }
    20 
    21 class EmailListViewController: UITableViewController {
    22     
    23     struct UIState {
    24         var isSynching: Bool = false
    25     }
    26 
    27     var config: EmailListConfig?
    28     var state = UIState()
    29     let searchController = UISearchController(searchResultsController: nil)
    30 
    31     override func viewDidLoad() {
    32         super.viewDidLoad()
    33         
    34         title = "EmailList.title".localized
    35         UIHelper.emailListTableHeight(self.tableView)
    36         addSearchBar()
    37     }
    38 
    39     override func viewWillAppear(_ animated: Bool) {
    40         super.viewWillAppear(animated)
    41         
    42         if MiscUtil.isUnitTest() {
    43             return
    44         }
    45 
    46         initialConfig()
    47         updateModel()
    48 
    49         MessageModelConfig.messageFolderDelegate = self
    50     }
    51 
    52     override func viewWillDisappear(_ animated: Bool) {
    53         super.viewWillDisappear(animated)
    54         MessageModelConfig.messageFolderDelegate = nil
    55     }
    56     
    57     func initialConfig() {
    58         guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
    59             return
    60         }
    61 
    62         if config == nil {
    63             config = EmailListConfig(appConfig: appDelegate.appConfig, folder: Folder.unifiedInbox())
    64         }
    65         if Account.all().isEmpty {
    66             performSegue(withIdentifier:.segueAddNewAccount, sender: self)
    67         }
    68     }
    69     
    70     func addSearchBar() {
    71         searchController.searchResultsUpdater = self
    72         searchController.dimsBackgroundDuringPresentation = false
    73         searchController.delegate = self
    74         definesPresentationContext = true
    75         tableView.tableHeaderView = searchController.searchBar
    76         tableView.setContentOffset(CGPoint(x: 0.0, y: 40.0), animated: false)
    77     }
    78 
    79     
    80     @IBAction func showUnreadButtonTapped(_ sender: UIBarButtonItem) {}
    81     
    82     func updateModel() {
    83         tableView.reloadData()
    84     }
    85 
    86     // MARK: - UI State
    87 
    88     func updateUI() {
    89         UIApplication.shared.isNetworkActivityIndicatorVisible = state.isSynching
    90         if !state.isSynching {
    91             refreshControl?.endRefreshing()
    92         }
    93     }
    94 
    95     // MARK: - UITableViewDataSource
    96 
    97     override func numberOfSections(in tableView: UITableView) -> Int {
    98         if let _ = config?.folder {
    99             return 1
   100         }
   101         return 0
   102     }
   103 
   104     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   105         if let fol = config?.folder  {
   106             return fol.messageCount()
   107         }
   108         return 0
   109     }
   110 
   111     override func tableView(_ tableView: UITableView,
   112                             cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   113         let cell = tableView.dequeueReusableCell(
   114             withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
   115         cell.configureCell(indexPath: indexPath, config: config)
   116         return cell
   117     }
   118 
   119     // MARK: - UITableViewDelegate
   120 
   121     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   122         let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
   123 
   124         if let fol = config?.folder {
   125             if fol.folderType == .drafts {
   126                 //performSegue(withIdentifier: .segueCompose, sender: cell)
   127                 return
   128             }
   129         }
   130         //performSegue(withIdentifier: .segueShowEmail, sender: cell)
   131     }
   132 
   133     override func tableView(_ tableView: UITableView, editActionsForRowAt
   134         indexPath: IndexPath)-> [UITableViewRowAction]? {
   135         let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
   136         if let email = cell.messageAt(indexPath: indexPath, config: config) {
   137             let isFlagAction = createIsFlagAction(message: email, cell: cell)
   138             let deleteAction = createDeleteAction(cell)
   139             //let isReadAction = createIsReadAction(message: email, cell: cell)
   140             let moreAction = createMoreAction(message: email, cell: cell)
   141             return [deleteAction,isFlagAction,moreAction]
   142         }
   143         return nil
   144     }
   145 
   146     // MARK: - Misc
   147 
   148     func createIsFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   149         // preparing action to trigger when user swipe
   150         let isFlagCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
   151             { (action, indexPath) in
   152                 if (cell.isImportant(message: message)) {
   153                     message.imapFlags?.flagged = false
   154 
   155                 } else {
   156                     message.imapFlags?.flagged = true
   157                 }
   158                 self.tableView.reloadRows(at: [indexPath], with: .none)
   159         }
   160         // creating the action
   161         let isFlagAction = UITableViewRowAction(style: .normal, title: "          ",
   162                                                 handler: isFlagCompletionHandler)
   163         // changing default action color
   164         let swipeFlagImage = UIImage(named: "swipe-flag")
   165         let flagIconColor = UIColor(patternImage: swipeFlagImage!)
   166         isFlagAction.backgroundColor = flagIconColor
   167 
   168         return isFlagAction
   169     }
   170 
   171     func createDeleteAction (_ cell: EmailListViewCell) -> UITableViewRowAction {
   172 
   173         // preparing the title action to show when user swipe
   174 
   175         let deleteCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
   176             { (action, indexPath) in
   177                 let message = cell.messageAt(indexPath: indexPath, config: self.config)
   178                 message?.imapFlags?.deleted = true
   179         }
   180 
   181         // creating the action
   182         let deleteAction = UITableViewRowAction(style: .normal, title: "          ",
   183                                                 handler: deleteCompletionHandler)
   184         let swipeTrashImage = UIImage(named: "swipe-trash")
   185         let trashIconColor = UIColor(patternImage: swipeTrashImage!)
   186         deleteAction.backgroundColor = trashIconColor
   187         return deleteAction
   188     }
   189 
   190     func createIsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   191         // preparing the title action to show when user swipe
   192         var localizedisReadTitle = " "
   193         if (cell.isRead(message: message)) {
   194             localizedisReadTitle = NSLocalizedString(
   195                 "Unread",
   196                 comment: "Unread button title in swipe action on EmailListViewController")
   197         } else {
   198             localizedisReadTitle = NSLocalizedString(
   199                 "Read",
   200                 comment: "Read button title in swipe action on EmailListViewController")
   201         }
   202 
   203         // creating the action
   204         let isReadCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
   205             { (action, indexPath) in
   206                 if (cell.isRead(message: message)) {
   207                     message.imapFlags?.seen = false
   208                 } else {
   209                     message.imapFlags?.seen = true
   210                 }
   211                 self.tableView.reloadRows(at: [indexPath], with: .none)
   212         }
   213         let isReadAction = UITableViewRowAction(style: .default, title: localizedisReadTitle,
   214                                                 handler: isReadCompletionHandler)
   215         isReadAction.backgroundColor = UIColor.blue
   216 
   217         return isReadAction
   218     }
   219     
   220     func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   221         let moreCompletitionHandler :(UITableViewRowAction, IndexPath) -> Void = {(action, indexPath) in
   222             self.showMoreActionSheet(cell: cell)
   223         }
   224         let moreAction = UITableViewRowAction(style: .normal, title: "          ", handler: moreCompletitionHandler)
   225         let swipeMoreImage = UIImage(named: "swipe-more")
   226         let moreIconColor = UIColor(patternImage: swipeMoreImage!)
   227         moreAction.backgroundColor = moreIconColor
   228         return moreAction
   229     }
   230     
   231     // MARK: - Action Sheet
   232     
   233     func showMoreActionSheet(cell: EmailListViewCell) {
   234         let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
   235         let cancelAction = createCancelAction()
   236         let replyAction = createReplyAction(cell: cell)
   237         let forwardAction = createForwardAction(cell: cell)
   238         let markAction = createMarkAction()
   239         alertControler.addAction(cancelAction)
   240         alertControler.addAction(replyAction)
   241         alertControler.addAction(forwardAction)
   242         alertControler.addAction(markAction)
   243         if let popoverPresentationController = alertControler.popoverPresentationController {
   244             popoverPresentationController.sourceView = cell
   245         }
   246         present(alertControler, animated: true, completion: nil)
   247     }
   248     
   249     // MARK: - Action Sheet Actions
   250 
   251     func createCancelAction() -> UIAlertAction {
   252       return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
   253     }
   254     
   255     func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
   256         return UIAlertAction(title: "Reply", style: .default) { (action) in
   257            // self.performSegue(withIdentifier: self.segueCompose, sender: cell)
   258             self.performSegue(withIdentifier: .segueCompose, sender: cell)
   259         }
   260     }
   261     
   262     func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
   263         return UIAlertAction(title: "Forward", style: .default) { (action) in
   264             //self.performSegue(withIdentifier: self.segueCompose, sender: cell)
   265             self.performSegue(withIdentifier: .segueCompose, sender: cell)
   266         }
   267     }
   268     
   269     func createMarkAction() -> UIAlertAction {
   270         return UIAlertAction(title: "Mark", style: .default) { (action) in
   271         }
   272     }
   273     
   274     // MARK: - Content Search
   275     
   276     func filterContentForSearchText(searchText: String) {
   277         
   278     }
   279  
   280     // MARK: - Actions
   281 //    @IBAction func unwindToEmailList(for unwindSegue: UIStoryboardSegue) {
   282 //        
   283 //    }
   284    
   285 }
   286 
   287 extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
   288     public func updateSearchResults(for searchController: UISearchController) {
   289         filterContentForSearchText(searchText: searchController.searchBar.text!)
   290     }
   291     
   292     func didDismissSearchController(_ searchController: UISearchController) {
   293     }
   294 }
   295 
   296 // MARK: - Navigation
   297 
   298 extension EmailListViewController: SegueHandlerType {
   299     
   300     // MARK: - SegueHandlerType
   301     
   302     enum SegueIdentifier: String {
   303         case segueAddNewAccount
   304         case segueEditAccounts
   305         case segueShowEmail
   306         case segueCompose
   307         case noSegue
   308     }
   309     
   310     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   311         switch segueIdentifier(for: segue) {
   312         case .segueCompose:
   313             //let destination = segue.destination as! ComposeTableViewController
   314             // destination.appConfig = config.appConfig
   315             //            if let draft = draftMessageToCompose {
   316             //                draft.imapFlags?.seen = true
   317             //
   318             //                destination.originalMessage = draft
   319             //                destination.composeMode = .draft
   320         //            }
   321             break
   322         case .segueShowEmail:
   323             guard
   324                 let vc = segue.destination as? EmailViewController,
   325                 let cell = sender as? EmailListViewCell,
   326                 let indexPath = self.tableView.indexPath(for: cell),
   327                 let email = cell.messageAt(indexPath: indexPath, config: config) else {
   328                     return
   329             }
   330             vc.appConfig = config?.appConfig
   331             vc.message = email
   332             break
   333         default: ()
   334         }
   335     }
   336 
   337     func didChangeInternal(messageFolder: MessageFolder) {
   338         if let folder = config?.folder,
   339             let message = messageFolder as? Message,
   340             folder.contains(message: message) {
   341             if let msg = messageFolder as? Message {
   342                 if msg.isOriginal {
   343                     self.tableView.reloadData()
   344                 }
   345             }
   346         }
   347     }
   348 }
   349 
   350 // MARK: - MessageFolderDelegate
   351 
   352 extension EmailListViewController: MessageFolderDelegate {
   353     func didChange(messageFolder: MessageFolder) {
   354         GCD.onMain {
   355             self.didChangeInternal(messageFolder: messageFolder)
   356         }
   357     }
   358 }