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