pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Xavier Algarra <xavier@pep-project.org>
Mon, 14 Aug 2017 12:05:13 +0200
changeset 2715 50b73f5ea496
parent 2697 a9130d482e40
child 2733 d11889937bcc
permissions -rw-r--r--
IOS-643 reply action on mail list view controller works correctly
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?
dirk@1906
    19
dirk@1906
    20
    let imageProvider = IdentityImageProvider()
igor@1260
    21
}
dirk@30
    22
xavier@2369
    23
class EmailListViewController: UITableViewController {
dirk@583
    24
    struct UIState {
dirk@583
    25
        var isSynching: Bool = false
dirk@583
    26
    }
dirk@30
    27
dirk@1344
    28
    var config: EmailListConfig?
xavier@2362
    29
    var viewModel: EmailListViewModel?
dirk@275
    30
    var state = UIState()
igor@1301
    31
    let searchController = UISearchController(searchResultsController: nil)
xavier@2363
    32
    //let cellsInUse = NSCache<NSString, EmailListViewCell>()
dirk@719
    33
dirk@1724
    34
    /**
dirk@1724
    35
     After trustwords have been invoked, this will be the partner identity that
dirk@1724
    36
     was either confirmed or mistrusted.
dirk@1724
    37
     */
dirk@1724
    38
    var partnerIdentity: Identity?
dirk@1724
    39
dirk@2067
    40
    @IBOutlet weak var enableFilterButton: UIBarButtonItem!
dirk@2067
    41
    @IBOutlet weak var textFilterButton: UIBarButtonItem!
dirk@2067
    42
xavier@2585
    43
    @IBOutlet var showFoldersButton: UIBarButtonItem!
xavier@2363
    44
    //private var filterEnabled = false
dirk@2067
    45
dirk@275
    46
    override func viewDidLoad() {
ylandert@935
    47
        super.viewDidLoad()
xavier@1623
    48
dirk@2189
    49
        title = NSLocalizedString("Inbox", comment: "General name for (unified) inbox")
igor@1284
    50
        UIHelper.emailListTableHeight(self.tableView)
igor@1301
    51
        addSearchBar()
dirk@275
    52
    }
dirk@31
    53
dirk@784
    54
    override func viewWillAppear(_ animated: Bool) {
igor@1301
    55
        super.viewWillAppear(animated)
xavier@2325
    56
        self.navigationController?.setToolbarHidden(false, animated: true)
dirk@1344
    57
        if MiscUtil.isUnitTest() {
dirk@1344
    58
            return
dirk@1344
    59
        }
dirk@1344
    60
xavier@2363
    61
        if let vm = viewModel {
xavier@2363
    62
            self.textFilterButton.isEnabled = vm.filterEnabled
xavier@2363
    63
        } else {
xavier@2363
    64
            self.textFilterButton.isEnabled = false
xavier@2363
    65
        }
xavier@1832
    66
dirk@1826
    67
        setDefaultColors()
andreas@2683
    68
        setupConfig()
dirk@854
    69
        updateModel()
dirk@1348
    70
dirk@2191
    71
        // Mark this folder as having been looked at by the user
andreas@2683
    72
        if let folder = config?.folder {
andreas@2683
    73
            updateLastLookAt(on: folder)
dirk@2191
    74
        }
xavier@2437
    75
        if viewModel == nil {
xavier@2437
    76
            viewModel = EmailListViewModel(config: config, delegate: self)
xavier@2437
    77
        }
dirk@1348
    78
        MessageModelConfig.messageFolderDelegate = self
xavier@2585
    79
xavier@2585
    80
        if let size = navigationController?.viewControllers.count, size > 1 {
xavier@2585
    81
            self.showFoldersButton.isEnabled = false
xavier@2585
    82
        } else {
xavier@2585
    83
            self.showFoldersButton.isEnabled = true
xavier@2585
    84
        }
xavier@2325
    85
        
dirk@1348
    86
    }
dirk@1348
    87
andreas@2683
    88
    private func updateLastLookAt(on folder: Folder) {
andreas@2683
    89
        if folder.isUnified {
andreas@2683
    90
            folder.updateLastLookAt()
andreas@2683
    91
        } else {
andreas@2683
    92
            folder.updateLastLookAtAndSave()
andreas@2683
    93
        }
andreas@2683
    94
    }
xavier@1948
    95
dirk@1348
    96
    override func viewWillDisappear(_ animated: Bool) {
dirk@1348
    97
        super.viewWillDisappear(animated)
dirk@1348
    98
        MessageModelConfig.messageFolderDelegate = nil
igor@1301
    99
    }
xavier@1623
   100
andreas@2683
   101
    func setupConfig() {
dirk@1344
   102
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
andreas@2683
   103
            Log.shared.errorAndCrash(component: #function, errorString: "No AppDelegate?")
igor@1338
   104
            return
igor@1338
   105
        }
xavier@2132
   106
        if config == nil {
dirk@2191
   107
            config = EmailListConfig(appConfig: appDelegate.appConfig,
dirk@2191
   108
                                     folder: Folder.unifiedInbox())
xavier@2132
   109
        }
xavier@2362
   110
dirk@2240
   111
        if Account.all().isEmpty {
dirk@2240
   112
            performSegue(withIdentifier:.segueAddNewAccount, sender: self)
dirk@2240
   113
        }
andreas@2683
   114
andreas@2697
   115
        guard let folder = config?.folder else {
andreas@2697
   116
            return
andreas@2697
   117
        }
andreas@2697
   118
        self.title = realName(of: folder)
igor@1338
   119
    }
xavier@1623
   120
igor@1301
   121
    func addSearchBar() {
igor@1301
   122
        searchController.searchResultsUpdater = self
igor@1301
   123
        searchController.dimsBackgroundDuringPresentation = false
igor@1301
   124
        searchController.delegate = self
igor@1301
   125
        definesPresentationContext = true
igor@1301
   126
        tableView.tableHeaderView = searchController.searchBar
igor@1301
   127
        tableView.setContentOffset(CGPoint(x: 0.0, y: 40.0), animated: false)
igor@1301
   128
    }
dirk@705
   129
dirk@854
   130
    func updateModel() {
igor@1318
   131
        tableView.reloadData()
dirk@31
   132
    }
dirk@31
   133
xavier@1832
   134
    @IBAction func showUnreadButtonTapped(_ sender: UIBarButtonItem) {
xavier@2363
   135
        if let vm = viewModel {
xavier@2363
   136
            if vm.filterEnabled {
xavier@2363
   137
                vm.filterEnabled = false
xavier@2363
   138
                textFilterButton.title = ""
xavier@2363
   139
                enableFilterButton.image = UIImage(named: "unread-icon")
xavier@2425
   140
                if config != nil {
xavier@2425
   141
                    vm.resetFilters()
xavier@2425
   142
                }
xavier@2363
   143
            } else {
xavier@2363
   144
                vm.filterEnabled = true
xavier@2363
   145
                textFilterButton.title = "Filter by: unread"
xavier@2363
   146
                enableFilterButton.image = UIImage(named: "unread-icon-active")
xavier@2363
   147
                if config != nil {
xavier@2369
   148
                    vm.updateFilter(filter: Filter.unread())
xavier@2363
   149
                }
xavier@1832
   150
            }
xavier@2363
   151
            self.textFilterButton.isEnabled = vm.filterEnabled
xavier@1832
   152
        }
andreas@2697
   153
    }
xavier@2363
   154
andreas@2697
   155
    // MARK: - Private
xavier@1832
   156
andreas@2697
   157
    private func realName(of folder: Folder) -> String? {
andreas@2697
   158
        if folder.isUnified {
andreas@2697
   159
            return folder.name
andreas@2697
   160
        } else {
andreas@2697
   161
            return folder.realName
andreas@2697
   162
        }
xavier@1832
   163
    }
xavier@1832
   164
dirk@58
   165
    // MARK: - UI State
dirk@58
   166
dirk@58
   167
    func updateUI() {
dirk@784
   168
        UIApplication.shared.isNetworkActivityIndicatorVisible = state.isSynching
dirk@748
   169
        if !state.isSynching {
igor@1260
   170
            refreshControl?.endRefreshing()
dirk@58
   171
        }
dirk@58
   172
    }
dirk@58
   173
dirk@31
   174
    // MARK: - UITableViewDataSource
dirk@31
   175
dirk@784
   176
    override func numberOfSections(in tableView: UITableView) -> Int {
xavier@2362
   177
        if let _ = viewModel?.folderToShow {
dirk@854
   178
            return 1
dirk@209
   179
        }
dirk@854
   180
        return 0
dirk@31
   181
    }
dirk@31
   182
dirk@784
   183
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
xavier@2362
   184
        if let vm = viewModel {
xavier@2362
   185
            return vm.count
dirk@31
   186
        }
dirk@31
   187
        return 0
dirk@31
   188
    }
