pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Mon, 13 Mar 2017 17:37:36 +0100
changeset 1826 f8fe61819ba6
parent 1737 8d234ddb4824
child 1832 8f9685564441
permissions -rw-r--r--
IOS-469 handling pEp status colors more systematically
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 {
dirk@583
    22
    struct UIState {
dirk@583
    23
        var isSynching: Bool = false
dirk@583
    24
    }
dirk@30
    25
dirk@1344
    26
    var config: EmailListConfig?
dirk@275
    27
    var state = UIState()
igor@1301
    28
    let searchController = UISearchController(searchResultsController: nil)
dirk@1737
    29
    let cellsInUse = NSCache<NSString, EmailListViewCell>()
dirk@719
    30
dirk@1724
    31
    /**
dirk@1724
    32
     After trustwords have been invoked, this will be the partner identity that
dirk@1724
    33
     was either confirmed or mistrusted.
dirk@1724
    34
     */
dirk@1724
    35
    var partnerIdentity: Identity?
dirk@1724
    36
dirk@275
    37
    override func viewDidLoad() {
ylandert@935
    38
        super.viewDidLoad()
xavier@1623
    39
igor@1408
    40
        title = "EmailList.title".localized
igor@1284
    41
        UIHelper.emailListTableHeight(self.tableView)
igor@1301
    42
        addSearchBar()
dirk@275
    43
    }
dirk@31
    44
dirk@784
    45
    override func viewWillAppear(_ animated: Bool) {
igor@1301
    46
        super.viewWillAppear(animated)
xavier@1623
    47
dirk@1344
    48
        if MiscUtil.isUnitTest() {
dirk@1344
    49
            return
dirk@1344
    50
        }
dirk@1344
    51
dirk@1826
    52
        setDefaultColors()
dirk@1344
    53
        initialConfig()
dirk@854
    54
        updateModel()
dirk@1348
    55
dirk@1348
    56
        MessageModelConfig.messageFolderDelegate = self
dirk@1348
    57
    }
dirk@1348
    58
dirk@1348
    59
    override func viewWillDisappear(_ animated: Bool) {
dirk@1348
    60
        super.viewWillDisappear(animated)
dirk@1348
    61
        MessageModelConfig.messageFolderDelegate = nil
igor@1301
    62
    }
xavier@1623
    63
igor@1338
    64
    func initialConfig() {
dirk@1344
    65
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
igor@1338
    66
            return
igor@1338
    67
        }
dirk@1344
    68
dirk@1344
    69
        if config == nil {
igor@1395
    70
            config = EmailListConfig(appConfig: appDelegate.appConfig, folder: Folder.unifiedInbox())
dirk@1344
    71
        }
dirk@1348
    72
        if Account.all().isEmpty {
igor@1338
    73
            performSegue(withIdentifier:.segueAddNewAccount, sender: self)
igor@1338
    74
        }
igor@1338
    75
    }
xavier@1623
    76
igor@1301
    77
    func addSearchBar() {
igor@1301
    78
        searchController.searchResultsUpdater = self
igor@1301
    79
        searchController.dimsBackgroundDuringPresentation = false
igor@1301
    80
        searchController.delegate = self
igor@1301
    81
        definesPresentationContext = true
igor@1301
    82
        tableView.tableHeaderView = searchController.searchBar
igor@1301
    83
        tableView.setContentOffset(CGPoint(x: 0.0, y: 40.0), animated: false)
igor@1301
    84
    }
dirk@705
    85
xavier@1623
    86
dirk@1330
    87
    @IBAction func showUnreadButtonTapped(_ sender: UIBarButtonItem) {}
xavier@1623
    88
dirk@854
    89
    func updateModel() {
igor@1318
    90
        tableView.reloadData()
dirk@31
    91
    }
dirk@31
    92
dirk@58
    93
    // MARK: - UI State
dirk@58
    94
dirk@58
    95
    func updateUI() {
dirk@784
    96
        UIApplication.shared.isNetworkActivityIndicatorVisible = state.isSynching
dirk@748
    97
        if !state.isSynching {
igor@1260
    98
            refreshControl?.endRefreshing()
dirk@58
    99
        }
dirk@58
   100
    }
