pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Mon, 02 May 2016 12:50:40 +0200
changeset 115 d75e39103b15
parent 107 e904616526e5
child 119 4df1dbe29f93
permissions -rw-r--r--
Move to interfaces for whole Model
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@58
    14
    let runningOpsUser = NSMutableOrderedSet()
dirk@58
    15
    let runningOpsAuto = NSMutableOrderedSet()
dirk@58
    16
dirk@58
    17
    func isSynching() -> Bool {
dirk@58
    18
        return runningOpsUser.count > 0 || runningOpsAuto.count > 0
dirk@58
    19
    }
dirk@58
    20
dirk@58
    21
    func isSynchingAuto() -> Bool {
dirk@58
    22
        return runningOpsAuto.count > 0
dirk@58
    23
    }
dirk@58
    24
dirk@58
    25
    func isSynchingUser() -> Bool {
dirk@58
    26
        return runningOpsUser.count > 0
dirk@58
    27
    }
dirk@58
    28
}
dirk@58
    29
dirk@31
    30
class EmailListViewController: UITableViewController {
dirk@31
    31
    let comp = "EmailListViewController"
dirk@30
    32
dirk@31
    33
    var appConfig: AppConfig?
dirk@31
    34
    var fetchController: NSFetchedResultsController?
dirk@58
    35
    let state = UIState()
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@31
    42
        }
dirk@31
    43
        prepareFetchRequest()
dirk@55
    44
dirk@115
    45
        if let account = appConfig?.model.fetchLastAccount() {
dirk@55
    46
            let connectInfo = account.connectInfo
dirk@55
    47
dirk@67
    48
            appConfig!.grandOperator.prefetchEmails(
dirk@55
    49
                connectInfo, folder: ImapSync.defaultImapInboxName, completionBlock: {
dirk@67
    50
                    [unowned self] error in
dirk@58
    51
                    GCD.onMain({
dirk@67
    52
                        Log.info(self.comp, "Sync completed, error: \(error)")
dirk@58
    53
                        self.updateUI()
dirk@58
    54
                    })
dirk@55
    55
            })
dirk@58
    56
            updateUI()
dirk@55
    57
        }
ana@107
    58
        //self.performSegueWithIdentifier("AccountSettings2", sender: self)
ana@107
    59
        //super.viewWillAppear(animated)
ana@107
    60
        //countWithName
dirk@31
    61
    }
dirk@31
    62
dirk@31
    63
    func prepareFetchRequest() {
dirk@31
    64
        let predicates: [NSPredicate] = [NSPredicate.init(value: true)]
dirk@31
    65
        let fetchRequest = NSFetchRequest.init(entityName: Message.entityName())
dirk@31
    66
        fetchRequest.predicate = NSCompoundPredicate.init(
dirk@31
    67
            andPredicateWithSubpredicates: predicates)
dirk@31
    68
        fetchRequest.sortDescriptors = [NSSortDescriptor.init(key: "sentDate", ascending: false)]
dirk@31
    69
        fetchController = NSFetchedResultsController.init(
dirk@31
    70
            fetchRequest: fetchRequest,
dirk@31
    71
            managedObjectContext: appConfig!.coreDataUtil.managedObjectContext,
dirk@31
    72
            sectionNameKeyPath: nil, cacheName: nil)
dirk@31
    73
        fetchController?.delegate = self
dirk@31
    74
        do {
dirk@31
    75
            try fetchController?.performFetch()
dirk@31
    76
        } catch let err as NSError {
dirk@31
    77
            Log.error(comp, error: err)
dirk@31
    78
        }
dirk@31
    79
    }
dirk@31
    80
dirk@58
    81
    // MARK: - UI State
dirk@58
    82
dirk@58
    83
    func updateUI() {
dirk@58
    84
        if state.isSynching() {
dirk@58
    85
            UIApplication.sharedApplication().networkActivityIndicatorVisible = true
dirk@58
    86
        } else {
dirk@58
    87
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
dirk@58
    88
        }
dirk@58
    89
    }
dirk@58
    90
dirk@31
    91
    // MARK: - UITableViewDataSource
dirk@31
    92
dirk@31
    93
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
dirk@31
    94
        return (fetchController?.sections?.count)!
dirk@31
    95
    }
dirk@31
    96
dirk@31
    97
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dirk@31
    98
        if fetchController?.sections?.count > 0 {
dirk@31
    99
            if let sections = fetchController?.sections {
dirk@31
   100
                let sectionInfo = sections[section]
dirk@31
   101
                return sectionInfo.numberOfObjects
dirk@31
   102
            }
dirk@31
   103
        }
dirk@31
   104
        return 0
dirk@31
   105
    }
dirk@31
   106
