pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author buff <andreas@pep-project.org>
Tue, 24 Oct 2017 19:37:18 +0200
branchIOS-694
changeset 3291 e8000000e820
parent 3289 f6a91b6ccbce
child 3368 e79ab6166c5b
permissions -rw-r--r--
IOS-964 move fetch-older-trigger logic to view model
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 UIKit
dirk@810
    10
import MessageModel
dirk@810
    11
andreas@3151
    12
class EmailListViewController: BaseTableViewController {
andreas@3223
    13
    var folderToShow: Folder?
andreas@3223
    14
andreas@3151
    15
    func updateLastLookAt() {
andreas@3224
    16
        guard let saveFolder = folderToShow else {
andreas@3151
    17
            return
andreas@3151
    18
        }
andreas@3151
    19
        saveFolder.updateLastLookAt()
andreas@3151
    20
    }
andreas@3208
    21
    
andreas@3151
    22
    private var model: EmailListViewModel?
andreas@3208
    23
    
andreas@3151
    24
    private let queue: OperationQueue = {
andreas@3151
    25
        let createe = OperationQueue()
andreas@3151
    26
        createe.qualityOfService = .userInteractive
andreas@3151
    27
        createe.maxConcurrentOperationCount = 10
andreas@3151
    28
        return createe
andreas@3151
    29
    }()
andreas@3151
    30
    private var operations = [IndexPath:Operation]()
andreas@2838
    31
    public static let storyboardId = "EmailListViewController"
andreas@3151
    32
    fileprivate var lastSelectedIndexPath: IndexPath?
andreas@3208
    33
    
igor@1301
    34
    let searchController = UISearchController(searchResultsController: nil)
andreas@3208
    35
    
andreas@3151
    36
    // MARK: - Outlets
andreas@3208
    37
    
dirk@2067
    38
    @IBOutlet weak var enableFilterButton: UIBarButtonItem!
dirk@2067
    39
    @IBOutlet weak var textFilterButton: UIBarButtonItem!
andreas@3151
    40
    @IBOutlet var showFoldersButton: UIBarButtonItem!
andreas@3208
    41
    
andreas@3151
    42
    // MARK: - Life Cycle
andreas@3208
    43
    
dirk@275
    44
    override func viewDidLoad() {
ylandert@935
    45
        super.viewDidLoad()
dirk@2189
    46
        title = NSLocalizedString("Inbox", comment: "General name for (unified) inbox")
igor@1284
    47
        UIHelper.emailListTableHeight(self.tableView)
andreas@3163
    48
        self.textFilterButton.isEnabled = false
igor@1301
    49
        addSearchBar()
dirk@275
    50
    }
andreas@3208
    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
        }
andreas@3223
    58
dirk@1826
    59
        setDefaultColors()
andreas@3151
    60
        setup()
andreas@3208
    61
        
dirk@2191
    62
        // Mark this folder as having been looked at by the user
andreas@3151
    63
        updateLastLookAt()
andreas@3151
    64
        setupFoldersBarButton()
andreas@3151
    65
    }
andreas@3208
    66
    
andreas@3151
    67
    // MARK: - NavigationBar
andreas@3208
    68
    
andreas@3151
    69
    private func hideFoldersNavigationBarButton() {
andreas@3151
    70
        self.showFoldersButton.isEnabled = false
andreas@3151
    71
        self.showFoldersButton.tintColor = UIColor.clear
andreas@3151
    72
    }
andreas@3208
    73
    
andreas@3151
    74
    private func showFoldersNavigationBarButton() {
andreas@3151
    75
        self.showFoldersButton.isEnabled = true
andreas@3151
    76
        self.showFoldersButton.tintColor = nil
andreas@3151
    77
    }
andreas@3208
    78
    
andreas@3151
    79
    private func resetModel() {
andreas@3223
    80
        if folderToShow != nil {
andreas@3291
    81
            model = EmailListViewModel(delegate: self,
andreas@3291
    82
                                       messageSyncService: appConfig.messageSyncService,
andreas@3291
    83
                                       folderToShow: folderToShow)
xavier@2585
    84
        }
dirk@1348
    85
    }
andreas@3208
    86
    
andreas@3151
    87
    private func setup() {
andreas@3289
    88
        if noAccountsExist() {
andreas@3272
    89
            // No account exists. Show account setup.
andreas@3272
    90
            performSegue(withIdentifier:.segueAddNewAccount, sender: self)
andreas@3289
    91
        } else if let vm = model {
andreas@3223
    92
            // We came back from e.g EmailView ...
andreas@3223
    93
            updateFilterText()
andreas@3223
    94
            // ... so we want to update "seen" status
andreas@3226
    95
            vm.reloadData()
andreas@3272
    96
        } else if folderToShow == nil {
andreas@3223
    97
            // We have not been created to show a specific folder, thus we show unified inbox
andreas@3151
    98
            folderToShow = UnifiedInbox()
andreas@3223
    99
            resetModel()
andreas@3272
   100
        } else if model == nil {
andreas@3223
   101
            // We still got no model, because:
andreas@3223
   102
            // - We are not coming back from a pushed view (for instance ComposeEmailView)
andreas@3223
   103
            // - We are not a UnifiedInbox
andreas@3226
   104
            // So we have been created to show a specific folder. Show it!
andreas@3223
   105
            resetModel()
andreas@3151
   106
        }
andreas@3272
   107
andreas@3151
   108
        self.title = realNameOfFolderToShow()
andreas@2683
   109
    }
andreas@3223
   110
andreas@3223
   111
    private func weCameBackFromAPushedView() -> Bool {
andreas@3223
   112
        return model != nil
andreas@3223
   113
    }
andreas@3208
   114
    
andreas@3151
   115
    private func noAccountsExist() -> Bool {
andreas@3151
   116
        return Account.all().isEmpty
igor@1301
   117
    }
andreas@3208
   118
    
andreas@3151
   119
    private func setupFoldersBarButton() {
andreas@3151
   120
        if let size = navigationController?.viewControllers.count, size > 1 {
andreas@3151
   121
            hideFoldersNavigationBarButton()
andreas@3151
   122
        } else {
andreas@3151
   123
            showFoldersNavigationBarButton()
xavier@2132
   124
        }
igor@1338
   125
    }
andreas@3208
   126
    
andreas@3151
   127
    private func addSearchBar() {
igor@1301
   128
        searchController.searchResultsUpdater = self
igor@1301
   129
        searchController.dimsBackgroundDuringPresentation = false
igor@1301
   130
        searchController.delegate = self
igor@1301
   131
        definesPresentationContext = true
igor@1301
   132
        tableView.tableHeaderView = searchController.searchBar
xavier@3209
   133
        tableView.setContentOffset(CGPoint(x: 0.0, y: searchController.searchBar.frame.size.height), animated: false)
igor@1301
   134
    }
andreas@3208
   135
    
andreas@3151
   136
    // MARK: - Other
andreas@3208
   137
    
andreas@3151
   138
    private func realNameOfFolderToShow() -> String? {
andreas@3224
   139
        return folderToShow?.realName
andreas@2697
   140
    }
andreas@3208
   141
    
