diff --git a/app/src/main/java/com/example/citrusclient/viewmodels/TodosViewModel.java b/app/src/main/java/com/example/citrusclient/viewmodels/TodosViewModel.java index 0a00eb2..03cd33f 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/TodosViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/TodosViewModel.java @@ -23,6 +23,7 @@ //LiveData private final MutableLiveData curBookLiveData; private final MutableLiveData> todosLiveData; + private final MutableLiveData errorLiveData; //Constructor public TodosViewModel() @@ -33,12 +34,14 @@ .build(); this.todosRest = retrofit.create(TodosRest.class); this.curBookLiveData = new MutableLiveData<>(); - this.todosLiveData = new MutableLiveData>(); + this.todosLiveData = new MutableLiveData<>(); + this.errorLiveData = new MutableLiveData<>(); } //getter public MutableLiveData getCurBookLiveData() {return curBookLiveData;} public MutableLiveData> getTodosLiveData() {return todosLiveData;} + public MutableLiveData getErrorLiveData() {return errorLiveData;} public void getAllTodos(String accountId,Integer bookId, String token) { @@ -49,12 +52,14 @@ if (response.isSuccessful()) { } else { - + System.out.println("Error: getAllTodos" + response.code()); + errorLiveData.setValue(parseStatusCode(response.code())); } } @Override public void onFailure(Call> call, Throwable t) { - System.out.println("ChangePw NetworkError" + t); + System.out.println("UnknownError: getAllTodos" + t); + errorLiveData.setValue(parseStatusCode(-1)); } }); @@ -70,12 +75,13 @@ if (response.isSuccessful()) { } else { - + System.out.println("Error: getTodosByMonth" + response.code()); } } @Override public void onFailure(Call> call, Throwable t) { - System.out.println("ChangePw NetworkError" + t); + System.out.println("UnknownError: getTodosByMonth" + t); + errorLiveData.setValue(parseStatusCode(-1)); } }); @@ -91,12 +97,13 @@ if (response.isSuccessful()) { } else { - + System.out.println("Error: getTodosByDay" + response.code()); } } @Override public void onFailure(Call> call, Throwable t) { - System.out.println("ChangePw NetworkError" + t); + System.out.println("UnknownError: getTodosByDay" + t); + errorLiveData.setValue(parseStatusCode(-1)); } }); @@ -112,12 +119,13 @@ if (response.isSuccessful()) { } else { - + System.out.println("Error: getTodoById" + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("ChangePw NetworkError" + t); + System.out.println("UnknownError: getTodoById" + t); + errorLiveData.setValue(parseStatusCode(-1)); } }); @@ -133,12 +141,13 @@ if (response.isSuccessful()) { } else { - + System.out.println("Error: createTodo" + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("ChangePw NetworkError" + t); + System.out.println("UnknownError: createTodo" + t); + errorLiveData.setValue(parseStatusCode(-1)); } }); @@ -154,12 +163,13 @@ if (response.isSuccessful()) { } else { - + System.out.println("Error: setCheck" + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - + System.out.println("UnknownError: setCheck" + t); + errorLiveData.setValue(parseStatusCode(-1)); } }); } @@ -174,14 +184,29 @@ if (response.isSuccessful()) { } else { - + System.out.println("Error: deleteTodo" + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - + System.out.println("UnknownError: deleteTodo" + t); + errorLiveData.setValue(parseStatusCode(-1)); } }); } + private String parseStatusCode(Integer stats) { + switch (stats) { + case 404: + return "NotFound"; + case 401: + return "InvalidToken"; + case 400: + return "ResponseError"; + case 500: + return "ServerError"; + default: + return "UnknownError"; + } + } }