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