dirk@58
   101
dirk@31
   102
    // MARK: - UITableViewDataSource
dirk@31
   103
dirk@784
   104
    override func numberOfSections(in tableView: UITableView) -> Int {
dirk@1344
   105
        if let _ = config?.folder {
dirk@854
   106
            return 1
dirk@209
   107
        }
dirk@854
   108
        return 0
dirk@31
   109
    }
dirk@31
   110
dirk@784
   111
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dirk@1344
   112
        if let fol = config?.folder  {
dirk@854
   113
            return fol.messageCount()
dirk@31
   114
        }
dirk@31
   115
        return 0
dirk@31
   116
    }
dirk@31
   117
dirk@784
   118
    override func tableView(_ tableView: UITableView,
dirk@784
   119
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dirk@784
   120
        let cell = tableView.dequeueReusableCell(
dirk@784
   121
            withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
dirk@1737
   122
        if let message = cell.configureCell(config: config, indexPath: indexPath) {
dirk@1737
   123
            associate(message: message, toCell: cell)
dirk@1542
   124
        }
dirk@31
   125
        return cell
dirk@31
   126
    }
dirk@31
   127
dirk@719
   128
    // MARK: - UITableViewDelegate
dirk@719
   129
dirk@784
   130
    override func tableView(_ tableView: UITableView, editActionsForRowAt
dirk@784
   131
        indexPath: IndexPath)-> [UITableViewRowAction]? {
xavier@1623
   132
dirk@784
   133
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
igor@1260
   134
        if let email = cell.messageAt(indexPath: indexPath, config: config) {
igor@1576
   135
            let flagAction = createFlagAction(message: email, cell: cell)
dirk@1507
   136
            let deleteAction = createDeleteAction(message: email, cell: cell)
igor@1242
   137
            let moreAction = createMoreAction(message: email, cell: cell)
igor@1576
   138
            return [deleteAction, flagAction, moreAction]
dirk@855
   139
        }
dirk@855
   140
        return nil
dirk@744
   141
    }
dirk@744
   142
dirk@719
   143
    // MARK: - Misc
dirk@719
   144
dirk@1507
   145
    func createRowAction(cell: EmailListViewCell,
xavier@1623
   146
                         image: UIImage?, action: @escaping (UITableViewRowAction, IndexPath) -> Void,
xavier@1623
   147
                         title: String) -> UITableViewRowAction {
dirk@1510
   148
        let rowAction = UITableViewRowAction(
dirk@1510
   149
            style: .normal, title: title, handler: action)
dirk@1507
   150
dirk@1507
   151
        if let theImage = image {
dirk@1507
   152
            let iconColor = UIColor(patternImage: theImage)
dirk@1507
   153
            rowAction.backgroundColor = iconColor
dirk@744
   154
        }
dirk@744
   155
dirk@1507
   156
        return rowAction
dirk@744
   157
    }
dirk@744
   158
dirk@1507
   159
    func createFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   160
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1522
   161
            if message.imapFlags == nil {
dirk@1522
   162
                Log.warn(component: #function, content: "message.imapFlags == nil")
dirk@1522
   163
            }
dirk@1605
   164
            if cell.isFlagged(message: message) {
dirk@1507
   165
                message.imapFlags?.flagged = false
dirk@1507
   166
            } else {
dirk@1507
   167
                message.imapFlags?.flagged = true
dirk@1507
   168
            }
dirk@1507
   169
            message.save()
dirk@1507
   170
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@1507
   171
        }
dirk@1510
   172
igor@1523
   173
        var title = "\n\nFlag".localized
dirk@1605
   174
        if message.imapFlags?.flagged ?? true {
igor@1530
   175
            title = "\n\nUnFlag".localized
dirk@744
   176
        }
dirk@744
   177
dirk@1510
   178
        return createRowAction(
dirk@1510
   179
            cell: cell, image: UIImage(named: "swipe-flag"), action: action, title: title)
dirk@744
   180
    }
dirk@744
   181
