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