andreas@3151
   142
    private func configure(cell: EmailListViewCell, for indexPath: IndexPath) {
andreas@3151
   143
        // Configure lightweight stuff on main thread ...
andreas@3151
   144
        guard let saveModel = model else {
andreas@3151
   145
            return
xavier@2967
   146
        }
andreas@3151
   147
        guard let row = saveModel.row(for: indexPath) else {
andreas@3151
   148
            Log.shared.errorAndCrash(component: #function, errorString: "We should have a row here")
andreas@3151
   149
            return
andreas@3151
   150
        }
andreas@3151
   151
        cell.senderLabel.text = row.from
andreas@3151
   152
        cell.subjectLabel.text = row.subject
andreas@3151
   153
        cell.summaryLabel.text = row.bodyPeek
andreas@3151
   154
        cell.isFlagged = row.isFlagged
andreas@3151
   155
        cell.isSeen = row.isSeen
andreas@3151
   156
        cell.hasAttachment = row.showAttchmentIcon
andreas@3151
   157
        cell.dateLabel.text = row.dateText
andreas@3151
   158
        // Set image from cache if any
andreas@3151
   159
        cell.setContactImage(image: row.senderContactImage)
andreas@3208
   160
        
andreas@3151
   161
        let op = BlockOperation() { [weak self] in
andreas@3151
   162
            // ... and expensive computations in background
andreas@3151
   163
            guard let strongSelf = self else {
andreas@3151
   164
                // View is gone, nothing to do.
andreas@3151
   165
                return
andreas@3151
   166
            }
andreas@3208
   167
            
andreas@3151
   168
            var senderImage: UIImage?
andreas@3151
   169
            if row.senderContactImage == nil {
andreas@3220
   170
                // image for identity has not been cached yet
andreas@3220
   171
                // Get and cache it here in the background ...
andreas@3151
   172
                senderImage = strongSelf.model?.senderImage(forCellAt: indexPath)
andreas@3220
   173
andreas@3220
   174
                // ... and set it on the main queue
andreas@3220
   175
                DispatchQueue.main.async {
andreas@3220
   176
                    if senderImage != nil && senderImage != cell.contactImageView.image {
andreas@3220
   177
                        cell.contactImageView.image  = senderImage
andreas@3220
   178
                    }
andreas@3220
   179
                }
andreas@3151
   180
            }
andreas@3220
   181
andreas@3220
   182
            let pEpRatingImage = strongSelf.model?.pEpRatingColorImage(forCellAt: indexPath)
andreas@3220
   183
andreas@3151
   184
            // In theory we want to set all data in *one* async call. But as pEpRatingColorImage takes
andreas@3151
   185
            // very long, we are setting the sender image seperatelly.
andreas@3151
   186
            DispatchQueue.main.async {
andreas@3151
   187
                if pEpRatingImage != nil {
andreas@3151
   188
                    cell.setPepRatingImage(image: pEpRatingImage)
andreas@3151
   189
                }
andreas@3151
   190
            }
andreas@3151
   191
        }
andreas@3151
   192
        queue(operation: op, for: indexPath)
xavier@2967
   193
    }
andreas@3208
   194
    
andreas@3151
   195
    // MARK: - Actions
andreas@3208
   196
    
andreas@3151
   197
    @IBAction func filterButtonHasBeenPressed(_ sender: UIBarButtonItem) {
andreas@3163
   198
        guard let vm = model else {
andreas@3163
   199
            Log.shared.errorAndCrash(component: #function, errorString: "We should have a model here")
andreas@3163
   200
            return
andreas@3163
   201
        }
andreas@3163
   202
        vm.isFilterEnabled = !vm.isFilterEnabled
xavier@3193
   203
        updateFilterButtonView()
xavier@1832
   204
    }
andreas@3208
   205
    
xavier@3193
   206
    func updateFilterButtonView() {
andreas@3163
   207
        guard let vm = model else {
andreas@3163
   208
            Log.shared.errorAndCrash(component: #function, errorString: "We should have a model here")
andreas@3163
   209
            return
andreas@3163
   210
        }
andreas@3208
   211
        
andreas@3163
   212
        textFilterButton.isEnabled = vm.isFilterEnabled
andreas@3163
   213
        if textFilterButton.isEnabled {
andreas@3163
   214
            enableFilterButton.image = UIImage(named: "unread-icon-active")
andreas@3163
   215
            updateFilterText()
andreas@3163
   216
        } else {
andreas@3163
   217
            textFilterButton.title = ""
andreas@3163
   218
            enableFilterButton.image = UIImage(named: "unread-icon")
andreas@3163
   219
        }
andreas@3163
   220
    }
andreas@3208
   221
    
andreas@3163
   222
    func updateFilterText() {
xavier@3193
   223
        if let vm = model, let txt = vm.activeFilter?.title {
andreas@3163
   224
            textFilterButton.title = "Filter by: " + txt
andreas@3163
   225
        }
andreas@3151
   226
    }
andreas@3208
   227
    
dirk@31
   228
    // MARK: - UITableViewDataSource
andreas@3208
   229
    
dirk@784
   230
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
andreas@3151
   231
        return model?.rowCount ?? 0
dirk@31
   232
    }
andreas@3208
   233
    
dirk@784
   234
    override func tableView(_ tableView: UITableView,
dirk@784
   235
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
andreas@3151
   236
        guard let cell = tableView.dequeueReusableCell(withIdentifier: EmailListViewCell.storyboardId,
andreas@3151
   237
                                                       for: indexPath) as? EmailListViewCell
andreas@3151
   238
            else {
andreas@3151
   239
                Log.shared.errorAndCrash(component: #function, errorString: "Wrong cell!")
andreas@3151
   240
                return UITableViewCell()
andreas@3151
   241
        }
andreas@3151
   242
        configure(cell: cell, for: indexPath)
dirk@31
   243
        return cell
dirk@31
   244
    }
andreas@3208
   245
    
dirk@719
   246
    // MARK: - UITableViewDelegate
andreas@3208
   247
    
dirk@784
   248
    override func tableView(_ tableView: UITableView, editActionsForRowAt
dirk@784
   249
        indexPath: IndexPath)-> [UITableViewRowAction]? {
andreas@3151
   250
        guard let flagAction = createFlagAction(forCellAt: indexPath),
andreas@3151
   251
            let deleteAction = createDeleteAction(forCellAt: indexPath),
andreas@3151
   252
            let moreAction = createMoreAction(forCellAt: indexPath) else {
andreas@3151
   253
                Log.shared.errorAndCrash(component: #function, errorString: "Error creating action.")
andreas@3151
   254
                return nil
dirk@855
   255
        }
andreas@3151
   256
        return [deleteAction, flagAction, moreAction]
dirk@744
   257
    }
andreas@3208
   258
    
andreas@3151
   259
    override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
andreas@3151
   260
        cancelOperation(for: indexPath)
dirk@744
   261
    }
andreas@3208
   262
    
andreas@3151
   263
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
andreas@3151
   264
        lastSelectedIndexPath = indexPath
andreas@3151
   265
        tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
andreas@3151
   266
        performSegue(withIdentifier: SegueIdentifier.segueShowEmail, sender: self)
dirk@744
   267
    }
andreas@3278
   268
andreas@3291
   269
    // Implemented to get informed about the scrolling position.
andreas@3291
   270
    // If the user has scrolled down (almost) to the end, we need to get older emails to display.
andreas@3291
   271
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell,
andreas@3291
   272
                            forRowAt indexPath: IndexPath) {
andreas@3278
   273
        guard let vm = model else {
andreas@3291
   274
            Log.shared.errorAndCrash(component: #function, errorString: "No model.")
andreas@3291
   275
            return
andreas@3278
   276
        }
andreas@3291
   277
        vm.fetchOlderMessagesIfRequired(forIndexPath: indexPath)
andreas@3278
   278
    }
andreas@3278
   279
andreas@3151
   280
    // MARK: - Queue Handling
andreas@3208
   281
    
andreas@3151
   282
    private func queue(operation op:Operation, for indexPath: IndexPath) {
andreas@3151
   283
        operations[indexPath] = op
andreas@3151
   284
        queue.addOperation(op)
dirk@1507
   285
    }
andreas@3208
   286
    
andreas@3151
   287
    private func cancelOperation(for indexPath:IndexPath) {
andreas@3151
   288
        guard let op = operations.removeValue(forKey: indexPath) else {
andreas@3151
   289
            return
dirk@744
   290
        }
andreas@3151
   291
        if !op.isCancelled  {
andreas@3151
   292
            op.cancel()
dirk@1507
   293
        }
dirk@744
   294
    }
andreas@3291
   295
andreas@3291
   296
    // MARK: -
andreas@3291
   297
andreas@3151
   298
    override func didReceiveMemoryWarning() {
andreas@3151
   299
        model?.freeMemory()
andreas@3151
   300
    }
andreas@3151
   301
}
andreas@3151
   302
andreas@3151
   303
// MARK: - UISearchResultsUpdating, UISearchControllerDelegate
andreas@3151
   304
andreas@3151
   305
extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
andreas@3151
   306
    public func updateSearchResults(for searchController: UISearchController) {
andreas@3151
   307
        guard let vm = model, let searchText = searchController.searchBar.text else {
andreas@3151
   308
            return
igor@1242
   309
        }
andreas@3163
   310
        vm.setSearchFilter(forSearchText: searchText)
igor@1242
   311
    }
andreas@3208
   312
    
andreas@3151
   313
    func didDismissSearchController(_ searchController: UISearchController) {
andreas@3151
   314
        guard let vm = model else {
andreas@3151
   315
            return
andreas@3151
   316
        }
andreas@3151
   317
        vm.removeSearchFilter()
andreas@3151
   318
    }
andreas@3151
   319
}
xavier@1623
   320
