2019-10-11 22:03:01 +02:00
|
|
|
package org.mian.gitnex.actions;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import androidx.annotation.NonNull;
|
2022-04-18 09:10:54 +02:00
|
|
|
import org.gitnex.tea4j.v2.models.WatchInfo;
|
2019-10-11 22:03:01 +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.MainActivity;
|
2019-10-11 22:03:01 +02:00
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
|
|
|
import org.mian.gitnex.helpers.AlertDialogs;
|
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
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.helpers.contexts.RepositoryContext;
|
2019-10-11 22:03:01 +02:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
|
|
|
|
/**
|
2022-04-06 17:27:02 +02:00
|
|
|
* @author M M Arif
|
2019-10-11 22:03:01 +02:00
|
|
|
*/
|
|
|
|
public class RepositoryActions {
|
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public static void starRepository(final Context context, RepositoryContext repository) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Call<Void> call =
|
|
|
|
RetrofitClient.getApiInterface(context)
|
|
|
|
.userCurrentPutStar(repository.getOwner(), repository.getName());
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
call.enqueue(
|
|
|
|
new Callback<>() {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<Void> call, @NonNull retrofit2.Response<Void> response) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
if (response.code() == 204) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
MainActivity.reloadRepos = true;
|
|
|
|
Toasty.success(
|
|
|
|
context, context.getString(R.string.starRepositorySuccess));
|
|
|
|
}
|
|
|
|
} else if (response.code() == 401) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
AlertDialogs.authorizationTokenRevokedDialog(context);
|
|
|
|
} else if (response.code() == 403) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(context, context.getString(R.string.authorizeError));
|
|
|
|
} else if (response.code() == 404) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.warning(context, context.getString(R.string.apiNotFound));
|
|
|
|
} else {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(context, context.getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(
|
|
|
|
context, context.getString(R.string.genericServerResponseError));
|
|
|
|
}
|
|
|
|
});
|
2022-08-15 16:26:02 +02:00
|
|
|
}
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public static void unStarRepository(final Context context, RepositoryContext repository) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Call<Void> call =
|
|
|
|
RetrofitClient.getApiInterface(context)
|
|
|
|
.userCurrentDeleteStar(repository.getOwner(), repository.getName());
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
call.enqueue(
|
|
|
|
new Callback<>() {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<Void> call, @NonNull retrofit2.Response<Void> response) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
if (response.code() == 204) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
MainActivity.reloadRepos = true;
|
|
|
|
Toasty.success(
|
|
|
|
context,
|
|
|
|
context.getString(R.string.unStarRepositorySuccess));
|
|
|
|
}
|
|
|
|
} else if (response.code() == 401) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
AlertDialogs.authorizationTokenRevokedDialog(context);
|
|
|
|
} else if (response.code() == 403) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(context, context.getString(R.string.authorizeError));
|
|
|
|
} else if (response.code() == 404) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.warning(context, context.getString(R.string.apiNotFound));
|
|
|
|
} else {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(context, context.getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(
|
|
|
|
context, context.getString(R.string.genericServerResponseError));
|
|
|
|
}
|
|
|
|
});
|
2022-08-15 16:26:02 +02:00
|
|
|
}
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public static void watchRepository(final Context context, RepositoryContext repository) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Call<WatchInfo> call =
|
|
|
|
RetrofitClient.getApiInterface(context)
|
|
|
|
.userCurrentPutSubscription(repository.getOwner(), repository.getName());
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
call.enqueue(
|
|
|
|
new Callback<>() {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<WatchInfo> call,
|
|
|
|
@NonNull retrofit2.Response<WatchInfo> response) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (response.isSuccessful()) {
|
|
|
|
if (response.code() == 200) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.success(
|
|
|
|
context,
|
|
|
|
context.getString(R.string.watchRepositorySuccess));
|
|
|
|
}
|
|
|
|
} else if (response.code() == 401) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
AlertDialogs.authorizationTokenRevokedDialog(context);
|
|
|
|
} else if (response.code() == 403) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(context, context.getString(R.string.authorizeError));
|
|
|
|
} else if (response.code() == 404) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.warning(context, context.getString(R.string.apiNotFound));
|
|
|
|
} else {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(context, context.getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<WatchInfo> call, @NonNull Throwable t) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(
|
|
|
|
context, context.getString(R.string.genericServerResponseError));
|
|
|
|
}
|
|
|
|
});
|
2022-08-15 16:26:02 +02:00
|
|
|
}
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public static void unWatchRepository(final Context context, RepositoryContext repository) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Call<Void> call =
|
|
|
|
RetrofitClient.getApiInterface(context)
|
|
|
|
.userCurrentDeleteStar(repository.getOwner(), repository.getName());
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
call.enqueue(
|
|
|
|
new Callback<>() {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(
|
|
|
|
@NonNull Call<Void> call, @NonNull retrofit2.Response<Void> response) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (response.code() == 204) {
|
2019-10-12 11:00:36 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.success(
|
|
|
|
context, context.getString(R.string.unWatchRepositorySuccess));
|
|
|
|
} else if (response.code() == 401) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
AlertDialogs.authorizationTokenRevokedDialog(context);
|
|
|
|
} else if (response.code() == 403) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(context, context.getString(R.string.authorizeError));
|
|
|
|
} else if (response.code() == 404) {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.warning(context, context.getString(R.string.apiNotFound));
|
|
|
|
} else {
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(context, context.getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 22:03:01 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
|
2022-08-15 16:26:02 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
Toasty.error(
|
|
|
|
context, context.getString(R.string.genericServerResponseError));
|
|
|
|
}
|
|
|
|
});
|
2022-08-15 16:26:02 +02:00
|
|
|
}
|
2019-10-11 22:03:01 +02:00
|
|
|
}
|