diff --git a/app/src/main/java/com/example/citrusclient/viewmodels/FavoritesViewModel.java b/app/src/main/java/com/example/citrusclient/viewmodels/FavoritesViewModel.java index bd53028..48322d5 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/FavoritesViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/FavoritesViewModel.java @@ -2,7 +2,14 @@ import com.example.citrusclient.rest.FavoritesRest; +import java.util.HashMap; +import java.util.HashSet; + +import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; @@ -10,11 +17,75 @@ private final Retrofit retrofit; private final FavoritesRest favoritesRest; + private final MutableLiveData>> favoritesBooksLiveData; + private final MutableLiveData> favoritesBookIdLiveData; + private final MutableLiveData> favoritedAccountsLiveData; + public FavoritesViewModel(){ this.retrofit = new Retrofit.Builder() .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/") .addConverterFactory(JacksonConverterFactory.create()) .build(); this.favoritesRest = retrofit.create(FavoritesRest.class); + this.favoritesBooksLiveData = new MutableLiveData<>(); + this.favoritesBookIdLiveData = new MutableLiveData<>(); + this.favoritedAccountsLiveData = new MutableLiveData<>(); + } + public MutableLiveData>> getFavoritesBooksLiveData(){return favoritesBooksLiveData;} + public MutableLiveData> getFavoritesBookIdLiveData(){return favoritesBookIdLiveData;} + public MutableLiveData> getFavoritedAccountsLiveData(){return favoritedAccountsLiveData;} + + public void loadFavoritesBooks(String accountId, String token){ + Call>> call = favoritesRest.getFavoritesBooks(accountId,token); + call.enqueue(new Callback>>() { + @Override + public void onResponse(Call>> call, Response>> response) { + if(response.isSuccessful()){ + System.out.println("success : getFavoritesBooks"); + favoritesBooksLiveData.setValue(response.body()); + }else System.out.println(response.code()); + } + + @Override + public void onFailure(Call>> call, Throwable t) { + System.out.println("NetworkError : getFavoritesBooks" + t); + } + }); + } + + public void loadFavoritesBookId(String accountId, String otherAccountId, String token){ + Call> call = favoritesRest.getFavoritesBooksById(accountId,otherAccountId,token); + call.enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if(response.isSuccessful()){ + System.out.println("success : getFavoritesBooksById"); + favoritesBookIdLiveData.setValue(response.body()); + }else System.out.println(response.code()); + } + + @Override + public void onFailure(Call> call, Throwable t) { + System.out.println("NetworkError : getFavoritesBooksById" + t); + } + }); + } + + public void loadFavoritedAccounts(String accountId, Integer bookId, String token){ + Call> call = favoritesRest.getFavoritedAccount(accountId,bookId,token); + call.enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if(response.isSuccessful()){ + System.out.println("success : getFavoritedAccount"); + favoritedAccountsLiveData.setValue(response.body()); + }else System.out.println(response.code()); + } + + @Override + public void onFailure(Call> call, Throwable t) { + System.out.println("NetworkError : getFavoritedAccount" + t); + } + }); } }