2022-04-04 10:00:54 +02:00
|
|
|
package org.mian.gitnex.viewmodels;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.lifecycle.LiveData;
|
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
|
import androidx.lifecycle.ViewModel;
|
2022-04-18 09:10:54 +02:00
|
|
|
import org.gitnex.tea4j.v2.models.Repository;
|
2022-04-06 17:27:02 +02:00
|
|
|
import org.mian.gitnex.R;
|
2022-04-04 10:00:54 +02:00
|
|
|
import org.mian.gitnex.adapters.ReposListAdapter;
|
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
2022-04-06 17:27:02 +02:00
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
2022-04-04 10:00:54 +02:00
|
|
|
import java.util.List;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class RepositoriesViewModel extends ViewModel {
|
|
|
|
|
2022-04-22 17:18:38 +02:00
|
|
|
private MutableLiveData<List<Repository>> reposList;
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
public LiveData<List<Repository>> getRepositories(int page, int resultLimit, String userLogin, String type, String orgName, Context ctx) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
|
|
|
reposList = new MutableLiveData<>();
|
2022-04-18 09:10:54 +02:00
|
|
|
loadReposList(page, resultLimit, userLogin, type, orgName, ctx);
|
2022-04-04 10:00:54 +02:00
|
|
|
|
|
|
|
return reposList;
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:18:38 +02:00
|
|
|
public void loadReposList(int page, int resultLimit, String userLogin, String type, String orgName, Context ctx) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
Call<List<Repository>> call;
|
2022-04-04 10:00:54 +02:00
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case "starredRepos":
|
2022-04-18 09:10:54 +02:00
|
|
|
call = RetrofitClient.getApiInterface(ctx).userCurrentListStarred(page, resultLimit);
|
2022-04-04 10:00:54 +02:00
|
|
|
break;
|
|
|
|
case "myRepos":
|
2022-04-18 09:10:54 +02:00
|
|
|
call = RetrofitClient.getApiInterface(ctx).userListRepos(userLogin, page, resultLimit);
|
2022-04-04 10:00:54 +02:00
|
|
|
break;
|
|
|
|
case "org":
|
2022-04-18 09:10:54 +02:00
|
|
|
call = RetrofitClient.getApiInterface(ctx).orgListRepos(orgName, page, resultLimit);
|
2022-04-04 10:00:54 +02:00
|
|
|
break;
|
2022-04-21 23:02:59 +02:00
|
|
|
case "team":
|
|
|
|
call = RetrofitClient.getApiInterface(ctx).orgListTeamRepos(Long.valueOf(userLogin), page, resultLimit);
|
|
|
|
break;
|
2022-04-04 10:00:54 +02:00
|
|
|
default:
|
2022-04-18 09:10:54 +02:00
|
|
|
call = RetrofitClient.getApiInterface(ctx).userCurrentListRepos(page, resultLimit);
|
2022-04-04 10:00:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
call.enqueue(new Callback<>() {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<Repository>> call, @NonNull Response<List<Repository>> response) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
if(response.isSuccessful()) {
|
|
|
|
if(response.code() == 200) {
|
|
|
|
reposList.postValue(response.body());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
@Override
|
2022-04-18 09:10:54 +02:00
|
|
|
public void onFailure(@NonNull Call<List<Repository>> call, @NonNull Throwable t) {
|
2022-04-06 17:27:02 +02:00
|
|
|
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericServerResponseError));
|
|
|
|
}
|
2022-04-04 10:00:54 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:18:38 +02:00
|
|
|
public void loadMoreRepos(int page, int resultLimit, String userLogin, String type, String orgName, Context ctx, ReposListAdapter adapter) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
Call<List<Repository>> call;
|
2022-04-04 10:00:54 +02:00
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case "starredRepos":
|
2022-04-18 09:10:54 +02:00
|
|
|
call = RetrofitClient.getApiInterface(ctx).userCurrentListStarred(page, resultLimit);
|
2022-04-04 10:00:54 +02:00
|
|
|
break;
|
|
|
|
case "myRepos":
|
2022-04-18 09:10:54 +02:00
|
|
|
call = RetrofitClient.getApiInterface(ctx).userListRepos(userLogin, page, resultLimit);
|
2022-04-04 10:00:54 +02:00
|
|
|
break;
|
|
|
|
case "org":
|
2022-04-18 09:10:54 +02:00
|
|
|
call = RetrofitClient.getApiInterface(ctx).orgListRepos(orgName, page, resultLimit);
|
2022-04-04 10:00:54 +02:00
|
|
|
break;
|
2022-04-21 23:02:59 +02:00
|
|
|
case "team":
|
|
|
|
call = RetrofitClient.getApiInterface(ctx).orgListTeamRepos(Long.valueOf(userLogin), page, resultLimit);
|
|
|
|
break;
|
2022-04-04 10:00:54 +02:00
|
|
|
default:
|
2022-04-18 09:10:54 +02:00
|
|
|
call = RetrofitClient.getApiInterface(ctx).userCurrentListRepos(page, resultLimit);
|
2022-04-04 10:00:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
call.enqueue(new Callback<>() {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
|
|
|
@Override
|
2022-04-18 09:10:54 +02:00
|
|
|
public void onResponse(@NonNull Call<List<Repository>> call, @NonNull Response<List<Repository>> response) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
|
|
|
if(response.isSuccessful()) {
|
2022-04-18 09:10:54 +02:00
|
|
|
List<Repository> list = reposList.getValue();
|
2022-04-04 10:00:54 +02:00
|
|
|
assert list != null;
|
|
|
|
assert response.body() != null;
|
|
|
|
|
|
|
|
if(response.body().size() != 0) {
|
|
|
|
list.addAll(response.body());
|
|
|
|
adapter.updateList(list);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
adapter.setMoreDataAvailable(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2022-04-06 17:27:02 +02:00
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericError));
|
2022-04-04 10:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-04-18 09:10:54 +02:00
|
|
|
public void onFailure(@NonNull Call<List<Repository>> call, @NonNull Throwable t) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericServerResponseError));
|
2022-04-04 10:00:54 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|