pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Wed, 20 Apr 2016 11:33:29 +0200
changeset 55 be52cd44b034
parent 54 7447822a3ff9
child 58 5519a5083c13
permissions -rw-r--r--
Move imap sync to email list view
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@31
    13
class EmailListViewController: UITableViewController {
dirk@31
    14
    let comp = "EmailListViewController"
dirk@30
    15
dirk@31
    16
    var appConfig: AppConfig?
dirk@31
    17
    var fetchController: NSFetchedResultsController?
dirk@31
    18
dirk@31
    19
    override func viewWillAppear(animated: Bool) {
dirk@31
    20
        if appConfig == nil {
dirk@31
    21
            if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
dirk@31
    22
                appConfig = appDelegate.appConfig
dirk@31
    23
            }
dirk@31
    24
        }
dirk@31
    25
        prepareFetchRequest()
dirk@55
    26
dirk@55
    27
        if let account = Account.fetchLastAccount(appConfig!.coreDataUtil.managedObjectContext) {
dirk@55
    28
            let connectInfo = account.connectInfo
dirk@55
    29
dirk@55
    30
            appConfig!.grandOperator.prefetchEmailsImap(
dirk@55
    31
                connectInfo, folder: ImapSync.defaultImapInboxName, completionBlock: {
dirk@55
    32
                    Log.info(self.comp, "Sync completed")
dirk@55
    33
            })
dirk@55
    34
        }
dirk@55
    35
        
dirk@31
    36
        super.viewWillAppear(animated)
dirk@31
    37
    }
dirk@31
    38
dirk@31
    39
    func prepareFetchRequest() {
dirk@31
    40
        let predicates: [NSPredicate] = [NSPredicate.init(value: true)]
dirk@31
    41
        let fetchRequest = NSFetchRequest.init(entityName: Message.entityName())
dirk@31
    42
        fetchRequest.predicate = NSCompoundPredicate.init(
dirk@31
    43
            andPredicateWithSubpredicates: predicates)
dirk@31
    44
        fetchRequest.sortDescriptors = [NSSortDescriptor.init(key: "sentDate", ascending: false)]
dirk@31
    45
        fetchController = NSFetchedResultsController.init(
dirk@31
    46
            fetchRequest: fetchRequest,
dirk@31
    47
            managedObjectContext: appConfig!.coreDataUtil.managedObjectContext,
dirk@31
    48
            sectionNameKeyPath: nil, cacheName: nil)
dirk@31
    49
        fetchController?.delegate = self
dirk@31
    50
        do {
dirk@31
    51
            try fetchController?.performFetch()
dirk@31
    52
        } catch let err as NSError {
dirk@31
    53
            Log.error(comp, error: err)
dirk@31
    54
        }
dirk@31
    55
    }
dirk@31
    56
dirk@31
    57
    // MARK: - UITableViewDataSource
dirk@31
    58
dirk@31
    59
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
dirk@31
    60
        return (fetchController?.sections?.count)!
dirk@31
    61
    }
dirk@31
    62
dirk@31
    63
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dirk@31
    64
        if fetchController?.sections?.count > 0 {
dirk@31
    65
            if let sections = fetchController?.sections {
dirk@31
    66
                let sectionInfo = sections[section]
dirk@31
    67
                return sectionInfo.numberOfObjects
dirk@31
    68
            }
dirk@31
    69
        }
dirk@31
    70
        return 0
dirk@31
    71
    }
dirk@31
    72
dirk@31
    73
    override func tableView(tableView: UITableView,
dirk@31
    74
                            cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
dirk@31
    75
        let cell = tableView.dequeueReusableCellWithIdentifier("EmailListViewCell", forIndexPath: indexPath) as! EmailListViewCell
dirk@31
    76
        configureCell(cell, indexPath: indexPath)
dirk@31
    77
        return cell
dirk@31
    78
    }
dirk@31
    79
