Skip to content

Commit

Permalink
Moving logic out of the view
Browse files Browse the repository at this point in the history
  • Loading branch information
mriddle committed May 2, 2016
1 parent 0cde2ff commit efca2b0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 51 deletions.
54 changes: 9 additions & 45 deletions AllListsViewController.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import UIKit

class AllListsViewController: UITableViewController, ListDetailViewControllerDelegate {

var lists: [Checklist] = []

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadChecklists()
}
var dataModel: DataModel!

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -18,21 +13,21 @@ class AllListsViewController: UITableViewController, ListDetailViewControllerDel
}

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = cellForTableView(tableView)

let checklist = lists[indexPath.row]
let checklist = dataModel.lists[indexPath.row]
cell.textLabel!.text = checklist.name
cell.accessoryType = .DetailDisclosureButton

return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let checklist = lists[indexPath.row]
let checklist = dataModel.lists[indexPath.row]
performSegueWithIdentifier("ShowChecklist", sender: checklist)
}

Expand Down Expand Up @@ -63,9 +58,9 @@ class AllListsViewController: UITableViewController, ListDetailViewControllerDel
}

func listDetailViewController(controller: ListDetailViewController, didFinishAddingChecklist checklist: Checklist) {
let newRowIndex = lists.count
let newRowIndex = dataModel.lists.count

lists.append(checklist)
dataModel.lists.append(checklist)

let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
Expand All @@ -74,7 +69,7 @@ class AllListsViewController: UITableViewController, ListDetailViewControllerDel
}

func listDetailViewController(controller: ListDetailViewController, didFinishEditingChecklist checklist: Checklist) {
if let index = lists.indexOf(checklist) {
if let index = dataModel.lists.indexOf(checklist) {
let indexPath = NSIndexPath(forRow: index, inSection: 0)
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.textLabel!.text = checklist.name
Expand All @@ -84,7 +79,7 @@ class AllListsViewController: UITableViewController, ListDetailViewControllerDel
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
lists.removeAtIndex(indexPath.row)
dataModel.lists.removeAtIndex(indexPath.row)

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
Expand All @@ -95,40 +90,9 @@ class AllListsViewController: UITableViewController, ListDetailViewControllerDel

controller.delegate = self

let checklist = lists[indexPath.row]
let checklist = dataModel.lists[indexPath.row]
controller.checklistToEdit = checklist

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()
}
}
}
}
4 changes: 4 additions & 0 deletions Checklists.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
A81172CC1CD0660F00BA3196 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A81172CB1CD0660F00BA3196 /* Assets.xcassets */; };
A81172CF1CD0660F00BA3196 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A81172CD1CD0660F00BA3196 /* LaunchScreen.storyboard */; };
A81172D91CD0859800BA3196 /* ChecklistItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = A81172D81CD0859800BA3196 /* ChecklistItem.swift */; };
A83B303C1CD6ECF800D38D88 /* DataModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A83B303B1CD6ECF800D38D88 /* DataModel.swift */; };
A8961D4F1CD19E87006AFAF4 /* ItemDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8961D4E1CD19E87006AFAF4 /* ItemDetailViewController.swift */; };
A8961D541CD309B0006AFAF4 /* AllListsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8961D531CD309B0006AFAF4 /* AllListsViewController.swift */; };
A8961D561CD31108006AFAF4 /* Checklist.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8961D551CD31108006AFAF4 /* Checklist.swift */; };
Expand All @@ -28,6 +29,7 @@
A81172CE1CD0660F00BA3196 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
A81172D01CD0660F00BA3196 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A81172D81CD0859800BA3196 /* ChecklistItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChecklistItem.swift; sourceTree = "<group>"; };
A83B303B1CD6ECF800D38D88 /* DataModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataModel.swift; sourceTree = "<group>"; };
A8961D4E1CD19E87006AFAF4 /* ItemDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = ItemDetailViewController.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
A8961D531CD309B0006AFAF4 /* AllListsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AllListsViewController.swift; path = ../AllListsViewController.swift; sourceTree = "<group>"; };
A8961D551CD31108006AFAF4 /* Checklist.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Checklist.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -66,6 +68,7 @@
children = (
A8961D531CD309B0006AFAF4 /* AllListsViewController.swift */,
A81172C41CD0660E00BA3196 /* AppDelegate.swift */,
A83B303B1CD6ECF800D38D88 /* DataModel.swift */,
A81172C61CD0660E00BA3196 /* ChecklistViewController.swift */,
A81172C81CD0660E00BA3196 /* Main.storyboard */,
A81172CB1CD0660F00BA3196 /* Assets.xcassets */,
Expand Down Expand Up @@ -156,6 +159,7 @@
A8961D4F1CD19E87006AFAF4 /* ItemDetailViewController.swift in Sources */,
A8961D561CD31108006AFAF4 /* Checklist.swift in Sources */,
A81172D91CD0859800BA3196 /* ChecklistItem.swift in Sources */,
A83B303C1CD6ECF800D38D88 /* DataModel.swift in Sources */,
A8961D541CD309B0006AFAF4 /* AllListsViewController.swift in Sources */,
A81172C51CD0660E00BA3196 /* AppDelegate.swift in Sources */,
);
Expand Down
14 changes: 8 additions & 6 deletions Checklists/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


let dataModel = DataModel()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let navigationController = window!.rootViewController as! UINavigationController
let controller = navigationController.viewControllers[0] as! AllListsViewController

controller.dataModel = dataModel

return true
}

Expand All @@ -41,10 +46,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

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

controller.saveChecklists()
dataModel.saveChecklists()
}


Expand Down
39 changes: 39 additions & 0 deletions Checklists/DataModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation

class DataModel {
var lists = [Checklist]()

init() {
loadChecklists()
}

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()
}
}
}
}

0 comments on commit efca2b0

Please sign in to comment.