andreas@3151
   321
// MARK: - EmailListModelDelegate
andreas@3151
   322
andreas@3151
   323
extension EmailListViewController: EmailListViewModelDelegate {
andreas@3151
   324
    func emailListViewModel(viewModel: EmailListViewModel, didInsertDataAt indexPath: IndexPath) {
andreas@3166
   325
        tableView.beginUpdates()
andreas@3151
   326
        tableView.insertRows(at: [indexPath], with: .automatic)
andreas@3151
   327
        tableView.endUpdates()
andreas@3151
   328
    }
andreas@3208
   329
    
andreas@3151
   330
    func emailListViewModel(viewModel: EmailListViewModel, didRemoveDataAt indexPath: IndexPath) {
andreas@3151
   331
        tableView.beginUpdates()
andreas@3151
   332
        tableView.deleteRows(at: [indexPath], with: .automatic)
andreas@3151
   333
        tableView.endUpdates()
andreas@3151
   334
    }
andreas@3208
   335
    
andreas@3151
   336
    func emailListViewModel(viewModel: EmailListViewModel, didUpdateDataAt indexPath: IndexPath) {
andreas@3151
   337
        tableView.beginUpdates()
andreas@3151
   338
        tableView.reloadRows(at: [indexPath], with: .none)
andreas@3151
   339
        tableView.endUpdates()
andreas@3151
   340
    }
andreas@3208
   341
    
andreas@3151
   342
    func updateView() {
andreas@3151
   343
        self.tableView.reloadData()
andreas@3151
   344
    }
andreas@3151
   345
}
andreas@3151
   346
andreas@3151
   347
// MARK: - ActionSheet & ActionSheet Actions
andreas@3151
   348
andreas@3151
   349
extension EmailListViewController {
andreas@3166
   350
    func showMoreActionSheet(forRowAt indexPath: IndexPath) {
andreas@3151
   351
        lastSelectedIndexPath = indexPath
igor@1242
   352
        let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
igor@1438
   353
        alertControler.view.tintColor = .pEpGreen
igor@1242
   354
        let cancelAction = createCancelAction()
andreas@3151
   355
        let replyAction = createReplyAction()
andreas@3151
   356
        let replyAllAction = createReplyAllAction()
andreas@3151
   357
        let forwardAction = createForwardAction()
igor@1242
   358
        alertControler.addAction(cancelAction)
igor@1242
   359
        alertControler.addAction(replyAction)
xavier@1620
   360
        alertControler.addAction(replyAllAction)
igor@1242
   361
        alertControler.addAction(forwardAction)
igor@1414
   362
        if let popoverPresentationController = alertControler.popoverPresentationController {
andreas@3151
   363
            popoverPresentationController.sourceView = tableView
igor@1414
   364
        }
igor@1242
   365
        present(alertControler, animated: true, completion: nil)
igor@1242
   366
    }
andreas@3208
   367
    
andreas@3151
   368
    // MARK: Action Sheet Actions
andreas@3208
   369
    
igor@1242
   370
    func createCancelAction() -> UIAlertAction {
andreas@3151
   371
        return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in
andreas@3151
   372
            self.tableView.beginUpdates()
andreas@3151
   373
            self.tableView.setEditing(false, animated: true)
andreas@3151
   374
            self.tableView.endUpdates()
igor@1242
   375
        }
igor@1242
   376
    }
