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 784795f..37989ea 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java @@ -20,14 +20,10 @@ private final Retrofit retrofit; private final BooksRest booksRest; private final MutableLiveData> booksLiveData; - private final MutableLiveData> booksIdLiveData; private final MutableLiveData titleLiveData; private final MutableLiveData colorLiveData; private final MutableLiveData publicityLiveData; private final MutableLiveData favoritedLiveData; - private final MutableLiveData errorLiveData; - private final MutableLiveData successDeleteBookLiveData; - private final MutableLiveData delBookErrorLivedata; public BooksViewModel(MutableLiveData> booksIdLiveData) { this.retrofit = new Retrofit.Builder() @@ -36,48 +32,41 @@ .build(); this.booksRest = retrofit.create(BooksRest.class); this.booksLiveData = new MutableLiveData<>(); - this.booksIdLiveData = booksIdLiveData; this.titleLiveData = new MutableLiveData<>(); this.colorLiveData = new MutableLiveData<>(); this.publicityLiveData = new MutableLiveData<>(); this.favoritedLiveData = new MutableLiveData<>(); - this.errorLiveData = new MutableLiveData<>(); - this.successDeleteBookLiveData = new MutableLiveData<>(); - this.delBookErrorLivedata = new MutableLiveData<>(); } public MutableLiveData> getBookLiveData() { return this.booksLiveData;}//本の一覧を返す - public MutableLiveData> getBooksIdLiveData() {return this.booksIdLiveData;}//本の情報を取得 + //public MutableLiveData> getBooksIdLiveData() {return this.booksIdLiveData;}//本の情報を取得 public MutableLiveData getTitleLiveData() {return this.titleLiveData;}//本のタイトルを返す public MutableLiveData getPublicityLiveData() {return this.publicityLiveData;}//本の公開状態を返す - - public MutableLiveData getSuccessDeleteBookLiveData() {return successDeleteBookLiveData;} - 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 @@ -91,19 +80,21 @@ @Override public void onFailure(Call> call, Throwable t) { - System.out.println("fail"); + System.out.println("NetWorkError" + t); } }); } - 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()) { - successDeleteBookLiveData.setValue(true); + HashMap book = response.body(); + book.remove(bookId); + booksLiveData.setValue(book); System.out.println("DELETE"); } else { System.out.println("response error"); @@ -111,32 +102,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) { - HashMap delData = new HashMap<>(); - for (Book book: booksLiveData.getValue()) { - if (book.getBookId().equals(book_id)) { - continue; - } - delData.add(book); - } - booksLiveData.setValue(delData); - }*/ - - 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"); @@ -150,15 +129,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"); @@ -172,8 +150,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 @@ -181,7 +159,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");