diff --git a/app/src/main/java/com/example/citrusclient/models/Schedule.java b/app/src/main/java/com/example/citrusclient/models/Schedule.java new file mode 100644 index 0000000..27d70ca --- /dev/null +++ b/app/src/main/java/com/example/citrusclient/models/Schedule.java @@ -0,0 +1,60 @@ +package com.example.citrusclient.models; + +public class Schedule { + + private String title; + private String startTime; + private String endTime; + private int bookId; + private int scheduleId; + + public Schedule(){} + + public Schedule(String title, String startTime, String endTime, int bookId, int scheduleId){ + this.title = title; + this.startTime = startTime; + this.endTime = endTime; + this.bookId = bookId; + this.scheduleId = scheduleId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getStartTime() { + return startTime; + } + + public void setStartTime(String startTime) { + this.startTime = startTime; + } + + public String getEndTime() { + return endTime; + } + + public void setEndTime(String endTime) { + this.endTime = endTime; + } + + public int getBookId() { + return bookId; + } + + public void setBookId(int bookId) { + this.bookId = bookId; + } + + public int getScheduleId() { + return scheduleId; + } + + public void setScheduleId(int scheduleId) { + this.scheduleId = scheduleId; + } +} diff --git a/app/src/main/java/com/example/citrusclient/rest/ScheduleRest.java b/app/src/main/java/com/example/citrusclient/rest/ScheduleRest.java new file mode 100644 index 0000000..341f85e --- /dev/null +++ b/app/src/main/java/com/example/citrusclient/rest/ScheduleRest.java @@ -0,0 +1,145 @@ +package com.example.citrusclient.rest; + +import com.example.citrusclient.models.Schedule; + +import java.util.HashMap; + +import retrofit2.Call; +import retrofit2.http.DELETE; +import retrofit2.http.Field; +import retrofit2.http.FormUrlEncoded; +import retrofit2.http.GET; +import retrofit2.http.POST; +import retrofit2.http.PUT; +import retrofit2.http.Path; +import retrofit2.http.Query; + +public interface ScheduleRest { + + String BASE_URL = "/accounts/{account_id}/schedule"; + + @GET(BASE_URL) + Call>>>> getAllSchedules( + @Path("account_id") String accountId, + @Query("token") String token + ); + + @GET(BASE_URL + "/{year}") + Call>>> getSchedulesByYear( + @Path("account_id") String accountId, + @Path("year") int year, + @Query("token") String token + ); + + @GET(BASE_URL + "/{year}/{month}") + Call>> getSchedulesByMonth( + @Path("account_id") String accountId, + @Path("year") int year, + @Path("month") int month, + @Query("token") String token + ); + + @GET(BASE_URL + "/{year}/{month}/{day}") + Call> getSchedulesByDay( + @Path("account_id") String accountId, + @Path("year") int year, + @Path("month") int month, + @Path("day") int day, + @Query("token") String token + ); + + @GET(BASE_URL + "/{year}/{month}/{day}/{schedule_id}") + Call getScheduleById( + @Path("account_id") String accountId, + @Path("year") int year, + @Path("month") int month, + @Path("day") int day, + @Path("schedule_id") int id, + @Query("token") String token + ); + + + + @FormUrlEncoded + @POST(BASE_URL + "/{year}/{month}/{day}") + Call createSchedule( + @Path("account_id") String accountId, + @Path("year") int year, + @Path("month") int month, + @Path("day") int day, + @Field("title") String title, + @Field("start_time") String startTime, + @Field("end_time") String endTime, + @Field("book_id") int bookId, + @Field("token") String token + ); + + @DELETE(BASE_URL + "/{year}/{month}/{day}/{schedule_id}") + Call deleteScheduleById( + @Path("account_id") String accountId, + @Path("year") int year, + @Path("month") int month, + @Path("day") int day, + @Path("schedule_id") int scheduleId, + @Query("token") String token + ); + + @FormUrlEncoded + @PUT(BASE_URL + "/{year}/{month}/{day}/{schedule_id}") + Call putStartTime( + @Path("account_id") String accountId, + @Path("year") int year, + @Path("month") int month, + @Path("day") int day, + @Path("schedule_id") int scheduleId, + @Field("token") String token, + @Field("start_time") String startTime + ); + + @FormUrlEncoded + @PUT(BASE_URL + "/{year}/{month}/{day}/{schedule_id}") + Call putEndTime( + @Path("account_id") String accountId, + @Path("year") int year, + @Path("month") int month, + @Path("day") int day, + @Path("schedule_id") int scheduleId, + @Field("token") String token, + @Field("end_time") String endTime + ); + + @FormUrlEncoded + @PUT(BASE_URL + "/{year}/{month}/{day}/{schedule_id}") + Call putTitle( + @Path("account_id") String accountId, + @Path("year") int year, + @Path("month") int month, + @Path("day") int day, + @Path("schedule_id") int scheduleId, + @Field("token") String token, + @Field("title") String title + ); + + @FormUrlEncoded + @PUT(BASE_URL + "/{year}/{month}/{day}/{schedule_id}") + Call putBookId( + @Path("account_id") String accountId, + @Path("year") int year, + @Path("month") int month, + @Path("day") int day, + @Path("schedule_id") int scheduleId, + @Field("token") String token, + @Field("book_id") int bookId + ); + +} diff --git a/app/src/main/java/com/example/citrusclient/viewmodels/ScheduleViewModel.java b/app/src/main/java/com/example/citrusclient/viewmodels/ScheduleViewModel.java new file mode 100644 index 0000000..435bafa --- /dev/null +++ b/app/src/main/java/com/example/citrusclient/viewmodels/ScheduleViewModel.java @@ -0,0 +1,285 @@ +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>>>> allSchedulesLiveData; + + private final MutableLiveData>>> schedulesByYear; + + private final MutableLiveData>> schedulesByMonth; + + private final MutableLiveData> schedulesByDay; + + private final MutableLiveData scheduleById; + + private final MutableLiveData 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>>>> getAllSchedulesLiveData() { + return allSchedulesLiveData; + } + + public MutableLiveData>>> getSchedulesByYear() { + return schedulesByYear; + } + + public MutableLiveData>> getSchedulesByMonth() { + return schedulesByMonth; + } + + public MutableLiveData> getSchedulesByDay() { + return schedulesByDay; + } + + public MutableLiveData getScheduleById() { + return scheduleById; + } + + public MutableLiveData getErrorLiveData() { + return errorLiveData; + } + + public void createSchedule(String accountId, int year, int month, int day, String token, + String title, String startTime, String endTime, int bookId){ + Call call = scheduleRest.createSchedule(accountId, year, month, day, title, startTime, endTime, bookId, token); + call.enqueue(new Callback(){ + @Override + public void onResponse(Call call, Response response){ + if(response.isSuccessful()){ + + }else{ + errorLiveData.setValue(response.message()); + } + } + + @Override + public void onFailure(Call call, Throwable t){ + errorLiveData.setValue(t.getMessage()); + } + }); + + } + + + public void updateAllSchedules(String accountId, String token){ + Call>>>> + call = scheduleRest.getAllSchedules(accountId, token); + call.enqueue(new Callback>>>>() { + @Override + public void onResponse(Call>>>> call, Response>>>> response) { + if(response.isSuccessful()){ + allSchedulesLiveData.setValue(response.body()); + }else{ + errorLiveData.setValue(response.message()); + } + } + @Override + public void onFailure(Call>>>> call, Throwable t) { + errorLiveData.setValue(t.getMessage()); + } + }); + } + + public void updateSchedulesByYear(String accountId, int year, String token){ + Call>>> call = scheduleRest.getSchedulesByYear(accountId, year, token); + call.enqueue(new Callback>>>() { + @Override + public void onResponse(Call>>> call, Response>>> response) { + if(response.isSuccessful()){ + schedulesByYear.setValue(response.body()); + } else { + errorLiveData.setValue(response.message()); + } + } + @Override + public void onFailure(Call>>> call, Throwable t) { + errorLiveData.setValue(t.getMessage()); + } + }); + + } + + public void updateSchedulesByMonth(String accountId, int year, int month, String token){ + Call>> call = scheduleRest.getSchedulesByMonth(accountId, year, month, token); + call.enqueue(new Callback>>() { + @Override + public void onResponse(Call>> call, Response>> response) { + if(response.isSuccessful()){ + schedulesByMonth.setValue(response.body()); + } else { + errorLiveData.setValue(response.message()); + } + } + @Override + public void onFailure(Call>> call, Throwable t) { + errorLiveData.setValue(t.getMessage()); + } + }); + } + + public void updateSchedulesByDay(String accountId, int year, int month, int day, String token){ + Call> call = scheduleRest.getSchedulesByDay(accountId, year, month, day, token); + call.enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if(response.isSuccessful()) schedulesByDay.setValue(response.body()); + else errorLiveData.setValue(response.message()); + } + + @Override + public void onFailure(Call> call, Throwable t) { + errorLiveData.setValue(t.getMessage()); + } + }); + } + + public void updateScheduleById(String accountId, int year, int month, int day, int id, String token){ + Call call = scheduleRest.getScheduleById(accountId, year, month, day, id, token); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()) scheduleById.setValue(response.body()); + else errorLiveData.setValue(response.message()); + } + + @Override + public void onFailure(Call call, Throwable t) { + errorLiveData.setValue(t.getMessage()); + } + }); + } + + public void deleteScheduleById(String accountId, int year, int month, int day, int id, String token){ + Call call = scheduleRest.deleteScheduleById(accountId, year, month, day, id, token); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()){ + schedulesByDay.setValue(schedulesByMonth.getValue().remove(day)); + } + else errorLiveData.setValue(response.message()); + } + + @Override + public void onFailure(Call call, Throwable t) { + errorLiveData.setValue(t.getMessage()); + } + }); + + } + + public void setStartTime(String accountId, int year, int month, int day, int id, String token, String startTime){ + Call call = scheduleRest.putStartTime(accountId, year, month, day, id, token, startTime); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()) { + Schedule schedule = scheduleById.getValue(); + schedule.setStartTime(startTime); + scheduleById.setValue(schedule); + } else errorLiveData.setValue(response.message()); + } + + @Override + public void onFailure(Call call, Throwable t) { + errorLiveData.setValue(t.getMessage()); + } + }); + } + + public void setEndTime(String accountId, int year, int month, int day, int id, String token, String endTime){ + Call call = scheduleRest.putEndTime(accountId, year, month, day, id, token, endTime); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()){ + Schedule schedule = scheduleById.getValue(); + schedule.setEndTime(endTime); + scheduleById.setValue(schedule); + }else errorLiveData.setValue(response.message()); + } + + @Override + public void onFailure(Call call, Throwable t) { + errorLiveData.setValue(t.getMessage()); + } + }); + } + + public void setTitle(String accountId, int year, int month, int day, int id, String token, String title){ + Call call = scheduleRest.putTitle(accountId, year, month, day, id, token, title); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()){ + Schedule schedule = scheduleById.getValue(); + schedule.setTitle(title); + scheduleById.setValue(schedule); + }else errorLiveData.setValue(response.message()); + } + + @Override + public void onFailure(Call call, Throwable t) { + errorLiveData.setValue(t.getMessage()); + } + }); + } + + public void setBookId(String accountId, int year, int month, int day, int id, String token, int bookId){ + Call call = scheduleRest.putBookId(accountId, year, month, day, id, token, bookId); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()){ + Schedule schedule = scheduleById.getValue(); + schedule.setBookId(bookId); + scheduleById.setValue(schedule); + } else errorLiveData.setValue(response.message()); + } + + @Override + public void onFailure(Call call, Throwable t) { + errorLiveData.setValue(t.getMessage()); + } + }); + } +}