pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author buff <andreas@pep-project.org>
Wed, 09 Aug 2017 19:14:25 +0200
branchIOS-615-duplicated-first-mail
changeset 2687 bb1dcce7a0d6
parent 2683 ef2249f630b0
child 2697 a9130d482e40
permissions -rw-r--r--
IOS-615 cleanup
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
xavier@2132
   115
        self.title = config?.folder?.realName
igor@1338
   116
    }
xavier@1623
   117
igor@1301
   118
    func addSearchBar() {
igor@1301
   119
        searchController.searchResultsUpdater = self
igor@1301
   120
        searchController.dimsBackgroundDuringPresentation = false
igor@1301
   121
        searchController.delegate = self
igor@1301
   122
        definesPresentationContext = true
igor@1301
   123
        tableView.tableHeaderView = searchController.searchBar
igor@1301
   124
        tableView.setContentOffset(CGPoint(x: 0.0, y: 40.0), animated: false)
igor@1301
   125
    }
dirk@705
   126
dirk@854
   127
    func updateModel() {
igor@1318
   128
        tableView.reloadData()
dirk@31
   129
    }
dirk@31
   130
xavier@1832
   131
xavier@1832
   132
    @IBAction func showUnreadButtonTapped(_ sender: UIBarButtonItem) {
xavier@2363
   133
        if let vm = viewModel {
xavier@2363
   134
            if vm.filterEnabled {
xavier@2363
   135
                vm.filterEnabled = false
xavier@2363
   136
                textFilterButton.title = ""
xavier@2363
   137
                enableFilterButton.image = UIImage(named: "unread-icon")
xavier@2425
   138
                if config != nil {
xavier@2425
   139
                    vm.resetFilters()
xavier@2425
   140
                }
xavier@2363
   141
            } else {
xavier@2363
   142
                vm.filterEnabled = true
xavier@2363
   143
                textFilterButton.title = "Filter by: unread"
xavier@2363
   144
                enableFilterButton.image = UIImage(named: "unread-icon-active")
xavier@2363
   145
                if config != nil {
xavier@2369
   146
                    vm.updateFilter(filter: Filter.unread())
xavier@2363
   147
                }
xavier@1832
   148
            }
xavier@2363
   149
            self.textFilterButton.isEnabled = vm.filterEnabled
xavier@1832
   150
        }
xavier@2363
   151
xavier@1832
   152
xavier@1832
   153
    }
xavier@1832
   154
dirk@58
   155
    // MARK: - UI State
dirk@58
   156
dirk@58
   157
    func updateUI() {
dirk@784
   158
        UIApplication.shared.isNetworkActivityIndicatorVisible = state.isSynching
dirk@748
   159
        if !state.isSynching {
igor@1260
   160
            refreshControl?.endRefreshing()
dirk@58
   161
        }
dirk@58
   162
    }
dirk@58
   163
dirk@31
   164
    // MARK: - UITableViewDataSource
dirk@31
   165
dirk@784
   166
    override func numberOfSections(in tableView: UITableView) -> Int {
xavier@2362
   167
        if let _ = viewModel?.folderToShow {
dirk@854
   168
            return 1
dirk@209
   169
        }
dirk@854
   170
        return 0
dirk@31
   171
    }
dirk@31
   172
dirk@784
   173
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
xavier@2362
   174
        if let vm = viewModel {
xavier@2362
   175
            return vm.count
dirk@31
   176
        }
dirk@31
   177
        return 0
dirk@31
   178
    }
dirk@31
   179
dirk@784
   180
    override func tableView(_ tableView: UITableView,
dirk@784
   181
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dirk@784
   182
        let cell = tableView.dequeueReusableCell(
dirk@784
   183
            withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
xavier@2344
   184
        //mantener el configure cell para tal de no generar un vm para celdas
xavier@2404
   185
        let _ = cell.configureCell(config: config, indexPath: indexPath)
xavier@2404
   186
        viewModel?.associate(cell: cell, position: indexPath.row)
dirk@31
   187
        return cell
dirk@31
   188
    }
dirk@31
   189
dirk@719
   190
    // MARK: - UITableViewDelegate
dirk@719
   191
dirk@784
   192
    override func tableView(_ tableView: UITableView, editActionsForRowAt
dirk@784
   193
        indexPath: IndexPath)-> [UITableViewRowAction]? {
xavier@1623
   194
dirk@784
   195
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
igor@1260
   196
        if let email = cell.messageAt(indexPath: indexPath, config: config) {
igor@1576
   197
            let flagAction = createFlagAction(message: email, cell: cell)
dirk@1507
   198
            let deleteAction = createDeleteAction(message: email, cell: cell)
igor@1242
   199
            let moreAction = createMoreAction(message: email, cell: cell)
igor@1576
   200
            return [deleteAction, flagAction, moreAction]
dirk@855
   201
        }
dirk@855
   202
        return nil
dirk@744
   203
    }
dirk@744
   204
dirk@719
   205
    // MARK: - Misc
dirk@719
   206
dirk@1507
   207
    func createRowAction(cell: EmailListViewCell,
xavier@1623
   208
                         image: UIImage?, action: @escaping (UITableViewRowAction, IndexPath) -> Void,
xavier@1623
   209
                         title: String) -> UITableViewRowAction {
dirk@1510
   210
        let rowAction = UITableViewRowAction(
dirk@1510
   211
            style: .normal, title: title, handler: action)
dirk@1507
   212
dirk@1507
   213
        if let theImage = image {
dirk@1507
   214
            let iconColor = UIColor(patternImage: theImage)
dirk@1507
   215
            rowAction.backgroundColor = iconColor
dirk@744
   216
        }
dirk@744
   217
dirk@1507
   218
        return rowAction
dirk@744
   219
    }
dirk@744
   220
dirk@1507
   221
    func createFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   222
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1522
   223
            if message.imapFlags == nil {
dirk@1522
   224
                Log.warn(component: #function, content: "message.imapFlags == nil")
dirk@1522
   225
            }
dirk@1605
   226
            if cell.isFlagged(message: message) {
dirk@1507
   227
                message.imapFlags?.flagged = false
dirk@1507
   228
            } else {
dirk@1507
   229
                message.imapFlags?.flagged = true
dirk@1507
   230
            }
dirk@1507
   231
            message.save()
dirk@1507
   232
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@1507
   233
        }
dirk@1510
   234
dirk@2639
   235
        let flagString = NSLocalizedString("Flag", comment: "Message action (on swipe)")
dirk@2639
   236
        var title = "\n\n\(flagString)"
dirk@2639
   237
        let unflagString = NSLocalizedString("Unflag", comment: "Message action (on swipe)")
dirk@1605
   238
        if message.imapFlags?.flagged ?? true {
dirk@2639
   239
            title = "\n\n\(unflagString)"
dirk@744
   240
        }
dirk@744
   241
dirk@1510
   242
        return createRowAction(
dirk@1510
   243
            cell: cell, image: UIImage(named: "swipe-flag"), action: action, title: title)
dirk@744
   244
    }
dirk@744
   245
dirk@1507
   246
    func createDeleteAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   247
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1507
   248
            guard let message = cell.messageAt(indexPath: indexPath, config: self.config) else {
dirk@1507
   249
                return
dirk@1507
   250
            }
xavier@1623
   251
dirk@1650
   252
            message.delete() // mark for deletion/trash
dirk@1507
   253
            message.save()
igor@1576
   254
            self.tableView.reloadData()
dirk@1507
   255
        }
dirk@744
   256
dirk@2639
   257
        let title = NSLocalizedString("Delete", comment: "Message action (on swipe)")
dirk@1510
   258
        return createRowAction(
dirk@1510
   259
            cell: cell, image: UIImage(named: "swipe-trash"), action: action,
dirk@2639
   260
            title: "\n\n\(title)")
dirk@1507
   261
    }
dirk@1507
   262
dirk@1507
   263
    func createMarkAsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   264
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1605
   265
            if cell.haveSeen(message: message) {
dirk@1507
   266
                message.imapFlags?.seen = false
dirk@1507
   267
            } else {
dirk@1507
   268
                message.imapFlags?.seen = true
dirk@1507
   269
            }
