Skip to content

Commit a2d7869

Browse files
committed
Rearrange TasksViewController TODOs to align with as presented in text
1 parent 79b14c0 commit a2d7869

File tree

1 file changed

+47
-48
lines changed

1 file changed

+47
-48
lines changed

tutorial/swift-ios/Task Tracker/TasksViewController.swift

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -142,25 +142,38 @@ class TasksViewController: UIViewController, UITableViewDelegate, UITableViewDat
142142
return cell
143143
}
144144

145-
// :code-block-start: delete-task
146-
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
147-
guard editingStyle == .delete else { return }
145+
@objc func addButtonDidClick() {
146+
let alertController = UIAlertController(title: "Add Task", message: "", preferredStyle: .alert)
148147

149-
// User can swipe to delete items.
150-
let task = tasks[indexPath.row]
151-
152-
// :hide-start:
153-
// All modifications to a realm must happen in a write block.
154-
try! realm.write {
155-
// Delete the Task.
156-
realm.delete(task)
157-
}
158-
// :replace-with:
159-
// // TODO: delete the task from the realm in a write block.
160-
// :hide-end:
148+
// When the user clicks the add button, present them with a dialog to enter the task name.
149+
alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: {
150+
alert -> Void in
151+
let textField = alertController.textFields![0] as UITextField
152+
153+
// :code-block-start: add-button-did-click
154+
// :hide-start:
155+
// Create a new Task with the text that the user entered.
156+
let task = Task(partition: self.partitionValue, name: textField.text ?? "New Task")
157+
158+
// Any writes to the Realm must occur in a write block.
159+
try! self.realm.write {
160+
// Add the Task to the Realm. That's it!
161+
self.realm.add(task)
162+
}
163+
// :replace-with:
164+
// // TODO: Create a Task instance and add it to the realm in a write block.
165+
// :hide-end:
166+
// :code-block-end:
167+
}))
168+
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel))
169+
alertController.addTextField(configurationHandler: { (textField: UITextField!) -> Void in
170+
textField.placeholder = "New Task Name"
171+
})
172+
173+
// Show the dialog.
174+
self.present(alertController, animated: true, completion: nil)
161175
}
162-
// :code-block-end:
163-
176+
164177
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
165178

166179
// User selected a task in the table. We will present a list of actions that the user can perform on this task.
@@ -211,39 +224,25 @@ class TasksViewController: UIViewController, UITableViewDelegate, UITableViewDat
211224
// Show the actions list.
212225
self.present(actionSheet, animated: true, completion: nil)
213226
}
227+
228+
// :code-block-start: delete-task
229+
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
230+
guard editingStyle == .delete else { return }
214231

215-
216-
@objc func addButtonDidClick() {
217-
let alertController = UIAlertController(title: "Add Task", message: "", preferredStyle: .alert)
218-
219-
// When the user clicks the add button, present them with a dialog to enter the task name.
220-
alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: {
221-
alert -> Void in
222-
let textField = alertController.textFields![0] as UITextField
223-
224-
// :code-block-start: add-button-did-click
225-
// :hide-start:
226-
// Create a new Task with the text that the user entered.
227-
let task = Task(partition: self.partitionValue, name: textField.text ?? "New Task")
228-
229-
// Any writes to the Realm must occur in a write block.
230-
try! self.realm.write {
231-
// Add the Task to the Realm. That's it!
232-
self.realm.add(task)
233-
}
234-
// :replace-with:
235-
// // TODO: Create a Task instance and add it to the realm in a write block.
236-
// :hide-end:
237-
// :code-block-end:
238-
}))
239-
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel))
240-
alertController.addTextField(configurationHandler: { (textField: UITextField!) -> Void in
241-
textField.placeholder = "New Task Name"
242-
})
243-
244-
// Show the dialog.
245-
self.present(alertController, animated: true, completion: nil)
232+
// User can swipe to delete items.
233+
let task = tasks[indexPath.row]
234+
235+
// :hide-start:
236+
// All modifications to a realm must happen in a write block.
237+
try! realm.write {
238+
// Delete the Task.
239+
realm.delete(task)
240+
}
241+
// :replace-with:
242+
// // TODO: delete the task from the realm in a write block.
243+
// :hide-end:
246244
}
245+
// :code-block-end:
247246

248247
@objc func manageTeamButtonDidClick() {
249248
present(UINavigationController(rootViewController: ManageTeamViewController()), animated: true)

0 commit comments

Comments
 (0)