pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Tue, 20 Sep 2016 16:52:55 +0200
changeset 744 de515991129e
parent 723 adbc37ab3f03
child 745 7131d36d386d
permissions -rw-r--r--
IOS-198 email list: clearer separation between fetch controller and rest
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 Foundation
dirk@30
    10
import UIKit
dirk@31
    11
import CoreData
dirk@30
    12
dirk@591
    13
class EmailListViewController: UITableViewController {
dirk@591
    14
    struct EmailListConfig {
dirk@591
    15
        let appConfig: AppConfig
dirk@583
    16
dirk@591
    17
        /** Set to whatever criteria you want to have mails displayed */
dirk@591
    18
        let predicate: NSPredicate?
dirk@583
    19
dirk@591
    20
        /** The sort descriptors to be used for displaying emails */
dirk@591
    21
        let sortDescriptors: [NSSortDescriptor]?
dirk@583
    22
dirk@606
    23
        /** If applicable, the account to refresh from */
dirk@591
    24
        let account: IAccount?
dirk@585
    25
dirk@591
    26
        /** If applicable, the folder name to sync */
dirk@591
    27
        let folderName: String?
dirk@593
    28
dirk@593
    29
        /** Should there be a sync directly when the view appears? */
dirk@593
    30
        let syncOnAppear: Bool
dirk@591
    31
    }
dirk@58
    32
dirk@583
    33
    struct UIState {
dirk@583
    34
        var isSynching: Bool = false
dirk@583
    35
    }
dirk@583
    36
    
dirk@273
    37
    let comp = "EmailListViewController"
dirk@361
    38
dirk@273
    39
    let segueShowEmail = "segueShowEmail"
dirk@397
    40
    let segueCompose = "segueCompose"
dirk@397
    41
    let segueUserSettings = "segueUserSettings"
dirk@30
    42
dirk@583
    43
    var config: EmailListConfig!
dirk@583
    44
dirk@212
    45
    var fetchController: NSFetchedResultsController?
dirk@275
    46
    var state = UIState()
dirk@342
    47
    let dateFormatter = UIHelper.dateFormatterEmailList()
dirk@275
    48
dirk@502
    49
    /**
dirk@502
    50
     The default background color for an email cell, as determined the first time a cell is
dirk@502
    51
     created.
dirk@502
    52
     */
dirk@502
    53
    var defaultCellBackgroundColor: UIColor?
dirk@502
    54
dirk@502
    55
    /**
dirk@502
    56
     Indicates whether `defaultCellBackgroundColor` has been determined or not.
dirk@502
    57
     */
dirk@502
    58
    var determinedCellBackgroundColor: Bool = false
dirk@502
    59
dirk@594
    60
    var refreshController: UIRefreshControl!
dirk@594
    61
dirk@705
    62
    /**
dirk@705
    63
     The message that should be saved as a draft when compose gets aborted.
dirk@705
    64
     */
dirk@705
    65
    var draftMessageToStore: IMessage?
dirk@705
    66
dirk@719
    67
    /**
dirk@719
    68
     When the user taps on a draft email, this is the message that was selected
dirk@719
    69
     and should be given to the compose view.
dirk@719
    70
     */
dirk@719
    71
    var draftMessageToCompose: IMessage?
dirk@719
    72
ana@620
    73
    func isReadedMessage(message: IMessage)-> Bool {
dirk@637
    74
        return message.flagSeen.boolValue
ana@614
    75
    }
ana@614
    76
ana@620
    77
    func isImportantMessage(message: IMessage)-> Bool {
ana@621
    78
        return message.flagFlagged.boolValue
ana@614
    79
    }
ana@614
    80
dirk@275
    81
    override func viewDidLoad() {
dirk@594
    82
        refreshController = UIRefreshControl.init()
dirk@593
    83
        refreshController.addTarget(self, action: #selector(self.fetchMailsRefreshControl(_:)),
dirk@275
    84
                                    forControlEvents: UIControlEvents.ValueChanged)
dirk@356
    85
        UIHelper.variableCellHeightsTableView(self.tableView)
dirk@275
    86
    }
dirk@31
    87
dirk@31
    88
    override func viewWillAppear(animated: Bool) {
dirk@594
    89
        // Disable fetching if there is no account
dirk@594
    90
        if config.account != nil {
dirk@594
    91
            self.refreshControl = refreshController
dirk@594
    92
        } else {
dirk@594
    93
            self.refreshControl = nil
dirk@594
    94
        }
dirk@594
    95
dirk@209
    96
        prepareFetchRequest()
dirk@593
    97
        if config.syncOnAppear {
dirk@593
    98
            fetchMailsRefreshControl()
dirk@593
    99
        }
ana@207
   100
        super.viewWillAppear(animated)
ana@207
   101
    }
ana@152
   102
dirk@275
   103
    func fetchMailsRefreshControl(refreshControl: UIRefreshControl? = nil) {
dirk@583
   104
        if let account = config.account {
dirk@55
   105
            let connectInfo = account.connectInfo
dirk@55
   106
dirk@275
   107
            state.isSynching = true
dirk@588
   108
            updateUI()
dirk@368
   109
dirk@588
   110
            config.appConfig.grandOperator.fetchEmailsAndDecryptConnectInfos(
dirk@590
   111
                [connectInfo], folderName: config.folderName,
dirk@466
   112
                completionBlock: { error in
dirk@466
   113
                    Log.infoComponent(self.comp, "Sync completed, error: \(error)")
dirk@707
   114
                    UIHelper.displayError(error, controller: self)
dirk@583
   115
                    self.config.appConfig.model.save()
dirk@466
   116
                    self.state.isSynching = false
dirk@466
   117
                    refreshControl?.endRefreshing()
dirk@466
   118
                    self.updateUI()
dirk@466
   119
            })
dirk@594
   120
        } else {
dirk@594
   121
            state.isSynching = false
dirk@594
   122
            updateUI()
dirk@55
   123
        }
dirk@31
   124
    }
dirk@31
   125
dirk@452
   126
    @IBAction func mailSentSegue(segue: UIStoryboardSegue) {
dirk@452
   127
    }
dirk@452
   128
dirk@705
   129
    @IBAction func backFromComposeWithoutSavingDraftSegue(segue: UIStoryboardSegue) {
dirk@705
   130
    }
dirk@705
   131
dirk@705
   132
    @IBAction func backFromComposeSaveDraftSegue(segue: UIStoryboardSegue) {
dirk@705
   133
        guard let msg = draftMessageToStore else {
dirk@705
   134
            return
dirk@705
   135
        }
dirk@707
   136
dirk@707
   137
        state.isSynching = true
dirk@707
   138
        updateUI()
dirk@707
   139
dirk@706
   140
        config.appConfig.grandOperator.saveDraftMail(
dirk@706
   141
            msg, account: msg.folder.account, completionBlock: { error in
dirk@707
   142
                GCD.onMain() {
dirk@709
   143
                    UIHelper.displayError(error, controller: self)
dirk@707
   144
                    self.state.isSynching = false
dirk@707
   145
                    self.updateUI()
dirk@707
   146
                }
dirk@706
   147
        })
dirk@705
   148
    }
dirk@705
   149
ana@143
   150
    func prepareFetchRequest() {
dirk@31
   151
        let fetchRequest = NSFetchRequest.init(entityName: Message.entityName())
dirk@583
   152
        fetchRequest.predicate = config.predicate
dirk@583
   153
        fetchRequest.sortDescriptors = config.sortDescriptors
dirk@31
   154
        fetchController = NSFetchedResultsController.init(
dirk@31
   155
            fetchRequest: fetchRequest,
dirk@583
   156
            managedObjectContext: config.appConfig.coreDataUtil.managedObjectContext,
dirk@31
   157
            sectionNameKeyPath: nil, cacheName: nil)
dirk@31
   158
        fetchController?.delegate = self
dirk@31
   159
        do {
dirk@31
   160
            try fetchController?.performFetch()
dirk@31
   161
        } catch let err as NSError {
dirk@414
   162
            Log.errorComponent(comp, error: err)
dirk@31
   163
        }
dirk@31
   164
    }
dirk@31
   165
dirk@58
   166
    // MARK: - UI State
dirk@58
   167
dirk@58
   168
    func updateUI() {
dirk@275
   169
        if state.isSynching {
dirk@58
   170
            UIApplication.sharedApplication().networkActivityIndicatorVisible = true
dirk@58
   171
        } else {
dirk@58
   172
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
dirk@594
   173
            self.refreshControl?.endRefreshing()
dirk@58
   174
        }
dirk@58
   175
    }
dirk@58
   176
dirk@31
   177
    // MARK: - UITableViewDataSource
dirk@31
   178
dirk@31
   179
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
dirk@209
   180
        if let count = fetchController?.sections?.count {
dirk@209
   181
            return count
dirk@209
   182
        } else {
dirk@209
   183
            return 0
dirk@209
   184
        }
dirk@31
   185
    }
