pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Mon, 04 Sep 2017 13:20:38 +0200
changeset 2957 eb8db133506e
parent 2956 21c8a6d8a5c0
child 2958 6ceb2df4a1d8
permissions -rw-r--r--
IOS-660 share some code
dirk@30
     1
//
dirk@30
     2
//  EmailListViewController.swift
dirk@30
     3
//  pEpForiOS
dirk@30
     4
//
dirk@30
     5
//  Created by Dirk Zimmermann on 16/04/16.
dirk@30
     6
//  Copyright © 2016 p≡p Security S.A. All rights reserved.
dirk@30
     7
//
dirk@30
     8
dirk@30
     9
import Foundation
dirk@30
    10
import UIKit
dirk@31
    11
import CoreData
dirk@810
    12
import MessageModel
dirk@810
    13
igor@1260
    14
struct EmailListConfig {
andreas@2838
    15
    var appConfig: AppConfig
dirk@2854
    16
igor@1260
    17
    /** The folder to display, if it exists */
igor@1260
    18
    var folder: Folder?
dirk@1906
    19
dirk@1906
    20
    let imageProvider = IdentityImageProvider()
igor@1260
    21
}
dirk@30
    22
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
dirk@2956
   384
    override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
dirk@2956
   385
        if identifier == SegueIdentifier.segueShowEmail.rawValue &&
dirk@2956
   386
            config?.folder?.folderType == .drafts {
dirk@2956
   387
            performSegue(withIdentifier: .segueCompose, sender: self)
dirk@2956
   388
            return false
dirk@2956
   389
        }
dirk@2956
   390
        return true
dirk@2956
   391
    }
dirk@2956
   392
dirk@2957
   393
    func currentMessage(senderCell: Any?) -> (Message, IndexPath)? {
dirk@2957
   394
        if let cell = senderCell as? EmailListViewCell,
dirk@2957
   395
            let indexPath = self.tableView.indexPath(for: cell),
dirk@2957
   396
            let message = cell.messageAt(indexPath: indexPath, config: config) {
dirk@2957
   397
            return (message, indexPath)
dirk@2957
   398
        }
dirk@2957
   399
        return nil
dirk@2957
   400
    }
dirk@2957
   401
igor@1338
   402
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
igor@1338
   403
        switch segueIdentifier(for: segue) {
xavier@2715
   404
        case .segueReply:
andreas@2850
   405
            guard let nav = segue.destination as? UINavigationController,
xavier@2715
   406
                let destination = nav.topViewController as? ComposeTableViewController,
dirk@2957
   407
                let (theMessage, _) = currentMessage(senderCell: sender) else {
andreas@2850
   408
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   409
                    return
xavier@2715
   410
            }
andreas@2850
   411
            destination.appConfig = appConfig
andreas@2850
   412
            destination.composeMode = .replyFrom
dirk@2957
   413
            destination.originalMessage = theMessage
xavier@1623
   414
        case .segueReplyAll:
andreas@2850
   415
            guard let nav = segue.destination as? UINavigationController,
xavier@1623
   416
                let destination = nav.topViewController as? ComposeTableViewController,
dirk@2957
   417
                let (theMessage, _) = currentMessage(senderCell: sender)  else {
andreas@2850
   418
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   419
                    return
xavier@1623
   420
            }
andreas@2850
   421
            destination.appConfig = appConfig
andreas@2850
   422
            destination.composeMode = .replyAll
dirk@2957
   423
            destination.originalMessage = theMessage
igor@1338
   424
        case .segueShowEmail:
andreas@2850
   425
            guard let vc = segue.destination as? EmailViewController,
dirk@2957
   426
                let (theMessage, indexPath) = currentMessage(senderCell: sender) else {
andreas@2850
   427
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   428
                    return
igor@1338
   429
            }
andreas@2850
   430
            vc.appConfig = appConfig
dirk@2957
   431
            vc.message = theMessage
andreas@2850
   432
            vc.folderShow = viewModel?.folderToShow
andreas@2850
   433
            vc.messageId = indexPath.row
xavier@1664
   434
        case .segueForward:
andreas@2850
   435
            guard let nav = segue.destination as? UINavigationController,
xavier@1664
   436
                let destination = nav.topViewController as? ComposeTableViewController,
dirk@2957
   437
                let (theMessage, _) = currentMessage(senderCell: sender) else {
andreas@2850
   438
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   439
                    return
xavier@1664
   440
            }
andreas@2850
   441
            destination.composeMode = .forward
andreas@2850
   442
            destination.appConfig = appConfig
dirk@2957
   443
            destination.originalMessage = theMessage
andreas@2850
   444
        case .segueFilter:
andreas@2850
   445
            guard let destiny = segue.destination as? FilterTableViewController  else {
andreas@2850
   446
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   447
                return
andreas@2850
   448
            }
andreas@2850
   449
            destiny.appConfig = appConfig
andreas@2850
   450
            destiny.filterDelegate = viewModel
andreas@2850
   451
            destiny.inFolder = false
andreas@2850
   452
            destiny.filterEnabled = viewModel?.folderToShow?.filter
andreas@2850
   453
            destiny.hidesBottomBarWhenPushed = true
andreas@2850
   454
        case .segueAddNewAccount:
andreas@2850
   455
            guard let vc = segue.destination as? LoginTableViewController  else {
andreas@2850
   456
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   457
                return
andreas@2850
   458
            }
andreas@2850
   459
            vc.appConfig = appConfig
andreas@2850
   460
            vc.hidesBottomBarWhenPushed = true
xavier@1664
   461
            break
andreas@2850
   462
        case .segueFolderViews:
andreas@2850
   463
            guard let vC = segue.destination as? FolderTableViewController  else {
andreas@2850
   464
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   465
                return
xavier@1865
   466
            }
andreas@2850
   467
            vC.appConfig = appConfig
andreas@2850
   468
            vC.hidesBottomBarWhenPushed = true
xavier@1865
   469
            break
andreas@2850
   470
        case .segueCompose:
andreas@2850
   471
            guard let nav = segue.destination as? UINavigationController,
dirk@2957
   472
                let destination = nav.rootViewController as? ComposeTableViewController else {
andreas@2850
   473
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   474
                    return
dirk@2240
   475
            }
andreas@2850
   476
            destination.appConfig = appConfig
dirk@2956
   477
dirk@2957
   478
            if let (theMessage, _) = currentMessage(senderCell: sender),
dirk@2957
   479
                config?.folder?.folderType == .drafts {
dirk@2956
   480
                destination.composeMode = .draft
dirk@2957
   481
                destination.originalMessage = theMessage
dirk@2956
   482
            } else {
dirk@2956
   483
                destination.composeMode = .normal
dirk@2956
   484
            }
andreas@2850
   485
        default:
andreas@2850
   486
            Log.shared.errorAndCrash(component: #function, errorString: "Unhandled segue")
dirk@1694
   487
            break
igor@1338
   488
        }
andreas@2838
   489
    }
xavier@1865
   490
xavier@2254
   491
    @IBAction func segueUnwindAccountAdded(segue: UIStoryboardSegue) {
xavier@2254
   492
    }
xavier@2254
   493
dirk@1350
   494
    func didChangeInternal(messageFolder: MessageFolder) {
dirk@1350
   495
        if let folder = config?.folder,
dirk@1350
   496
            let message = messageFolder as? Message,
dirk@1558
   497
            folder.contains(message: message, deletedMessagesAreContained: true) {
dirk@2181
   498
            if message.isOriginal {
dirk@2181
   499
                // new message has arrived
dirk@2181
   500
                if let index = folder.indexOf(message: message) {
dirk@2181
   501
                    let ip = IndexPath(row: index, section: 0)
dirk@2181
   502
                    Log.info(
dirk@2181
   503
                        component: #function,
dirk@2181
   504
                        content: "insert message at \(index), \(folder.messageCount()) messages")
dirk@2181
   505
                    tableView.insertRows(at: [ip], with: .automatic)
dirk@1535
   506
                } else {
dirk@2181
   507
                    tableView.reloadData()
dirk@2181
   508
                }
dirk@2181
   509
            } else if message.isGhost {
xavier@2363
   510
                if let vm = viewModel, let cell = vm.cellFor(message: message), let ip = tableView.indexPath(for: cell) {
dirk@2181
   511
                    Log.info(
dirk@2181
   512
                        component: #function,
dirk@2181
   513
                        content: "delete message at \(index), \(folder.messageCount()) messages")
dirk@2181
   514
                    tableView.deleteRows(at: [ip], with: .automatic)
dirk@2181
   515
                } else {
dirk@2181
   516
                    tableView.reloadData()
dirk@2181
   517
                }
dirk@2181
   518
            } else {
dirk@2181
   519
                // other flags than delete must have been changed
xavier@2363
   520
                if let vm = viewModel, let cell = vm.cellFor(message: message) {
dirk@2181
   521
                    cell.updateFlags(message: message)
dirk@2181
   522
                } else {
dirk@2181
   523
                    tableView.reloadData()
dirk@1350
   524
                }
dirk@1350
   525
            }
dirk@1350
   526
        }
dirk@1350
   527
    }
dirk@1737
   528
igor@1338
   529
}
dirk@1348
   530
dirk@1348
   531
// MARK: - MessageFolderDelegate
dirk@1348
   532
dirk@1348
   533
extension EmailListViewController: MessageFolderDelegate {
dirk@1348
   534
    func didChange(messageFolder: MessageFolder) {
dirk@1675
   535
        GCD.onMainWait {
dirk@1350
   536
            self.didChangeInternal(messageFolder: messageFolder)
dirk@1348
   537
        }
dirk@1348
   538
    }
dirk@1348
   539
}
xavier@2369
   540
dirk@2818
   541
extension EmailListViewController: TableViewUpdate {
xavier@2369
   542
    func updateView() {
xavier@2369
   543
        self.tableView.reloadData()
xavier@2369
   544
    }
xavier@2369
   545
}