Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions to-do-list/to-do-list/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
63 changes: 58 additions & 5 deletions to-do-list/to-do-list/DataModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
5 changes: 3 additions & 2 deletions to-do-list/to-do-list/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------------------------------------------------------------------------------------
Expand All @@ -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
}
}
4 changes: 3 additions & 1 deletion to-do-list/to-do-list/ItemViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -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()
}

Expand Down
10 changes: 10 additions & 0 deletions to-do-list/to-do-list/ListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -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()
}

Expand All @@ -65,5 +67,13 @@ class ListViewController: UIViewController, UITableViewDataSource, UITableViewDe
override func viewWillAppear(_ animated: Bool) {
ListTableOutlet.reloadData()
}

override func viewDidLoad() {
super.viewDidLoad()



}

}