dirk@31
   189
dirk@784
   190
    override func tableView(_ tableView: UITableView,
dirk@784
   191
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dirk@784
   192
        let cell = tableView.dequeueReusableCell(
dirk@784
   193
            withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
xavier@2344
   194
        //mantener el configure cell para tal de no generar un vm para celdas
xavier@2404
   195
        let _ = cell.configureCell(config: config, indexPath: indexPath)
xavier@2404
   196
        viewModel?.associate(cell: cell, position: indexPath.row)
dirk@31
   197
        return cell
dirk@31
   198
    }
dirk@31
   199
dirk@719
   200
    // MARK: - UITableViewDelegate
dirk@719
   201
dirk@784
   202
    override func tableView(_ tableView: UITableView, editActionsForRowAt
dirk@784
   203
        indexPath: IndexPath)-> [UITableViewRowAction]? {
xavier@1623
   204
dirk@784
   205
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
igor@1260
   206
        if let email = cell.messageAt(indexPath: indexPath, config: config) {
igor@1576
   207
            let flagAction = createFlagAction(message: email, cell: cell)
dirk@1507
   208
            let deleteAction = createDeleteAction(message: email, cell: cell)
igor@1242
   209
            let moreAction = createMoreAction(message: email, cell: cell)
igor@1576
   210
            return [deleteAction, flagAction, moreAction]
dirk@855
   211
        }
dirk@855
   212
        return nil
dirk@744
   213
    }
dirk@744
   214
dirk@719
   215
    // MARK: - Misc
dirk@719
   216
dirk@1507
   217
    func createRowAction(cell: EmailListViewCell,
xavier@1623
   218
                         image: UIImage?, action: @escaping (UITableViewRowAction, IndexPath) -> Void,
xavier@1623
   219
                         title: String) -> UITableViewRowAction {
dirk@1510
   220
        let rowAction = UITableViewRowAction(
dirk@1510
   221
            style: .normal, title: title, handler: action)
dirk@1507
   222
dirk@1507
   223
        if let theImage = image {
dirk@1507
   224
            let iconColor = UIColor(patternImage: theImage)
dirk@1507
   225
            rowAction.backgroundColor = iconColor
dirk@744
   226
        }
dirk@744
   227
dirk@1507
   228
        return rowAction
dirk@744
   229
    }
dirk@744
   230
dirk@1507
   231
    func createFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   232
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1522
   233
            if message.imapFlags == nil {
dirk@1522
   234
                Log.warn(component: #function, content: "message.imapFlags == nil")
dirk@1522
   235
            }
dirk@1605
   236
            if cell.isFlagged(message: message) {
dirk@1507
   237
                message.imapFlags?.flagged = false
dirk@1507
   238
            } else {
dirk@1507
   239
                message.imapFlags?.flagged = true
dirk@1507
   240
            }
dirk@1507
   241
            message.save()
dirk@1507
   242
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@1507
   243
        }
dirk@1510
   244
dirk@2639
   245
        let flagString = NSLocalizedString("Flag", comment: "Message action (on swipe)")
dirk@2639
   246
        var title = "\n\n\(flagString)"
dirk@2639
   247
        let unflagString = NSLocalizedString("Unflag", comment: "Message action (on swipe)")
dirk@1605
   248
        if message.imapFlags?.flagged ?? true {
dirk@2639
   249
            title = "\n\n\(unflagString)"
dirk@744
   250
        }
dirk@744
   251
dirk@1510
   252
        return createRowAction(
dirk@1510
   253
            cell: cell, image: UIImage(named: "swipe-flag"), action: action, title: title)
dirk@744
   254
    }
dirk@744
   255
dirk@1507
   256
    func createDeleteAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   257
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1507
   258
            guard let message = cell.messageAt(indexPath: indexPath, config: self.config) else {
dirk@1507
   259
                return
dirk@1507
   260
            }
xavier@1623
   261
dirk@1650
   262
            message.delete() // mark for deletion/trash
dirk@1507
   263
            message.save()
igor@1576
   264
            self.tableView.reloadData()
dirk@1507
   265
        }
dirk@744
   266
dirk@2639
   267
        let title = NSLocalizedString("Delete", comment: "Message action (on swipe)")
dirk@1510
   268
        return createRowAction(
dirk@1510
   269
            cell: cell, image: UIImage(named: "swipe-trash"), action: action,
dirk@2639
   270
            title: "\n\n\(title)")
dirk@1507
   271
    }
dirk@1507
   272
dirk@1507
   273
    func createMarkAsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   274
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1605
   275
            if cell.haveSeen(message: message) {
dirk@1507
   276
                message.imapFlags?.seen = false
dirk@1507
   277
            } else {
dirk@1507
   278
                message.imapFlags?.seen = true
dirk@1507
   279
            }
dirk@1507
   280
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   281
        }
dirk@1510
   282
dirk@1510
   283
        var title = NSLocalizedString(
dirk@2190
   284
            "Unread", comment: "Message action (on swipe)")
