pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author buff <andreas@pep-project.org>
Wed, 27 Sep 2017 12:21:43 +0200
changeset 3111 97faecad8d57
parent 3110 18fe499bc0cc
child 3120 f03eb100b4af
child 3161 b47f06f24dc1
permissions -rw-r--r--
IOS-744 first step refactoring compose email cells
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
dirk@2854
    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
dirk@3100
    23
class EmailListViewController: BaseTableViewController {
andreas@2838
    24
    public static let storyboardId = "EmailListViewController"
dirk@583
    25
    struct UIState {
dirk@583
    26
        var isSynching: Bool = false
dirk@583
    27
    }
dirk@30
    28
dirk@1344
    29
    var config: EmailListConfig?
xavier@2362
    30
    var viewModel: EmailListViewModel?
dirk@275
    31
    var state = UIState()
igor@1301
    32
    let searchController = UISearchController(searchResultsController: nil)
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!
dirk@2067
    44
dirk@275
    45
    override func viewDidLoad() {
ylandert@935
    46
        super.viewDidLoad()
dirk@2189
    47
        title = NSLocalizedString("Inbox", comment: "General name for (unified) inbox")
igor@1284
    48
        UIHelper.emailListTableHeight(self.tableView)
igor@1301
    49
        addSearchBar()
dirk@275
    50
    }
dirk@31
    51
dirk@784
    52
    override func viewWillAppear(_ animated: Bool) {
igor@1301
    53
        super.viewWillAppear(animated)
xavier@2325
    54
        self.navigationController?.setToolbarHidden(false, animated: true)
dirk@1344
    55
        if MiscUtil.isUnitTest() {
dirk@1344
    56
            return
dirk@1344
    57
        }
dirk@1344
    58
xavier@2363
    59
        if let vm = viewModel {
xavier@2363
    60
            self.textFilterButton.isEnabled = vm.filterEnabled
xavier@2967
    61
            updateFilterText()
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
        }
dirk@1348
    84
    }
dirk@1348
    85
andreas@2683
    86
    private func updateLastLookAt(on folder: Folder) {
andreas@2683
    87
        if folder.isUnified {
andreas@2683
    88
            folder.updateLastLookAt()
andreas@2683
    89
        } else {
andreas@2683
    90
            folder.updateLastLookAtAndSave()
andreas@2683
    91
        }
andreas@2683
    92
    }
xavier@1948
    93
dirk@1348
    94
    override func viewWillDisappear(_ animated: Bool) {
dirk@1348
    95
        super.viewWillDisappear(animated)
dirk@1348
    96
        MessageModelConfig.messageFolderDelegate = nil
igor@1301
    97
    }
xavier@1623
    98
andreas@2683
    99
    func setupConfig() {
xavier@2132
   100
        if config == nil {
dirk@3091
   101
            config = EmailListConfig(appConfig: appConfig,
dirk@2191
   102
                                     folder: Folder.unifiedInbox())
xavier@2132
   103
        }
xavier@2362
   104
dirk@2240
   105
        if Account.all().isEmpty {
dirk@2240
   106
            performSegue(withIdentifier:.segueAddNewAccount, sender: self)
dirk@2240
   107
        }
andreas@2683
   108
andreas@2697
   109
        guard let folder = config?.folder else {
andreas@2697
   110
            return
andreas@2697
   111
        }
andreas@2697
   112
        self.title = realName(of: folder)
igor@1338
   113
    }
xavier@1623
   114
igor@1301
   115
    func addSearchBar() {
igor@1301
   116
        searchController.searchResultsUpdater = self
igor@1301
   117
        searchController.dimsBackgroundDuringPresentation = false
igor@1301
   118
        searchController.delegate = self
igor@1301
   119
        definesPresentationContext = true
igor@1301
   120
        tableView.tableHeaderView = searchController.searchBar
igor@1301
   121
        tableView.setContentOffset(CGPoint(x: 0.0, y: 40.0), animated: false)
igor@1301
   122
    }
dirk@705
   123
dirk@854
   124
    func updateModel() {
igor@1318
   125
        tableView.reloadData()
dirk@31
   126
    }
dirk@31
   127
xavier@1832
   128
    @IBAction func showUnreadButtonTapped(_ sender: UIBarButtonItem) {
xavier@2979
   129
        handlefilter()
xavier@2979
   130
    }
xavier@2979
   131
xavier@2979
   132
    func handlefilter() {
xavier@2363
   133
        if let vm = viewModel {
xavier@2363
   134
            if vm.filterEnabled {
xavier@2363
   135
                vm.filterEnabled = false
xavier@2979
   136
                handleButtonFilter(enabled: false)
xavier@2425
   137
                if config != nil {
xavier@2425
   138
                    vm.resetFilters()
xavier@2425
   139
                }
xavier@2363
   140
            } else {
xavier@2363
   141
                vm.filterEnabled = true
xavier@2363
   142
                if config != nil {
xavier@3097
   143
                    vm.enableFilter()
xavier@2363
   144
                }
xavier@2979
   145
                handleButtonFilter(enabled: true)
xavier@1832
   146
            }
xavier@2363
   147
            self.textFilterButton.isEnabled = vm.filterEnabled
xavier@1832
   148
        }
andreas@2697
   149
    }
xavier@2363
   150
xavier@2979
   151
    func handleButtonFilter(enabled: Bool) {
xavier@2979
   152
        if enabled == false {
xavier@2979
   153
            textFilterButton.title = ""
xavier@2979
   154
            enableFilterButton.image = UIImage(named: "unread-icon")
xavier@2979
   155
        } else {
xavier@2979
   156
            enableFilterButton.image = UIImage(named: "unread-icon-active")
xavier@2979
   157
            updateFilterText()
xavier@2979
   158
        }
xavier@2979
   159
    }
xavier@2979
   160
xavier@2967
   161
    func updateFilterText() {
xavier@2967
   162
        if let vm = viewModel, let txt = vm.enabledFilters?.text {
xavier@2967
   163
            textFilterButton.title = "Filter by: " + txt
xavier@2967
   164
        }
xavier@2967
   165
    }
xavier@2967
   166
andreas@2697
   167
    // MARK: - Private
xavier@1832
   168
andreas@2697
   169
    private func realName(of folder: Folder) -> String? {
andreas@2697
   170
        if folder.isUnified {
andreas@2697
   171
            return folder.name
andreas@2697
   172
        } else {
andreas@2697
   173
            return folder.realName
andreas@2697
   174
        }
xavier@1832
   175
    }
xavier@1832
   176
dirk@58
   177
    // MARK: - UI State
dirk@58
   178
dirk@58
   179
    func updateUI() {
dirk@784
   180
        UIApplication.shared.isNetworkActivityIndicatorVisible = state.isSynching
dirk@748
   181
        if !state.isSynching {
igor@1260
   182
            refreshControl?.endRefreshing()
dirk@58
   183
        }
dirk@58
   184
    }
dirk@58
   185
dirk@31
   186
    // MARK: - UITableViewDataSource
dirk@31
   187
dirk@784
   188
    override func numberOfSections(in tableView: UITableView) -> Int {
xavier@2362
   189
        if let _ = viewModel?.folderToShow {
dirk@854
   190
            return 1
dirk@209
   191
        }
dirk@854
   192
        return 0
dirk@31
   193
    }
dirk@31
   194
dirk@784
   195
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
xavier@2362
   196
        if let vm = viewModel {
xavier@2362
   197
            return vm.count
dirk@31
   198
        }
dirk@31
   199
        return 0
dirk@31
   200
    }
dirk@31
   201
dirk@784
   202
    override func tableView(_ tableView: UITableView,
dirk@784
   203
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dirk@784
   204
        let cell = tableView.dequeueReusableCell(
dirk@784
   205
            withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
andreas@2838
   206
        let _ = cell.configureCell(config: config, indexPath: indexPath, session: session)
xavier@2404
   207
        viewModel?.associate(cell: cell, position: indexPath.row)
dirk@31
   208
        return cell
dirk@31
   209
    }
dirk@31
   210
dirk@719
   211
    // MARK: - UITableViewDelegate
dirk@719
   212
dirk@784
   213
    override func tableView(_ tableView: UITableView, editActionsForRowAt
dirk@784
   214
        indexPath: IndexPath)-> [UITableViewRowAction]? {
xavier@1623
   215
dirk@784
   216
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
igor@1260
   217
        if let email = cell.messageAt(indexPath: indexPath, config: config) {
igor@1576
   218
            let flagAction = createFlagAction(message: email, cell: cell)
dirk@1507
   219
            let deleteAction = createDeleteAction(message: email, cell: cell)
igor@1242
   220
            let moreAction = createMoreAction(message: email, cell: cell)
igor@1576
   221
            return [deleteAction, flagAction, moreAction]
dirk@855
   222
        }
dirk@855
   223
        return nil
dirk@744
   224
    }
dirk@744
   225
dirk@719
   226
    // MARK: - Misc
dirk@719
   227
dirk@1507
   228
    func createRowAction(cell: EmailListViewCell,
xavier@1623
   229
                         image: UIImage?, action: @escaping (UITableViewRowAction, IndexPath) -> Void,
xavier@1623
   230
                         title: String) -> UITableViewRowAction {
dirk@1510
   231
        let rowAction = UITableViewRowAction(
dirk@1510
   232
            style: .normal, title: title, handler: action)
dirk@1507
   233
dirk@1507
   234
        if let theImage = image {
dirk@1507
   235
            let iconColor = UIColor(patternImage: theImage)
dirk@1507
   236
            rowAction.backgroundColor = iconColor
dirk@744
   237
        }
dirk@744
   238
dirk@1507
   239
        return rowAction
dirk@744
   240
    }
dirk@744
   241
dirk@1507
   242
    func createFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   243
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1522
   244
            if message.imapFlags == nil {
dirk@1522
   245
                Log.warn(component: #function, content: "message.imapFlags == nil")
dirk@1522
   246
            }
dirk@1605
   247
            if cell.isFlagged(message: message) {
dirk@1507
   248
                message.imapFlags?.flagged = false
dirk@1507
   249
            } else {
dirk@1507
   250
                message.imapFlags?.flagged = true
dirk@1507
   251
            }
dirk@1507
   252
            message.save()
dirk@1507
   253
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@1507
   254
        }
dirk@1510
   255
dirk@2639
   256
        let flagString = NSLocalizedString("Flag", comment: "Message action (on swipe)")
dirk@2639
   257
        var title = "\n\n\(flagString)"
dirk@2639
   258
        let unflagString = NSLocalizedString("Unflag", comment: "Message action (on swipe)")
dirk@1605
   259
        if message.imapFlags?.flagged ?? true {
dirk@2639
   260
            title = "\n\n\(unflagString)"
dirk@744
   261
        }
dirk@744
   262
dirk@1510
   263
        return createRowAction(
dirk@1510
   264
            cell: cell, image: UIImage(named: "swipe-flag"), action: action, title: title)
dirk@744
   265
    }
dirk@744
   266
dirk@1507
   267
    func createDeleteAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   268
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1507
   269
            guard let message = cell.messageAt(indexPath: indexPath, config: self.config) else {
dirk@1507
   270
                return
dirk@1507
   271
            }
xavier@1623
   272
dirk@1650
   273
            message.delete() // mark for deletion/trash
igor@1576
   274
            self.tableView.reloadData()
dirk@1507
   275
        }
dirk@744
   276
dirk@2639
   277
        let title = NSLocalizedString("Delete", comment: "Message action (on swipe)")
dirk@1510
   278
        return createRowAction(
dirk@1510
   279
            cell: cell, image: UIImage(named: "swipe-trash"), action: action,
dirk@2639
   280
            title: "\n\n\(title)")
dirk@1507
   281
    }
dirk@1507
   282
dirk@1507
   283
    func createMarkAsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   284
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1605
   285
            if cell.haveSeen(message: message) {
dirk@1507
   286
                message.imapFlags?.seen = false
dirk@1507
   287
            } else {
dirk@1507
   288
                message.imapFlags?.seen = true
dirk@1507
   289
            }
dirk@1507
   290
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   291
        }
dirk@1510
   292
dirk@1510
   293
        var title = NSLocalizedString(
dirk@2190
   294
            "Unread", comment: "Message action (on swipe)")
dirk@1605
   295
        if !cell.haveSeen(message: message) {
dirk@1510
   296
            title = NSLocalizedString(
dirk@2190
   297
                "Read", comment: "Message action (on swipe)")
dirk@1507
   298
        }
dirk@1507
   299
dirk@1507
   300
        let isReadAction = createRowAction(cell: cell, image: nil, action: action,
dirk@1510
   301
                                           title: title)
dirk@784
   302
        isReadAction.backgroundColor = UIColor.blue
dirk@744
   303
dirk@744
   304
        return isReadAction
dirk@744
   305
    }
xavier@1623
   306
igor@1242
   307
    func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1510
   308
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
igor@1242
   309
            self.showMoreActionSheet(cell: cell)
igor@1242
   310
        }
dirk@1510
   311
dirk@2639
   312
        let title = NSLocalizedString("More", comment: "Message action (on swipe)")
dirk@1510
   313
        return createRowAction(
dirk@1510
   314
            cell: cell, image: UIImage(named: "swipe-more"), action: action,
dirk@2639
   315
            title: "\n\n\(title)")
igor@1242
   316
    }
xavier@1623
   317
igor@1242
   318
    // MARK: - Action Sheet
xavier@1623
   319
igor@1242
   320
    func showMoreActionSheet(cell: EmailListViewCell) {
igor@1242
   321
        let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
igor@1438
   322
        alertControler.view.tintColor = .pEpGreen
igor@1242
   323
        let cancelAction = createCancelAction()
igor@1242
   324
        let replyAction = createReplyAction(cell: cell)
xavier@1623
   325
        let replyAllAction = createReplyAllAction(cell: cell)
igor@1242
   326
        let forwardAction = createForwardAction(cell: cell)
igor@1242
   327
        alertControler.addAction(cancelAction)
igor@1242
   328
        alertControler.addAction(replyAction)
xavier@1620
   329
        alertControler.addAction(replyAllAction)
igor@1242
   330
        alertControler.addAction(forwardAction)
igor@1414
   331
        if let popoverPresentationController = alertControler.popoverPresentationController {
igor@1414
   332
            popoverPresentationController.sourceView = cell
igor@1414
   333
        }
igor@1242
   334
        present(alertControler, animated: true, completion: nil)
igor@1242
   335
    }
xavier@1623
   336
igor@1242
   337
    // MARK: - Action Sheet Actions
igor@1242
   338
igor@1242
   339
    func createCancelAction() -> UIAlertAction {
xavier@1623
   340
        return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
igor@1242
   341
    }
xavier@1623
   342
igor@1242
   343
    func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
igor@1242
   344
        return UIAlertAction(title: "Reply", style: .default) { (action) in
xavier@2715
   345
            self.performSegue(withIdentifier: .segueReply, sender: cell)
igor@1242
   346
        }
igor@1242
   347
    }
xavier@1620
   348
xavier@1620
   349
    func createReplyAllAction(cell: EmailListViewCell) ->  UIAlertAction {
xavier@1623
   350
        return UIAlertAction(title: "Reply All", style: .default) { (action) in
xavier@1623
   351
            self.performSegue(withIdentifier: .segueReplyAll, sender: cell)
xavier@1620
   352
        }
xavier@1620
   353
    }
xavier@1620
   354
igor@1242
   355
    func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
igor@1242
   356
        return UIAlertAction(title: "Forward", style: .default) { (action) in
xavier@1664
   357
            self.performSegue(withIdentifier: .segueForward, sender: cell)
igor@1242
   358
        }
igor@1242
   359
    }
xavier@1623
   360
dirk@784
   361
}
igor@1301
   362
igor@1301
   363
extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
igor@1301
   364
    public func updateSearchResults(for searchController: UISearchController) {
xavier@2369
   365
        if let vm = viewModel {
xavier@2369
   366
            vm.filterContentForSearchText(searchText: searchController.searchBar.text!, clear: false)
xavier@2369
   367
        }
igor@1301
   368
    }
xavier@1623
   369
igor@1301
   370
    func didDismissSearchController(_ searchController: UISearchController) {
xavier@2369
   371
        if let vm = viewModel {
xavier@2369
   372
            vm.filterContentForSearchText(clear: true)
xavier@2369
   373
        }
igor@1301
   374
    }
igor@1301
   375
}
igor@1338
   376
igor@1338
   377
// MARK: - Navigation
igor@1338
   378
igor@1338
   379
extension EmailListViewController: SegueHandlerType {
xavier@1623
   380
igor@1338
   381
    // MARK: - SegueHandlerType
xavier@1623
   382
igor@1338
   383
    enum SegueIdentifier: String {
igor@1338
   384
        case segueAddNewAccount
igor@1338
   385
        case segueShowEmail
igor@1338
   386
        case segueCompose
xavier@2715
   387
        case segueReply
xavier@1623
   388
        case segueReplyAll
xavier@1664
   389
        case segueForward
xavier@1865
   390
        case segueFilter
xavier@2248
   391
        case segueFolderViews
igor@1338
   392
        case noSegue
igor@1338
   393
    }
xavier@1623
   394
andreas@3110
   395
    private func currentMessage(senderCell: Any?) -> (Message, IndexPath)? {
dirk@2957
   396
        if let cell = senderCell as? EmailListViewCell,
dirk@2957
   397
            let indexPath = self.tableView.indexPath(for: cell),
dirk@2957
   398
            let message = cell.messageAt(indexPath: indexPath, config: config) {
dirk@2957
   399
            return (message, indexPath)
dirk@2957
   400
        }
dirk@2957
   401
        return nil
dirk@2957
   402
    }
dirk@2957
   403
andreas@3111
   404
    /// Figures out the the appropriate account to use as sender ("from" field) when composing a mail.
andreas@3110
   405
    ///
andreas@3110
   406
    /// - Parameter vc: viewController to set the origin on
andreas@3111
   407
    private func origin() -> Identity? {
andreas@3110
   408
        guard let folder = viewModel?.folderToShow else {
andreas@3110
   409
            Log.shared.errorAndCrash(component: #function, errorString: "No folder shown?")
andreas@3111
   410
            return Account.defaultAccount()?.user
andreas@3110
   411
        }
andreas@3110
   412
        if folder.isUnified {
andreas@3110
   413
            //Set compose views sender ("from" field) to the default account.
andreas@3111
   414
            return Account.defaultAccount()?.user
andreas@3110
   415
        } else {
andreas@3110
   416
            //Set compose views sender ("from" field) to the account we are currently viewing emails for
andreas@3111
   417
            return folder.account.user
andreas@3110
   418
        }
andreas@3110
   419
    }
andreas@3110
   420
andreas@3110
   421
    private func setup(composeViewController vc: ComposeTableViewController,
andreas@3110
   422
                       composeMode: ComposeTableViewController.ComposeMode = .normal,
andreas@3110
   423
                       originalMessage: Message? = nil) {
andreas@3110
   424
        vc.appConfig = appConfig
andreas@3110
   425
        vc.composeMode = composeMode
andreas@3110
   426
        vc.originalMessage = originalMessage
andreas@3111
   427
        vc.origin = origin()
andreas@3110
   428
    }
andreas@3110
   429
igor@1338
   430
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
igor@1338
   431
        switch segueIdentifier(for: segue) {
xavier@2715
   432
        case .segueReply:
andreas@2850
   433
            guard let nav = segue.destination as? UINavigationController,
xavier@2715
   434
                let destination = nav.topViewController as? ComposeTableViewController,
dirk@2957
   435
                let (theMessage, _) = currentMessage(senderCell: sender) else {
andreas@2850
   436
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   437
                    return
xavier@2715
   438
            }
andreas@3110
   439
            setup(composeViewController: destination, composeMode: .replyFrom,
andreas@3110
   440
                  originalMessage: theMessage)
xavier@1623
   441
        case .segueReplyAll:
andreas@2850
   442
            guard let nav = segue.destination as? UINavigationController,
xavier@1623
   443
                let destination = nav.topViewController as? ComposeTableViewController,
dirk@2957
   444
                let (theMessage, _) = currentMessage(senderCell: sender)  else {
andreas@2850
   445
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   446
                    return
xavier@1623
   447
            }
andreas@3110
   448
            setup(composeViewController: destination, composeMode: .replyAll,
andreas@3110
   449
                  originalMessage: theMessage)
andreas@3111
   450
        case .segueShowEmail:
andreas@2850
   451
            guard let vc = segue.destination as? EmailViewController,
dirk@2957
   452
                let (theMessage, indexPath) = currentMessage(senderCell: sender) else {
andreas@2850
   453
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   454
                    return
igor@1338
   455
            }
andreas@2850
   456
            vc.appConfig = appConfig
dirk@2957
   457
            vc.message = theMessage
andreas@2850
   458
            vc.folderShow = viewModel?.folderToShow
andreas@2850
   459
            vc.messageId = indexPath.row
xavier@1664
   460
        case .segueForward:
andreas@2850
   461
            guard let nav = segue.destination as? UINavigationController,
xavier@1664
   462
                let destination = nav.topViewController as? ComposeTableViewController,
dirk@2957
   463
                let (theMessage, _) = currentMessage(senderCell: sender) else {
andreas@2850
   464
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   465
                    return
xavier@1664
   466
            }
andreas@3110
   467
            setup(composeViewController: destination, composeMode: .forward,
andreas@3110
   468
                  originalMessage: theMessage)
andreas@2850
   469
        case .segueFilter:
andreas@2850
   470
            guard let destiny = segue.destination as? FilterTableViewController  else {
andreas@2850
   471
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   472
                return
andreas@2850
   473
            }
andreas@2850
   474
            destiny.appConfig = appConfig
andreas@2850
   475
            destiny.filterDelegate = viewModel
andreas@2850
   476
            destiny.inFolder = false
andreas@2850
   477
            destiny.filterEnabled = viewModel?.folderToShow?.filter
andreas@2850
   478
            destiny.hidesBottomBarWhenPushed = true
andreas@2850
   479
        case .segueAddNewAccount:
andreas@2850
   480
            guard let vc = segue.destination as? LoginTableViewController  else {
andreas@2850
   481
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   482
                return
andreas@2850
   483
            }
andreas@2850
   484
            vc.appConfig = appConfig
andreas@2850
   485
            vc.hidesBottomBarWhenPushed = true
xavier@1664
   486
            break
andreas@2850
   487
        case .segueFolderViews:
andreas@2850
   488
            guard let vC = segue.destination as? FolderTableViewController  else {
andreas@2850
   489
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   490
                return
xavier@1865
   491
            }
andreas@2850
   492
            vC.appConfig = appConfig
andreas@2850
   493
            vC.hidesBottomBarWhenPushed = true
xavier@1865
   494
            break
andreas@2850
   495
        case .segueCompose:
andreas@2850
   496
            guard let nav = segue.destination as? UINavigationController,
dirk@2957
   497
                let destination = nav.rootViewController as? ComposeTableViewController else {
andreas@2850
   498
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   499
                    return
dirk@2240
   500
            }
andreas@3110
   501
            setup(composeViewController: destination)
andreas@2850
   502
        default:
andreas@2850
   503
            Log.shared.errorAndCrash(component: #function, errorString: "Unhandled segue")
dirk@1694
   504
            break
igor@1338
   505
        }
andreas@2838
   506
    }
xavier@1865
   507
xavier@2254
   508
    @IBAction func segueUnwindAccountAdded(segue: UIStoryboardSegue) {
xavier@2254
   509
    }
xavier@2254
   510
dirk@1350
   511
    func didChangeInternal(messageFolder: MessageFolder) {
dirk@1350
   512
        if let folder = config?.folder,
dirk@1350
   513
            let message = messageFolder as? Message,
dirk@1558
   514
            folder.contains(message: message, deletedMessagesAreContained: true) {
dirk@2181
   515
            if message.isOriginal {
dirk@2181
   516
                // new message has arrived
dirk@2181
   517
                if let index = folder.indexOf(message: message) {
dirk@2181
   518
                    let ip = IndexPath(row: index, section: 0)
dirk@2181
   519
                    Log.info(
dirk@2181
   520
                        component: #function,
dirk@2181
   521
                        content: "insert message at \(index), \(folder.messageCount()) messages")
dirk@2181
   522
                    tableView.insertRows(at: [ip], with: .automatic)
dirk@1535
   523
                } else {
dirk@2181
   524
                    tableView.reloadData()
dirk@2181
   525
                }
dirk@2181
   526
            } else if message.isGhost {
xavier@2363
   527
                if let vm = viewModel, let cell = vm.cellFor(message: message), let ip = tableView.indexPath(for: cell) {
dirk@2181
   528
                    Log.info(
dirk@2181
   529
                        component: #function,
dirk@2181
   530
                        content: "delete message at \(index), \(folder.messageCount()) messages")
dirk@2181
   531
                    tableView.deleteRows(at: [ip], with: .automatic)
dirk@2181
   532
                } else {
dirk@2181
   533
                    tableView.reloadData()
dirk@2181
   534
                }
dirk@2181
   535
            } else {
dirk@2181
   536
                // other flags than delete must have been changed
xavier@2363
   537
                if let vm = viewModel, let cell = vm.cellFor(message: message) {
dirk@2181
   538
                    cell.updateFlags(message: message)
dirk@2181
   539
                } else {
dirk@2181
   540
                    tableView.reloadData()
dirk@1350
   541
                }
dirk@1350
   542
            }
dirk@1350
   543
        }
dirk@1350
   544
    }
dirk@1737
   545
igor@1338
   546
}
dirk@1348
   547
dirk@1348
   548
// MARK: - MessageFolderDelegate
dirk@1348
   549
dirk@1348
   550
extension EmailListViewController: MessageFolderDelegate {
dirk@1348
   551
    func didChange(messageFolder: MessageFolder) {
dirk@1675
   552
        GCD.onMainWait {
dirk@1350
   553
            self.didChangeInternal(messageFolder: messageFolder)
dirk@1348
   554
        }
dirk@1348
   555
    }
dirk@1348
   556
}
xavier@2369
   557
dirk@2818
   558
extension EmailListViewController: TableViewUpdate {
xavier@2369
   559
    func updateView() {
xavier@2979
   560
        if let vm = self.viewModel, let filter = vm.folderToShow?.filter, filter.isDefault() {
xavier@2979
   561
            vm.filterEnabled = false
xavier@2979
   562
            handleButtonFilter(enabled: false)
xavier@2979
   563
        }
xavier@2369
   564
        self.tableView.reloadData()
xavier@2369
   565
    }
xavier@2369
   566
}