diff --git a/app/src/main/java/com/example/citrusclient/viewmodels/PublicBooksViewModel.java b/app/src/main/java/com/example/citrusclient/viewmodels/PublicBooksViewModel.java index 2819df4..4577601 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/PublicBooksViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/PublicBooksViewModel.java @@ -1,6 +1,77 @@ package com.example.citrusclient.viewmodels; +import com.example.citrusclient.models.Book; +import com.example.citrusclient.rest.PublicBooksRest; + +import java.util.ArrayList; + +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; public class PublicBooksViewModel extends ViewModel { + private final Retrofit retrofit; + private final PublicBooksRest publicBooksRest; + + private final MutableLiveData> allBooksLiveData; + private final MutableLiveData> searchBooksLiveData; + + public PublicBooksViewModel(){ + retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + this.publicBooksRest = retrofit.create(PublicBooksRest.class); + this.allBooksLiveData = new MutableLiveData<>(); + this.searchBooksLiveData = new MutableLiveData<>(); + } + + public MutableLiveData> getAllBooksLiveData(){ + return this.allBooksLiveData; + } + public MutableLiveData> getSearchBooksLiveData(){ + return this.searchBooksLiveData; + } + + public void loadAllBooks(){ + Call> call = publicBooksRest.getAllPublicBooks(); + + call.enqueue(new Callback>(){ + @Override + public void onResponse(Call> call, Response>response){ + if(response.isSuccessful()){ + ArrayList book = response.body(); + allBooksLiveData.setValue(book); + System.out.println(response.code()); + }else System.out.println(response.code()); + } + + @Override + public void onFailure(Call> call, Throwable t){ + System.out.println("NetWorkError" + t); + } + }); + } + public void loadSearchBooks(String title,String accountId,int sortBy){ + Call> call = publicBooksRest.searchBooksByTitleAndAccount(title,accountId,sortBy); + call.enqueue(new Callback>(){ + @Override + public void onResponse(Call> call, Response>response){ + if(response.isSuccessful()){ + ArrayList book = response.body(); + allBooksLiveData.setValue(book); + System.out.println(response.code()); + }else System.out.println(response.code()); + } + + @Override + public void onFailure(Call> call, Throwable t){ + System.out.println("NetWorkError" + t); + } + }); + } }