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-09-21 07:43:00 +02:00
|
|
|
import java.util.List;
|
2022-04-18 09:10:54 +02:00
|
|
|
import org.gitnex.tea4j.v2.models.Organization;
|
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.OrganizationsListAdapter;
|
|
|
|
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 retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author M M Arif
|
|
|
|
*/
|
|
|
|
public class OrganizationsViewModel extends ViewModel {
|
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
private MutableLiveData<List<Organization>> orgList;
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public LiveData<List<Organization>> getUserOrg(int page, int resultLimit, Context ctx) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
orgList = new MutableLiveData<>();
|
|
|
|
loadOrgList(page, resultLimit, ctx);
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
return orgList;
|
|
|
|
}
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public void loadOrgList(int page, int resultLimit, Context ctx) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Call<List<Organization>> call =
|
|
|
|
RetrofitClient.getApiInterface(ctx).orgListCurrentUserOrgs(page, resultLimit);
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
call.enqueue(
|
|
|
|
new Callback<>() {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<List<Organization>> call,
|
|
|
|
@NonNull Response<List<Organization>> response) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
orgList.postValue(response.body());
|
|
|
|
} else {
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(
|
|
|
|
@NonNull Call<List<Organization>> call, @NonNull Throwable t) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericServerResponseError));
|
|
|
|
}
|
|
|
|
});
|
2022-08-15 16:26:02 +02:00
|
|
|
}
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
public void loadMoreOrgList(
|
|
|
|
int page, int resultLimit, Context ctx, OrganizationsListAdapter adapter) {
|
|
|
|
|
|
|
|
Call<List<Organization>> call =
|
|
|
|
RetrofitClient.getApiInterface(ctx).orgListCurrentUserOrgs(page, resultLimit);
|
|
|
|
|
|
|
|
call.enqueue(
|
|
|
|
new Callback<>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<List<Organization>> call,
|
|
|
|
@NonNull Response<List<Organization>> response) {
|
|
|
|
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
List<Organization> list = orgList.getValue();
|
|
|
|
assert list != null;
|
|
|
|
assert response.body() != null;
|
|
|
|
|
2024-03-28 13:02:17 +01:00
|
|
|
if (!response.body().isEmpty()) {
|
2022-09-21 07:43:00 +02:00
|
|
|
list.addAll(response.body());
|
|
|
|
adapter.updateList(list);
|
|
|
|
} else {
|
|
|
|
adapter.setMoreDataAvailable(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(
|
|
|
|
@NonNull Call<List<Organization>> call, @NonNull Throwable t) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericServerResponseError));
|
2022-04-04 10:00:54 +02:00
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
});
|
2022-04-04 10:00:54 +02:00
|
|
|
}
|
|
|
|
}
|