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