andreas@3208
   377
    
andreas@3151
   378
    func createReplyAction() ->  UIAlertAction {
andreas@3151
   379
        return UIAlertAction(title: "Reply", style: .default) { (action) in
andreas@3151
   380
            self.performSegue(withIdentifier: .segueReply, sender: self)
xavier@1620
   381
        }
xavier@1620
   382
    }
andreas@3208
   383
    
andreas@3151
   384
    func createReplyAllAction() ->  UIAlertAction {
andreas@3151
   385
        return UIAlertAction(title: "Reply All", style: .default) { (action) in
andreas@3151
   386
            self.performSegue(withIdentifier: .segueReplyAll, sender: self)
igor@1242
   387
        }
igor@1242
   388
    }
andreas@3208
   389
    
andreas@3151
   390
    func createForwardAction() -> UIAlertAction {
andreas@3151
   391
        return UIAlertAction(title: "Forward", style: .default) { (action) in
andreas@3151
   392
            self.performSegue(withIdentifier: .segueForward, sender: self)
xavier@2369
   393
        }
igor@1301
   394
    }
igor@1301
   395
}
igor@1338
   396
andreas@3151
   397
// MARK: - TableViewCell Actions
andreas@3151
   398
andreas@3151
   399
extension EmailListViewController {
andreas@3151
   400
    private func createRowAction(image: UIImage?,
xavier@3266
   401
                                 action: @escaping (UITableViewRowAction, IndexPath) -> Void
xavier@3266
   402
        ) -> UITableViewRowAction {
xavier@3266
   403
        let rowAction = UITableViewRowAction(style: .normal, title: nil, handler: action)
andreas@3151
   404
        if let theImage = image {
andreas@3151
   405
            let iconColor = UIColor(patternImage: theImage)
andreas@3151
   406
            rowAction.backgroundColor = iconColor
andreas@3151
   407
        }
andreas@3151
   408
        return rowAction
andreas@3151
   409
    }
andreas@3208
   410
    
andreas@3151
   411
    func createFlagAction(forCellAt indexPath: IndexPath) -> UITableViewRowAction? {
andreas@3151
   412
        guard let row = model?.row(for: indexPath) else {
andreas@3151
   413
            Log.shared.errorAndCrash(component: #function, errorString: "No data for indexPath!")
andreas@3151
   414
            return nil
andreas@3151
   415
        }
andreas@3151
   416
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
andreas@3151
   417
            if row.isFlagged {
andreas@3151
   418
                model?.unsetFlagged(forIndexPath: indexPath)
andreas@3151
   419
            } else {
andreas@3151
   420
                model?.setFlagged(forIndexPath: indexPath)
andreas@3151
   421
            }
andreas@3151
   422
            tableView.beginUpdates()
andreas@3151
   423
            tableView.setEditing(false, animated: true)
andreas@3166
   424
            tableView.reloadRows(at: [indexPath], with: .none)
andreas@3151
   425
            tableView.endUpdates()
andreas@3151
   426
        }
xavier@3266
   427
        return createRowAction(image: UIImage(named: "swipe-flag"), action: action)
andreas@3151
   428
    }
andreas@3208
   429
    
andreas@3151
   430
    func createDeleteAction(forCellAt indexPath: IndexPath) -> UITableViewRowAction? {
andreas@3151
   431
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
andreas@3151
   432
            tableView.beginUpdates()
andreas@3151
   433
            model?.delete(forIndexPath: indexPath) // mark for deletion/trash
andreas@3151
   434
            tableView.deleteRows(at: [indexPath], with: .none)
andreas@3151
   435
            tableView.endUpdates()
andreas@3151
   436
        }
xavier@3266
   437
xavier@3266
   438
        return createRowAction(image: UIImage(named: "swipe-trash"), action: action)
andreas@3151
   439
    }
