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 a1260c8..f406770 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java @@ -7,6 +7,7 @@ import com.example.citrusclient.rest.BooksRest; import java.util.ArrayList; +import java.util.HashMap; import retrofit2.Call; import retrofit2.Callback; @@ -20,7 +21,7 @@ private final BooksRest booksRest; - private final MutableLiveData> booksLiveData; + private final MutableLiveData> booksLiveData; private final MutableLiveData titleLiveData; @@ -49,18 +50,16 @@ this.delBookErrorLivedata = new MutableLiveData<>(); } - //public MutableLiveData getBookLiveData() { return bookLiveData;} + public MutableLiveData> getBookLiveData() { return this.booksLiveData;} - public MutableLiveData getSuccessDeleteBookLiveData() { - return successDeleteBookLiveData; - } + public MutableLiveData getSuccessDeleteBookLiveData() {return successDeleteBookLiveData;} - /*public void createBook(String accountId, String token , String title, String color, Boolean publicity){ - Call call = booksRest.createBook(accountId, token , title, color, publicity); + public void createBook(String accountId, String title, String color, Boolean publicity, String 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()) { System.out.println("success!" + response.body()); }else { @@ -70,38 +69,45 @@ } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(Call call, Throwable t) { } }); - }*/ - - public void loadBooks(String account_id, String token){ -// Call> call = booksRest.getBooks(account_id); -// -// call.enqueue(new Callback>() { -// @Override -// public void onResponse(Call> call, Response> response) { -// if (response.isSuccessful()) { -// ArrayList book = response.body(); -// //for() -// } -// } -// -// @Override -// public void onFailure(Call> call, Throwable t) { -// -// } -// }); } - /*public void deleteBook(String account_id, Integer book_id, String token){ - Call> call = booksRest.deleteBook(account_id, book_id, token); + public void loadBooks(String account_id, String token){ + Call> call = booksRest.getBooks(account_id, 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); + } + } + + @Override + public void onFailure(Call> call, Throwable 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); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { if (response.isSuccessful()) { deleteBookLiveData(account_id, book_id); System.out.println("DELETE"); @@ -111,23 +117,23 @@ } @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.setTitle(account_id, book_id, title, token); + public void setTitle(String account_id, Integer book_id, String title, String token){ + Call call = booksRest.putTitle(account_id, book_id, title, 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()){ titleLiveData.setValue(title); System.out.println(response.code()); @@ -138,18 +144,18 @@ } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(Call call, Throwable t) { System.out.println("NetWorkError" + t); } }); } public void setColor(String account_id, Integer book_id, String color, String token){ - Call call = booksRest.setColor(account_id, book_id, color, token); + Call call = booksRest.putColor(account_id, book_id, color, 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()){ colorLiveData.setValue(color); System.out.println(response.code()); @@ -160,20 +166,21 @@ } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(Call call, Throwable t) { System.out.println("NetWorkError" + t); } }); } - public void setPublicity(String account_id, Integer book_id, String publicity, String token){ - Call call = booksRest.setpublicity(account_id, book_id, publicity, token); + public void setPublicity(String account_id, Integer book_id, Boolean publicity, String token){ + Call call = booksRest.putPublicity(account_id, book_id, 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()){ - publicityLiveData.setValue(publicity); + String pub = String.valueOf(publicity); + publicityLiveData.setValue(pub); System.out.println(response.code()); System.out.println("Success SetTiTle" + publicity); } else { @@ -182,28 +189,28 @@ } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(Call call, Throwable t) { System.out.println("NetWorkError" + t); } }); - }*/ + } private String parseStatusCode(Integer code) { switch (code) { case 404: System.out.println("見つかりませんでした"); return null; - /*case 401: - System.out.println("見つかりませんでした"); - return ErrorType.InvalidToken.getText(); + case 401: + System.out.println("トークンが違います"); + return null; case 400: - System.out.println("見つかりませんでした"); - return ErrorType.ResponseError.getText(); + System.out.println("レスポンスエラー"); + return null; case 500: - System.out.println("見つかりませんでした"); - return ErrorType.ServerError.getText();*/ + System.out.println("サーバーエラー"); + return null; default: - System.out.println("見つかりませんでした"); + System.out.println("不明なエラー"); return null; } }