pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Tue, 26 Sep 2017 10:31:43 +0200
changeset 3091 6c2d7f0016a1
parent 3042 a9e8ff283a44
child 3097 3f3527e92503
permissions -rw-r--r--
IOS-714 appConfig no longer optional
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
andreas@2838
    23
class EmailListViewController: TableViewControllerBase {
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@2369
   143
                    vm.updateFilter(filter: Filter.unread())
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
xavier@2344
   206
        //mantener el configure cell para tal de no generar un vm para celdas
andreas@2838
   207
        let _ = cell.configureCell(config: config, indexPath: indexPath, session: session)
xavier@2404
   208
        viewModel?.associate(cell: cell, position: indexPath.row)
dirk@31
   209
        return cell
dirk@31
   210
    }
dirk@31
   211
dirk@719
   212
    // MARK: - UITableViewDelegate
dirk@719
   213
dirk@784
   214
    override func tableView(_ tableView: UITableView, editActionsForRowAt
dirk@784
   215
        indexPath: IndexPath)-> [UITableViewRowAction]? {
xavier@1623
   216
dirk@784
   217
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
igor@1260
   218
        if let email = cell.messageAt(indexPath: indexPath, config: config) {
igor@1576
   219
            let flagAction = createFlagAction(message: email, cell: cell)
dirk@1507
   220
            let deleteAction = createDeleteAction(message: email, cell: cell)
igor@1242
   221
            let moreAction = createMoreAction(message: email, cell: cell)
igor@1576
   222
            return [deleteAction, flagAction, moreAction]
dirk@855
   223
        }
dirk@855
   224
        return nil
dirk@744
   225
    }
dirk@744
   226
dirk@719
   227
    // MARK: - Misc
dirk@719
   228
dirk@1507
   229
    func createRowAction(cell: EmailListViewCell,
xavier@1623
   230
                         image: UIImage?, action: @escaping (UITableViewRowAction, IndexPath) -> Void,
xavier@1623
   231
                         title: String) -> UITableViewRowAction {
dirk@1510
   232
        let rowAction = UITableViewRowAction(
dirk@1510
   233
            style: .normal, title: title, handler: action)
dirk@1507
   234
dirk@1507
   235
        if let theImage = image {
dirk@1507
   236
            let iconColor = UIColor(patternImage: theImage)
dirk@1507
   237
            rowAction.backgroundColor = iconColor
dirk@744
   238
        }
dirk@744
   239
dirk@1507
   240
        return rowAction
dirk@744
   241
    }
dirk@744
   242
dirk@1507
   243
    func createFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   244
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1522
   245
            if message.imapFlags == nil {
dirk@1522
   246
                Log.warn(component: #function, content: "message.imapFlags == nil")
dirk@1522
   247
            }
dirk@1605
   248
            if cell.isFlagged(message: message) {
dirk@1507
   249
                message.imapFlags?.flagged = false
dirk@1507
   250
            } else {
dirk@1507
   251
                message.imapFlags?.flagged = true
dirk@1507
   252
            }
dirk@1507
   253
            message.save()
dirk@1507
   254
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@1507
   255
        }
dirk@1510
   256
dirk@2639
   257
        let flagString = NSLocalizedString("Flag", comment: "Message action (on swipe)")
dirk@2639
   258
        var title = "\n\n\(flagString)"
dirk@2639
   259
        let unflagString = NSLocalizedString("Unflag", comment: "Message action (on swipe)")
dirk@1605
   260
        if message.imapFlags?.flagged ?? true {
dirk@2639
   261
            title = "\n\n\(unflagString)"
dirk@744
   262
        }
dirk@744
   263
dirk@1510
   264
        return createRowAction(
dirk@1510
   265
            cell: cell, image: UIImage(named: "swipe-flag"), action: action, title: title)
dirk@744
   266
    }
dirk@744
   267
dirk@1507
   268
    func createDeleteAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   269
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1507
   270
            guard let message = cell.messageAt(indexPath: indexPath, config: self.config) else {
dirk@1507
   271
                return
dirk@1507
   272
            }
xavier@1623
   273
dirk@1650
   274
            message.delete() // mark for deletion/trash
igor@1576
   275
            self.tableView.reloadData()
dirk@1507
   276
        }
dirk@744
   277
dirk@2639
   278
        let title = NSLocalizedString("Delete", comment: "Message action (on swipe)")
dirk@1510
   279
        return createRowAction(
dirk@1510
   280
            cell: cell, image: UIImage(named: "swipe-trash"), action: action,
dirk@2639
   281
            title: "\n\n\(title)")
dirk@1507
   282
    }
dirk@1507
   283
dirk@1507
   284
    func createMarkAsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   285
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1605
   286
            if cell.haveSeen(message: message) {
dirk@1507
   287
                message.imapFlags?.seen = false
dirk@1507
   288
            } else {
dirk@1507
   289
                message.imapFlags?.seen = true
dirk@1507
   290
            }
dirk@1507
   291
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   292
        }
dirk@1510
   293
dirk@1510
   294
        var title = NSLocalizedString(
dirk@2190
   295
            "Unread", comment: "Message action (on swipe)")
dirk@1605
   296
        if !cell.haveSeen(message: message) {
dirk@1510
   297
            title = NSLocalizedString(
dirk@2190
   298
                "Read", comment: "Message action (on swipe)")
dirk@1507
   299
        }
dirk@1507
   300
dirk@1507
   301
        let isReadAction = createRowAction(cell: cell, image: nil, action: action,
dirk@1510
   302
                                           title: title)
dirk@784
   303
        isReadAction.backgroundColor = UIColor.blue
dirk@744
   304
dirk@744
   305
        return isReadAction
dirk@744
   306
    }
xavier@1623
   307
igor@1242
   308
    func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1510
   309
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
igor@1242
   310
            self.showMoreActionSheet(cell: cell)
igor@1242
   311
        }
dirk@1510
   312
dirk@2639
   313
        let title = NSLocalizedString("More", comment: "Message action (on swipe)")
dirk@1510
   314
        return createRowAction(
dirk@1510
   315
            cell: cell, image: UIImage(named: "swipe-more"), action: action,
dirk@2639
   316
            title: "\n\n\(title)")
igor@1242
   317
    }
xavier@1623
   318
igor@1242
   319
    // MARK: - Action Sheet
xavier@1623
   320
igor@1242
   321
    func showMoreActionSheet(cell: EmailListViewCell) {
igor@1242
   322
        let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
igor@1438
   323
        alertControler.view.tintColor = .pEpGreen
igor@1242
   324
        let cancelAction = createCancelAction()
igor@1242
   325
        let replyAction = createReplyAction(cell: cell)
xavier@1623
   326
        let replyAllAction = createReplyAllAction(cell: cell)
igor@1242
   327
        let forwardAction = createForwardAction(cell: cell)
igor@1242
   328
        alertControler.addAction(cancelAction)
igor@1242
   329
        alertControler.addAction(replyAction)
xavier@1620
   330
        alertControler.addAction(replyAllAction)
igor@1242
   331
        alertControler.addAction(forwardAction)
igor@1414
   332
        if let popoverPresentationController = alertControler.popoverPresentationController {
igor@1414
   333
            popoverPresentationController.sourceView = cell
igor@1414
   334
        }
igor@1242
   335
        present(alertControler, animated: true, completion: nil)
igor@1242
   336
    }
xavier@1623
   337
igor@1242
   338
    // MARK: - Action Sheet Actions
igor@1242
   339
igor@1242
   340
    func createCancelAction() -> UIAlertAction {
xavier@1623
   341
        return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
igor@1242
   342
    }
xavier@1623
   343
igor@1242
   344
    func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
igor@1242
   345
        return UIAlertAction(title: "Reply", style: .default) { (action) in
xavier@2715
   346
            self.performSegue(withIdentifier: .segueReply, sender: cell)
igor@1242
   347
        }
igor@1242
   348
    }
xavier@1620
   349
xavier@1620
   350
    func createReplyAllAction(cell: EmailListViewCell) ->  UIAlertAction {
xavier@1623
   351
        return UIAlertAction(title: "Reply All", style: .default) { (action) in
xavier@1623
   352
            self.performSegue(withIdentifier: .segueReplyAll, sender: cell)
xavier@1620
   353
        }
xavier@1620
   354
    }
xavier@1620
   355
igor@1242
   356
    func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
igor@1242
   357
        return UIAlertAction(title: "Forward", style: .default) { (action) in
xavier@1664
   358
            self.performSegue(withIdentifier: .segueForward, sender: cell)
igor@1242
   359
        }
igor@1242
   360
    }
xavier@1623
   361
dirk@784
   362
}
igor@1301
   363
igor@1301
   364
extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
igor@1301
   365
    public func updateSearchResults(for searchController: UISearchController) {
xavier@2369
   366
        if let vm = viewModel {
xavier@2369
   367
            vm.filterContentForSearchText(searchText: searchController.searchBar.text!, clear: false)
xavier@2369
   368
        }
igor@1301
   369
    }
xavier@1623
   370
igor@1301
   371
    func didDismissSearchController(_ searchController: UISearchController) {
xavier@2369
   372
        if let vm = viewModel {
xavier@2369
   373
            vm.filterContentForSearchText(clear: true)
xavier@2369
   374
        }
igor@1301
   375
    }