dirk@31
   186
dirk@31
   187
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dirk@31
   188
        if fetchController?.sections?.count > 0 {
dirk@31
   189
            if let sections = fetchController?.sections {
dirk@31
   190
                let sectionInfo = sections[section]
dirk@31
   191
                return sectionInfo.numberOfObjects
dirk@31
   192
            }
dirk@31
   193
        }
dirk@31
   194
        return 0
dirk@31
   195
    }
dirk@31
   196
dirk@31
   197
    override func tableView(tableView: UITableView,
dirk@31
   198
                            cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
dirk@209
   199
        let cell = tableView.dequeueReusableCellWithIdentifier(
dirk@209
   200
            "EmailListViewCell", forIndexPath: indexPath) as! EmailListViewCell
dirk@502
   201
        if !determinedCellBackgroundColor {
dirk@502
   202
            defaultCellBackgroundColor = cell.backgroundColor
dirk@502
   203
            determinedCellBackgroundColor = true
dirk@502
   204
        }
dirk@31
   205
        configureCell(cell, indexPath: indexPath)
dirk@31
   206
        return cell
dirk@31
   207
    }
dirk@31
   208
dirk@719
   209
    // MARK: - UITableViewDelegate
dirk@719
   210
dirk@719
   211
    override func tableView(tableView: UITableView,
dirk@719
   212
                            didSelectRowAtIndexPath indexPath: NSIndexPath) {
dirk@719
   213
        draftMessageToCompose = nil
dirk@719
   214
dirk@719
   215
        let cell = tableView.cellForRowAtIndexPath(indexPath)
dirk@719
   216
dirk@719
   217
        if let fn = config.folderName, let ac = config.account,
dirk@719
   218
            let folder = config.appConfig.model.folderByName(fn, email: ac.email) {
dirk@719
   219
            if folder.folderType.integerValue == FolderType.Drafts.rawValue {
dirk@719
   220
                draftMessageToCompose = fetchController?.objectAtIndexPath(indexPath)
dirk@719
   221
                    as? IMessage
dirk@719
   222
                performSegueWithIdentifier(segueCompose, sender: cell)
dirk@719
   223
                return
dirk@719
   224
            }
dirk@719
   225
        }
dirk@719
   226
dirk@719
   227
        performSegueWithIdentifier(segueShowEmail, sender: cell)
dirk@719
   228
    }
