@@ -50,6 +50,7 @@ import kotlinx.coroutines.flow.stateIn
5050import kotlinx.coroutines.launch
5151import kotlinx.serialization.json.JsonObject
5252import kotlinx.serialization.json.JsonPrimitive
53+ import kotlinx.serialization.json.int
5354import kotlinx.serialization.json.jsonPrimitive
5455import kotlinx.serialization.json.long
5556
@@ -74,11 +75,11 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
7475 todoRepository.addTodo(taskDescription)
7576 }
7677
77- fun removeTodo (todoId : Long ) {
78+ fun removeTodo (todoId : Int ) {
7879 todoRepository.removeTodo(todoId)
7980 }
8081
81- fun toggleTodoStatus (todoId : Long ) {
82+ fun toggleTodoStatus (todoId : Int ) {
8283 todoRepository.toggleTodoStatus(todoId)
8384 }
8485
@@ -194,28 +195,51 @@ class TodoScreenViewModel @Inject constructor(private val todoRepository: TodoRe
194195 }
195196 " addTodo" -> {
196197 val taskDescription = functionCall.args[" taskDescription" ]!! .jsonPrimitive.content
197- todoRepository.addTodo(taskDescription)
198- val response = JsonObject (
199- mapOf (
200- " success" to JsonPrimitive (true ),
201- " message" to JsonPrimitive (" Task $taskDescription added to the todo list" ),
202- ),
203- )
204- FunctionResponsePart (functionCall.name, response)
198+ val id = todoRepository.addTodo(taskDescription)
199+
200+ if (id!= null ) {
201+ val response = JsonObject (
202+ mapOf (
203+ " success" to JsonPrimitive (true ),
204+ " message" to JsonPrimitive (" Task $taskDescription added to the todo list (id: $id )" ),
205+ ),
206+ )
207+ FunctionResponsePart (functionCall.name, response)
208+ } else {
209+ val response = JsonObject (
210+ mapOf (
211+ " success" to JsonPrimitive (false ),
212+ " message" to JsonPrimitive (" Task $taskDescription wasn't properly added to the list" ),
213+ ),
214+ )
215+ FunctionResponsePart (functionCall.name, response)
216+ }
217+
205218 }
206219 " removeTodo" -> {
207- val taskId = functionCall.args[" todoId" ]!! .jsonPrimitive.long
208- todoRepository.removeTodo(taskId)
209- val response = JsonObject (
210- mapOf (
211- " success" to JsonPrimitive (true ),
212- " message" to JsonPrimitive (" Task was removed from the todo list" ),
213- ),
214- )
215- FunctionResponsePart (functionCall.name, response)
220+ try {
221+ val taskId = functionCall.args[" todoId" ]!! .jsonPrimitive.int
222+ todoRepository.removeTodo(taskId)
223+ val response = JsonObject (
224+ mapOf (
225+ " success" to JsonPrimitive (true ),
226+ " message" to JsonPrimitive (" Task was removed from the todo list" ),
227+ ),
228+ )
229+ FunctionResponsePart (functionCall.name, response)
230+ } catch (e: Exception ) {
231+ val response = JsonObject (
232+ mapOf (
233+ " success" to JsonPrimitive (false ),
234+ " message" to JsonPrimitive (" Something went wrong: ${e.message} " ),
235+ ),
236+ )
237+ FunctionResponsePart (functionCall.name, response)
238+ }
239+
216240 }
217241 " toggleTodoStatus" -> {
218- val taskId = functionCall.args[" todoId" ]!! .jsonPrimitive.long
242+ val taskId = functionCall.args[" todoId" ]!! .jsonPrimitive.int
219243 todoRepository.toggleTodoStatus(taskId)
220244 val response = JsonObject (
221245 mapOf (
0 commit comments