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