andreas@3208
   440
    
andreas@3151
   441
    func createMoreAction(forCellAt indexPath: IndexPath) -> UITableViewRowAction? {
andreas@3151
   442
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
andreas@3151
   443
            self.showMoreActionSheet(forRowAt: indexPath)
andreas@3151
   444
        }
xavier@3266
   445
andreas@3151
   446
        return createRowAction(image: UIImage(named: "swipe-more"),
xavier@3266
   447
                               action: action)
andreas@3151
   448
    }
andreas@3151
   449
}
andreas@3151
   450
andreas@3151
   451
// MARK: - SegueHandlerType
igor@1338
   452
igor@1338
   453
extension EmailListViewController: SegueHandlerType {
andreas@3208
   454
    
igor@1338
   455
    enum SegueIdentifier: String {
igor@1338
   456
        case segueAddNewAccount
igor@1338
   457
        case segueShowEmail
igor@1338
   458
        case segueCompose
xavier@2715
   459
        case segueReply
xavier@1623
   460
        case segueReplyAll
xavier@1664
   461
        case segueForward
xavier@1865
   462
        case segueFilter
xavier@2248
   463
        case segueFolderViews
igor@1338
   464
        case noSegue
igor@1338
   465
    }
andreas@3208
   466
    
andreas@3110
   467
    private func setup(composeViewController vc: ComposeTableViewController,
andreas@3110
   468
                       composeMode: ComposeTableViewController.ComposeMode = .normal,
andreas@3110
   469
                       originalMessage: Message? = nil) {
andreas@3110
   470
        vc.appConfig = appConfig
andreas@3110
   471
        vc.composeMode = composeMode
andreas@3110
   472
        vc.originalMessage = originalMessage
andreas@3223
   473
        vc.origin = folderToShow?.account.user
andreas@3110
   474
    }
andreas@3208
   475
    
igor@1338
   476
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
igor@1338
   477
        switch segueIdentifier(for: segue) {
xavier@2715
   478
        case .segueReply:
andreas@2850
   479
            guard let nav = segue.destination as? UINavigationController,
xavier@2715
   480
                let destination = nav.topViewController as? ComposeTableViewController,
andreas@3151
   481
                let indexPath = lastSelectedIndexPath,
andreas@3151
   482
                let message = model?.message(representedByRowAt: indexPath) else {
andreas@2850
   483
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   484
                    return
xavier@2715
   485
            }
andreas@3110
   486
            setup(composeViewController: destination, composeMode: .replyFrom,
andreas@3151
   487
                  originalMessage: message)
xavier@1623
   488
        case .segueReplyAll:
andreas@2850
   489
            guard let nav = segue.destination as? UINavigationController,
xavier@1623
   490
                let destination = nav.topViewController as? ComposeTableViewController,
andreas@3151
   491
                let indexPath = lastSelectedIndexPath,
andreas@3151
   492
                let message = model?.message(representedByRowAt: indexPath) else {
andreas@2850
   493
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   494
                    return
xavier@1623
   495
            }
andreas@3110
   496
            setup(composeViewController: destination, composeMode: .replyAll,
andreas@3151
   497
                  originalMessage: message)
andreas@3111
   498
        case .segueShowEmail:
andreas@2850
   499
            guard let vc = segue.destination as? EmailViewController,
andreas@3151
   500
                let indexPath = lastSelectedIndexPath,
andreas@3208
   501
                let message = model?.message(representedByRowAt: indexPath) else {
andreas@2850
   502
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   503
                    return
igor@1338
   504
            }
andreas@2850
   505
            vc.appConfig = appConfig
andreas@3151
   506
            vc.message = message
andreas@3223
   507
            vc.folderShow = folderToShow
andreas@3166
   508
            vc.messageId = indexPath.row //that looks wrong
xavier@1664
   509
        case .segueForward:
andreas@2850
   510
            guard let nav = segue.destination as? UINavigationController,
xavier@1664
   511
                let destination = nav.topViewController as? ComposeTableViewController,
andreas@3151
   512
                let indexPath = lastSelectedIndexPath,
andreas@3151
   513
                let message = model?.message(representedByRowAt: indexPath) else {
andreas@2850
   514
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   515
                    return
xavier@1664
   516
            }
andreas@3110
   517
            setup(composeViewController: destination, composeMode: .forward,
andreas@3151
   518
                  originalMessage: message)
andreas@2850
   519
        case .segueFilter:
andreas@2850
   520
            guard let destiny = segue.destination as? FilterTableViewController  else {
andreas@2850
   521
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   522
                return
andreas@2850
   523
            }
andreas@2850
   524
            destiny.appConfig = appConfig
andreas@3163
   525
            destiny.filterDelegate = model
andreas@2850
   526
            destiny.inFolder = false
andreas@3223
   527
            destiny.filterEnabled = folderToShow?.filter
andreas@2850
   528
            destiny.hidesBottomBarWhenPushed = true
andreas@2850
   529
        case .segueAddNewAccount:
andreas@2850
   530
            guard let vc = segue.destination as? LoginTableViewController  else {
andreas@2850
   531
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   532
                return
andreas@2850
   533
            }
andreas@2850
   534
            vc.appConfig = appConfig
andreas@2850
   535
            vc.hidesBottomBarWhenPushed = true
xavier@1664
   536
            break
andreas@2850
   537
        case .segueFolderViews:
andreas@2850
   538
            guard let vC = segue.destination as? FolderTableViewController  else {
andreas@2850
   539
                Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   540
                return
xavier@1865
   541
            }
andreas@2850
   542
            vC.appConfig = appConfig
andreas@2850
   543
            vC.hidesBottomBarWhenPushed = true
xavier@1865
   544
            break
andreas@2850
   545
        case .segueCompose:
andreas@2850
   546
            guard let nav = segue.destination as? UINavigationController,
dirk@2957
   547
                let destination = nav.rootViewController as? ComposeTableViewController else {
andreas@2850
   548
                    Log.shared.errorAndCrash(component: #function, errorString: "Segue issue")
andreas@2850
   549
                    return
dirk@2240
   550
            }
andreas@3110
   551
            setup(composeViewController: destination)
andreas@2850
   552
        default:
andreas@2850
   553
            Log.shared.errorAndCrash(component: #function, errorString: "Unhandled segue")
dirk@1694
   554
            break
igor@1338
   555
        }
andreas@2838
   556
    }
andreas@3208
   557
    
andreas@3166
   558
    @IBAction func segueUnwindAccountAdded(segue: UIStoryboardSegue) {
andreas@3151
   559
        // nothing to do.
andreas@3150
   560
    }
dirk@1348
   561
}