igor@1301
   376
}
igor@1338
   377
igor@1338
   378
// MARK: - Navigation
igor@1338
   379
igor@1338
   380
extension EmailListViewController: SegueHandlerType {
xavier@1623
   381
igor@1338
   382
    // MARK: - SegueHandlerType
xavier@1623
   383
igor@1338
   384
    enum SegueIdentifier: String {
igor@1338
   385
        case segueAddNewAccount
igor@1338
   386
        case segueShowEmail
igor@1338
   387
        case segueCompose
xavier@2715
   388
        case segueReply
xavier@1623
   389
        case segueReplyAll
xavier@1664
   390
        case segueForward
xavier@1865
   391
        case segueFilter
xavier@2248
   392
        case segueFolderViews
igor@1338
   393
        case noSegue
igor@1338
   394
    }
xavier@1623
   395
dirk@2957
   396
    func currentMessage(senderCell: Any?) -> (Message, IndexPath)? {
dirk@2957
   397
        if let cell = senderCell as? EmailListViewCell,
dirk@2957
   398
            let indexPath = self.tableView.indexPath(for: cell),
dirk@2957
   399
            let message = cell.messageAt(indexPath: indexPath, config: config) {
dirk@2957
   400
            return (message, indexPath)
dirk@2957
   401
        }
dirk@2957
   402
        return nil
dirk@2957
   403
    }
dirk@2957
   404
igor@1338
   405
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
igor@1338
   406
        switch segueIdentifier(for: segue) {
xavier@2715
   407
        case .segueReply:
andreas@2850
   408
            guard let nav = segue.destination as? UINavigationController,
xavier@2715
   409
                let destination = nav.topViewController as? ComposeTableViewController,
dirk@2957
   410
                let (theMessage, _) = currentMessage(senderCell: sender) else {
andreas@2850
   411
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   412
                    return
xavier@2715
   413
            }
andreas@2850
   414
            destination.appConfig = appConfig
andreas@2850
   415
            destination.composeMode = .replyFrom
dirk@2957
   416
            destination.originalMessage = theMessage
xavier@1623
   417
        case .segueReplyAll:
andreas@2850
   418
            guard let nav = segue.destination as? UINavigationController,
xavier@1623
   419
                let destination = nav.topViewController as? ComposeTableViewController,
dirk@2957
   420
                let (theMessage, _) = currentMessage(senderCell: sender)  else {
andreas@2850
   421
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   422
                    return
xavier@1623
   423
            }
andreas@2850
   424
            destination.appConfig = appConfig
andreas@2850
   425
            destination.composeMode = .replyAll
dirk@2957
   426
            destination.originalMessage = theMessage
igor@1338
   427
        case .segueShowEmail:
andreas@2850
   428
            guard let vc = segue.destination as? EmailViewController,
dirk@2957
   429
                let (theMessage, indexPath) = currentMessage(senderCell: sender) else {
andreas@2850
   430
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   431
                    return
igor@1338
   432
            }
andreas@2850
   433
            vc.appConfig = appConfig
dirk@2957
   434
            vc.message = theMessage
andreas@2850
   435
            vc.folderShow = viewModel?.folderToShow
andreas@2850
   436
            vc.messageId = indexPath.row
xavier@1664
   437
        case .segueForward:
andreas@2850
   438
            guard let nav = segue.destination as? UINavigationController,
xavier@1664
   439
                let destination = nav.topViewController as? ComposeTableViewController,
dirk@2957
   440
                let (theMessage, _) = currentMessage(senderCell: sender) else {
andreas@2850
   441
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   442
                    return
xavier@1664
   443
            }
andreas@2850
   444
            destination.composeMode = .forward
andreas@2850
   445
            destination.appConfig = appConfig
dirk@2957
   446
            destination.originalMessage = theMessage
andreas@2850
   447
        case .segueFilter:
andreas@2850
   448
            guard let destiny = segue.destination as? FilterTableViewController  else {
andreas@2850
   449
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   450
                return
andreas@2850
   451
            }
andreas@2850
   452
            destiny.appConfig = appConfig
andreas@2850
   453
            destiny.filterDelegate = viewModel
andreas@2850
   454
            destiny.inFolder = false
andreas@2850
   455
            destiny.filterEnabled = viewModel?.folderToShow?.filter
andreas@2850
   456
            destiny.hidesBottomBarWhenPushed = true
andreas@2850
   457
        case .segueAddNewAccount:
andreas@2850
   458
            guard let vc = segue.destination as? LoginTableViewController  else {
andreas@2850
   459
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   460
                return
andreas@2850
   461
            }
andreas@2850
   462
            vc.appConfig = appConfig
andreas@2850
   463
            vc.hidesBottomBarWhenPushed = true
xavier@1664
   464
            break
andreas@2850
   465
        case .segueFolderViews:
andreas@2850
   466
            guard let vC = segue.destination as? FolderTableViewController  else {
andreas@2850
   467
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   468
                return
xavier@1865
   469
            }
andreas@2850
   470
            vC.appConfig = appConfig
andreas@2850
   471
            vC.hidesBottomBarWhenPushed = true
xavier@1865
   472
            break
andreas@2850
   473
        case .segueCompose:
andreas@2850
   474
            guard let nav = segue.destination as? UINavigationController,
dirk@2957
   475
                let destination = nav.rootViewController as? ComposeTableViewController else {
andreas@2850
   476
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   477
                    return
dirk@2240
   478
            }
andreas@2850
   479
            destination.appConfig = appConfig
dirk@2959
   480
            destination.composeMode = .normal
andreas@2850
   481
        default:
andreas@2850
   482
            Log.shared.errorAndCrash(component: #function, errorString: "Unhandled segue")
dirk@1694
   483
            break
igor@1338
   484
        }
andreas@2838
   485
    }
xavier@1865
   486
xavier@2254
   487
    @IBAction func segueUnwindAccountAdded(segue: UIStoryboardSegue) {
xavier@2254
   488
    }
xavier@2254
   489
dirk@1350
   490
    func didChangeInternal(messageFolder: MessageFolder) {
dirk@1350
   491
        if let folder = config?.folder,
dirk@1350
   492
            let message = messageFolder as? Message,
dirk@1558
   493
            folder.contains(message: message, deletedMessagesAreContained: true) {
dirk@2181
   494
            if message.isOriginal {
dirk@2181
   495
                // new message has arrived
dirk@2181
   496
                if let index = folder.indexOf(message: message) {
dirk@2181
   497
                    let ip = IndexPath(row: index, section: 0)
dirk@2181
   498
                    Log.info(
dirk@2181
   499
                        component: #function,
dirk@2181
   500
                        content: "insert message at \(index), \(folder.messageCount()) messages")
dirk@2181
   501
                    tableView.insertRows(at: [ip], with: .automatic)
dirk@1535
   502
                } else {
dirk@2181
   503
                    tableView.reloadData()
dirk@2181
   504
                }
dirk@2181
   505
            } else if message.isGhost {
xavier@2363
   506
                if let vm = viewModel, let cell = vm.cellFor(message: message), let ip = tableView.indexPath(for: cell) {
dirk@2181
   507
                    Log.info(
dirk@2181
   508
                        component: #function,
dirk@2181
   509
                        content: "delete message at \(index), \(folder.messageCount()) messages")
dirk@2181
   510
                    tableView.deleteRows(at: [ip], with: .automatic)
dirk@2181
   511
                } else {
dirk@2181
   512
                    tableView.reloadData()
dirk@2181
   513
                }
dirk@2181
   514
            } else {
dirk@2181
   515
                // other flags than delete must have been changed
xavier@2363
   516
                if let vm = viewModel, let cell = vm.cellFor(message: message) {
dirk@2181
   517
                    cell.updateFlags(message: message)
dirk@2181
   518
                } else {
dirk@2181
   519
                    tableView.reloadData()
dirk@1350
   520
                }
dirk@1350
   521
            }
dirk@1350
   522
        }
dirk@1350
   523
    }
dirk@1737
   524
igor@1338
   525
}
dirk@1348
   526
dirk@1348
   527
// MARK: - MessageFolderDelegate
dirk@1348
   528
dirk@1348
   529
extension EmailListViewController: MessageFolderDelegate {
dirk@1348
   530
    func didChange(messageFolder: MessageFolder) {
dirk@1675
   531
        GCD.onMainWait {
dirk@1350
   532
            self.didChangeInternal(messageFolder: messageFolder)
dirk@1348
   533
        }
dirk@1348
   534
    }
dirk@1348
   535
}
xavier@2369
   536
dirk@2818
   537
extension EmailListViewController: TableViewUpdate {
xavier@2369
   538
    func updateView() {
xavier@2979
   539
        if let vm = self.viewModel, let filter = vm.folderToShow?.filter, filter.isDefault() {
xavier@2979
   540
            vm.filterEnabled = false
xavier@2979
   541
            handleButtonFilter(enabled: false)
xavier@2979
   542
        }
xavier@2369
   543
        self.tableView.reloadData()
xavier@2369
   544
    }
xavier@2369
   545
}