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