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