pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Tue, 14 Feb 2017 16:04:17 +0100
changeset 1696 a95b55a243a3
parent 1694 898bb5eaa391
child 1718 b3c04048bce9
permissions -rw-r--r--
IOS-441 cleanup
     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     struct UIState {
    23         var isSynching: Bool = false
    24     }
    25 
    26     var config: EmailListConfig?
    27     var state = UIState()
    28     let searchController = UISearchController(searchResultsController: nil)
    29     let cellsByMessageID = NSCache<NSString, EmailListViewCell>()
    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         if let messageID = cell.configureCell(config: config, indexPath: indexPath) {
   116             cellsByMessageID.setObject(cell, forKey: messageID as NSString)
   117         }
   118         return cell
   119     }
   120 
   121     // MARK: - UITableViewDelegate
   122 
   123     override func tableView(_ tableView: UITableView, editActionsForRowAt
   124         indexPath: IndexPath)-> [UITableViewRowAction]? {
   125 
   126         let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
   127         if let email = cell.messageAt(indexPath: indexPath, config: config) {
   128             let flagAction = createFlagAction(message: email, cell: cell)
   129             let deleteAction = createDeleteAction(message: email, cell: cell)
   130             let moreAction = createMoreAction(message: email, cell: cell)
   131             return [deleteAction, flagAction, moreAction]
   132         }
   133         return nil
   134     }
   135 
   136     // MARK: - Misc
   137 
   138     func createRowAction(cell: EmailListViewCell,
   139                          image: UIImage?, action: @escaping (UITableViewRowAction, IndexPath) -> Void,
   140                          title: String) -> UITableViewRowAction {
   141         let rowAction = UITableViewRowAction(
   142             style: .normal, title: title, handler: action)
   143 
   144         if let theImage = image {
   145             let iconColor = UIColor(patternImage: theImage)
   146             rowAction.backgroundColor = iconColor
   147         }
   148 
   149         return rowAction
   150     }
   151 
   152     func createFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   153         func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
   154             if message.imapFlags == nil {
   155                 Log.warn(component: #function, content: "message.imapFlags == nil")
   156             }
   157             if cell.isFlagged(message: message) {
   158                 message.imapFlags?.flagged = false
   159             } else {
   160                 message.imapFlags?.flagged = true
   161             }
   162             message.save()
   163             self.tableView.reloadRows(at: [indexPath], with: .none)
   164         }
   165 
   166         var title = "\n\nFlag".localized
   167         if message.imapFlags?.flagged ?? true {
   168             title = "\n\nUnFlag".localized
   169         }
   170 
   171         return createRowAction(
   172             cell: cell, image: UIImage(named: "swipe-flag"), action: action, title: title)
   173     }
   174 
   175     func createDeleteAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   176         func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
   177             guard let message = cell.messageAt(indexPath: indexPath, config: self.config) else {
   178                 return
   179             }
   180 
   181             message.delete() // mark for deletion/trash
   182             message.save()
   183             self.tableView.reloadData()
   184         }
   185 
   186         return createRowAction(
   187             cell: cell, image: UIImage(named: "swipe-trash"), action: action,
   188             title: "\n\nDelete".localized)
   189     }
   190 
   191     func createMarkAsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   192         func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
   193             if cell.haveSeen(message: message) {
   194                 message.imapFlags?.seen = false
   195             } else {
   196                 message.imapFlags?.seen = true
   197             }
   198             self.tableView.reloadRows(at: [indexPath], with: .none)
   199         }
   200 
   201         var title = NSLocalizedString(
   202             "Unread", comment: "Unread button title in swipe action on EmailListViewController")
   203         if !cell.haveSeen(message: message) {
   204             title = NSLocalizedString(
   205                 "Read", comment: "Read button title in swipe action on EmailListViewController")
   206         }
   207 
   208         let isReadAction = createRowAction(cell: cell, image: nil, action: action,
   209                                            title: title)
   210         isReadAction.backgroundColor = UIColor.blue
   211 
   212         return isReadAction
   213     }
   214 
   215     func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   216         func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
   217             self.showMoreActionSheet(cell: cell)
   218         }
   219 
   220         return createRowAction(
   221             cell: cell, image: UIImage(named: "swipe-more"), action: action,
   222             title: "\n\nMore".localized)
   223     }
   224 
   225     // MARK: - Action Sheet
   226 
   227     func showMoreActionSheet(cell: EmailListViewCell) {
   228         let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
   229         alertControler.view.tintColor = .pEpGreen
   230         let cancelAction = createCancelAction()
   231         let replyAction = createReplyAction(cell: cell)
   232         let replyAllAction = createReplyAllAction(cell: cell)
   233         let forwardAction = createForwardAction(cell: cell)
   234         let markAction = createMarkAction()
   235         alertControler.addAction(cancelAction)
   236         alertControler.addAction(replyAction)
   237         alertControler.addAction(replyAllAction)
   238         alertControler.addAction(forwardAction)
   239         alertControler.addAction(markAction)
   240         if let popoverPresentationController = alertControler.popoverPresentationController {
   241             popoverPresentationController.sourceView = cell
   242         }
   243         present(alertControler, animated: true, completion: nil)
   244     }
   245 
   246     // MARK: - Action Sheet Actions
   247 
   248     func createCancelAction() -> UIAlertAction {
   249         return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
   250     }
   251 
   252     func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
   253         return UIAlertAction(title: "Reply", style: .default) { (action) in
   254             // self.performSegue(withIdentifier: self.segueCompose, sender: cell)
   255             self.performSegue(withIdentifier: .segueCompose, sender: cell)
   256         }
   257     }
   258 
   259     func createReplyAllAction(cell: EmailListViewCell) ->  UIAlertAction {
   260         return UIAlertAction(title: "Reply All", style: .default) { (action) in
   261             self.performSegue(withIdentifier: .segueReplyAll, sender: cell)
   262         }
   263     }
   264 
   265     func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
   266         return UIAlertAction(title: "Forward", style: .default) { (action) in
   267             //self.performSegue(withIdentifier: self.segueCompose, sender: cell)
   268             self.performSegue(withIdentifier: .segueForward, sender: cell)
   269         }
   270     }
   271 
   272     func createMarkAction() -> UIAlertAction {
   273         return UIAlertAction(title: "Mark", style: .default) { (action) in
   274         }
   275     }
   276 
   277     // MARK: - Content Search
   278 
   279     func filterContentForSearchText(searchText: String) {
   280 
   281     }
   282 
   283 }
   284 
   285 extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
   286     public func updateSearchResults(for searchController: UISearchController) {
   287         filterContentForSearchText(searchText: searchController.searchBar.text!)
   288     }
   289 
   290     func didDismissSearchController(_ searchController: UISearchController) {
   291     }
   292 }
   293 
   294 // MARK: - Navigation
   295 
   296 extension EmailListViewController: SegueHandlerType {
   297 
   298     // MARK: - SegueHandlerType
   299 
   300     enum SegueIdentifier: String {
   301         case segueAddNewAccount
   302         case segueEditAccounts
   303         case segueShowEmail
   304         case segueCompose
   305         case segueReplyAll
   306         case segueForward
   307         case noSegue
   308     }
   309 
   310     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   311         switch segueIdentifier(for: segue) {
   312         case .segueReplyAll:
   313             if let nav = segue.destination as? UINavigationController,
   314                 let destination = nav.topViewController as? ComposeTableViewController,
   315                 let cell = sender as? EmailListViewCell,
   316                 let indexPath = self.tableView.indexPath(for: cell),
   317                 let email = cell.messageAt(indexPath: indexPath, config: config) {
   318                 destination.composeMode = .replyAll
   319                 destination.appConfig = config?.appConfig
   320                 destination.originalMessage = email
   321             }
   322             break
   323         case .segueShowEmail:
   324             if 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) {
   328                 vc.appConfig = config?.appConfig
   329                 vc.message = email
   330             }
   331             break
   332         case .segueForward:
   333             if let nav = segue.destination as? UINavigationController,
   334                 let destination = nav.topViewController as? ComposeTableViewController,
   335                 let cell = sender as? EmailListViewCell,
   336                 let indexPath = self.tableView.indexPath(for: cell),
   337                 let email = cell.messageAt(indexPath: indexPath, config: config) {
   338                 destination.composeMode = .forward
   339                 destination.appConfig = config?.appConfig
   340                 destination.originalMessage = email
   341             }
   342             break
   343         case .segueAddNewAccount, .segueEditAccounts, .segueCompose, .noSegue:
   344             break
   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, deletedMessagesAreContained: true) {
   352             if let msg = messageFolder as? Message {
   353                 if msg.isOriginal {
   354                     // new message has arrived
   355                     if let index = folder.indexOf(message: msg) {
   356                         let ip = IndexPath(row: index, section: 0)
   357                         tableView.insertRows(at: [ip], with: .automatic)
   358                     } else {
   359                         tableView.reloadData()
   360                     }
   361                 } else if msg.isGhost {
   362                     if let cell = cellsByMessageID.object(forKey: msg.uuid as NSString) {
   363                         if let ip = tableView.indexPath(for: cell) {
   364                             tableView.deleteRows(at: [ip], with: .automatic)
   365                         }
   366                     }
   367                 } else {
   368                     // other flags than delete must have been changed
   369                     if let cell = cellsByMessageID.object(forKey: msg.uuid as NSString) {
   370                         cell.updateFlags(message: message)
   371                     }
   372                 }
   373             }
   374         }
   375     }
   376 }
   377 
   378 // MARK: - MessageFolderDelegate
   379 
   380 extension EmailListViewController: MessageFolderDelegate {
   381     func didChange(messageFolder: MessageFolder) {
   382         GCD.onMainWait {
   383             self.didChangeInternal(messageFolder: messageFolder)
   384         }
   385     }
   386 }