dirk@1507
   270
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   271
        }
dirk@1510
   272
dirk@1510
   273
        var title = NSLocalizedString(
dirk@2190
   274
            "Unread", comment: "Message action (on swipe)")
dirk@1605
   275
        if !cell.haveSeen(message: message) {
dirk@1510
   276
            title = NSLocalizedString(
dirk@2190
   277
                "Read", comment: "Message action (on swipe)")
dirk@1507
   278
        }
dirk@1507
   279
dirk@1507
   280
        let isReadAction = createRowAction(cell: cell, image: nil, action: action,
dirk@1510
   281
                                           title: title)
dirk@784
   282
        isReadAction.backgroundColor = UIColor.blue
dirk@744
   283
dirk@744
   284
        return isReadAction
dirk@744
   285
    }
xavier@1623
   286
igor@1242
   287
    func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1510
   288
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
igor@1242
   289
            self.showMoreActionSheet(cell: cell)
igor@1242
   290
        }
dirk@1510
   291
dirk@2639
   292
        let title = NSLocalizedString("More", comment: "Message action (on swipe)")
dirk@1510
   293
        return createRowAction(
dirk@1510
   294
            cell: cell, image: UIImage(named: "swipe-more"), action: action,
dirk@2639
   295
            title: "\n\n\(title)")
igor@1242
   296
    }
xavier@1623
   297
igor@1242
   298
    // MARK: - Action Sheet
xavier@1623
   299
igor@1242
   300
    func showMoreActionSheet(cell: EmailListViewCell) {
igor@1242
   301
        let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
igor@1438
   302
        alertControler.view.tintColor = .pEpGreen
igor@1242
   303
        let cancelAction = createCancelAction()
igor@1242
   304
        let replyAction = createReplyAction(cell: cell)
xavier@1623
   305
        let replyAllAction = createReplyAllAction(cell: cell)
igor@1242
   306
        let forwardAction = createForwardAction(cell: cell)
igor@1242
   307
        let markAction = createMarkAction()
igor@1242
   308
        alertControler.addAction(cancelAction)
igor@1242
   309
        alertControler.addAction(replyAction)
xavier@1620
   310
        alertControler.addAction(replyAllAction)
igor@1242
   311
        alertControler.addAction(forwardAction)
igor@1242
   312
        alertControler.addAction(markAction)
igor@1414
   313
        if let popoverPresentationController = alertControler.popoverPresentationController {
igor@1414
   314
            popoverPresentationController.sourceView = cell
igor@1414
   315
        }
igor@1242
   316
        present(alertControler, animated: true, completion: nil)
igor@1242
   317
    }
xavier@1623
   318
igor@1242
   319
    // MARK: - Action Sheet Actions
igor@1242
   320
igor@1242
   321
    func createCancelAction() -> UIAlertAction {
xavier@1623
   322
        return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
igor@1242
   323
    }
xavier@1623
   324
igor@1242
   325
    func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
igor@1242
   326
        return UIAlertAction(title: "Reply", style: .default) { (action) in
xavier@1623
   327
            // self.performSegue(withIdentifier: self.segueCompose, sender: cell)
igor@1338
   328
            self.performSegue(withIdentifier: .segueCompose, sender: cell)
igor@1242
   329
        }
igor@1242
   330
    }
xavier@1620
   331
xavier@1620
   332
    func createReplyAllAction(cell: EmailListViewCell) ->  UIAlertAction {
xavier@1623
   333
        return UIAlertAction(title: "Reply All", style: .default) { (action) in
xavier@1623
   334
            self.performSegue(withIdentifier: .segueReplyAll, sender: cell)
xavier@1620
   335
        }
xavier@1620
   336
    }
xavier@1620
   337
igor@1242
   338
    func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
igor@1242
   339
        return UIAlertAction(title: "Forward", style: .default) { (action) in
xavier@1664
   340
            self.performSegue(withIdentifier: .segueForward, sender: cell)
igor@1242
   341
        }
igor@1242
   342
    }
xavier@1623
   343