dirk@719
   229
dirk@744
   230
    override func tableView(tableView: UITableView, editActionsForRowAtIndexPath
dirk@744
   231
        indexPath: NSIndexPath)-> [UITableViewRowAction]? {
dirk@744
   232
dirk@744
   233
        let cell = tableView.cellForRowAtIndexPath(indexPath) as! EmailListViewCell
dirk@744
   234
        let email = fetchController?.objectAtIndexPath(indexPath) as! Message
dirk@744
   235
dirk@744
   236
        let isFlagAction = createIsFlagAction(email, cell: cell)
dirk@744
   237
        let deleteAction = createDeleteAction(cell)
dirk@744
   238
        let isReadAction = createIsReadAction(email, cell: cell)
dirk@744
   239
        return [deleteAction,isFlagAction,isReadAction]
dirk@744
   240
    }
dirk@744
   241
dirk@719
   242
    // MARK: - Misc
dirk@719
   243
dirk@31
   244
    func configureCell(cell: EmailListViewCell, indexPath: NSIndexPath) {
dirk@31
   245
        if let email = fetchController?.objectAtIndexPath(indexPath) as? Message {
dirk@502
   246
            if let colorRating = PEPUtil.colorRatingFromInt(email.pepColorRating?.integerValue) {
dirk@574
   247
                let privacyColor = PEPUtil.colorFromPepRating(colorRating)
dirk@502
   248
                if let uiColor = UIHelper.textBackgroundUIColorFromPrivacyColor(privacyColor) {
dirk@502
   249
                    cell.backgroundColor = uiColor
dirk@502
   250
                } else {
dirk@502
   251
                    if determinedCellBackgroundColor {
dirk@502
   252
                        cell.backgroundColor = defaultCellBackgroundColor
dirk@502
   253
                    }
dirk@502
   254
                }
dirk@502
   255
            }
dirk@342
   256
            UIHelper.putString(email.from?.displayString(), toLabel: cell.senderLabel)
dirk@342
   257
            UIHelper.putString(email.subject, toLabel: cell.subjectLabel)
dirk@572
   258
dirk@572
   259
            // Snippet
dirk@565
   260
            if let text = email.longMessage {
dirk@572
   261
                let theText = text.replaceNewLinesWith(" ").trimmedWhiteSpace()
dirk@565
   262
                UIHelper.putString(theText, toLabel: cell.summaryLabel)
dirk@572
   263
            } else if let html = email.longMessageFormatted {
dirk@568
   264
                var text = html.extractTextFromHTML()
dirk@572
   265
                text = text.replaceNewLinesWith(" ").trimmedWhiteSpace()
dirk@568
   266
                UIHelper.putString(text, toLabel: cell.summaryLabel)
dirk@568
   267
            } else {
dirk@568
   268
                UIHelper.putString(nil, toLabel: cell.summaryLabel)
dirk@565
   269
            }
dirk@32
   270
dirk@480
   271
            if let receivedDate = email.receivedDate {
dirk@480
   272
                UIHelper.putString(dateFormatter.stringFromDate(receivedDate),
dirk@342
   273
                                   toLabel: cell.dateLabel)
dirk@32
   274
            } else {
dirk@342
   275
                UIHelper.putString(nil, toLabel: cell.dateLabel)
dirk@32
   276
            }
ana@620
   277
ana@656
   278
            if (isImportantMessage(email) && isReadedMessage(email)) {
ana@666
   279
                cell.isImportantImage.hidden = false
ana@666
   280
                cell.isImportantImage.backgroundColor = UIColor.orangeColor()
ana@666
   281
            }
ana@666
   282
            else if (isImportantMessage(email) && !isReadedMessage(email)) {
ana@666
   283
                cell.isImportantImage.hidden = false
ana@656
   284
                cell.isImportantImage.backgroundColor = UIColor.blueColor()
ana@656
   285
                cell.isImportantImage.layer.borderWidth = 2
ana@656
   286
                cell.isImportantImage.layer.borderColor = UIColor.orangeColor().CGColor
ana@666
   287
            } else if (!isImportantMessage(email) && isReadedMessage(email)) {
ana@666
   288
                    cell.isImportantImage.hidden = true
ana@666
   289
            } else if (!isImportantMessage(email) && !isReadedMessage(email)) {
ana@620
   290
                cell.isImportantImage.hidden = false
ana@666
   291
                cell.isImportantImage.backgroundColor = UIColor.blueColor()
ana@620
   292
            }
dirk@31
   293
        }
dirk@30
   294
    }
