IBActions

We will need to define two IBAction, one to add a new item to the list and the other to filter the items:

// MARK: Actions 
extension MasterViewController {
@IBAction func addTapped(_ sender: UIBarButtonItem) {
let alertController = UIAlertController(title: "Create",
message: "Create a new todo item",
preferredStyle: .alert)

alertController.addTextField {
textField in
textField.placeholder = "Id"
}

alertController.addTextField {
textField in
textField.placeholder = "Name"
}

alertController.addTextField {
textField in
textField.placeholder = "Description"
}

alertController.addTextField {
textField in
textField.placeholder = "Notes"
}

alertController.addAction(UIAlertAction(title: "Cancel",
style: .cancel) { _ in })

alertController.addAction(UIAlertAction(title: "Create",
style: .default) { _ in
guard let id = alertController.textFields?[0].text,
let name = alertController.textFields?[1].text,
let description = alertController.textFields?[2].text,
let notes = alertController.textFields?[3].text
else { return }

store.dispatch(CreateTodoAction(todoId: Int(id)!,
name: name,
description: description,
notes: notes))
})
present(alertController, animated: false, completion: nil)
}

func filterValueChanged() {
guard let newFilter = TodoFilter(rawValue:
filterSegmentedControl.selectedSegmentIndex)
else { return }

store.dispatch(SetFilterAction(filter: newFilter))
}
}

In the addTapped method, we use createTodoAction to add an item to the list with the completed and synced values as false. Therefore, store.notSyncedWithBackend.startWithNext in viewDidLoad will view this item as not synced and will sync it with the backend.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset