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