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