dirk@1507
   182
    func createDeleteAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   183
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1507
   184
            guard let message = cell.messageAt(indexPath: indexPath, config: self.config) else {
dirk@1507
   185
                return
dirk@1507
   186
            }
xavier@1623
   187
dirk@1650
   188
            message.delete() // mark for deletion/trash
dirk@1507
   189
            message.save()
igor@1576
   190
            self.tableView.reloadData()
dirk@1507
   191
        }
dirk@744
   192
dirk@1510
   193
        return createRowAction(
dirk@1510
   194
            cell: cell, image: UIImage(named: "swipe-trash"), action: action,
igor@1523
   195
            title: "\n\nDelete".localized)
dirk@1507
   196
    }
dirk@1507
   197
dirk@1507
   198
    func createMarkAsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   199
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1605
   200
            if cell.haveSeen(message: message) {
dirk@1507
   201
                message.imapFlags?.seen = false
dirk@1507
   202
            } else {
dirk@1507
   203
                message.imapFlags?.seen = true
dirk@1507
   204
            }
dirk@1507
   205
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   206
        }
dirk@1510
   207
dirk@1510
   208
        var title = NSLocalizedString(
dirk@1510
   209
            "Unread", comment: "Unread button title in swipe action on EmailListViewController")
dirk@1605
   210
        if !cell.haveSeen(message: message) {
dirk@1510
   211
            title = NSLocalizedString(
dirk@1510
   212
                "Read", comment: "Read button title in swipe action on EmailListViewController")
dirk@1507
   213
        }
dirk@1507
   214
dirk@1507
   215
        let isReadAction = createRowAction(cell: cell, image: nil, action: action,
dirk@1510
   216
                                           title: title)
dirk@784
   217
        isReadAction.backgroundColor = UIColor.blue
dirk@744
   218
dirk@744
   219
        return isReadAction
dirk@744
   220
    }
xavier@1623
   221
igor@1242
   222
    func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1510
   223
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
igor@1242
   224
            self.showMoreActionSheet(cell: cell)
igor@1242
   225
        }
dirk@1510
   226
dirk@1510
   227
        return createRowAction(
dirk@1510
   228
            cell: cell, image: UIImage(named: "swipe-more"), action: action,
igor@1523
   229
            title: "\n\nMore".localized)
igor@1242
   230
    }
xavier@1623
   231
igor@1242
   232
    // MARK: - Action Sheet
xavier@1623
   233
igor@1242
   234
    func showMoreActionSheet(cell: EmailListViewCell) {
igor@1242
   235
        let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
igor@1438
   236
        alertControler.view.tintColor = .pEpGreen
igor@1242
   237
        let cancelAction = createCancelAction()
igor@1242
   238
        let replyAction = createReplyAction(cell: cell)
xavier@1623
   239
        let replyAllAction = createReplyAllAction(cell: cell)
igor@1242
   240
        let forwardAction = createForwardAction(cell: cell)
igor@1242
   241
        let markAction = createMarkAction()
igor@1242
   242
        alertControler.addAction(cancelAction)
igor@1242
   243
        alertControler.addAction(replyAction)
xavier@1620
   244
        alertControler.addAction(replyAllAction)
igor@1242
   245
        alertControler.addAction(forwardAction)
igor@1242
   246
        alertControler.addAction(markAction)
igor@1414
   247
        if let popoverPresentationController = alertControler.popoverPresentationController {
igor@1414
   248
            popoverPresentationController.sourceView = cell
igor@1414
   249
        }
igor@1242
   250
        present(alertControler, animated: true, completion: nil)
igor@1242
   251
    }
xavier@1623
   252
igor@1242
   253
    // MARK: - Action Sheet Actions
igor@1242
   254
igor@1242
   255
    func createCancelAction() -> UIAlertAction {
xavier@1623
   256
        return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
igor@1242
   257
    }
xavier@1623
   258
igor@1242
   259
    func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
igor@1242
   260
        return UIAlertAction(title: "Reply", style: .default) { (action) in
xavier@1623
   261
            // self.performSegue(withIdentifier: self.segueCompose, sender: cell)
igor@1338
   262
            self.performSegue(withIdentifier: .segueCompose, sender: cell)
igor@1242
   263
        }
