pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Mon, 20 Jun 2016 10:59:09 +0200
changeset 361 fff53ba1bbbb
parent 356 e8d2f5b226ce
child 365 47994352e9ba
permissions -rw-r--r--
EmailHeaderView
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@58
    13
struct UIState {
dirk@275
    14
    var isSynching: Bool = false
dirk@58
    15
}
dirk@58
    16
dirk@31
    17
class EmailListViewController: UITableViewController {
dirk@273
    18
    let comp = "EmailListViewController"
dirk@361
    19
dirk@273
    20
    let segueShowEmail = "segueShowEmail"
dirk@361
    21
    let segueCompose = "composeSegue"
dirk@361
    22
    let segueUserSettings = "userSettings"
dirk@30
    23
dirk@31
    24
    var appConfig: AppConfig?
dirk@212
    25
    var fetchController: NSFetchedResultsController?
dirk@275
    26
    var state = UIState()
dirk@342
    27
    let dateFormatter = UIHelper.dateFormatterEmailList()
dirk@275
    28
dirk@275
    29
    override func viewDidLoad() {
dirk@275
    30
        let refreshController = UIRefreshControl.init()
dirk@275
    31
        refreshController.addTarget(self, action: #selector(self.refresh(_:)),
dirk@275
    32
                                    forControlEvents: UIControlEvents.ValueChanged)
dirk@275
    33
        self.refreshControl = refreshController
dirk@356
    34
        UIHelper.variableCellHeightsTableView(self.tableView)
dirk@275
    35
    }
dirk@31
    36
dirk@31
    37
    override func viewWillAppear(animated: Bool) {
dirk@31
    38
        if appConfig == nil {
dirk@31
    39
            if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
dirk@31
    40
                appConfig = appDelegate.appConfig
dirk@31
    41
            }
dirk@273
    42
        }
dirk@209
    43
        prepareFetchRequest()
dirk@209
    44
ana@152
    45
        let account:IAccount? = appConfig!.model.fetchLastAccount()
ana@152
    46
        if (account == nil)  {
dirk@361
    47
            self.performSegueWithIdentifier(segueUserSettings, sender: self)
ana@207
    48
        } else {
dirk@275
    49
            fetchMailsRefreshControl()
ana@152
    50
        }
ana@207
    51
        super.viewWillAppear(animated)
ana@207
    52
    }
ana@152
    53
dirk@275
    54
    func refresh(refreshControl: UIRefreshControl) {
dirk@275
    55
        fetchMailsRefreshControl(refreshControl)
dirk@275
    56
    }
dirk@275
    57
dirk@275
    58
    func fetchMailsRefreshControl(refreshControl: UIRefreshControl? = nil) {
dirk@115
    59
        if let account = appConfig?.model.fetchLastAccount() {
dirk@55
    60
            let connectInfo = account.connectInfo
dirk@55
    61
dirk@275
    62
            state.isSynching = true
dirk@67
    63
            appConfig!.grandOperator.prefetchEmails(
dirk@55
    64
                connectInfo, folder: ImapSync.defaultImapInboxName, completionBlock: {
dirk@67
    65
                    [unowned self] error in
dirk@58
    66
                    GCD.onMain({
dirk@67
    67
                        Log.info(self.comp, "Sync completed, error: \(error)")
dirk@325
    68
                        self.appConfig?.model.save()
dirk@275
    69
                        self.state.isSynching = false
dirk@275
    70
                        refreshControl?.endRefreshing()
dirk@149
    71
                        self.updateUI()
dirk@58
    72
                    })
ana@207
    73
                })
dirk@149
    74
            updateUI()
dirk@55
    75
        }
dirk@31
    76
    }
dirk@31
    77
dirk@196
    78
    @IBAction func newAccountCreatedSegue(segue: UIStoryboardSegue) {
dirk@275
    79
        fetchMailsRefreshControl()
dirk@196
    80
    }
dirk@196
    81
ana@143
    82
    func prepareFetchRequest() {
dirk@324
    83
        let predicates: [NSPredicate] = [NSPredicate.init(format: "bodyFetched = true")]
dirk@31
    84
        let fetchRequest = NSFetchRequest.init(entityName: Message.entityName())
dirk@31
    85
        fetchRequest.predicate = NSCompoundPredicate.init(
dirk@31
    86
            andPredicateWithSubpredicates: predicates)
dirk@324
    87
        fetchRequest.sortDescriptors = [NSSortDescriptor.init(key: "originationDate",
dirk@324
    88
            ascending: false)]
dirk@31
    89
        fetchController = NSFetchedResultsController.init(
dirk@31
    90
            fetchRequest: fetchRequest,
dirk@31
    91
            managedObjectContext: appConfig!.coreDataUtil.managedObjectContext,
dirk@31
    92
            sectionNameKeyPath: nil, cacheName: nil)
dirk@31
    93
        fetchController?.delegate = self
dirk@31
    94
        do {
dirk@31
    95
            try fetchController?.performFetch()
dirk@31
    96
        } catch let err as NSError {
dirk@31
    97
            Log.error(comp, error: err)
dirk@31
    98
        }
dirk@31
    99
    }
dirk@31
   100
dirk@58
   101
    // MARK: - UI State
dirk@58
   102
dirk@58
   103
    func updateUI() {
dirk@275
   104
        if state.isSynching {
dirk@58
   105
            UIApplication.sharedApplication().networkActivityIndicatorVisible = true
dirk@58
   106
        } else {
dirk@58
   107
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
dirk@58
   108
        }
dirk@58
   109
    }
dirk@58
   110
dirk@31
   111
    // MARK: - UITableViewDataSource
dirk@31
   112
dirk@31
   113
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
dirk@209
   114
        if let count = fetchController?.sections?.count {
dirk@209
   115
            return count
dirk@209
   116
        } else {
dirk@209
   117
            return 0
dirk@209
   118
        }
dirk@31
   119
    }
dirk@31
   120
dirk@31
   121
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dirk@31
   122
        if fetchController?.sections?.count > 0 {
dirk@31
   123
            if let sections = fetchController?.sections {
dirk@31
   124
                let sectionInfo = sections[section]
dirk@31
   125
                return sectionInfo.numberOfObjects
dirk@31
   126
            }
dirk@31
   127
        }
dirk@31
   128
        return 0
dirk@31
   129
    }
dirk@31
   130
dirk@31
   131
    override func tableView(tableView: UITableView,
dirk@31
   132
                            cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
dirk@209
   133
        let cell = tableView.dequeueReusableCellWithIdentifier(
dirk@209
   134
            "EmailListViewCell", forIndexPath: indexPath) as! EmailListViewCell
dirk@31
   135
        configureCell(cell, indexPath: indexPath)
dirk@31
   136
        return cell
dirk@31
   137
    }
dirk@31
   138
dirk@31
   139
    func configureCell(cell: EmailListViewCell, indexPath: NSIndexPath) {
dirk@31
   140
        if let email = fetchController?.objectAtIndexPath(indexPath) as? Message {
dirk@342
   141
            UIHelper.putString(email.from?.displayString(), toLabel: cell.senderLabel)
dirk@342
   142
            UIHelper.putString(email.subject, toLabel: cell.subjectLabel)
dirk@342
   143
            UIHelper.putString(nil, toLabel: cell.summaryLabel)
dirk@32
   144
dirk@119
   145
            if let originationDate = email.originationDate {
dirk@342
   146
                UIHelper.putString(dateFormatter.stringFromDate(originationDate),
dirk@342
   147
                                   toLabel: cell.dateLabel)
dirk@32
   148
            } else {
dirk@342
   149
                UIHelper.putString(nil, toLabel: cell.dateLabel)
dirk@32
   150
            }
dirk@31
   151
        }
dirk@30
   152
    }
dirk@30
   153
ana@246
   154
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
dirk@361
   155
        if segue.identifier == segueCompose {
ana@334
   156
            let destination = segue.destinationViewController as! ComposeWithAutocompleteViewController
ana@246
   157
            destination.appConfig = appConfig
dirk@273
   158
        } else if segue.identifier == segueShowEmail {
dirk@273
   159
            guard
dirk@273
   160
                let vc = segue.destinationViewController as? EmailViewController,
dirk@273
   161
                let cell = sender as? UITableViewCell,
dirk@273
   162
                let indexPath = self.tableView.indexPathForCell(cell),
dirk@273
   163
                let email = fetchController?.objectAtIndexPath(indexPath) as? Message else {
dirk@273
   164
                    return
dirk@273
   165
            }
dirk@273
   166
            vc.appConfig = appConfig
dirk@273
   167
            vc.message = email
ana@246
   168
        }
ana@246
   169
    }
ana@246
   170
dirk@30
   171
}
dirk@30
   172
dirk@31
   173
extension EmailListViewController: NSFetchedResultsControllerDelegate {
dirk@31
   174
    func controllerWillChangeContent(controller: NSFetchedResultsController) {
dirk@31
   175
        tableView.beginUpdates()
dirk@30
   176
    }
dirk@30
   177
dirk@31
   178
    func controller(controller: NSFetchedResultsController,
dirk@31
   179
                    didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
dirk@31
   180
                                     atIndex sectionIndex: Int,
dirk@31
   181
                                             forChangeType type: NSFetchedResultsChangeType) {
dirk@31
   182
        switch (type) {
dirk@31
   183
        case .Insert:
dirk@31
   184
            tableView.insertSections(NSIndexSet.init(index: sectionIndex),
dirk@31
   185
                                     withRowAnimation: .Fade)
dirk@31
   186
        case .Delete:
dirk@31
   187
            tableView.deleteSections(NSIndexSet.init(index: sectionIndex),
dirk@31
   188
                                     withRowAnimation: .Fade)
dirk@31
   189
        default:
dirk@31
   190
            Log.info(comp, "unhandled changeSectionType: \(type)")
dirk@31
   191
        }
dirk@30
   192
    }
dirk@30
   193
dirk@31
   194
    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject,
dirk@31
   195
                    atIndexPath indexPath: NSIndexPath?,
dirk@31
   196
                                forChangeType type: NSFetchedResultsChangeType,
dirk@31
   197
                                              newIndexPath: NSIndexPath?) {
dirk@40
   198
        switch type {
dirk@40
   199
        case .Insert:
dirk@40
   200
            tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
dirk@40
   201
        case .Delete:
dirk@40
   202
            tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
dirk@40
   203
        case .Update:
dirk@40
   204
            if let cell = tableView.cellForRowAtIndexPath(indexPath!) {
dirk@40
   205
                self.configureCell(cell as! EmailListViewCell, indexPath: indexPath!)
dirk@40
   206
            } else {
dirk@40
   207
                Log.warn(comp, "Could not find cell for changed indexPath: \(indexPath!)")
dirk@31
   208
            }
dirk@40
   209
        case .Move:
dirk@40
   210
            if newIndexPath != indexPath {
dirk@40
   211
                tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
dirk@40
   212
                tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
dirk@40
   213
            }
dirk@31
   214
        }
dirk@31
   215
    }
dirk@31
   216
dirk@31
   217
    func controllerDidChangeContent(controller: NSFetchedResultsController) {
dirk@31
   218
        tableView.endUpdates()
dirk@31
   219
    }
dirk@30
   220
}