package com.example.citrusclient.viewmodels;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.example.citrusclient.models.Schedule;
import com.example.citrusclient.rest.ScheduleRest;
import java.util.HashMap;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
public class ScheduleViewModel extends ViewModel {
private final Retrofit retrofit;
private final ScheduleRest scheduleRest;
private final MutableLiveData<HashMap<Integer, //年
HashMap<Integer, //月
HashMap<Integer, //日
HashMap<Integer, //Id
Schedule>>>>> allSchedulesLiveData;
private final MutableLiveData<HashMap<Integer, //月
HashMap<Integer, //日
HashMap<Integer, //id
Schedule>>>> schedulesByYear;
private final MutableLiveData<HashMap<Integer, //日
HashMap<Integer, //id
Schedule>>> schedulesByMonth;
private final MutableLiveData<HashMap<Integer, Schedule>> schedulesByDay;
private final MutableLiveData<Schedule> scheduleById;
private final MutableLiveData<String> errorLiveData;
public ScheduleViewModel(){
retrofit = new Retrofit.Builder()
.baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/")
.addConverterFactory(JacksonConverterFactory.create())
.build();
this.scheduleRest = retrofit.create(ScheduleRest.class);
allSchedulesLiveData = new MutableLiveData<>();
schedulesByYear = new MutableLiveData<>();
schedulesByMonth = new MutableLiveData<>();
schedulesByDay = new MutableLiveData<>();
scheduleById = new MutableLiveData<>();
errorLiveData = new MutableLiveData<>();
}
public MutableLiveData<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Schedule>>>>> getAllSchedulesLiveData() {
return allSchedulesLiveData;
}
public MutableLiveData<HashMap<Integer, HashMap<Integer, HashMap<Integer, Schedule>>>> getSchedulesByYear() {
return schedulesByYear;
}
public MutableLiveData<HashMap<Integer, HashMap<Integer, Schedule>>> getSchedulesByMonth() {
return schedulesByMonth;
}
public MutableLiveData<HashMap<Integer, Schedule>> getSchedulesByDay() {
return schedulesByDay;
}
public MutableLiveData<Schedule> getScheduleById() {
return scheduleById;
}
public MutableLiveData<String> getErrorLiveData() {
return errorLiveData;
}
/**
* スケジュールを作成する
*
* @param accountId アカウントid
* @param year 年
* @param month 月
* @param day 日
* @param token トークン
* @param title タイトル
* @param startTime スケジュールの開始日時
* @param endTime スケジュールの終了日時
* @param bookId スケジュールを追加する本
*/
public void createSchedule(String accountId, int year, int month, int day, String token,
String title, String startTime, String endTime, int bookId){
Call<Schedule> call = scheduleRest.createSchedule(accountId, year, month, day, title, startTime, endTime, bookId, token);
call.enqueue(new Callback<Schedule>(){
@Override
public void onResponse(Call<Schedule> call, Response<Schedule> response){
if(response.isSuccessful()){
}else{
errorLiveData.setValue(response.message());
}
}
@Override
public void onFailure(Call<Schedule> call, Throwable t){
errorLiveData.setValue(t.getMessage());
}
});
}
/**
* アカウントを指定してallSchedulesLiveDataを更新する
*
* @param accountId アカウントid
* @param token トークン
*/
public void updateAllSchedules(String accountId, String token){
Call<HashMap<Integer, HashMap<Integer,HashMap<Integer,HashMap<Integer,Schedule>>>>>
call = scheduleRest.getAllSchedules(accountId, token);
call.enqueue(new Callback<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Schedule>>>>>() {
@Override
public void onResponse(Call<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Schedule>>>>> call, Response<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Schedule>>>>> response) {
if(response.isSuccessful()){
allSchedulesLiveData.setValue(response.body());
}else{
errorLiveData.setValue(response.message());
}
}
@Override
public void onFailure(Call<HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<Integer, Schedule>>>>> call, Throwable t) {
errorLiveData.setValue(t.getMessage());
}
});
}
/**
* アカウントと年を指定してschedulesByYearを更新する
*
* @param accountId アカウントid
* @param year 年
* @param token トークン
*/
public void updateSchedulesByYear(String accountId, int year, String token){
Call<HashMap<Integer, HashMap<Integer, HashMap<Integer, Schedule>>>> call = scheduleRest.getSchedulesByYear(accountId, year, token);
call.enqueue(new Callback<HashMap<Integer, HashMap<Integer, HashMap<Integer, Schedule>>>>() {
@Override
public void onResponse(Call<HashMap<Integer, HashMap<Integer, HashMap<Integer, Schedule>>>> call, Response<HashMap<Integer, HashMap<Integer, HashMap<Integer, Schedule>>>> response) {
if(response.isSuccessful()){
schedulesByYear.setValue(response.body());
} else {
errorLiveData.setValue(response.message());
}
}
@Override
public void onFailure(Call<HashMap<Integer, HashMap<Integer, HashMap<Integer, Schedule>>>> call, Throwable t) {
errorLiveData.setValue(t.getMessage());
}
});
}
/**
* アカウントと年と月を指定してschedulesByMonthを更新する
*
* @param accountId アカウントid
* @param year 年
* @param month 月
* @param token トークン
*/
public void updateSchedulesByMonth(String accountId, int year, int month, String token){
Call<HashMap<Integer, HashMap<Integer, Schedule>>> call = scheduleRest.getSchedulesByMonth(accountId, year, month, token);
call.enqueue(new Callback<HashMap<Integer, HashMap<Integer, Schedule>>>() {
@Override
public void onResponse(Call<HashMap<Integer, HashMap<Integer, Schedule>>> call, Response<HashMap<Integer, HashMap<Integer, Schedule>>> response) {
if(response.isSuccessful()){
schedulesByMonth.setValue(response.body());
} else {
errorLiveData.setValue(response.message());
}
}
@Override
public void onFailure(Call<HashMap<Integer, HashMap<Integer, Schedule>>> call, Throwable t) {
errorLiveData.setValue(t.getMessage());
}
});
}
/**
* アカウントと年と月と日を指定してスケジュールを更新する
*
* @param accountId アカウントid
* @param year 年
* @param month 月
* @param day 日
* @param token トークン
*/
public void updateSchedulesByDay(String accountId, int year, int month, int day, String token){
Call<HashMap<Integer, Schedule>> call = scheduleRest.getSchedulesByDay(accountId, year, month, day, token);
call.enqueue(new Callback<HashMap<Integer, Schedule>>() {
@Override
public void onResponse(Call<HashMap<Integer, Schedule>> call, Response<HashMap<Integer, Schedule>> response) {
if(response.isSuccessful()) schedulesByDay.setValue(response.body());
else errorLiveData.setValue(response.message());
}
@Override
public void onFailure(Call<HashMap<Integer, Schedule>> call, Throwable t) {
errorLiveData.setValue(t.getMessage());
}
});
}
/**
* アカウントと年と月と日とスケジュールのidを指定してスケジュールを更新する
*
* @param accountId アカウントid
* @param year 年
* @param month 月
* @param day 日
* @param id id
* @param token トークン
*/
public void updateScheduleById(String accountId, int year, int month, int day, int id, String token){
Call<Schedule> call = scheduleRest.getScheduleById(accountId, year, month, day, id, token);
call.enqueue(new Callback<Schedule>() {
@Override
public void onResponse(Call<Schedule> call, Response<Schedule> response) {
if(response.isSuccessful()) scheduleById.setValue(response.body());
else errorLiveData.setValue(response.message());
}
@Override
public void onFailure(Call<Schedule> call, Throwable t) {
errorLiveData.setValue(t.getMessage());
}
});
}
/**
* アカウントid, 年, 月, 日, スケジュールidを指定してスケジュールを削除する
*
* @param accountId アカウントid
* @param year 年
* @param month 月
* @param day 日
* @param id スケジュールid
* @param token トークン
*/
public void deleteScheduleById(String accountId, int year, int month, int day, int id, String token){
Call<String> call = scheduleRest.deleteScheduleById(accountId, year, month, day, id, token);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(response.isSuccessful()){
schedulesByDay.setValue(schedulesByMonth.getValue().remove(day));
}
else errorLiveData.setValue(response.message());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
errorLiveData.setValue(t.getMessage());
}
});
}
/**
* アカウントid, 年, 月, 日, スケジュールidを指定してスケジュールの開始日時を更新する
*
* @param accountId アカウントid
* @param year 年
* @param month 月
* @param day 日
* @param id スケジュールid
* @param token トークン
* @param startTime 開始日時
*/
public void setStartTime(String accountId, int year, int month, int day, int id, String token, String startTime){
Call<String> call = scheduleRest.putStartTime(accountId, year, month, day, id, token, startTime);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(response.isSuccessful()) {
Schedule schedule = scheduleById.getValue();
schedule.setStartTime(startTime);
scheduleById.setValue(schedule);
} else errorLiveData.setValue(response.message());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
errorLiveData.setValue(t.getMessage());
}
});
}
/**
*
* アカウントid, 年, 月, 日, スケジュールidを指定してスケジュールの終了日時を更新する
*
* @param accountId アカウントid
* @param year 年
* @param month 月
* @param day 日
* @param id スケジュールid
* @param token トークン
* @param endTime 終了日時
*/
public void setEndTime(String accountId, int year, int month, int day, int id, String token, String endTime){
Call<String> call = scheduleRest.putEndTime(accountId, year, month, day, id, token, endTime);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(response.isSuccessful()){
Schedule schedule = scheduleById.getValue();
schedule.setEndTime(endTime);
scheduleById.setValue(schedule);
}else errorLiveData.setValue(response.message());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
errorLiveData.setValue(t.getMessage());
}
});
}
/**
* アカウントid, 年, 月, 日, スケジュールidを指定してスケジュールのタイトルを更新する
*
* @param accountId アカウントid
* @param year 年
* @param month 月
* @param day 日
* @param id スケジュールid
* @param token トークン
* @param title タイトル
*/
public void setTitle(String accountId, int year, int month, int day, int id, String token, String title){
Call<String> call = scheduleRest.putTitle(accountId, year, month, day, id, token, title);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(response.isSuccessful()){
Schedule schedule = scheduleById.getValue();
schedule.setTitle(title);
scheduleById.setValue(schedule);
}else errorLiveData.setValue(response.message());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
errorLiveData.setValue(t.getMessage());
}
});
}
/**
* アカウントid, 年, 月, 日, スケジュールidを指定してスケジュールの所属する本を更新する
*
* @param accountId アカウントid
* @param year 年
* @param month 月
* @param day 日
* @param id スケジュールid
* @param token トークン
* @param bookId 本のid
*/
public void setBookId(String accountId, int year, int month, int day, int id, String token, int bookId){
Call<String> call = scheduleRest.putBookId(accountId, year, month, day, id, token, bookId);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(response.isSuccessful()){
Schedule schedule = scheduleById.getValue();
schedule.setBookId(bookId);
scheduleById.setValue(schedule);
} else errorLiveData.setValue(response.message());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
errorLiveData.setValue(t.getMessage());
}
});
}
}