dirk@31
   107
    override func tableView(tableView: UITableView,
dirk@31
   108
                            cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
dirk@31
   109
        let cell = tableView.dequeueReusableCellWithIdentifier("EmailListViewCell", forIndexPath: indexPath) as! EmailListViewCell
dirk@31
   110
        configureCell(cell, indexPath: indexPath)
dirk@31
   111
        return cell
dirk@31
   112
    }
dirk@31
   113
dirk@31
   114
    func putString(string: String?, toLabel: UILabel) {
dirk@31
   115
        if string?.characters.count > 0 {
dirk@31
   116
            toLabel.hidden = false
dirk@31
   117
            toLabel.text = string!
dirk@31
   118
        } else {
dirk@31
   119
            toLabel.hidden = true
dirk@31
   120
        }
dirk@31
   121
    }
dirk@31
   122
dirk@31
   123
    func configureCell(cell: EmailListViewCell, indexPath: NSIndexPath) {
dirk@31
   124
        if let email = fetchController?.objectAtIndexPath(indexPath) as? Message {
dirk@31
   125
            putString(email.from?.displayString(), toLabel: cell.senderLabel)
dirk@31
   126
            putString(email.subject, toLabel: cell.subjectLabel)
dirk@31
   127
            putString(nil, toLabel: cell.summaryLabel)
dirk@32
   128
dirk@32
   129
            if let sentDate = email.sentDate {
dirk@32
   130
                let formatter = NSDateFormatter.init()
dirk@32
   131
                formatter.dateStyle = .ShortStyle
dirk@32
   132
                formatter.timeStyle = .ShortStyle
dirk@32
   133
                putString(formatter.stringFromDate(sentDate), toLabel: cell.dateLabel)
dirk@32
   134
            } else {
dirk@32
   135
                putString(nil, toLabel: cell.dateLabel)
dirk@32
   136
            }
dirk@31
   137
        }
dirk@30
   138
    }
dirk@30
   139
dirk@30
   140
}
dirk@30
   141
dirk@31
   142
extension EmailListViewController: NSFetchedResultsControllerDelegate {
dirk@31
   143
    func controllerWillChangeContent(controller: NSFetchedResultsController) {
dirk@31
   144
        tableView.beginUpdates()
dirk@30
   145
    }
dirk@30
   146
dirk@31
   147
    func controller(controller: NSFetchedResultsController,
dirk@31
   148
                    didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
dirk@31
   149
                                     atIndex sectionIndex: Int,
dirk@31
   150
                                             forChangeType type: NSFetchedResultsChangeType) {
dirk@31
   151
        switch (type) {
dirk@31
   152
        case .Insert:
dirk@31
   153
            tableView.insertSections(NSIndexSet.init(index: sectionIndex),
dirk@31
   154
                                     withRowAnimation: .Fade)
dirk@31
   155
        case .Delete:
dirk@31
   156
            tableView.deleteSections(NSIndexSet.init(index: sectionIndex),
dirk@31
   157
                                     withRowAnimation: .Fade)
dirk@31
   158
        default:
dirk@31
   159
            Log.info(comp, "unhandled changeSectionType: \(type)")
dirk@31
   160
        }
dirk@30
   161
    }
dirk@30
   162
dirk@31
   163
    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject,
dirk@31
   164
                    atIndexPath indexPath: NSIndexPath?,
dirk@31
   165
                                forChangeType type: NSFetchedResultsChangeType,
dirk@31
   166
                                              newIndexPath: NSIndexPath?) {
dirk@40
   167
        switch type {
dirk@40
   168
        case .Insert:
dirk@40
   169
            tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
dirk@40
   170
        case .Delete:
dirk@40
   171
            tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
dirk@40
   172
        case .Update:
dirk@40
   173
            if let cell = tableView.cellForRowAtIndexPath(indexPath!) {
dirk@40
   174
                self.configureCell(cell as! EmailListViewCell, indexPath: indexPath!)
dirk@40
   175
            } else {
dirk@40
   176
                Log.warn(comp, "Could not find cell for changed indexPath: \(indexPath!)")
dirk@31
   177
            }
dirk@40
   178
        case .Move:
dirk@40
   179
            if newIndexPath != indexPath {
dirk@40
   180
                tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
dirk@40
   181
                tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
dirk@40
   182
            }
dirk@31
   183
        }
dirk@31
   184
    }
dirk@31
   185
dirk@31
   186
    func controllerDidChangeContent(controller: NSFetchedResultsController) {
dirk@31
   187
        tableView.endUpdates()
dirk@31
   188
    }
ana@107
   189
    /*override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
ana@107
   190
        if segue.identifier == "AccountSettings2" {
ana@107
   191
            let destination = segue.destinationViewController as? UserInfoTableView
ana@107
   192
        }
ana@107
   193
    }*/
dirk@30
   194
}