dirk@31
    80
    func putString(string: String?, toLabel: UILabel) {
dirk@31
    81
        if string?.characters.count > 0 {
dirk@31
    82
            toLabel.hidden = false
dirk@31
    83
            toLabel.text = string!
dirk@31
    84
        } else {
dirk@31
    85
            toLabel.hidden = true
dirk@31
    86
        }
dirk@31
    87
    }
dirk@31
    88
dirk@31
    89
    func configureCell(cell: EmailListViewCell, indexPath: NSIndexPath) {
dirk@31
    90
        if let email = fetchController?.objectAtIndexPath(indexPath) as? Message {
dirk@31
    91
            putString(email.from?.displayString(), toLabel: cell.senderLabel)
dirk@31
    92
            putString(email.subject, toLabel: cell.subjectLabel)
dirk@31
    93
            putString(nil, toLabel: cell.summaryLabel)
dirk@32
    94
dirk@32
    95
            if let sentDate = email.sentDate {
dirk@32
    96
                let formatter = NSDateFormatter.init()
dirk@32
    97
                formatter.dateStyle = .ShortStyle
dirk@32
    98
                formatter.timeStyle = .ShortStyle
dirk@32
    99
                putString(formatter.stringFromDate(sentDate), toLabel: cell.dateLabel)
dirk@32
   100
            } else {
dirk@32
   101
                putString(nil, toLabel: cell.dateLabel)
dirk@32
   102
            }
dirk@31
   103
        }
dirk@30
   104
    }
dirk@30
   105
dirk@30
   106
}
dirk@30
   107
dirk@31
   108
extension EmailListViewController: NSFetchedResultsControllerDelegate {
dirk@31
   109
    func controllerWillChangeContent(controller: NSFetchedResultsController) {
dirk@31
   110
        tableView.beginUpdates()
dirk@30
   111
    }
dirk@30
   112
dirk@31
   113
    func controller(controller: NSFetchedResultsController,
dirk@31
   114
                    didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
dirk@31
   115
                                     atIndex sectionIndex: Int,
dirk@31
   116
                                             forChangeType type: NSFetchedResultsChangeType) {
dirk@31
   117
        switch (type) {
dirk@31
   118
        case .Insert:
dirk@31
   119
            tableView.insertSections(NSIndexSet.init(index: sectionIndex),
dirk@31
   120
                                     withRowAnimation: .Fade)
dirk@31
   121
        case .Delete:
dirk@31
   122
            tableView.deleteSections(NSIndexSet.init(index: sectionIndex),
dirk@31
   123
                                     withRowAnimation: .Fade)
dirk@31
   124
        default:
dirk@31
   125
            Log.info(comp, "unhandled changeSectionType: \(type)")
dirk@31
   126
        }
dirk@30
   127
    }
dirk@30
   128
dirk@31
   129
    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject,
dirk@31
   130
                    atIndexPath indexPath: NSIndexPath?,
dirk@31
   131
                                forChangeType type: NSFetchedResultsChangeType,
dirk@31
   132
                                              newIndexPath: NSIndexPath?) {
dirk@40
   133
        switch type {
dirk@40
   134
        case .Insert:
dirk@40
   135
            tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
dirk@40
   136
        case .Delete:
dirk@40
   137
            tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
dirk@40
   138
        case .Update:
dirk@40
   139
            if let cell = tableView.cellForRowAtIndexPath(indexPath!) {
dirk@40
   140
                self.configureCell(cell as! EmailListViewCell, indexPath: indexPath!)
dirk@40
   141
            } else {
dirk@40
   142
                Log.warn(comp, "Could not find cell for changed indexPath: \(indexPath!)")
dirk@31
   143
            }
dirk@40
   144
        case .Move:
dirk@40
   145
            if newIndexPath != indexPath {
dirk@40
   146
                tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
dirk@40
   147
                tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
dirk@40
   148
            }
dirk@31
   149
        }
dirk@31
   150
    }
dirk@31
   151
dirk@31
   152
    func controllerDidChangeContent(controller: NSFetchedResultsController) {
dirk@31
   153
        tableView.endUpdates()
dirk@31
   154
    }
dirk@30
   155
}