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/BooksRest.java b/app/src/main/java/com/example/citrusclient/rest/BooksRest.java index 47ba3ef..420da7f 100644 --- a/app/src/main/java/com/example/citrusclient/rest/BooksRest.java +++ b/app/src/main/java/com/example/citrusclient/rest/BooksRest.java @@ -28,7 +28,7 @@ //本の新規作成 @FormUrlEncoded @POST("accounts/{account_id}/books") - Call createBook( + Call> createBook( @Path("account_id") String account_id, @Field("title") String title, @Field("color") String color, @@ -46,7 +46,7 @@ //本の削除 @DELETE("accounts/{account_id}/books/{book_id}") - Call deleteBook( + Call> deleteBook( @Path("account_id") String account_id, @Path("book_id") Integer book_id, @Query("token") String token 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/BooksViewModel.java b/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java index e1e54fe..cbf3f2a 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java @@ -18,22 +18,12 @@ public class BooksViewModel extends ViewModel { private final Retrofit retrofit; - private final BooksRest booksRest; - private final MutableLiveData> booksLiveData; - private final MutableLiveData titleLiveData; - private final MutableLiveData colorLiveData; - private final MutableLiveData publicityLiveData; - - private final MutableLiveData errorLiveData; - - private final MutableLiveData successDeleteBookLiveData; - - private final MutableLiveData delBookErrorLivedata; + private final MutableLiveData favoritedLiveData; public BooksViewModel() { this.retrofit = new Retrofit.Builder() @@ -45,45 +35,42 @@ this.titleLiveData = new MutableLiveData<>(); this.colorLiveData = new MutableLiveData<>(); this.publicityLiveData = new MutableLiveData<>(); - this.errorLiveData = new MutableLiveData<>(); - this.successDeleteBookLiveData = new MutableLiveData<>(); - this.delBookErrorLivedata = new MutableLiveData<>(); + this.favoritedLiveData = new MutableLiveData<>(); } - public MutableLiveData> getBookLiveData() { return this.booksLiveData;} - - public MutableLiveData getSuccessDeleteBookLiveData() {return successDeleteBookLiveData;} + public MutableLiveData> getBookLiveData() { return this.booksLiveData;}//本の一覧を返す + public MutableLiveData getTitleLiveData() {return this.titleLiveData;}//本のタイトルを返す + public MutableLiveData getPublicityLiveData() {return this.publicityLiveData;}//本の公開状態を返す public void createBook(String accountId, String title, String color, Boolean publicity, String token ){ - Call call = booksRest.createBook(accountId , title, color, publicity, token); + Call> call = booksRest.createBook(accountId , title, color, publicity, token); - call.enqueue(new Callback() { + call.enqueue(new Callback>() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(Call> call, Response> response) { if(response.isSuccessful()) { + HashMap book = response.body(); + booksLiveData.setValue(book); System.out.println("success!" + response.body()); }else { System.out.println("fail"); - errorLiveData.setValue(parseStatusCode(response.code())); + parseStatusCode(response.code()); } } - @Override - public void onFailure(Call call, Throwable t) { - + public void onFailure(Call> call, Throwable t) { + System.out.println("NetWorkError" + t); } }); - } - public void loadBooks(String account_id, String token){ - Call> call = booksRest.getBooks(account_id, token); + public void loadBooks(String accountId, String token){ + Call> call = booksRest.getBooks(accountId, token); call.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.isSuccessful()) { - System.out.println(response.code()); HashMap book = response.body(); booksLiveData.setValue(book); System.out.println(response.code()); @@ -92,26 +79,21 @@ @Override public void onFailure(Call> call, Throwable t) { - System.out.println("fail"); + System.out.println("NetWorkError" + t); } }); } -// private void setBooksLiveData(HashMap bookall){ -// HashMap books = new HashMap<>(); -// for(int i = 0; bookall.get(i).keySet().equals(null); i++){ -// -// } -// booksLiveData.setValue(books); -// } - public void deleteBook(String account_id, Integer book_id, String token){ - Call call = booksRest.deleteBook(account_id, book_id, token); + public void deleteBook(String accountId, Integer bookId, String token){ + Call> call = booksRest.deleteBook(accountId, bookId, token); - call.enqueue(new Callback() { + call.enqueue(new Callback>() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(Call> call, Response> response) { if (response.isSuccessful()) { - deleteBookLiveData(account_id, book_id); + HashMap book = response.body(); + book.remove(bookId); + booksLiveData.setValue(book); System.out.println("DELETE"); } else { System.out.println("response error"); @@ -119,26 +101,20 @@ } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(Call> call, Throwable t) { System.out.println("correspondence error" + t); } }); } - private void deleteBookLiveData(String account_id, Integer book_id) { - ArrayList delData = new ArrayList<>(); - - } - - public void setTitle(String account_id, Integer book_id, String title, String token){ - Call call = booksRest.putTitle(account_id, book_id, title, token); + public void setTitle(String accountId, Integer bookId, String title, String token){ + Call call = booksRest.putTitle(accountId, bookId, title, token); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()){ titleLiveData.setValue(title); - System.out.println(response.code()); System.out.println("Success SetTiTle" + title); } else { System.out.println("response error"); @@ -152,15 +128,14 @@ }); } - public void setColor(String account_id, Integer book_id, String color, String token){ - Call call = booksRest.putColor(account_id, book_id, color, token); + public void setColor(String accountId, Integer bookId, String color, String token){ + Call call = booksRest.putColor(accountId, bookId, color, token); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()){ colorLiveData.setValue(color); - System.out.println(response.code()); System.out.println("Success SetTiTle" + color); } else { System.out.println("response error"); @@ -174,8 +149,8 @@ }); } - public void setPublicity(String account_id, Integer book_id, Boolean publicity, String token){ - Call call = booksRest.putPublicity(account_id, book_id, publicity, token); + public void setPublicity(String accountId, Integer bookId, Boolean publicity, String token){ + Call call = booksRest.putPublicity(accountId, bookId, publicity, token); call.enqueue(new Callback() { @Override @@ -183,7 +158,6 @@ if (response.isSuccessful()){ String pub = String.valueOf(publicity); publicityLiveData.setValue(pub); - System.out.println(response.code()); System.out.println("Success SetTiTle" + publicity); } else { System.out.println("response error"); 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()); + } + }); + } +} 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 0d9935a..bfb88d3 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/TodosViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/TodosViewModel.java @@ -23,9 +23,11 @@ //LiveData private final MutableLiveData>>>> allTodosLiveData; - private final MutableLiveData>> TodosByMonthLiveData; - private final MutableLiveData> TodosByDayLiveData; - private final MutableLiveData TodoByIdLiveData; + private final MutableLiveData>> todosByMonthLiveData; + private final MutableLiveData> todosByDayLiveData; + private final MutableLiveData todoLiveData; + private final MutableLiveData successSetCheckLiveData; + private final MutableLiveData successDeleteTodoLiveData; private final MutableLiveData errorLiveData; //Constructor @@ -37,9 +39,11 @@ .build(); this.todosRest = retrofit.create(TodosRest.class); this.allTodosLiveData = new MutableLiveData<>(); - this.TodosByMonthLiveData = new MutableLiveData<>(); - this.TodosByDayLiveData = new MutableLiveData<>(); - this.TodoByIdLiveData = new MutableLiveData<>(); + this.todosByMonthLiveData = new MutableLiveData<>(); + this.todosByDayLiveData = new MutableLiveData<>(); + this.todoLiveData = new MutableLiveData<>(); + this.successSetCheckLiveData = new MutableLiveData<>(); + this.successDeleteTodoLiveData = new MutableLiveData<>(); this.errorLiveData = new MutableLiveData<>(); } @@ -48,13 +52,16 @@ { return allTodosLiveData; } - public MutableLiveData>> getTodosByMonthLiveData() {return TodosByMonthLiveData;} - public MutableLiveData> getTodosByDayLiveData() {return TodosByDayLiveData;} - public MutableLiveData getTodoByIdLiveData() {return TodoByIdLiveData;} + public MutableLiveData>> getTodosByMonthLiveData() {return todosByMonthLiveData;} + public MutableLiveData> getTodosByDayLiveData() {return todosByDayLiveData;} + public MutableLiveData getTodoLiveData() {return todoLiveData;} + public MutableLiveData getSuccessSetCheckLiveData() {return successSetCheckLiveData;} + public MutableLiveData getSuccessDeleteTodoLiveData() {return successDeleteTodoLiveData;} public MutableLiveData getErrorLiveData() {return errorLiveData;} - public void getAllTodos(String accountId,Integer bookId, String token) + + public void loadAllTodos(String accountId,Integer bookId, String token) { Call>>>> call = todosRest.getAllTodos(accountId, bookId, token); call.enqueue(new Callback>>>>() { @@ -70,14 +77,14 @@ } @Override public void onFailure(Call>>>> call, Throwable t) { - System.out.println("CommunicationError: getAllTodos" + t); + System.out.println("NetworkError : getAllTodos" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } - public void getTodosByMonth(String accountId,Integer bookId, Integer year, Integer month, + public void loadTodosByMonth(String accountId,Integer bookId, Integer year, Integer month, String token) { Call>> call = todosRest.getTodosByMonth(accountId, bookId, year, month, token); @@ -86,21 +93,21 @@ public void onResponse(Call>> call, Response>> response) { if (response.isSuccessful()) { System.out.println("Success: getTodosByMonth"); - TodosByMonthLiveData.setValue(response.body()); + todosByMonthLiveData.setValue(response.body()); } else { System.out.println("Error: getTodosByMonth" + response.code()); } } @Override public void onFailure(Call>> call, Throwable t) { - System.out.println("CommunicationError: getTodosByMonth" + t); + System.out.println("NetworkError : getTodosByMonth" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } - public void getTodosByDay(String accountId,Integer bookId, Integer year, Integer month, + public void loadTodosByDay(String accountId,Integer bookId, Integer year, Integer month, Integer day, String token) { Call> call = todosRest.getTodosByDay(accountId, bookId, year, month, day, token); @@ -109,21 +116,21 @@ public void onResponse(Call> call, Response> response) { if (response.isSuccessful()) { System.out.println("Success: getTodosByDay"); - TodosByDayLiveData.setValue(response.body()); + todosByDayLiveData.setValue(response.body()); } else { System.out.println("Error: getTodosByDay" + response.code()); } } @Override public void onFailure(Call> call, Throwable t) { - System.out.println("CommunicationError: getTodosByDay" + t); + System.out.println("NetworkError : getTodosByDay" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); } - public void getTodoById(String accountId,Integer bookId, Integer year, Integer month, + public void loadTodoById(String accountId,Integer bookId, Integer year, Integer month, Integer day, Integer todoId, String token) { Call call = todosRest.getTodoById(accountId, bookId, year, month, day, todoId, token); @@ -132,14 +139,14 @@ public void onResponse(Call call, Response response) { if (response.isSuccessful()) { System.out.println("Success: getTodoById"); - TodoByIdLiveData.setValue(response.body()); + todoLiveData.setValue(response.body()); } else { System.out.println("Error: getTodoById" + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("CommunicationError: getTodoById" + t); + System.out.println("NetworkError : getTodoById" + t); errorLiveData.setValue(parseStatusCode(-1)); } @@ -155,14 +162,13 @@ public void onResponse(Call call, Response response) { if (response.isSuccessful()) { System.out.println("Success: createTodo"); - TodoByIdLiveData.setValue(response.body()); } else { System.out.println("Error: createTodo" + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("CommunicationError: createTodo" + t); + System.out.println("NetworkError : createTodo" + t); errorLiveData.setValue(parseStatusCode(-1)); } @@ -178,14 +184,14 @@ public void onResponse(Call call, Response 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 call, Throwable t) { - System.out.println("CommunicationError: setCheck" + t); + System.out.println("NetworkError : setCheck" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); @@ -200,14 +206,14 @@ public void onResponse(Call call, Response 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 call, Throwable t) { - System.out.println("CommunicationError: deleteTodo" + t); + System.out.println("NetworkError : deleteTodo" + t); errorLiveData.setValue(parseStatusCode(-1)); } }); diff --git a/app/src/main/java/com/example/citrusclient/views/CreateBookFragment.java b/app/src/main/java/com/example/citrusclient/views/CreateBookFragment.java index 9d11a24..1984939 100644 --- a/app/src/main/java/com/example/citrusclient/views/CreateBookFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CreateBookFragment.java @@ -5,6 +5,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; @@ -13,6 +14,10 @@ import android.widget.Button; import com.example.citrusclient.R; +import android.graphics.Color;//ボタンの色変更 +import android.os.Bundle; +import android.widget.Button; + /** * A simple {@link Fragment} subclass. @@ -71,15 +76,26 @@ @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); + super.onViewCreated(view, savedInstanceState); //親クラスである Fragment の onViewCreated() メソッドを呼び出す - //View.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener()) - Button saveButton = (Button)view.findViewById(R.id.save_button); -// saveButton.setOnClickListener(new View.OnClickListener(){ -// public void onClick(View v){ -// Intent intent = Intent(CreateBookFragment.this,MainActivity.class); -// startActivity(intent); -// } -// }) + //ここから書く + // MainActivityにこのフラグメントを設定するした二つのどっちか + //((MainActivity) getContext()).setCreateBookFragment(this);//フラグメントとアクティビティ間での情報のやり取りがあるとき + //((MainActivity) getActivity()).showFragment(new MyBookshelfFragment()); + + view.findViewById(R.id.save_button).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + ((MainActivity) getActivity()).showFragment(new MyBookshelfFragment()); + } + }); + + view.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + ((MainActivity) getActivity()).showFragment(new MyBookshelfFragment()); + } + }); + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java b/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java index 9ec5406..e092f40 100644 --- a/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java +++ b/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java @@ -1,19 +1,35 @@ package com.example.citrusclient.views; +import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; +import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.jackson.JacksonConverterFactory; +import com.example.citrusclient.Citrus; import com.example.citrusclient.R; +import com.example.citrusclient.rest.AccountsRest; + +import org.w3c.dom.Text; public class SignUpActivity extends AppCompatActivity { + private Retrofit retrofit; + private AccountsRest accountsRest; + private Citrus citrus; + + //画面起動時 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -28,6 +44,26 @@ }); + //citrusと連携 + citrus = (Citrus) this.getApplication(); + + //端末に情報が登録されているとき + if ((citrus.getToken() != null)&&(citrus.getAccountId() != null)){ + System.out.println("アカウント登録済み"); + //MainActivityに遷移 + Intent intent = new Intent(SignUpActivity.this,MainActivity.class); + startActivity(intent); + } + + //通信の初期化 + this.retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + this.accountsRest = retrofit.create(AccountsRest.class); + + + findViewById(R.id.button_signup).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -37,11 +73,49 @@ EditText editpw = (EditText) findViewById(R.id.put_pw); String pw = editpw.getText().toString().trim(); - if(!id.isEmpty() && !pw.isEmpty() && !id.trim().isEmpty() && !pw.trim().isEmpty() ){ - //通信 + //入力欄が空欄の時 + if(id.isEmpty() && pw.isEmpty() && id.trim().isEmpty() && pw.trim().isEmpty()){ + System.out.println("不適切入力"); + ((TextView)findViewById(R.id.textView_respons)).setText("適切に入力してください"); + + }else if(!id.isEmpty() && !pw.isEmpty() && !id.trim().isEmpty() && !pw.trim().isEmpty() ){ + //入力欄が全て適切に入力された場合の処理 + Call call = accountsRest.signup(id,pw); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + System.out.println("通信成功"); + //端末にtoken.id登録 + String token = response.body(); + citrus.setToken(token); + citrus.setAccountId(id); + + //画面遷移 + Intent intent = new Intent(SignUpActivity.this,MainActivity.class); + startActivity(intent); + + }else{ + //通信可能(ただしエラーが発生した場合) + //404がPW入力されてないときだけど、前で定義しているからいらないよね..? + System.out.println("通信可能"); + if(response.code()==409){ + ((TextView)findViewById(R.id.textView_respons)).setText(("入力したアカウントIDは既に使用されています")); + } + } + } + //通信失敗 + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("通信失敗"); + System.out.println(t); + } + }); } } }); + + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_sign_up.xml b/app/src/main/res/layout/activity_sign_up.xml index fecfc4b..a6ba2c0 100644 --- a/app/src/main/res/layout/activity_sign_up.xml +++ b/app/src/main/res/layout/activity_sign_up.xml @@ -83,7 +83,8 @@ android:id="@+id/textView_respons" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="error表示" + android:text="" + android:textColor="#B10922" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.501" diff --git a/app/src/main/res/layout/fragment_create_book.xml b/app/src/main/res/layout/fragment_create_book.xml index 2fccf53..b35d8d7 100644 --- a/app/src/main/res/layout/fragment_create_book.xml +++ b/app/src/main/res/layout/fragment_create_book.xml @@ -14,19 +14,26 @@ android:id="@+id/save_button" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:background="#ECFDE3" android:text="保存" + android:textColor="#296DC1" + app:backgroundTint="#EDFDE4" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" - app:layout_constraintHorizontal_bias="0.90" + app:layout_constraintHorizontal_bias="0.9" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" - app:layout_constraintVertical_bias="0.10" /> + app:layout_constraintVertical_bias="0.099" />