Skip to content

Commit

Permalink
Persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
mriddle committed Apr 28, 2016
1 parent 8ee2a12 commit a3c034b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Checklists/ChecklistItem.swift"
timestampString = "483431778.647364"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "16"
endingLineNumber = "16"
landmarkName = "init(text:checked:)"
landmarkType = "5">
scope = "0"
stopOnStyle = "0">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
Expand Down
13 changes: 11 additions & 2 deletions Checklists/ChecklistItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@

import Foundation

class ChecklistItem: NSObject {
class ChecklistItem: NSObject, NSCoding {
var text: String
var checked: Bool

required init?(coder aDecoder: NSCoder) {
text = aDecoder.decodeObjectForKey("Text") as! String
checked = aDecoder.decodeBoolForKey("Checked")
super.init()
}

init(text: String, checked: Bool) {
self.text = text
self.checked = checked
Expand All @@ -21,5 +27,8 @@ class ChecklistItem: NSObject {
checked = !checked
}


func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(text, forKey: "Text")
aCoder.encodeBool(checked, forKey: "Checked")
}
}
42 changes: 40 additions & 2 deletions Checklists/ChecklistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ class ChecklistViewController: UITableViewController, ItemDetailViewControllerDe
ChecklistItem(text: "Soccer practice", checked: false),
ChecklistItem(text: "Eat ice cream", checked: false)
]


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 Down Expand Up @@ -69,6 +74,7 @@ class ChecklistViewController: UITableViewController, ItemDetailViewControllerDe

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

}

Expand All @@ -79,6 +85,7 @@ class ChecklistViewController: UITableViewController, ItemDetailViewControllerDe

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

tableView.deselectRowAtIndexPath(indexPath, animated: true)
Expand All @@ -103,8 +110,9 @@ class ChecklistViewController: UITableViewController, ItemDetailViewControllerDe
}

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

func itemDetailViewController(controller: ItemDetailViewController, didFinishEditingItem item: ChecklistItem) {
Expand All @@ -114,7 +122,37 @@ class ChecklistViewController: UITableViewController, ItemDetailViewControllerDe
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()
}
}
}
}

0 comments on commit a3c034b

Please sign in to comment.