dirk@1605
   285
        if !cell.haveSeen(message: message) {
dirk@1510
   286
            title = NSLocalizedString(
dirk@2190
   287
                "Read", comment: "Message action (on swipe)")
dirk@1507
   288
        }
dirk@1507
   289
dirk@1507
   290
        let isReadAction = createRowAction(cell: cell, image: nil, action: action,
dirk@1510
   291
                                           title: title)
dirk@784
   292
        isReadAction.backgroundColor = UIColor.blue
dirk@744
   293
dirk@744
   294
        return isReadAction
dirk@744
   295
    }
xavier@1623
   296
igor@1242
   297
    func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1510
   298
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
igor@1242
   299
            self.showMoreActionSheet(cell: cell)
igor@1242
   300
        }
dirk@1510
   301
dirk@2639
   302
        let title = NSLocalizedString("More", comment: "Message action (on swipe)")
dirk@1510
   303
        return createRowAction(
dirk@1510
   304
            cell: cell, image: UIImage(named: "swipe-more"), action: action,
dirk@2639
   305
            title: "\n\n\(title)")
igor@1242
   306
    }
xavier@1623
   307
igor@1242
   308
    // MARK: - Action Sheet
xavier@1623
   309
igor@1242
   310
    func showMoreActionSheet(cell: EmailListViewCell) {
igor@1242
   311
        let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
igor@1438
   312
        alertControler.view.tintColor = .pEpGreen
igor@1242
   313
        let cancelAction = createCancelAction()
igor@1242
   314
        let replyAction = createReplyAction(cell: cell)
xavier@1623
   315
        let replyAllAction = createReplyAllAction(cell: cell)
igor@1242
   316
        let forwardAction = createForwardAction(cell: cell)
igor@1242
   317
        let markAction = createMarkAction()
igor@1242
   318
        alertControler.addAction(cancelAction)
igor@1242
   319
        alertControler.addAction(replyAction)
xavier@1620
   320
        alertControler.addAction(replyAllAction)
igor@1242
   321
        alertControler.addAction(forwardAction)
igor@1242
   322
        alertControler.addAction(markAction)
igor@1414
   323
        if let popoverPresentationController = alertControler.popoverPresentationController {
igor@1414
   324
            popoverPresentationController.sourceView = cell
igor@1414
   325
        }
igor@1242
   326
        present(alertControler, animated: true, completion: nil)
igor@1242
   327
    }
xavier@1623
   328
igor@1242
   329
    // MARK: - Action Sheet Actions
igor@1242
   330
igor@1242
   331
    func createCancelAction() -> UIAlertAction {
xavier@1623
   332
        return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
igor@1242
   333
    }
xavier@1623
   334
igor@1242
   335
    func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
igor@1242
   336
        return UIAlertAction(title: "Reply", style: .default) { (action) in
xavier@1623
   337
            // self.performSegue(withIdentifier: self.segueCompose, sender: cell)
xavier@2715
   338
            self.performSegue(withIdentifier: .segueReply, sender: cell)
igor@1242
   339
        }
igor@1242
   340
    }
xavier@1620
   341
xavier@1620
   342
    func createReplyAllAction(cell: EmailListViewCell) ->  UIAlertAction {
xavier@1623
   343
        return UIAlertAction(title: "Reply All", style: .default) { (action) in
xavier@1623
   344
            self.performSegue(withIdentifier: .segueReplyAll, sender: cell)
xavier@1620
   345
        }
xavier@1620
   346
    }
xavier@1620
   347
igor@1242
   348
    func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
igor@1242
   349
        return UIAlertAction(title: "Forward", style: .default) { (action) in
xavier@1664
   350
            self.performSegue(withIdentifier: .segueForward, sender: cell)
igor@1242
   351
        }
igor@1242
   352
    }
xavier@1623
   353
