Listing Todo items

To retrieve all Todo items, unlike our post call, we do not need to pass any header parameters, just cookie information. So, we add the following struct to handle this scenario:

struct RequestModel: RequestProtocol { 
subscript(key: String) -> (String?, String?) {
get {
switch key {
default: return ("Cookie","test=123")
}
}
}
}

Then, we can retrieve a list of Todo items using the following code:

sendRequest(Urls.getTodos, request: RequestModel()) { 
(response, error) in
if error == nil {
let todos: [Todo]? = decode(response!)
completion(todos, nil)
print("request was successfull: (todos)")
} else {
completion(nil, error)
print("Error: (error?.localizedDescription)")
}
}

Although we added better error printing, we need to improve it further.

Let's extract the preceding function calls, create a Swift file named TodoManager, and put these functions in it:

import Alamofire 
import Argo

func addTodo(_ completion:@escaping (_ responseData: [Todo]?,
_ error: Error?) -> Void) {
let newRequest = TodoRequest(todoId: 1,
name: "Saturday Grocery",
description: "Bananas, Pineapple,
Beer, Orange juice, ...",
notes: "Check expiry date of
orange juice",
completed: false,
synced: true)

sendRequest(Urls.postTodo, request: newRequest) {
(response, error) in
if error == nil {
let todos: [Todo]? = decode(response!)
completion(todos, nil)
print("request was successfull: (todos)")
} else {
completion(nil, error)
print("Error: (error?.localizedDescription)")
}
}
}

func listTodos(_ completion:@escaping (_ responseData:[Todo]?,
_ error: Error?) -> Void) {
sendRequest(Urls.getTodos, request: RequestModel()) {
(response, error) in
if error == nil {
let todos: [Todo]? = decode(response!)
completion(todos, nil)
print("request was successfull: (todos)")
} else {
completion(nil, error)
print("Error: (error?.localizedDescription)")
}
}
}

Finally, we will develop two other functions: one adds or updates a Todo item and the other only updates a specific Todo item. Deleting items will be easy to implement as well. The code is as follows:

func addOrUpdateTodo(_ todo: [Todo]?,
completion:@escaping (_ responseData:[Todo]?,
_ error: Error?) -> Void) {
if let todoItem = todo?.first {
let newRequest = TodoRequest(todoId: todoItem.todoId,
name: todoItem.name,
description: todoItem.description,
notes: todoItem.notes!,
completed: todoItem.completed,
synced: true)

sendRequest(Urls.postTodo, request: newRequest) {
(response, error) in
if error == nil {
let todos: [Todo]? = decode(response!)
let newTodo = todoSyncedLens.set(true, todoItem)
store.dispatch(UpdateTodoAction(todo: newTodo))
completion(todos, nil)
print("request was successfull: (todos)")
} else {
completion(nil, error)
print("Error: (error?.localizedDescription)")
}
}
}
}

func updateTodo(_ todo: [Todo]?, completion:@escaping (
_ responseData: [Todo]?, _ error: Error?) -> Void) {
if let todoItem = todo?.first {
let newRequest = TodoRequest(todoId: todoItem.todoId,
name: todoItem.name,
description: todoItem.description,
notes: todoItem.notes!,
completed: todoItem.completed,
synced: true)

sendRequest(Urls.update, request: newRequest) {
(response, error) in
if error == nil {
let todos: [Todo]? = decode(response!)
let newTodo = todoSyncedLens.set(true, todoItem)
store.dispatch(UpdateTodoAction(todo: newTodo))
completion(todos, nil)
print("request was successfull: (todos)")
} else {
completion(nil, error)
print("Error: (error?.localizedDescription)")
}
}

}
}

In these functions, we have not yet covered the following concepts in detail:

  • dispatch: This function dispatches an action (here, UpdateTodoAction) by setting the State struct's value to the result of calling its reduce method.
  • todoSyncedLens: This is a Lens to modify the synced property of the Todo item. We will define these lenses in an upcoming section.
  • UpdateTodoAction: This is a struct that conforms to ActionType, which is used when we want to make modifications to the State of the Store. All changes to the Store go through this type. We will define our Actions in an upcoming section.
  • State: This is a struct that will be used to manage the State. We will define it later.
  • Store: As the name suggests, this is where we Store the State. We will define it later.
..................Content has been hidden....................

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