Created
May 9, 2020 16:11
-
-
Save anta40/ff1571b0692a0ee8dbd641c88829a2fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.divbyzero.app.githubusersearch.viewmodel; | |
import android.util.Log; | |
import androidx.lifecycle.LiveData; | |
import androidx.lifecycle.MutableLiveData; | |
import androidx.lifecycle.ViewModel; | |
import com.divbyzero.app.githubusersearch.api.APIEndPoint; | |
import com.divbyzero.app.githubusersearch.api.APIService; | |
import com.divbyzero.app.githubusersearch.response.SearchResponse; | |
import com.divbyzero.app.githubusersearch.model.User; | |
import java.util.ArrayList; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
import retrofit2.Retrofit; | |
public class UserViewModel extends ViewModel { | |
private MutableLiveData<ArrayList<User>> mutableLiveData = new MutableLiveData<>(); | |
private int totalPage; | |
public void setSearchResult(String who, int page){ | |
Retrofit retrofit = APIService.getRetrofitService(); | |
APIEndPoint apiEndpoint = retrofit.create(APIEndPoint.class); | |
Call<SearchResponse> call = apiEndpoint.getSearchResult(who, page); | |
call.enqueue(new Callback<SearchResponse>() { | |
@Override | |
public void onResponse(Call<SearchResponse> call, Response<SearchResponse> response) { | |
mutableLiveData.setValue((ArrayList<User>) response.body().getItems()); | |
totalPage = response.body().getTotalCount(); | |
Log.d("DBG", "OK"); | |
} | |
@Override | |
public void onFailure(Call<SearchResponse> call, Throwable t) { | |
Log.d("DBG", "Failed: "+t.getMessage()); | |
} | |
}); | |
} | |
public LiveData<ArrayList<User>> getSearchResult(){ | |
return mutableLiveData; | |
} | |
public void clear(){ | |
mutableLiveData.setValue(new ArrayList<User>()); | |
} | |
public int getTotalPage(){ | |
return totalPage; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment