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