2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.viewmodels;
|
|
|
|
|
2019-11-13 16:52:49 +01:00
|
|
|
import android.content.Context;
|
2019-06-03 23:25:05 +02:00
|
|
|
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.Release;
|
|
|
|
import org.gitnex.tea4j.v2.models.Tag;
|
2022-04-06 17:27:02 +02:00
|
|
|
import org.mian.gitnex.R;
|
2022-02-12 14:41:11 +01:00
|
|
|
import org.mian.gitnex.adapters.ReleasesAdapter;
|
|
|
|
import org.mian.gitnex.adapters.TagsAdapter;
|
2020-11-02 16:17:00 +01:00
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
2022-02-12 14:41:11 +01:00
|
|
|
import org.mian.gitnex.helpers.Constants;
|
2022-04-06 17:27:02 +02:00
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
2019-06-03 23:25:05 +02:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
|
|
|
|
/**
|
2022-04-06 17:27:02 +02:00
|
|
|
* @author M M Arif
|
2019-06-03 23:25:05 +02:00
|
|
|
*/
|
|
|
|
public class ReleasesViewModel extends ViewModel {
|
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
private MutableLiveData<List<Release>> releasesList;
|
2022-04-22 17:18:38 +02:00
|
|
|
private int resultLimit;
|
2022-08-15 16:26:02 +02:00
|
|
|
private MutableLiveData<List<Tag>> tagsList;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public LiveData<List<Release>> getReleasesList(String owner, String repo, Context ctx) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
releasesList = new MutableLiveData<>();
|
|
|
|
resultLimit = Constants.getCurrentResultLimit(ctx);
|
|
|
|
loadReleasesList(owner, repo, ctx);
|
|
|
|
return releasesList;
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public void loadReleasesList(String owner, String repo, Context ctx) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Call<List<Release>> call =
|
|
|
|
RetrofitClient.getApiInterface(ctx)
|
2024-03-25 18:31:59 +01:00
|
|
|
.repoListReleases(owner, repo, null, null, 1, resultLimit);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
call.enqueue(
|
|
|
|
new Callback<>() {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<List<Release>> call,
|
|
|
|
@NonNull Response<List<Release>> response) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
releasesList.postValue(response.body());
|
|
|
|
} else {
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<Release>> call, @NonNull Throwable t) {
|
2022-04-06 17:27:02 +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
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
public void loadMoreReleases(
|
|
|
|
String owner, String repo, int page, Context ctx, ReleasesAdapter adapter) {
|
|
|
|
|
|
|
|
Call<List<Release>> call =
|
|
|
|
RetrofitClient.getApiInterface(ctx)
|
2024-03-25 18:31:59 +01:00
|
|
|
.repoListReleases(owner, repo, null, null, page, resultLimit);
|
2022-09-21 07:43:00 +02:00
|
|
|
|
|
|
|
call.enqueue(
|
|
|
|
new Callback<>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<List<Release>> call,
|
|
|
|
@NonNull Response<List<Release>> response) {
|
|
|
|
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
List<Release> list = releasesList.getValue();
|
|
|
|
assert list != null;
|
|
|
|
assert response.body() != null;
|
|
|
|
|
2024-03-25 18:31:59 +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-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<Release>> call, @NonNull Throwable t) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericServerResponseError));
|
2022-02-12 14:41:11 +01:00
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
});
|
2022-02-12 14:41:11 +01:00
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
public LiveData<List<Tag>> getTagsList(String owner, String repo, Context ctx) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
|
|
|
tagsList = new MutableLiveData<>();
|
2022-04-22 17:18:38 +02:00
|
|
|
resultLimit = Constants.getCurrentResultLimit(ctx);
|
2022-04-18 09:10:54 +02:00
|
|
|
loadTagsList(owner, repo, ctx);
|
2022-02-12 14:41:11 +01:00
|
|
|
return tagsList;
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:18:38 +02:00
|
|
|
public void loadTagsList(String owner, String repo, Context ctx) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Call<List<Tag>> call =
|
|
|
|
RetrofitClient.getApiInterface(ctx).repoListTags(owner, repo, 1, resultLimit);
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
call.enqueue(
|
|
|
|
new Callback<>() {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<List<Tag>> call, @NonNull Response<List<Tag>> response) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
tagsList.postValue(response.body());
|
|
|
|
} else {
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<Tag>> call, @NonNull Throwable t) {
|
2022-04-06 17:27:02 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericServerResponseError));
|
|
|
|
}
|
|
|
|
});
|
2022-02-12 14:41:11 +01:00
|
|
|
}
|
2022-04-06 17:27:02 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
public void loadMoreTags(
|
|
|
|
String owner, String repo, int page, Context ctx, TagsAdapter adapter) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Call<List<Tag>> call =
|
|
|
|
RetrofitClient.getApiInterface(ctx).repoListTags(owner, repo, page, resultLimit);
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
call.enqueue(
|
|
|
|
new Callback<>() {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<List<Tag>> call, @NonNull Response<List<Tag>> response) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (response.isSuccessful()) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
List<Tag> list = tagsList.getValue();
|
|
|
|
assert list != null;
|
|
|
|
assert response.body() != null;
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2024-03-25 18:31:59 +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-02-12 14:41:11 +01:00
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<Tag>> call, @NonNull Throwable t) {
|
|
|
|
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericServerResponseError));
|
2022-02-12 14:41:11 +01:00
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
});
|
2022-02-12 14:41:11 +01:00
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|