Skip to content

Commit

Permalink
Add ability to edit existing checklist
Browse files Browse the repository at this point in the history
  • Loading branch information
bballentine committed Sep 1, 2016
1 parent 1b373c6 commit 3ac8979
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
20 changes: 16 additions & 4 deletions MinimaList/MinimaList/AddCheckistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ import UIKit
protocol AddChecklistViewControllerDelegate: class {
func addChecklistViewControllerDidCancel(controller: AddCheckistViewController)
func addChecklistViewController(controller: AddCheckistViewController, didFinishAddingChecklist checklist: Checklist)
func addChecklistViewController(controller: AddCheckistViewController, didFinishEditingChecklist checklist: Checklist)
}

class AddCheckistViewController: UIViewController, UITextFieldDelegate {

weak var delegate: AddChecklistViewControllerDelegate?
var checklistToEdit: Checklist?


@IBOutlet weak var titleLabel: UITextField!
@IBOutlet weak var doneButton: UIBarButtonItem!

override func viewDidLoad() {
super.viewDidLoad()

if let checklist = checklistToEdit {
self.title = checklist.name
titleLabel.text = checklist.name
} else {
self.title = "Add Checklist"
}

// Do any additional setup after loading the view.
}
Expand Down Expand Up @@ -53,12 +62,15 @@ class AddCheckistViewController: UIViewController, UITextFieldDelegate {
}
}
@IBAction func doneButtonPressed(_ sender: AnyObject) {
if let newChecklistName = titleLabel.text {
let newChecklist = Checklist(name: newChecklistName)
if let editedChecklist = checklistToEdit {
editedChecklist.name = titleLabel.text!
delegate?.addChecklistViewController(controller: self, didFinishEditingChecklist: editedChecklist)

} else {
let newChecklistName = titleLabel.text
let newChecklist = Checklist(name: newChecklistName!)
delegate?.addChecklistViewController(controller: self, didFinishAddingChecklist: newChecklist)
}


}

/*
Expand Down
25 changes: 25 additions & 0 deletions MinimaList/MinimaList/AllListsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ class AllListsViewController: UITableViewController, AddChecklistViewControllerD
dismiss(animated: true, completion: nil)
}

func addChecklistViewController(controller: AddCheckistViewController, didFinishEditingChecklist checklist: Checklist) {
tableView.reloadData()
dismiss(animated: true, completion: nil)
}

// MARK: Cell Configuration

func configureCell(cell: ChecklistCell, forList list: Checklist) {
Expand All @@ -114,6 +119,21 @@ class AllListsViewController: UITableViewController, AddChecklistViewControllerD
// Return false if you do not want the specified item to be editable.
return true
}

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { action, index in
self.dataModel.lists.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}

let edit = UITableViewRowAction(style: .normal, title: "Edit") { action, index in
print("edit button tapped")
let listToEdit = self.dataModel.lists[indexPath.row]
self.performSegue(withIdentifier: "EditChecklist", sender: listToEdit)
}

return [delete, edit]
}

// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
Expand Down Expand Up @@ -155,6 +175,11 @@ class AllListsViewController: UITableViewController, AddChecklistViewControllerD
} else if segue.identifier == "ListDetail" {
let controller = segue.destination as! ChecklistViewController
controller.checklist = sender as! Checklist
} else if segue.identifier == "EditChecklist" {
let navigationController = segue.destination as! UINavigationController
let controller = navigationController.topViewController as! AddCheckistViewController
controller.delegate = self
controller.checklistToEdit = (sender as! Checklist)
}

}
Expand Down
6 changes: 5 additions & 1 deletion MinimaList/MinimaList/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@
<connections>
<segue destination="b2v-3e-3Tz" kind="show" identifier="ListDetail" id="HZW-U7-eUb"/>
<segue destination="6rC-0I-b7W" kind="presentation" identifier="AddChecklist" id="nVe-1n-CsX"/>
<segue destination="6rC-0I-b7W" kind="presentation" identifier="EditChecklist" id="Ijs-RW-l5u"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="PMY-g9-Oz1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1028" y="113"/>
<point key="canvasLocation" x="954" y="113"/>
</scene>
<!--Add Checklist-->
<scene sceneID="2jn-wb-kcd">
Expand Down Expand Up @@ -319,4 +320,7 @@
<resources>
<image name="incomplete" width="80" height="80"/>
</resources>
<inferredMetricsTieBreakers>
<segue reference="Ijs-RW-l5u"/>
</inferredMetricsTieBreakers>
</document>

0 comments on commit 3ac8979

Please sign in to comment.