Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / viewmodels / TodosViewModel.java
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> TodoByIdLiveData;
    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.TodoByIdLiveData = new MutableLiveData<>();
        this.errorLiveData = new MutableLiveData<>();
    }

    //getter
    public MutableLiveData<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Todo>>>>> getTodosLiveData()
    {
        return allTodosLiveData;
    }
    public MutableLiveData<String> getErrorLiveData() {return errorLiveData;}


    public void getAllTodos(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("UnknownError: getAllTodos" + t);
                errorLiveData.setValue(parseStatusCode(-1));
            }

        });
    }

    public void getTodosByMonth(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("UnknownError: getTodosByMonth" + t);
                errorLiveData.setValue(parseStatusCode(-1));
            }

        });
    }

    public void getTodosByDay(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("UnknownError: getTodosByDay" + t);
                errorLiveData.setValue(parseStatusCode(-1));
            }

        });
    }

    public void getTodoById(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");
                    TodoByIdLiveData.setValue(response.body());
                } else {
                    System.out.println("Error: getTodoById" + response.code());
                }
            }
            @Override
            public void onFailure(Call<Todo> call, Throwable t) {
                System.out.println("UnknownError: 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("UnknownError: 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");

                } else {
                    System.out.println("Error: setCheck" + response.code());
                }
            }
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                System.out.println("UnknownError: setCheck" + 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");

                } else {
                    System.out.println("Error: deleteTodo" + response.code());
                }
            }
            @Override
            public void onFailure(Call<Void> 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";
        }
    }
}