pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Tue, 24 Jan 2017 10:47:08 +0100
changeset 1507 7270fb02923c
parent 1506 11d3b3dd5865
child 1510 2b4d784a6800
permissions -rw-r--r--
IOS-417 consolidating creation of swipe actions
     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 _ = 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 = createFlagAction(message: email, cell: cell)
   138             let deleteAction = createDeleteAction(message: email, cell: cell)
   139             let moreAction = createMoreAction(message: email, cell: cell)
   140             return [deleteAction,isFlagAction,moreAction]
   141         }
   142         return nil
   143     }
   144 
   145     // MARK: - Misc
   146 
   147     func createRowAction(cell: EmailListViewCell,
   148         image: UIImage?, action: @escaping (UITableViewRowAction, IndexPath) -> Void,
   149         titleBlock: () -> String) -> UITableViewRowAction {
   150         let rowAction = UITableViewRowAction(style: .normal, title: titleBlock(),
   151                                              handler: action)
   152 
   153         if let theImage = image {
   154             let iconColor = UIColor(patternImage: theImage)
   155             rowAction.backgroundColor = iconColor
   156         }
   157 
   158         return rowAction
   159     }
   160 
   161     func createFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   162         func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
   163             if (cell.isImportant(message: message)) {
   164                 message.imapFlags?.flagged = false
   165             } else {
   166                 message.imapFlags?.flagged = true
   167             }
   168             message.save()
   169             self.tableView.reloadRows(at: [indexPath], with: .none)
   170         }
   171         func title() -> String {
   172             var title = "Flag".localized
   173             if (message.imapFlags?.flagged ?? true) {
   174                 title = "UnFlag".localized
   175             }
   176             return title
   177         }
   178 
   179         let image = UIImage(named: "swipe-flag")
   180 
   181         return createRowAction(cell: cell, image: image, action: action, titleBlock: title)
   182     }
   183 
   184     func createDeleteAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   185         func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
   186             guard let message = cell.messageAt(indexPath: indexPath, config: self.config) else {
   187                 return
   188             }
   189             message.imapFlags?.deleted = true
   190             message.save()
   191             self.tableView.reloadRows(at: [indexPath], with: .none)
   192         }
   193         func title() -> String {
   194             return "Delete".localized
   195         }
   196 
   197         let image = UIImage(named: "swipe-trash")
   198 
   199         return createRowAction(cell: cell, image: image, action: action, titleBlock: title)
   200     }
   201 
   202     func createMarkAsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   203         func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
   204             if (cell.isRead(message: message)) {
   205                 message.imapFlags?.seen = false
   206             } else {
   207                 message.imapFlags?.seen = true
   208             }
   209             self.tableView.reloadRows(at: [indexPath], with: .none)
   210         }
   211         func title() -> String {
   212             if (cell.isRead(message: message)) {
   213                 return NSLocalizedString(
   214                     "Unread",
   215                     comment: "Unread button title in swipe action on EmailListViewController")
   216             } else {
   217                 return NSLocalizedString(
   218                     "Read",
   219                     comment: "Read button title in swipe action on EmailListViewController")
   220             }
   221         }
   222 
   223         let isReadAction = createRowAction(cell: cell, image: nil, action: action,
   224                                            titleBlock: title)
   225         isReadAction.backgroundColor = UIColor.blue
   226 
   227         return isReadAction
   228     }
   229     
   230     func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   231         let moreCompletitionHandler :(UITableViewRowAction, IndexPath) -> Void = {(action, indexPath) in
   232             self.showMoreActionSheet(cell: cell)
   233         }
   234         let moreAction = UITableViewRowAction(style: .normal, title: "          ", handler: moreCompletitionHandler)
   235         let swipeMoreImage = UIImage(named: "swipe-more")
   236         let moreIconColor = UIColor(patternImage: swipeMoreImage!)
   237         moreAction.backgroundColor = moreIconColor
   238         return moreAction
   239     }
   240     
   241     // MARK: - Action Sheet
   242     
   243     func showMoreActionSheet(cell: EmailListViewCell) {
   244         let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
   245         alertControler.view.tintColor = .pEpGreen
   246         let cancelAction = createCancelAction()
   247         let replyAction = createReplyAction(cell: cell)
   248         let forwardAction = createForwardAction(cell: cell)
   249         let markAction = createMarkAction()
   250         alertControler.addAction(cancelAction)
   251         alertControler.addAction(replyAction)
   252         alertControler.addAction(forwardAction)
   253         alertControler.addAction(markAction)
   254         if let popoverPresentationController = alertControler.popoverPresentationController {
   255             popoverPresentationController.sourceView = cell
   256         }
   257         present(alertControler, animated: true, completion: nil)
   258     }
   259     
   260     // MARK: - Action Sheet Actions
   261 
   262     func createCancelAction() -> UIAlertAction {
   263       return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
   264     }
   265     
   266     func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
   267         return UIAlertAction(title: "Reply", style: .default) { (action) in
   268            // self.performSegue(withIdentifier: self.segueCompose, sender: cell)
   269             self.performSegue(withIdentifier: .segueCompose, sender: cell)
   270         }
   271     }
   272     
   273     func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
   274         return UIAlertAction(title: "Forward", style: .default) { (action) in
   275             //self.performSegue(withIdentifier: self.segueCompose, sender: cell)
   276             self.performSegue(withIdentifier: .segueCompose, sender: cell)
   277         }
   278     }
   279     
   280     func createMarkAction() -> UIAlertAction {
   281         return UIAlertAction(title: "Mark", style: .default) { (action) in
   282         }
   283     }
   284     
   285     // MARK: - Content Search
   286     
   287     func filterContentForSearchText(searchText: String) {
   288         
   289     }
   290  
   291     // MARK: - Actions
   292 //    @IBAction func unwindToEmailList(for unwindSegue: UIStoryboardSegue) {
   293 //        
   294 //    }
   295    
   296 }
   297 
   298 extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
   299     public func updateSearchResults(for searchController: UISearchController) {
   300         filterContentForSearchText(searchText: searchController.searchBar.text!)
   301     }
   302     
   303     func didDismissSearchController(_ searchController: UISearchController) {
   304     }
   305 }
   306 
   307 // MARK: - Navigation
   308 
   309 extension EmailListViewController: SegueHandlerType {
   310     
   311     // MARK: - SegueHandlerType
   312     
   313     enum SegueIdentifier: String {
   314         case segueAddNewAccount
   315         case segueEditAccounts
   316         case segueShowEmail
   317         case segueCompose
   318         case noSegue
   319     }
   320     
   321     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   322         switch segueIdentifier(for: segue) {
   323         case .segueCompose:
   324             //let destination = segue.destination as! ComposeTableViewController
   325             // destination.appConfig = config.appConfig
   326             //            if let draft = draftMessageToCompose {
   327             //                draft.imapFlags?.seen = true
   328             //
   329             //                destination.originalMessage = draft
   330             //                destination.composeMode = .draft
   331         //            }
   332             break
   333         case .segueShowEmail:
   334             guard
   335                 let vc = segue.destination as? EmailViewController,
   336                 let cell = sender as? EmailListViewCell,
   337                 let indexPath = self.tableView.indexPath(for: cell),
   338                 let email = cell.messageAt(indexPath: indexPath, config: config) else {
   339                     return
   340             }
   341             vc.appConfig = config?.appConfig
   342             vc.message = email
   343             break
   344         default: ()
   345         }
   346     }
   347 
   348     func didChangeInternal(messageFolder: MessageFolder) {
   349         if let folder = config?.folder,
   350             let message = messageFolder as? Message,
   351             folder.contains(message: message) {
   352             if let msg = messageFolder as? Message {
   353                 if msg.isOriginal {
   354                     self.tableView.reloadData()
   355                 }
   356             }
   357         }
   358     }
   359 }
   360 
   361 // MARK: - MessageFolderDelegate
   362 
   363 extension EmailListViewController: MessageFolderDelegate {
   364     func didChange(messageFolder: MessageFolder) {
   365         GCD.onMain {
   366             self.didChangeInternal(messageFolder: messageFolder)
   367         }
   368     }
   369 }