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