Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / viewmodels / PublicBooksViewModel.java
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<ArrayList<Book>> allBooksLiveData;
    private final MutableLiveData<ArrayList<Book>> 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<ArrayList<Book>> getAllBooksLiveData(){
        return this.allBooksLiveData;
    }
    public MutableLiveData<ArrayList<Book>> getSearchBooksLiveData(){
        return this.searchBooksLiveData;
    }

    public void loadAllBooks(){
        Call<ArrayList<Book>> call = publicBooksRest.getAllPublicBooks();

        call.enqueue(new Callback<ArrayList<Book>>(){
            @Override
            public  void onResponse(Call<ArrayList<Book>> call, Response<ArrayList<Book>>response){
                if(response.isSuccessful()){
                    ArrayList<Book> book = response.body();
                    allBooksLiveData.setValue(book);
                    System.out.println(response.code());
                }else System.out.println(response.code());
            }

            @Override
            public void onFailure(Call<ArrayList<Book>> call, Throwable t){
                System.out.println("NetWorkError" + t);
            }
        });
    }
    public void loadSearchBooks(String title,String accountId,int sortBy){
        Call<ArrayList<Book>> call = publicBooksRest.searchBooksByTitleAndAccount(title,accountId,sortBy);
        call.enqueue(new Callback<ArrayList<Book>>(){
            @Override
            public  void onResponse(Call<ArrayList<Book>> call, Response<ArrayList<Book>>response){
                if(response.isSuccessful()){
                    ArrayList<Book> book = response.body();
                    allBooksLiveData.setValue(book);
                    System.out.println(response.code());
                }else System.out.println(response.code());
            }

            @Override
            public void onFailure(Call<ArrayList<Book>> call, Throwable t){
                System.out.println("NetWorkError" + t);
            }
        });
    }
}