dirk@30
   295
ana@246
   296
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
dirk@606
   297
        // Make sure the current account is set, if defined
dirk@606
   298
        config.appConfig.currentAccount = config.account
dirk@606
   299
dirk@361
   300
        if segue.identifier == segueCompose {
dirk@397
   301
            let destination = segue.destinationViewController
dirk@434
   302
                as! ComposeViewController
dirk@583
   303
            destination.appConfig = config.appConfig
dirk@719
   304
            if let draft = draftMessageToCompose {
dirk@723
   305
                draft.flagSeen = true
dirk@723
   306
                draft.updateFlags()
dirk@723
   307
                config.appConfig.model.save()
dirk@723
   308
dirk@719
   309
                destination.originalMessage = draft
dirk@719
   310
                destination.composeMode = .ComposeDraft
dirk@719
   311
            }
dirk@273
   312
        } else if segue.identifier == segueShowEmail {
dirk@273
   313
            guard
dirk@273
   314
                let vc = segue.destinationViewController as? EmailViewController,
dirk@273
   315
                let cell = sender as? UITableViewCell,
dirk@273
   316
                let indexPath = self.tableView.indexPathForCell(cell),
dirk@273
   317
                let email = fetchController?.objectAtIndexPath(indexPath) as? Message else {
dirk@273
   318
                    return
dirk@273
   319
            }
dirk@583
   320
            vc.appConfig = config.appConfig
dirk@273
   321
            vc.message = email
ana@246
   322
        }
ana@246
   323
    }
dirk@744
   324
dirk@744
   325
    func syncFlagsToServer(message: IMessage) {
dirk@744
   326
        self.config.appConfig.grandOperator.syncFlagsToServerForFolder(
dirk@744
   327
            message.folder,
dirk@744
   328
            completionBlock: { error in
dirk@744
   329
                UIHelper.displayError(error, controller: self)
dirk@744
   330
        })
dirk@744
   331
    }
dirk@744
   332
dirk@744
   333
    func createIsFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   334
dirk@744
   335
        // preparing the title action to show when user swipe
dirk@744
   336
        var localizedIsFlagTitle = " "
dirk@744
   337
        if (isImportantMessage(message)) {
dirk@744
   338
            localizedIsFlagTitle = NSLocalizedString("Unflag",
dirk@744
   339
                                                     comment: "Unflag button title in swipe action on EmailListViewController")
dirk@744
   340
        } else {
dirk@744
   341
            localizedIsFlagTitle = NSLocalizedString("Flag",
dirk@744
   342
                                                     comment: "Flag button title in swipe action on EmailListViewController")
dirk@744
   343
        }
dirk@744
   344
dirk@744
   345
        // preparing action to trigger when user swipe
dirk@744
   346
        let isFlagCompletionHandler: (UITableViewRowAction, NSIndexPath) -> Void =
dirk@744
   347
            { (action, indexPath) in
dirk@744
   348
                if (self.isImportantMessage(message)) {
dirk@744
   349
                    message.flagFlagged = false
dirk@744
   350
dirk@744
   351
                } else {
dirk@744
   352
                    message.flagFlagged = true
dirk@744
   353
                }
dirk@744
   354
                message.updateFlags()
dirk@744
   355
                self.syncFlagsToServer(message)
dirk@744
   356
                self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
dirk@744
   357
        }
dirk@744
   358
        // creating the action
dirk@744
   359
        let isFlagAction = UITableViewRowAction(style: .Default, title: localizedIsFlagTitle,
dirk@744
   360
                                                handler: isFlagCompletionHandler)
dirk@744
   361
        // changing default action color
dirk@744
   362
        isFlagAction.backgroundColor = UIColor.orangeColor()
dirk@744
   363
dirk@744
   364
        return isFlagAction
dirk@744
   365
    }
dirk@744
   366
dirk@744
   367
    func createDeleteAction (cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   368
dirk@744
   369
        // preparing the title action to show when user swipe
dirk@744
   370
        let localizedDeleteTitle = NSLocalizedString("Erase",
dirk@744
   371
                                                     comment: "Erase button title in swipe action on EmailListViewController")
dirk@744
   372
dirk@744
   373
        let deleteCompletionHandler: (UITableViewRowAction, NSIndexPath) -> Void =
dirk@744
   374
            { (action, indexPath) in
dirk@744
   375
                let managedObject = self.fetchController?.objectAtIndexPath(indexPath) as? IMessage
dirk@744
   376
                managedObject?.flagDeleted = true
dirk@744
   377
                managedObject?.updateFlags()
dirk@744
   378
                self.syncFlagsToServer(managedObject!)
dirk@744
   379
        }
dirk@744
   380
dirk@744
   381
        // creating the action
dirk@744
   382
        let deleteAction = UITableViewRowAction(style: .Default, title: localizedDeleteTitle,
dirk@744
   383
                                                handler: deleteCompletionHandler)
dirk@744
   384
        return deleteAction
dirk@744
   385
    }
dirk@744
   386
