From 910b56c3161a075e269cdbf829a0d3a4441b7368 Mon Sep 17 00:00:00 2001 From: Grace Yu Date: Thu, 15 Dec 2016 14:12:30 -0500 Subject: [PATCH] Added persistent data call and retrival --- to-do-list/to-do-list/AppDelegate.swift | 1 + to-do-list/to-do-list/DataModel.swift | 63 +++++++++++++++++-- .../to-do-list/DetailViewController.swift | 5 +- .../to-do-list/ItemViewController.swift | 4 +- .../to-do-list/ListViewController.swift | 10 +++ 5 files changed, 75 insertions(+), 8 deletions(-) diff --git a/to-do-list/to-do-list/AppDelegate.swift b/to-do-list/to-do-list/AppDelegate.swift index f2f8d7b..e8ab194 100644 --- a/to-do-list/to-do-list/AppDelegate.swift +++ b/to-do-list/to-do-list/AppDelegate.swift @@ -16,6 +16,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. + Model.shared.loadPersistedListFromDefaults() return true } diff --git a/to-do-list/to-do-list/DataModel.swift b/to-do-list/to-do-list/DataModel.swift index 19dad6e..659cfe8 100644 --- a/to-do-list/to-do-list/DataModel.swift +++ b/to-do-list/to-do-list/DataModel.swift @@ -9,23 +9,76 @@ import Foundation import UIKit +class Model { + static let shared = Model() + private init() {} + + let key = "todoList" + + func persistListToDefaults() { + let data = NSKeyedArchiver.archivedData(withRootObject: lists) + UserDefaults.standard.set(data, forKey: key) + } + + func loadPersistedListFromDefaults() { + if let data = UserDefaults.standard.object(forKey: key) as? Data { + let listOfItems = NSKeyedUnarchiver.unarchiveObject(with: data) as! [List] + lists = listOfItems + } + } +} + var lists = [List]() -class List { +class List: NSObject, NSCoding { + + private struct Keys { + static let listTitle = "listTittle" + static let items = "items" + } + var listTitle: String var items = [Item]() init(listTitle: String) { self.listTitle = listTitle } + + required init?(coder aDecoder: NSCoder) { + listTitle = aDecoder.decodeObject(forKey: Keys.listTitle) as! String + items = aDecoder.decodeObject(forKey: Keys.items) as! [Item] + } + + func encode(with aCoder: NSCoder) { + aCoder.encode(listTitle, forKey: Keys.listTitle) + aCoder.encode(items, forKey: Keys.items) + } } -class Item { +class Item: NSObject, NSCoding { + + private struct Keys { + static let itemTitle = "itemTitle" + static let description1 = "description1" + } + var itemTitle: String - var description = "static" + var description1 = "description" - init(itemTitle: String, description: String) { + init(itemTitle: String, description1: String) { self.itemTitle = itemTitle - self.description = description + self.description1 = description1 + } + + required convenience init?(coder aDecoder: NSCoder) { + self.init( + itemTitle: aDecoder.decodeObject(forKey: Keys.itemTitle) as! String, + description1: aDecoder.decodeObject(forKey: Keys.description1) as! String + ) + } + + func encode(with aCoder: NSCoder) { + aCoder.encode(itemTitle, forKey: Keys.itemTitle) + aCoder.encode(description1, forKey: Keys.description1) } } diff --git a/to-do-list/to-do-list/DetailViewController.swift b/to-do-list/to-do-list/DetailViewController.swift index 068b8c8..b571725 100644 --- a/to-do-list/to-do-list/DetailViewController.swift +++ b/to-do-list/to-do-list/DetailViewController.swift @@ -28,8 +28,9 @@ class DetailViewController: UIViewController { // Saves description and displays it on the page @IBAction func tappedSaveBtnAction(_ sender: Any) { - lists[currentListIndex].items[currentItemIndex].description = yourDescriptionTextFieldOutlet.text! + lists[currentListIndex].items[currentItemIndex].description1 = yourDescriptionTextFieldOutlet.text! yourDescriptionTextFieldOutlet.backgroundColor = UIColor.clear + Model.shared.persistListToDefaults() } // MARK: override ---------------------------------------------------------------------------------------- @@ -39,6 +40,6 @@ class DetailViewController: UIViewController { super.viewDidLoad() yourListOutlet.text = lists[currentListIndex].listTitle.uppercased() yourItemOutlet.text = lists[currentListIndex].items[currentItemIndex].itemTitle - yourDescriptionTextFieldOutlet.text = lists[currentListIndex].items[currentItemIndex].description + yourDescriptionTextFieldOutlet.text = lists[currentListIndex].items[currentItemIndex].description1 } } diff --git a/to-do-list/to-do-list/ItemViewController.swift b/to-do-list/to-do-list/ItemViewController.swift index df9a190..96e8e3c 100644 --- a/to-do-list/to-do-list/ItemViewController.swift +++ b/to-do-list/to-do-list/ItemViewController.swift @@ -38,6 +38,7 @@ class ItemViewController: UIViewController, UITableViewDataSource, UITableViewDe func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { lists[currentItemIndex].items.remove(at: indexPath.row) + Model.shared.persistListToDefaults() ItemTableOutlet.reloadData() } } @@ -50,8 +51,9 @@ class ItemViewController: UIViewController, UITableViewDataSource, UITableViewDe // It clears the input field ItemTextFieldOutlet.text = "" // Creates an instance of List object and appends new item to the lists array - lists[currentItemIndex].items.append(Item.init(itemTitle: itemTextField, description: "")) + lists[currentItemIndex].items.append(Item.init(itemTitle: itemTextField, description1: "")) // reloads the table so that it gets new info + Model.shared.persistListToDefaults() ItemTableOutlet.reloadData() } diff --git a/to-do-list/to-do-list/ListViewController.swift b/to-do-list/to-do-list/ListViewController.swift index e0202ec..03a71cd 100644 --- a/to-do-list/to-do-list/ListViewController.swift +++ b/to-do-list/to-do-list/ListViewController.swift @@ -35,6 +35,7 @@ class ListViewController: UIViewController, UITableViewDataSource, UITableViewDe func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { lists.remove(at: indexPath.row) + Model.shared.persistListToDefaults() ListTableOutlet.reloadData() } } @@ -49,6 +50,7 @@ class ListViewController: UIViewController, UITableViewDataSource, UITableViewDe // Creates an instance of List object and appends new item to the lists array lists.append(List.init(listTitle: listTextField)) // reloads the table so that it gets new info + Model.shared.persistListToDefaults() ListTableOutlet.reloadData() } @@ -65,5 +67,13 @@ class ListViewController: UIViewController, UITableViewDataSource, UITableViewDe override func viewWillAppear(_ animated: Bool) { ListTableOutlet.reloadData() } + + override func viewDidLoad() { + super.viewDidLoad() + + + + } + }