igor@1242
   354
    func createMarkAction() -> UIAlertAction {
igor@1242
   355
        return UIAlertAction(title: "Mark", style: .default) { (action) in
igor@1242
   356
        }
igor@1242
   357
    }
xavier@1623
   358
dirk@784
   359
}
igor@1301
   360
igor@1301
   361
extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
igor@1301
   362
    public func updateSearchResults(for searchController: UISearchController) {
xavier@2369
   363
        if let vm = viewModel {
xavier@2369
   364
            vm.filterContentForSearchText(searchText: searchController.searchBar.text!, clear: false)
xavier@2369
   365
        }
igor@1301
   366
    }
xavier@1623
   367
igor@1301
   368
    func didDismissSearchController(_ searchController: UISearchController) {
xavier@2369
   369
        if let vm = viewModel {
xavier@2369
   370
            vm.filterContentForSearchText(clear: true)
xavier@2369
   371
        }
igor@1301
   372
    }
igor@1301
   373
}
igor@1338
   374
igor@1338
   375
// MARK: - Navigation
igor@1338
   376
igor@1338
   377
extension EmailListViewController: SegueHandlerType {
xavier@1623
   378
igor@1338
   379
    // MARK: - SegueHandlerType
xavier@1623
   380
igor@1338
   381
    enum SegueIdentifier: String {
igor@1338
   382
        case segueAddNewAccount
igor@1338
   383
        case segueEditAccounts
igor@1338
   384
        case segueShowEmail
igor@1338
   385
        case segueCompose
xavier@2715
   386
        case segueReply
xavier@1623
   387
        case segueReplyAll
xavier@1664
   388
        case segueForward
xavier@1865
   389
        case segueFilter
xavier@2248
   390
        case segueFolderViews
igor@1338
   391
        case noSegue
igor@1338
   392
    }
xavier@1623
   393
igor@1338
   394
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
igor@1338
   395
        switch segueIdentifier(for: segue) {
xavier@2715
   396
        case .segueReply:
xavier@2715
   397
            if let nav = segue.destination as? UINavigationController,
xavier@2715
   398
                let destination = nav.topViewController as? ComposeTableViewController,
xavier@2715
   399
                let cell = sender as? EmailListViewCell,
xavier@2715
   400
                let indexPath = self.tableView.indexPath(for: cell),
xavier@2715
   401
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@2715
   402
                destination.composeMode = .replyFrom
xavier@2715
   403
                destination.appConfig = config?.appConfig
xavier@2715
   404
                destination.originalMessage = email
xavier@2715
   405
            }
xavier@1623
   406
        case .segueReplyAll:
xavier@1623
   407
            if let nav = segue.destination as? UINavigationController,
xavier@1623
   408
                let destination = nav.topViewController as? ComposeTableViewController,
xavier@1623
   409
                let cell = sender as? EmailListViewCell,
xavier@1623
   410
                let indexPath = self.tableView.indexPath(for: cell),
xavier@1623
   411
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1623
   412
                destination.composeMode = .replyAll
xavier@1623
   413
                destination.appConfig = config?.appConfig
xavier@1623
   414
                destination.originalMessage = email
xavier@1623
   415
            }
igor@1338
   416
            break
igor@1338
   417
        case .segueShowEmail:
dirk@1594
   418
            if let vc = segue.destination as? EmailViewController,
igor@1338
   419
                let cell = sender as? EmailListViewCell,
igor@1338
   420
                let indexPath = self.tableView.indexPath(for: cell),
dirk@1594
   421
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1623
   422
                vc.appConfig = config?.appConfig
xavier@1623
   423
                vc.message = email
xavier@2404
   424
                vc.folderShow = viewModel?.folderToShow
xavier@2404
   425
                vc.messageId = indexPath.row
igor@1338
   426
            }
igor@1338
   427
            break
xavier@1664
   428
        case .segueForward:
xavier@1664
   429
            if let nav = segue.destination as? UINavigationController,
xavier@1664
   430
                let destination = nav.topViewController as? ComposeTableViewController,
xavier@1664
   431
                let cell = sender as? EmailListViewCell,
xavier@1664
   432
                let indexPath = self.tableView.indexPath(for: cell),
xavier@1664
   433
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1664
   434
                destination.composeMode = .forward
xavier@1664
   435
                destination.appConfig = config?.appConfig
xavier@1664
   436
                destination.originalMessage = email
xavier@1664
   437
            }
xavier@1664
   438
            break