igor@1242
   264
    }
xavier@1620
   265
xavier@1620
   266
    func createReplyAllAction(cell: EmailListViewCell) ->  UIAlertAction {
xavier@1623
   267
        return UIAlertAction(title: "Reply All", style: .default) { (action) in
xavier@1623
   268
            self.performSegue(withIdentifier: .segueReplyAll, sender: cell)
xavier@1620
   269
        }
xavier@1620
   270
    }
xavier@1620
   271
igor@1242
   272
    func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
igor@1242
   273
        return UIAlertAction(title: "Forward", style: .default) { (action) in
igor@1338
   274
            //self.performSegue(withIdentifier: self.segueCompose, sender: cell)
xavier@1664
   275
            self.performSegue(withIdentifier: .segueForward, sender: cell)
igor@1242
   276
        }
igor@1242
   277
    }
xavier@1623
   278
igor@1242
   279
    func createMarkAction() -> UIAlertAction {
igor@1242
   280
        return UIAlertAction(title: "Mark", style: .default) { (action) in
igor@1242
   281
        }
igor@1242
   282
    }
xavier@1623
   283
igor@1301
   284
    // MARK: - Content Search
xavier@1623
   285
igor@1301
   286
    func filterContentForSearchText(searchText: String) {
xavier@1623
   287
igor@1301
   288
    }
xavier@1623
   289
dirk@784
   290
}
igor@1301
   291
igor@1301
   292
extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
igor@1301
   293
    public func updateSearchResults(for searchController: UISearchController) {
igor@1301
   294
        filterContentForSearchText(searchText: searchController.searchBar.text!)
igor@1301
   295
    }
xavier@1623
   296
igor@1301
   297
    func didDismissSearchController(_ searchController: UISearchController) {
igor@1301
   298
    }
igor@1301
   299
}
igor@1338
   300
igor@1338
   301
// MARK: - Navigation
igor@1338
   302
igor@1338
   303
extension EmailListViewController: SegueHandlerType {
xavier@1623
   304
igor@1338
   305
    // MARK: - SegueHandlerType
xavier@1623
   306
igor@1338
   307
    enum SegueIdentifier: String {
igor@1338
   308
        case segueAddNewAccount
igor@1338
   309
        case segueEditAccounts
igor@1338
   310
        case segueShowEmail
igor@1338
   311
        case segueCompose
xavier@1623
   312
        case segueReplyAll
xavier@1664
   313
        case segueForward
igor@1338
   314
        case noSegue
igor@1338
   315
    }
xavier@1623
   316
igor@1338
   317
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
igor@1338
   318
        switch segueIdentifier(for: segue) {
xavier@1623
   319
        case .segueReplyAll:
xavier@1623
   320
            if let nav = segue.destination as? UINavigationController,
xavier@1623
   321
                let destination = nav.topViewController as? ComposeTableViewController,
xavier@1623
   322
                let cell = sender as? EmailListViewCell,
xavier@1623
   323
                let indexPath = self.tableView.indexPath(for: cell),
xavier@1623
   324
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1623
   325
                destination.composeMode = .replyAll
xavier@1623
   326
                destination.appConfig = config?.appConfig
xavier@1623
   327
                destination.originalMessage = email
xavier@1623
   328
            }
igor@1338
   329
            break
igor@1338
   330
        case .segueShowEmail:
dirk@1594
   331
            if let vc = segue.destination as? EmailViewController,
igor@1338
   332
                let cell = sender as? EmailListViewCell,
igor@1338
   333
                let indexPath = self.tableView.indexPath(for: cell),
dirk@1594
   334
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1623
   335
                vc.appConfig = config?.appConfig
xavier@1623
   336
                vc.message = email
igor@1338
   337
            }
igor@1338
   338
            break
xavier@1664
   339
        case .segueForward:
xavier@1664
   340
            if let nav = segue.destination as? UINavigationController,
xavier@1664
   341
                let destination = nav.topViewController as? ComposeTableViewController,
xavier@1664
   342
                let cell = sender as? EmailListViewCell,
