package com.example.citrusclient.viewmodels; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import com.example.citrusclient.models.Todo; import com.example.citrusclient.models.Book; import com.example.citrusclient.rest.TodosRest; import java.util.ArrayList; import java.util.HashMap; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; public class TodosViewModel extends ViewModel { private final Retrofit retrofit; private final TodosRest todosRest; //LiveData private final MutableLiveData<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Todo>>>>> allTodosLiveData; private final MutableLiveData<HashMap<Integer, HashMap<Integer, Todo>>> todosByMonthLiveData; private final MutableLiveData<HashMap<Integer, Todo>> todosByDayLiveData; private final MutableLiveData<Todo> todoLiveData; private final MutableLiveData<Boolean> successSetCheckLiveData; private final MutableLiveData<Boolean> successSetTodoLiveData; private final MutableLiveData<Boolean> successDeleteTodoLiveData; private final MutableLiveData<String> errorLiveData; //Constructor public TodosViewModel() { this.retrofit = new Retrofit.Builder() .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/") .addConverterFactory(JacksonConverterFactory.create()) .build(); this.todosRest = retrofit.create(TodosRest.class); this.allTodosLiveData = new MutableLiveData<>(); this.todosByMonthLiveData = new MutableLiveData<>(); this.todosByDayLiveData = new MutableLiveData<>(); this.todoLiveData = new MutableLiveData<>(); this.successSetCheckLiveData = new MutableLiveData<>(); this.successSetTodoLiveData = new MutableLiveData<>(); this.successDeleteTodoLiveData = new MutableLiveData<>(); this.errorLiveData = new MutableLiveData<>(); } //LiveDataのgetter public MutableLiveData<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Todo>>>>> getAllTodosLiveData() { return allTodosLiveData; } public MutableLiveData<HashMap<Integer, HashMap<Integer, Todo>>> getTodosByMonthLiveData() {return todosByMonthLiveData;} public MutableLiveData<HashMap<Integer, Todo>> getTodosByDayLiveData() {return todosByDayLiveData;} public MutableLiveData<Todo> getTodoLiveData() {return todoLiveData;} public MutableLiveData<Boolean> getSuccessSetCheckLiveData() {return successSetCheckLiveData;} public MutableLiveData<Boolean> getSuccessSetTodoLiveData() {return successSetTodoLiveData;} public MutableLiveData<Boolean> getSuccessDeleteTodoLiveData() {return successDeleteTodoLiveData;} public MutableLiveData<String> getErrorLiveData() {return errorLiveData;} public void loadAllTodos(String accountId,Integer bookId, String token) { Call<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Todo>>>>> call = todosRest.getAllTodos(accountId, bookId, token); call.enqueue(new Callback<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Todo>>>>>() { @Override public void onResponse(Call<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Todo>>>>> call, Response<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Todo>>>>> response) { if (response.isSuccessful()) { System.out.println("Success: getAllTodos"); allTodosLiveData.setValue(response.body()); } else { System.out.println("Error: getAllTodos" + response.code()); errorLiveData.setValue(parseStatusCode(response.code())); } } @Override public void onFailure(Call<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Todo>>>>> call, Throwable t) { System.out.println("NetworkError : getAllTodos" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } public void loadTodosByMonth(String accountId,Integer bookId, Integer year, Integer month, String token) { Call<HashMap<Integer, HashMap<Integer, Todo>>> call = todosRest.getTodosByMonth(accountId, bookId, year, month, token); call.enqueue(new Callback<HashMap<Integer, HashMap<Integer, Todo>>>() { @Override public void onResponse(Call<HashMap<Integer, HashMap<Integer, Todo>>> call, Response<HashMap<Integer, HashMap<Integer, Todo>>> response) { if (response.isSuccessful()) { System.out.println("Success: getTodosByMonth"); todosByMonthLiveData.setValue(response.body()); } else { System.out.println("Error: getTodosByMonth" + response.code()); } } @Override public void onFailure(Call<HashMap<Integer, HashMap<Integer, Todo>>> call, Throwable t) { System.out.println("NetworkError : getTodosByMonth" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } public void loadTodosByDay(String accountId,Integer bookId, Integer year, Integer month, Integer day, String token) { Call<HashMap<Integer, Todo>> call = todosRest.getTodosByDay(accountId, bookId, year, month, day, token); call.enqueue(new Callback<HashMap<Integer, Todo>>() { @Override public void onResponse(Call<HashMap<Integer, Todo>> call, Response<HashMap<Integer, Todo>> response) { if (response.isSuccessful()) { System.out.println("Success: getTodosByDay"); todosByDayLiveData.setValue(response.body()); } else { System.out.println("Error: getTodosByDay" + response.code()); } } @Override public void onFailure(Call<HashMap<Integer, Todo>> call, Throwable t) { System.out.println("NetworkError : getTodosByDay" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } public void loadTodoById(String accountId,Integer bookId, Integer year, Integer month, Integer day, Integer todoId, String token) { Call<Todo> call = todosRest.getTodoById(accountId, bookId, year, month, day, todoId, token); call.enqueue(new Callback<Todo>() { @Override public void onResponse(Call<Todo> call, Response<Todo> response) { if (response.isSuccessful()) { System.out.println("Success: getTodoById"); todoLiveData.setValue(response.body()); } else { System.out.println("Error: getTodoById" + response.code()); } } @Override public void onFailure(Call<Todo> call, Throwable t) { System.out.println("NetworkError : getTodoById" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } public void createTodo(String accountId,Integer bookId, Integer year, Integer month, Integer day, String title, String token) { Call<Todo> call = todosRest.createTodo(accountId, bookId, year, month, day, title, token); call.enqueue(new Callback<Todo>() { @Override public void onResponse(Call<Todo> call, Response<Todo> response) { if (response.isSuccessful()) { System.out.println("Success: createTodo"); } else { System.out.println("Error: createTodo" + response.code()); } } @Override public void onFailure(Call<Todo> call, Throwable t) { System.out.println("NetworkError : createTodo" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } public void setCheck(String accountId, Integer BookId, Integer year, Integer month, Integer day, Integer todoId, boolean check, String token) { Call<Void> call = todosRest.setCheck(accountId, BookId, year, month, day, todoId, check, token); call.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { if (response.isSuccessful()) { System.out.println("Success: setCheck"); successSetCheckLiveData.setValue(true); } else { System.out.println("Error: setCheck" + response.code()); } } @Override public void onFailure(Call<Void> call, Throwable t) { System.out.println("NetworkError : setCheck" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } public void setTodo(String accountId, Integer BookId, Integer year, Integer month, Integer day, Integer todoId, String title, Integer newYear,Integer newMonth,Integer newDay, String token) { Call<Void> call = todosRest.setTodo(accountId, BookId, year, month, day, todoId, title, newYear, newMonth, newDay, token); call.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { if (response.isSuccessful()) { System.out.println("Success: setTodo"); successSetTodoLiveData.setValue(true); } else { System.out.println("Error: setTodo" + response.code()); } } @Override public void onFailure(Call<Void> call, Throwable t) { System.out.println("NetworkError : setTodo" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } public void deleteTodo(String accountId, Integer BookId, Integer year, Integer month, Integer day, Integer todoId, String token) { Call<Void> call = todosRest.deleteTodoById(accountId, BookId, year, month, day, todoId, token); call.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { if (response.isSuccessful()) { System.out.println("Success: deleteTodo"); successDeleteTodoLiveData.setValue(true); } else { System.out.println("Error: deleteTodo" + response.code()); } } @Override public void onFailure(Call<Void> call, Throwable t) { System.out.println("NetworkError : 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"; } } }