Skip to content

Commit

Permalink
Checklist persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
mriddle committed May 2, 2016
1 parent 6bd5e87 commit 0cde2ff
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 66 deletions.
43 changes: 37 additions & 6 deletions AllListsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import UIKit

class AllListsViewController: UITableViewController, ListDetailViewControllerDelegate {

var lists = [
Checklist(name: "Birthdays"),
Checklist(name: "Groceries"),
Checklist(name: "Cool Apps"),
Checklist(name: "To Do")
]
var lists: [Checklist] = []

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadChecklists()
}

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -100,4 +100,35 @@ class AllListsViewController: UITableViewController, ListDetailViewControllerDel

presentViewController(navigationController, animated: true, completion: nil)
}


func documentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
return paths[0]
}

func dataFilePath() -> String {
return documentsDirectory().stringByAppendingPathComponent("Checklists.plist")
}

func saveChecklists() {
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
archiver.encodeObject(lists, forKey: "Checklists")
archiver.finishEncoding()
data.writeToFile(dataFilePath(), atomically: true)
}

func loadChecklists() {
print("Loading file \(dataFilePath())")
let path = dataFilePath()
if NSFileManager.defaultManager().fileExistsAtPath(path) {
if let data = NSData(contentsOfFile: path) {
let unarchiver = NSKeyedUnarchiver(forReadingWithData: data)
lists = unarchiver.decodeObjectForKey("Checklists")
as! [Checklist]
unarchiver.finishDecoding()
}
}
}
}
12 changes: 9 additions & 3 deletions Checklists/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
saveData()
}

func applicationWillEnterForeground(application: UIApplication) {
Expand All @@ -38,7 +37,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
saveData()
}

func saveData() {
let navigationController = window!.rootViewController as! UINavigationController
let controller = navigationController.viewControllers[0] as! AllListsViewController

controller.saveChecklists()
}


Expand Down
17 changes: 15 additions & 2 deletions Checklists/Checklist.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import UIKit

class Checklist: NSObject {
class Checklist: NSObject, NSCoding {
var name: String
var items: [ChecklistItem]

init(name: String) {
init(name: String, items: [ChecklistItem]) {
self.name = name
self.items = items
super.init()
}

required init?(coder aDecoder: NSCoder) {
name = aDecoder.decodeObjectForKey("Name") as! String
items = aDecoder.decodeObjectForKey("Items") as! [ChecklistItem]
super.init()
}

func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey: "Name")
aCoder.encodeObject(items, forKey: "Items")
}
}
63 changes: 9 additions & 54 deletions Checklists/ChecklistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,9 @@
import UIKit

class ChecklistViewController: UITableViewController, ItemDetailViewControllerDelegate {
var items: Array = [
ChecklistItem(text: "Walk the dog", checked: false),
ChecklistItem(text: "Brush teeth", checked: false),
ChecklistItem(text: "Learn iOS development", checked: false),
ChecklistItem(text: "Soccer practice", checked: false),
ChecklistItem(text: "Eat ice cream", checked: false)
]


var checklist: Checklist!

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadChecklistItems()
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "AddItem" {
let navigationController = segue.destinationViewController as! UINavigationController
Expand All @@ -36,7 +24,7 @@ class ChecklistViewController: UITableViewController, ItemDetailViewControllerDe

controller.delegate = self
if let indexPath = tableView.indexPathForCell(sender as! UITableViewCell) {
controller.itemToEdit = items[indexPath.row]
controller.itemToEdit = checklist.items[indexPath.row]
}
}
}
Expand All @@ -52,20 +40,20 @@ class ChecklistViewController: UITableViewController, ItemDetailViewControllerDe
}

func addItem(item: ChecklistItem) {
let newRowIndex = items.count
items.append(item)
let newRowIndex = checklist.items.count
checklist.items.append(item)

let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
return checklist.items.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ChecklistItem", forIndexPath: indexPath)
let item = items[indexPath.row]
let item = checklist.items[indexPath.row]

configureTextForCell(cell, withChecklistItem: item)
configureCheckmarkForCell(cell, withChecklistItem: item)
Expand All @@ -74,20 +62,18 @@ class ChecklistViewController: UITableViewController, ItemDetailViewControllerDe

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

items.removeAtIndex(indexPath.row)
checklist.items.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
saveChecklistItems()

}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

if let cell = tableView.cellForRowAtIndexPath(indexPath) {
let item = items[indexPath.row]
let item = checklist.items[indexPath.row]

item.toggleChecked()
configureCheckmarkForCell(cell, withChecklistItem: item)
saveChecklistItems()
}

tableView.deselectRowAtIndexPath(indexPath, animated: true)
Expand All @@ -113,48 +99,17 @@ class ChecklistViewController: UITableViewController, ItemDetailViewControllerDe

func itemDetailViewController(controller: ItemDetailViewController, didFinishAddingItem item: ChecklistItem) {
addItem(item)
saveChecklistItems()
dismissViewControllerAnimated(true, completion: nil)
}

func itemDetailViewController(controller: ItemDetailViewController, didFinishEditingItem item: ChecklistItem) {
if let index = items.indexOf(item) {
if let index = checklist.items.indexOf(item) {
let indexPath = NSIndexPath(forRow: index, inSection: 0)
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
configureTextForCell(cell, withChecklistItem: item)
}
}
saveChecklistItems()
dismissViewControllerAnimated(true, completion: nil)
}

func documentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
return paths[0]
}

func dataFilePath() -> String {
return documentsDirectory().stringByAppendingPathComponent("Checklists.plist")
}

func saveChecklistItems() {
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
archiver.encodeObject(items, forKey: "ChecklistItems")
archiver.finishEncoding()
data.writeToFile(dataFilePath(), atomically: true)
}

func loadChecklistItems() {
let path = dataFilePath()
if NSFileManager.defaultManager().fileExistsAtPath(path) {
if let data = NSData(contentsOfFile: path) {
let unarchiver = NSKeyedUnarchiver(forReadingWithData: data)
items = unarchiver.decodeObjectForKey("ChecklistItems")
as! [ChecklistItem]
unarchiver.finishDecoding()
}
}
}
}

2 changes: 1 addition & 1 deletion Checklists/ListDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ListDetailViewController: UITableViewController, UITextFieldDelegate {
checklist.name = textField.text!
delegate?.listDetailViewController(self, didFinishEditingChecklist: checklist)
} else {
let checklist = Checklist(name: textField.text!)
let checklist = Checklist(name: textField.text!, items: [])
delegate?.listDetailViewController(self, didFinishAddingChecklist: checklist)
}
}
Expand Down

0 comments on commit 0cde2ff

Please sign in to comment.