From efca2b0da6ff1f027cc87a2c42fc359821001580 Mon Sep 17 00:00:00 2001 From: mriddle Date: Mon, 2 May 2016 12:12:03 +1000 Subject: [PATCH] Moving logic out of the view --- AllListsViewController.swift | 54 +++++----------------------- Checklists.xcodeproj/project.pbxproj | 4 +++ Checklists/AppDelegate.swift | 14 ++++---- Checklists/DataModel.swift | 39 ++++++++++++++++++++ 4 files changed, 60 insertions(+), 51 deletions(-) create mode 100644 Checklists/DataModel.swift diff --git a/AllListsViewController.swift b/AllListsViewController.swift index abf29c0..a500009 100644 --- a/AllListsViewController.swift +++ b/AllListsViewController.swift @@ -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() @@ -18,13 +13,13 @@ 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 @@ -32,7 +27,7 @@ class AllListsViewController: UITableViewController, ListDetailViewControllerDel } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { - let checklist = lists[indexPath.row] + let checklist = dataModel.lists[indexPath.row] performSegueWithIdentifier("ShowChecklist", sender: checklist) } @@ -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) @@ -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 @@ -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) } @@ -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() - } - } - } } diff --git a/Checklists.xcodeproj/project.pbxproj b/Checklists.xcodeproj/project.pbxproj index e4871ce..10902ec 100644 --- a/Checklists.xcodeproj/project.pbxproj +++ b/Checklists.xcodeproj/project.pbxproj @@ -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 */; }; @@ -28,6 +29,7 @@ A81172CE1CD0660F00BA3196 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; A81172D01CD0660F00BA3196 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; A81172D81CD0859800BA3196 /* ChecklistItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChecklistItem.swift; sourceTree = ""; }; + A83B303B1CD6ECF800D38D88 /* DataModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataModel.swift; sourceTree = ""; }; A8961D4E1CD19E87006AFAF4 /* ItemDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = ItemDetailViewController.swift; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; A8961D531CD309B0006AFAF4 /* AllListsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AllListsViewController.swift; path = ../AllListsViewController.swift; sourceTree = ""; }; A8961D551CD31108006AFAF4 /* Checklist.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Checklist.swift; sourceTree = ""; }; @@ -66,6 +68,7 @@ children = ( A8961D531CD309B0006AFAF4 /* AllListsViewController.swift */, A81172C41CD0660E00BA3196 /* AppDelegate.swift */, + A83B303B1CD6ECF800D38D88 /* DataModel.swift */, A81172C61CD0660E00BA3196 /* ChecklistViewController.swift */, A81172C81CD0660E00BA3196 /* Main.storyboard */, A81172CB1CD0660F00BA3196 /* Assets.xcassets */, @@ -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 */, ); diff --git a/Checklists/AppDelegate.swift b/Checklists/AppDelegate.swift index 85fc3e9..7633d9f 100644 --- a/Checklists/AppDelegate.swift +++ b/Checklists/AppDelegate.swift @@ -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 } @@ -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() } diff --git a/Checklists/DataModel.swift b/Checklists/DataModel.swift new file mode 100644 index 0000000..9f68314 --- /dev/null +++ b/Checklists/DataModel.swift @@ -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() + } + } + } +} \ No newline at end of file