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-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;
|
Don't use TinyDB as cache (#1034)
Do not use TinyDB as a cache or a way to send data between activities.
### How is this working
Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so).
Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**.
Additional changes:
* after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account)
* repositories and organizations are clickable on user profiles
* deleted two unused classes
Once finished, hopefully
* closes #354
* replaces #897
* fixes #947
* closes #1001
* closes #1015
* marks #876 and #578 as `Wontfix` since they are not necessary at this point
* and all the other TinyDB issues
Co-authored-by: qwerty287 <ndev@web.de>
Co-authored-by: M M Arif <mmarif@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
|
|
|
import org.mian.gitnex.activities.BaseActivity;
|
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;
|
2020-11-02 16:17:00 +01:00
|
|
|
import java.util.List;
|
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-04-18 09:10:54 +02:00
|
|
|
private static MutableLiveData<List<Release>> releasesList;
|
2022-02-12 14:41:11 +01:00
|
|
|
private static int resultLimit = Constants.resultLimitOldGiteaInstances;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
public LiveData<List<Release>> getReleasesList(String owner, String repo, Context ctx) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
releasesList = new MutableLiveData<>();
|
2022-02-12 14:41:11 +01:00
|
|
|
|
|
|
|
// if gitea is 1.12 or higher use the new limit
|
Don't use TinyDB as cache (#1034)
Do not use TinyDB as a cache or a way to send data between activities.
### How is this working
Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so).
Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**.
Additional changes:
* after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account)
* repositories and organizations are clickable on user profiles
* deleted two unused classes
Once finished, hopefully
* closes #354
* replaces #897
* fixes #947
* closes #1001
* closes #1015
* marks #876 and #578 as `Wontfix` since they are not necessary at this point
* and all the other TinyDB issues
Co-authored-by: qwerty287 <ndev@web.de>
Co-authored-by: M M Arif <mmarif@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
|
|
|
if(((BaseActivity) ctx).getAccount().requiresVersion("1.12.0")) {
|
2022-02-12 14:41:11 +01:00
|
|
|
resultLimit = Constants.resultLimitNewGiteaInstances;
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
loadReleasesList(owner, repo, ctx);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
return releasesList;
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
public static void loadReleasesList(String owner, String repo, Context ctx) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
Call<List<Release>> call = RetrofitClient
|
2020-11-02 16:17:00 +01:00
|
|
|
.getApiInterface(ctx)
|
2022-04-18 09:10:54 +02:00
|
|
|
.repoListReleases(owner, repo, null, null, null, 1, resultLimit);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
call.enqueue(new Callback<>() {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-18 09:10:54 +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-04-06 17:27:02 +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-04-06 17:27:02 +02:00
|
|
|
@Override
|
2022-04-18 09:10:54 +02:00
|
|
|
public void onFailure(@NonNull Call<List<Release>> call, @NonNull Throwable t) {
|
2022-04-06 17:27:02 +02:00
|
|
|
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericServerResponseError));
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
public static void loadMoreReleases(String owner, String repo, int page, Context ctx, ReleasesAdapter adapter) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
Call<List<Release>> call = RetrofitClient
|
2022-02-12 14:41:11 +01:00
|
|
|
.getApiInterface(ctx)
|
2022-04-18 09:10:54 +02:00
|
|
|
.repoListReleases(owner, repo, null, null, null, page, resultLimit);
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
call.enqueue(new Callback<>() {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
|
|
|
@Override
|
2022-04-18 09:10:54 +02:00
|
|
|
public void onResponse(@NonNull Call<List<Release>> call, @NonNull Response<List<Release>> response) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
if(response.isSuccessful()) {
|
2022-04-18 09:10:54 +02:00
|
|
|
List<Release> list = releasesList.getValue();
|
2022-02-12 14:41:11 +01: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-02-12 14:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-04-18 09:10:54 +02:00
|
|
|
public void onFailure(@NonNull Call<List<Release>> call, @NonNull Throwable t) {
|
2022-04-06 17:27:02 +02:00
|
|
|
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericServerResponseError));
|
2022-02-12 14:41:11 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
private static MutableLiveData<List<Tag>> tagsList;
|
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<>();
|
|
|
|
|
|
|
|
// if gitea is 1.12 or higher use the new limit
|
Don't use TinyDB as cache (#1034)
Do not use TinyDB as a cache or a way to send data between activities.
### How is this working
Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so).
Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**.
Additional changes:
* after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account)
* repositories and organizations are clickable on user profiles
* deleted two unused classes
Once finished, hopefully
* closes #354
* replaces #897
* fixes #947
* closes #1001
* closes #1015
* marks #876 and #578 as `Wontfix` since they are not necessary at this point
* and all the other TinyDB issues
Co-authored-by: qwerty287 <ndev@web.de>
Co-authored-by: M M Arif <mmarif@noreply.codeberg.org>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
|
|
|
if(((BaseActivity) ctx).getAccount().requiresVersion("1.12.0")) {
|
2022-02-12 14:41:11 +01:00
|
|
|
resultLimit = Constants.resultLimitNewGiteaInstances;
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
loadTagsList(owner, repo, ctx);
|
2022-02-12 14:41:11 +01:00
|
|
|
|
|
|
|
return tagsList;
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
public static void loadTagsList(String owner, String repo, Context ctx) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
Call<List<Tag>> call = RetrofitClient
|
2022-02-12 14:41:11 +01:00
|
|
|
.getApiInterface(ctx)
|
2022-04-18 09:10:54 +02:00
|
|
|
.repoListTags(owner, repo, 1, resultLimit);
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
call.enqueue(new Callback<>() {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
|
|
|
@Override
|
2022-04-18 09:10:54 +02:00
|
|
|
public void onResponse(@NonNull Call<List<Tag>> call, @NonNull Response<List<Tag>> response) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
if(response.isSuccessful()) {
|
2022-02-12 14:41:11 +01:00
|
|
|
tagsList.postValue(response.body());
|
|
|
|
}
|
|
|
|
else {
|
2022-04-06 17:27:02 +02:00
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericError));
|
2022-02-12 14:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-04-18 09:10:54 +02:00
|
|
|
public void onFailure(@NonNull Call<List<Tag>> call, @NonNull Throwable t) {
|
2022-04-06 17:27:02 +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-04-18 09:10:54 +02:00
|
|
|
public static void loadMoreTags(String owner, String repo, int page, Context ctx, TagsAdapter adapter) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
Call<List<Tag>> call = RetrofitClient
|
2022-02-12 14:41:11 +01:00
|
|
|
.getApiInterface(ctx)
|
2022-04-18 09:10:54 +02:00
|
|
|
.repoListTags(owner, repo, page, resultLimit);
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
call.enqueue(new Callback<>() {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
|
|
|
@Override
|
2022-04-18 09:10:54 +02:00
|
|
|
public void onResponse(@NonNull Call<List<Tag>> call, @NonNull Response<List<Tag>> response) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-04-06 17:27:02 +02:00
|
|
|
if(response.isSuccessful()) {
|
2022-02-12 14:41:11 +01:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
List<Tag> list = tagsList.getValue();
|
2022-02-12 14:41:11 +01: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-02-12 14:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-04-18 09:10:54 +02:00
|
|
|
public void onFailure(@NonNull Call<List<Tag>> call, @NonNull Throwable t) {
|
2022-04-06 17:27:02 +02:00
|
|
|
|
|
|
|
Toasty.error(ctx, ctx.getString(R.string.genericServerResponseError));
|
2022-02-12 14:41:11 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|