igor@1242
   344
    func createMarkAction() -> UIAlertAction {
igor@1242
   345
        return UIAlertAction(title: "Mark", style: .default) { (action) in
igor@1242
   346
        }
igor@1242
   347
    }
xavier@1623
   348
dirk@784
   349
}
igor@1301
   350
igor@1301
   351
extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
igor@1301
   352
    public func updateSearchResults(for searchController: UISearchController) {
xavier@2369
   353
        if let vm = viewModel {
xavier@2369
   354
            vm.filterContentForSearchText(searchText: searchController.searchBar.text!, clear: false)
xavier@2369
   355
        }
igor@1301
   356
    }
xavier@1623
   357
igor@1301
   358
    func didDismissSearchController(_ searchController: UISearchController) {
xavier@2369
   359
        if let vm = viewModel {
xavier@2369
   360
            vm.filterContentForSearchText(clear: true)
xavier@2369
   361
        }
igor@1301
   362
    }
igor@1301
   363
}
igor@1338
   364
igor@1338
   365
// MARK: - Navigation
igor@1338
   366
igor@1338
   367
extension EmailListViewController: SegueHandlerType {
xavier@1623
   368
igor@1338
   369
    // MARK: - SegueHandlerType
xavier@1623
   370
igor@1338
   371
    enum SegueIdentifier: String {
igor@1338
   372
        case segueAddNewAccount
igor@1338
   373
        case segueEditAccounts
igor@1338
   374
        case segueShowEmail
igor@1338
   375
        case segueCompose
xavier@1623
   376
        case segueReplyAll
xavier@1664
   377
        case segueForward
xavier@1865
   378
        case segueFilter
xavier@2248
   379
        case segueFolderViews
igor@1338
   380
        case noSegue
igor@1338
   381
    }
xavier@1623
   382
igor@1338
   383
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
igor@1338
   384
        switch segueIdentifier(for: segue) {
xavier@1623
   385
        case .segueReplyAll:
xavier@1623
   386
            if let nav = segue.destination as? UINavigationController,
xavier@1623
   387
                let destination = nav.topViewController as? ComposeTableViewController,
xavier@1623
   388
                let cell = sender as? EmailListViewCell,
xavier@1623
   389
                let indexPath = self.tableView.indexPath(for: cell),
xavier@1623
   390
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1623
   391
                destination.composeMode = .replyAll
xavier@1623
   392
                destination.appConfig = config?.appConfig
xavier@1623
   393
                destination.originalMessage = email
xavier@1623
   394
            }
igor@1338
   395
            break
igor@1338
   396
        case .segueShowEmail:
dirk@1594
   397
            if let vc = segue.destination as? EmailViewController,
igor@1338
   398
                let cell = sender as? EmailListViewCell,
igor@1338
   399
                let indexPath = self.tableView.indexPath(for: cell),
dirk@1594
   400
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1623
   401
                vc.appConfig = config?.appConfig
xavier@1623
   402
                vc.message = email
xavier@2404
   403
                vc.folderShow = viewModel?.folderToShow
xavier@2404
   404
                vc.messageId = indexPath.row
igor@1338
   405
            }
igor@1338
   406
            break
xavier@1664
   407
        case .segueForward:
xavier@1664
   408
            if let nav = segue.destination as? UINavigationController,
xavier@1664
   409
                let destination = nav.topViewController as? ComposeTableViewController,
xavier@1664
   410
                let cell = sender as? EmailListViewCell,
xavier@1664
   411
                let indexPath = self.tableView.indexPath(for: cell),
xavier@1664
   412
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1664
   413
                destination.composeMode = .forward
xavier@1664
   414
                destination.appConfig = config?.appConfig
xavier@1664
   415
                destination.originalMessage = email
xavier@1664
   416
            }
xavier@1664
   417
            break
xavier@1865
   418
        case .segueFilter:
xavier@1865
   419
            if let destiny = segue.destination as? FilterTableViewController {
xavier@2369
   420
                destiny.filterDelegate = self.viewModel
xavier@1865
   421
                destiny.inFolder = false
xavier@2445
   422
                destiny.filterEnabled = self.viewModel?.folderToShow?.filter
xavier@2325
   423
                destiny.hidesBottomBarWhenPushed = true
xavier@1865
   424
            }
xavier@1865
   425
            break
dirk@2240
   426
        case .segueAddNewAccount:
xavier@2254
   427
            if let vc = segue.destination as? LoginTableViewController {
dirk@2240
   428
                vc.appConfig = config?.appConfig
xavier@2254
   429
                vc.hidesBottomBarWhenPushed = true
dirk@2240
   430
            }
xavier@2248
   431
        case .segueFolderViews:
xavier@2248
   432
            if let vC = segue.destination as? FolderTableViewController {
xavier@2248
   433
                vC.appConfig = config?.appConfig
xavier@2254
   434
                vC.hidesBottomBarWhenPushed = true
xavier@2248
   435
            }
dirk@2240
   436
        case .segueEditAccounts, .segueCompose, .noSegue:
dirk@1694
   437
            break
igor@1338
   438
        }
xavier@1865
   439
igor@1338
   440
    }
xavier@2254
   441
    
xavier@2254
   442
    @IBAction func segueUnwindAccountAdded(segue: UIStoryboardSegue) {
xavier@2254
   443
    }
xavier@2254
   444
dirk@1350
   445
    func didChangeInternal(messageFolder: MessageFolder) {
dirk@1350
   446
        if let folder = config?.folder,
dirk@1350
   447
            let message = messageFolder as? Message,
dirk@1558
   448
            folder.contains(message: message, deletedMessagesAreContained: true) {
dirk@2181
   449
            if message.isOriginal {
dirk@2181
   450
                // new message has arrived
dirk@2181
   451
                if let index = folder.indexOf(message: message) {
dirk@2181
   452
                    let ip = IndexPath(row: index, section: 0)
dirk@2181
   453
                    Log.info(
dirk@2181
   454
                        component: #function,
dirk@2181
   455
                        content: "insert message at \(index), \(folder.messageCount()) messages")
dirk@2181
   456
                    tableView.insertRows(at: [ip], with: .automatic)
dirk@1535
   457
                } else {
dirk@2181
   458
                    tableView.reloadData()
dirk@2181
   459
                }
dirk@2181
   460
            } else if message.isGhost {
xavier@2363
   461
                if let vm = viewModel, let cell = vm.cellFor(message: message), let ip = tableView.indexPath(for: cell) {
dirk@2181
   462
                    Log.info(
dirk@2181
   463
                        component: #function,
dirk@2181
   464
                        content: "delete message at \(index), \(folder.messageCount()) messages")
dirk@2181
   465
                    tableView.deleteRows(at: [ip], with: .automatic)
dirk@2181
   466
                } else {
dirk@2181
   467
                    tableView.reloadData()
dirk@2181
   468
                }
dirk@2181
   469
            } else {
dirk@2181
   470
                // other flags than delete must have been changed
xavier@2363
   471
                if let vm = viewModel, let cell = vm.cellFor(message: message) {
dirk@2181
   472
                    cell.updateFlags(message: message)
dirk@2181
   473
                } else {
dirk@2181
   474
                    tableView.reloadData()
dirk@1350
   475
                }
dirk@1350
   476
            }
dirk@1350
   477
        }
dirk@1350
   478
    }
dirk@1737
   479
igor@1338
   480
}
dirk@1348
   481
dirk@1348
   482
// MARK: - MessageFolderDelegate
dirk@1348
   483
dirk@1348
   484
extension EmailListViewController: MessageFolderDelegate {
dirk@1348
   485
    func didChange(messageFolder: MessageFolder) {
dirk@1675
   486
        GCD.onMainWait {
dirk@1350
   487
            self.didChangeInternal(messageFolder: messageFolder)
dirk@1348
   488
        }
dirk@1348
   489
    }
dirk@1348
   490
}
xavier@2369
   491
xavier@2369
   492
extension EmailListViewController: tableViewUpdate {
xavier@2369
   493
    func updateView() {
xavier@2369
   494
        self.tableView.reloadData()
xavier@2369
   495
    }
xavier@2369
   496
}