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