dirk@744
   387
    func createIsReadAction (message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   388
dirk@744
   389
        // preparing the title action to show when user swipe
dirk@744
   390
        var localizedisReadTitle = " "
dirk@744
   391
        if (isReadedMessage(message)) {
dirk@744
   392
            localizedisReadTitle = NSLocalizedString("Unread",
dirk@744
   393
                                                     comment: "Unread button title in swipe action on EmailListViewController")
dirk@744
   394
        } else {
dirk@744
   395
            localizedisReadTitle = NSLocalizedString("Read",
dirk@744
   396
                                                     comment: "Read button title in swipe action on EmailListViewController")
dirk@744
   397
        }
dirk@744
   398
dirk@744
   399
        // creating the action
dirk@744
   400
        let isReadCompletionHandler: (UITableViewRowAction, NSIndexPath) -> Void =
dirk@744
   401
            { (action, indexPath) in
dirk@744
   402
                if (self.isReadedMessage(message)) {
dirk@744
   403
                    message.flagSeen = false
dirk@744
   404
                    message.updateFlags()
dirk@744
   405
                } else {
dirk@744
   406
                    message.flagSeen = true
dirk@744
   407
                    message.updateFlags()
dirk@744
   408
                }
dirk@744
   409
                self.syncFlagsToServer(message)
dirk@744
   410
                self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
dirk@744
   411
        }
dirk@744
   412
        let isReadAction = UITableViewRowAction(style: .Default, title: localizedisReadTitle,
dirk@744
   413
                                                handler: isReadCompletionHandler)
dirk@744
   414
        isReadAction.backgroundColor = UIColor.blueColor()
dirk@744
   415
dirk@744
   416
        return isReadAction
dirk@744
   417
    }
dirk@30
   418
}
dirk@30
   419
dirk@31
   420
extension EmailListViewController: NSFetchedResultsControllerDelegate {
dirk@31
   421
    func controllerWillChangeContent(controller: NSFetchedResultsController) {
dirk@31
   422
        tableView.beginUpdates()
dirk@30
   423
    }
dirk@30
   424
dirk@31
   425
    func controller(controller: NSFetchedResultsController,
dirk@31
   426
                    didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
dirk@31
   427
                                     atIndex sectionIndex: Int,
dirk@31
   428
                                             forChangeType type: NSFetchedResultsChangeType) {
dirk@31
   429
        switch (type) {
dirk@31
   430
        case .Insert:
dirk@31
   431
            tableView.insertSections(NSIndexSet.init(index: sectionIndex),
dirk@31
   432
                                     withRowAnimation: .Fade)
dirk@31
   433
        case .Delete:
dirk@31
   434
            tableView.deleteSections(NSIndexSet.init(index: sectionIndex),
dirk@31
   435
                                     withRowAnimation: .Fade)
dirk@31
   436
        default:
dirk@414
   437
            Log.infoComponent(comp, "unhandled changeSectionType: \(type)")
dirk@31
   438
        }
dirk@30
   439
    }
dirk@30
   440
dirk@31
   441
    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject,
dirk@31
   442
                    atIndexPath indexPath: NSIndexPath?,
dirk@31
   443
                                forChangeType type: NSFetchedResultsChangeType,
dirk@31
   444
                                              newIndexPath: NSIndexPath?) {
dirk@40
   445
        switch type {
dirk@40
   446
        case .Insert:
dirk@40
   447
            tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
dirk@40
   448
        case .Delete:
dirk@40
   449
            tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
dirk@40
   450
        case .Update:
dirk@40
   451
            if let cell = tableView.cellForRowAtIndexPath(indexPath!) {
dirk@40
   452
                self.configureCell(cell as! EmailListViewCell, indexPath: indexPath!)
dirk@40
   453
            } else {
dirk@414
   454
                Log.warnComponent(comp, "Could not find cell for changed indexPath: \(indexPath!)")
dirk@31
   455
            }
dirk@40
   456
        case .Move:
dirk@40
   457
            if newIndexPath != indexPath {
dirk@40
   458
                tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
dirk@40
   459
                tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
dirk@40
   460
            }
dirk@31
   461
        }
dirk@31
   462
    }
dirk@31
   463
dirk@31
   464
    func controllerDidChangeContent(controller: NSFetchedResultsController) {
dirk@31
   465
        tableView.endUpdates()
dirk@31
   466
    }
dirk@30
   467
}