xavier@1865
   439
        case .segueFilter:
xavier@1865
   440
            if let destiny = segue.destination as? FilterTableViewController {
xavier@2369
   441
                destiny.filterDelegate = self.viewModel
xavier@1865
   442
                destiny.inFolder = false
xavier@2445
   443
                destiny.filterEnabled = self.viewModel?.folderToShow?.filter
xavier@2325
   444
                destiny.hidesBottomBarWhenPushed = true
xavier@1865
   445
            }
xavier@1865
   446
            break
dirk@2240
   447
        case .segueAddNewAccount:
xavier@2254
   448
            if let vc = segue.destination as? LoginTableViewController {
dirk@2240
   449
                vc.appConfig = config?.appConfig
xavier@2254
   450
                vc.hidesBottomBarWhenPushed = true
dirk@2240
   451
            }
xavier@2248
   452
        case .segueFolderViews:
xavier@2248
   453
            if let vC = segue.destination as? FolderTableViewController {
xavier@2248
   454
                vC.appConfig = config?.appConfig
xavier@2254
   455
                vC.hidesBottomBarWhenPushed = true
xavier@2248
   456
            }
dirk@2240
   457
        case .segueEditAccounts, .segueCompose, .noSegue:
dirk@1694
   458
            break
igor@1338
   459
        }
xavier@1865
   460
igor@1338
   461
    }
xavier@2254
   462
    
xavier@2254
   463
    @IBAction func segueUnwindAccountAdded(segue: UIStoryboardSegue) {
xavier@2254
   464
    }
xavier@2254
   465
dirk@1350
   466
    func didChangeInternal(messageFolder: MessageFolder) {
dirk@1350
   467
        if let folder = config?.folder,
dirk@1350
   468
            let message = messageFolder as? Message,
dirk@1558
   469
            folder.contains(message: message, deletedMessagesAreContained: true) {
dirk@2181
   470
            if message.isOriginal {
dirk@2181
   471
                // new message has arrived
dirk@2181
   472
                if let index = folder.indexOf(message: message) {
dirk@2181
   473
                    let ip = IndexPath(row: index, section: 0)
dirk@2181
   474
                    Log.info(
dirk@2181
   475
                        component: #function,
dirk@2181
   476
                        content: "insert message at \(index), \(folder.messageCount()) messages")
dirk@2181
   477
                    tableView.insertRows(at: [ip], with: .automatic)
dirk@1535
   478
                } else {
dirk@2181
   479
                    tableView.reloadData()
dirk@2181
   480
                }
dirk@2181
   481
            } else if message.isGhost {
xavier@2363
   482
                if let vm = viewModel, let cell = vm.cellFor(message: message), let ip = tableView.indexPath(for: cell) {
dirk@2181
   483
                    Log.info(
dirk@2181
   484
                        component: #function,
dirk@2181
   485
                        content: "delete message at \(index), \(folder.messageCount()) messages")
dirk@2181
   486
                    tableView.deleteRows(at: [ip], with: .automatic)
dirk@2181
   487
                } else {
dirk@2181
   488
                    tableView.reloadData()
dirk@2181
   489
                }
dirk@2181
   490
            } else {
dirk@2181
   491
                // other flags than delete must have been changed
xavier@2363
   492
                if let vm = viewModel, let cell = vm.cellFor(message: message) {
dirk@2181
   493
                    cell.updateFlags(message: message)
dirk@2181
   494
                } else {
dirk@2181
   495
                    tableView.reloadData()
dirk@1350
   496
                }
dirk@1350
   497
            }
dirk@1350
   498
        }
dirk@1350
   499
    }
dirk@1737
   500
igor@1338
   501
}
dirk@1348
   502
dirk@1348
   503
// MARK: - MessageFolderDelegate
dirk@1348
   504
dirk@1348
   505
extension EmailListViewController: MessageFolderDelegate {
dirk@1348
   506
    func didChange(messageFolder: MessageFolder) {
dirk@1675
   507
        GCD.onMainWait {
dirk@1350
   508
            self.didChangeInternal(messageFolder: messageFolder)
dirk@1348
   509
        }
dirk@1348
   510
    }
dirk@1348
   511
}
xavier@2369
   512
xavier@2369
   513
extension EmailListViewController: tableViewUpdate {
xavier@2369
   514
    func updateView() {
xavier@2369
   515
        self.tableView.reloadData()
xavier@2369
   516
    }
xavier@2369
   517
}