xavier@1664
   343
                let indexPath = self.tableView.indexPath(for: cell),
xavier@1664
   344
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1664
   345
                destination.composeMode = .forward
xavier@1664
   346
                destination.appConfig = config?.appConfig
xavier@1664
   347
                destination.originalMessage = email
xavier@1664
   348
            }
xavier@1664
   349
            break
dirk@1694
   350
        case .segueAddNewAccount, .segueEditAccounts, .segueCompose, .noSegue:
dirk@1694
   351
            break
igor@1338
   352
        }
igor@1338
   353
    }
dirk@1350
   354
dirk@1350
   355
    func didChangeInternal(messageFolder: MessageFolder) {
dirk@1350
   356
        if let folder = config?.folder,
dirk@1350
   357
            let message = messageFolder as? Message,
dirk@1558
   358
            folder.contains(message: message, deletedMessagesAreContained: true) {
dirk@1350
   359
            if let msg = messageFolder as? Message {
dirk@1350
   360
                if msg.isOriginal {
dirk@1535
   361
                    // new message has arrived
dirk@1675
   362
                    if let index = folder.indexOf(message: msg) {
dirk@1675
   363
                        let ip = IndexPath(row: index, section: 0)
dirk@1737
   364
                        Log.info(
dirk@1737
   365
                            component: #function,
dirk@1737
   366
                            content: "insert message at \(index), \(folder.messageCount()) messages")
dirk@1675
   367
                        tableView.insertRows(at: [ip], with: .automatic)
dirk@1675
   368
                    } else {
dirk@1675
   369
                        tableView.reloadData()
dirk@1675
   370
                    }
dirk@1558
   371
                } else if msg.isGhost {
dirk@1737
   372
                    if let cell = cellFor(message: msg), let ip = tableView.indexPath(for: cell) {
dirk@1737
   373
                        Log.info(
dirk@1737
   374
                            component: #function,
dirk@1737
   375
                            content: "delete message at \(index), \(folder.messageCount()) messages")
dirk@1737
   376
                        tableView.deleteRows(at: [ip], with: .automatic)
dirk@1737
   377
                    } else {
dirk@1737
   378
                        tableView.reloadData()
dirk@1558
   379
                    }
dirk@1535
   380
                } else {
dirk@1558
   381
                    // other flags than delete must have been changed
dirk@1737
   382
                    if let cell = cellFor(message: msg) {
dirk@1542
   383
                        cell.updateFlags(message: message)
dirk@1737
   384
                    } else {
dirk@1737
   385
                        tableView.reloadData()
dirk@1542
   386
                    }
dirk@1350
   387
                }
dirk@1350
   388
            }
dirk@1350
   389
        }
dirk@1350
   390
    }
dirk@1737
   391
dirk@1737
   392
    // MARK: - Message -> Cell association
dirk@1737
   393
dirk@1737
   394
    func keyFor(message: Message) -> NSString {
dirk@1737
   395
        let parentName = message.parent?.name ?? "unknown"
dirk@1737
   396
        return "\(message.uuid) \(parentName) \(message.uuid)" as NSString
dirk@1737
   397
    }
dirk@1737
   398
dirk@1737
   399
    func associate(message: Message, toCell: EmailListViewCell) {
dirk@1737
   400
        cellsInUse.setObject(toCell, forKey: keyFor(message: message))
dirk@1737
   401
    }
dirk@1737
   402
dirk@1737
   403
    func cellFor(message: Message) -> EmailListViewCell? {
dirk@1737
   404
        return cellsInUse.object(forKey: keyFor(message: message))
dirk@1737
   405
    }
igor@1338
   406
}
dirk@1348
   407
dirk@1348
   408
// MARK: - MessageFolderDelegate
dirk@1348
   409
dirk@1348
   410
extension EmailListViewController: MessageFolderDelegate {
dirk@1348
   411
    func didChange(messageFolder: MessageFolder) {
dirk@1675
   412
        GCD.onMainWait {
dirk@1350
   413
            self.didChangeInternal(messageFolder: messageFolder)
dirk@1348
   414
        }
dirk@1348
   415
    }
dirk@1348
   416
}