forked from mslathrop/SwiftNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesTableViewController.swift
More file actions
134 lines (110 loc) · 5.88 KB
/
Copy pathNotesTableViewController.swift
File metadata and controls
134 lines (110 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// NotesTableViewController.swift
// SwiftNote
//
// Created by AppBrew LLC (appbrewllc.com) on 6/3/14.
// Copyright (c) 2014 Matt Lathrop. All rights reserved.
//
import CoreData
import UIKit
class NotesTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
var managedObjectContext: NSManagedObjectContext {
get {
if !_managedObjectContext {
_managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
}
return _managedObjectContext!
}
}
var _managedObjectContext: NSManagedObjectContext? = nil
var fetchedResultsController: NSFetchedResultsController {
get {
if !_fetchedResultsController {
// set up fetch request
var fetchRequest = NSFetchRequest()
fetchRequest.entity = NSEntityDescription.entityForName(kEntityNameNoteEntity, inManagedObjectContext: (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext)
// sort by last updated
var sortDescriptor = NSSortDescriptor(key: "modifiedAt", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.fetchBatchSize = 20
_fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext, sectionNameKeyPath: nil, cacheName: "allNotesCache")
_fetchedResultsController!.delegate = self
}
return _fetchedResultsController!;
}
}
var _fetchedResultsController: NSFetchedResultsController? = nil
init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
self.fetchedResultsController.performFetch(nil)
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == kSegueIdentifierNotesTableToNoteDetailEdit) {
let entity = self.fetchedResultsController.objectAtIndexPath(self.tableView.indexPathForSelectedRow()) as NSManagedObject
let note = Note.noteFromNoteEntity(entity)
let viewController = segue.destinationViewController as NoteDetailViewController
viewController.note = note
}
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return self.fetchedResultsController.sections.count
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = self.fetchedResultsController.sections[section] as NSFetchedResultsSectionInfo
return sectionInfo.numberOfObjects
}
override func tableView(_: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(kReuseIdentifierNotesTableViewCell, forIndexPath: indexPath) as NotesTableViewCell
let entity = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
let note = Note.noteFromNoteEntity(entity)
cell.configure(note: note, indexPath: indexPath)
return cell
}
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
return 70
}
override func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
let entity = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
let note = Note.noteFromNoteEntity(entity)
note.deleteInManagedObjectContext((UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext)
(UIApplication.sharedApplication().delegate as AppDelegate).saveContext()
}
// MARK: - fetched results controller delegate
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case NSFetchedResultsChangeInsert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case NSFetchedResultsChangeDelete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
return
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath) {
switch type {
case NSFetchedResultsChangeInsert:
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
case NSFetchedResultsChangeDelete:
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
case NSFetchedResultsChangeUpdate:
let cell = tableView.cellForRowAtIndexPath(indexPath) as NotesTableViewCell
let note = self.fetchedResultsController.sections[indexPath.section][indexPath.row] as Note
cell.configure(note: note, indexPath: indexPath)
case NSFetchedResultsChangeMove:
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
default:
return
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}
}