-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
269 additions
and
60 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// CategoryViewController.swift | ||
// ToDoLIst | ||
// | ||
// Created by Micheal Griffin on 05/03/2018. | ||
// Copyright © 2018 Micheal Griffin. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import CoreData | ||
|
||
class CategoryViewController: UITableViewController { | ||
|
||
var categories = [Category]() | ||
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
loadCategories() | ||
|
||
} | ||
|
||
//MARK -- Table View DataSource Methods | ||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
return categories.count | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
|
||
let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for: indexPath) | ||
cell.textLabel?.text = categories[indexPath.row].name | ||
return cell | ||
} | ||
|
||
//MARK -- Table View Delegats Methods | ||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
performSegue(withIdentifier: "goToItems", sender: self) | ||
|
||
} | ||
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | ||
let destinationVC = segue.destination as! ToDoListViewController | ||
if let indexPath = tableView.indexPathForSelectedRow { | ||
destinationVC.selectedCategory = categories[indexPath.row] | ||
} | ||
} | ||
|
||
//MARK -- Data Manipulation Methods | ||
func saveCategories(){ | ||
do{ | ||
try context.save() | ||
}catch{ | ||
print("Error Saving Category \(error)") | ||
} | ||
tableView.reloadData() | ||
} | ||
|
||
func loadCategories(with request : NSFetchRequest<Item> = Item.fetchRequest()){ | ||
let request : NSFetchRequest<Category> = Category.fetchRequest() | ||
do{ | ||
categories = try context.fetch(request) | ||
}catch{ | ||
print("Error Loading Categories \(error)") | ||
} | ||
tableView.reloadData() | ||
} | ||
|
||
//MARK -- Add new Categories | ||
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) { | ||
var textField = UITextField() | ||
let alert = UIAlertController(title: "Add new Category", message: "", preferredStyle: .alert) | ||
let action = UIAlertAction(title: "Add", style: .default) { (action) in | ||
//what will happen when user clicks add item | ||
let newCategory = Category(context: self.context) | ||
newCategory.name = textField.text! | ||
self.categories.append(newCategory) | ||
self.saveCategories() | ||
} | ||
|
||
alert.addAction(action) | ||
alert.addTextField { (field) in | ||
textField = field | ||
textField.placeholder = "Add a new Category" | ||
|
||
} | ||
|
||
present(alert,animated: true, completion: nil) | ||
} | ||
|
||
|
||
|
||
} |
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
16 changes: 16 additions & 0 deletions
16
ToDoLIst/Data Model/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="13772" systemVersion="17D102" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier=""> | ||
<entity name="Category" representedClassName="Category" syncable="YES" codeGenerationType="class"> | ||
<attribute name="name" attributeType="String" syncable="YES"/> | ||
<relationship name="items" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Item" inverseName="parentCategory" inverseEntity="Item" syncable="YES"/> | ||
</entity> | ||
<entity name="Item" representedClassName=".Item" syncable="YES" codeGenerationType="class"> | ||
<attribute name="done" attributeType="Boolean" usesScalarValueType="YES" syncable="YES"/> | ||
<attribute name="title" attributeType="String" syncable="YES"/> | ||
<relationship name="parentCategory" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Category" inverseName="items" inverseEntity="Category" syncable="YES"/> | ||
</entity> | ||
<elements> | ||
<element name="Item" positionX="-63" positionY="-18" width="128" height="88"/> | ||
<element name="Category" positionX="-65" positionY="-115" width="128" height="73"/> | ||
</elements> | ||
</model> |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.