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