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 7dde738..a1260c8 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/BooksViewModel.java @@ -1,4 +1,210 @@ package com.example.citrusclient.viewmodels; -public class BooksViewModel { -} +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +import com.example.citrusclient.models.Book; +import com.example.citrusclient.rest.BooksRest; + +import java.util.ArrayList; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.jackson.JacksonConverterFactory; + +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; + + public BooksViewModel() { + this.retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + this.booksRest = retrofit.create(BooksRest.class); + this.booksLiveData = new MutableLiveData<>(); + this.titleLiveData = new MutableLiveData<>(); + this.colorLiveData = new MutableLiveData<>(); + this.publicityLiveData = new MutableLiveData<>(); + this.errorLiveData = new MutableLiveData<>(); + this.successDeleteBookLiveData = new MutableLiveData<>(); + this.delBookErrorLivedata = new MutableLiveData<>(); + } + + //public MutableLiveData getBookLiveData() { return bookLiveData;} + + 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); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()) { + System.out.println("success!" + response.body()); + }else { + System.out.println("fail"); + errorLiveData.setValue(parseStatusCode(response.code())); + } + } + + @Override + 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); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()) { + deleteBookLiveData(account_id, book_id); + System.out.println("DELETE"); + } else { + System.out.println("response error"); + } + } + + @Override + 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); + + 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"); + } + } + + @Override + 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.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"); + } + } + + @Override + 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); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + publicityLiveData.setValue(publicity); + System.out.println(response.code()); + System.out.println("Success SetTiTle" + publicity); + } else { + System.out.println("response error"); + } + } + + @Override + 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 400: + System.out.println("見つかりませんでした"); + return ErrorType.ResponseError.getText(); + case 500: + System.out.println("見つかりませんでした"); + return ErrorType.ServerError.getText();*/ + default: + System.out.println("見つかりませんでした"); + return null; + } + } +} \ No newline at end of file