Last active
March 25, 2018 04:18
-
-
Save Mr-Perfection/8b80d2d30a4cc46a073d698eef28017c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Item.swift | |
// todosy | |
// | |
// Created by Stephen lee on 3/6/18. | |
// Copyright © 2018 Stephen Lee. All rights reserved. | |
// | |
import Foundation | |
class Item: Codable { | |
var title : String = "" | |
var done : Bool = false | |
init() {} | |
init(title: String, done: Bool) { | |
self.title = title | |
self.done = done | |
} | |
} | |
// | |
// ViewController.swift | |
// todosy | |
// | |
// Created by Stephen lee on 3/3/18. | |
// Copyright © 2018 Stephen Lee. All rights reserved. | |
// | |
import UIKit | |
class TodoListViewController: UITableViewController { | |
var itemArray = [Item]() | |
let defaults = UserDefaults.standard | |
let dataFilePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("Items.plist") | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
print(self.dataFilePath) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
itemArray.append(Item(title: "Find Mike", done: false)) | |
// Do any additional setup after loading the view, typically from a nib. | |
// if let items = defaults.array(forKey: "TodoListArray") as? [Item] { | |
// itemArray = items | |
// } | |
} | |
// Mark: tableView Datasource methods | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return itemArray.count | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "TodoItemCell", for: indexPath) | |
let item = itemArray[indexPath.row] | |
cell.textLabel?.text = item.title | |
cell.accessoryType = item.done ? .checkmark : .none | |
return cell | |
} | |
// Mark - TableView Delegate methods | |
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
itemArray[indexPath.row].done = !itemArray[indexPath.row].done | |
self.saveItems() | |
self.tableView.reloadData() | |
tableView.deselectRow(at: indexPath, animated: true) | |
} | |
// Mark - Add new items | |
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) { | |
var textField = UITextField() | |
let alert = UIAlertController(title: "Add New Todosy Item", message: "", preferredStyle: .alert) | |
let action = UIAlertAction(title: "Add Item", style: .default) { (action) in | |
// TODO: what will happen when user clicks on the action button | |
if (textField.text != nil) { | |
let newItem = Item() | |
newItem.title = textField.text! | |
self.itemArray.append(newItem) | |
self.saveItems() | |
// self.defaults.set(self.itemArray, forKey: "TodoListArray") | |
self.tableView.reloadData() | |
} | |
} | |
alert.addTextField { (alertTextField) in | |
alertTextField.placeholder = "Create new item" | |
textField = alertTextField | |
} | |
alert.addAction(action) | |
present(alert, animated: true, completion: nil) | |
} | |
func saveItems() { | |
let encoder = PropertyListEncoder() | |
do { | |
let data = try encoder.encode(itemArray) | |
try data.write(to: dataFilePath!) | |
} catch { | |
print("Error encoding item array, \(error)") | |
} | |
} | |
func loadItems() { | |
if let data = try? Data(contentsOf: dataFilePath!) { | |
let decoder = PropertyListDecoder() | |
do { | |
itemArray = try decoder.decode([Item].self, from: data) | |
} catch { | |
print("ERror decoding [Item]") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment