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