2020-05-17 18:14:06 +02:00
|
|
|
package org.mian.gitnex.helpers;
|
|
|
|
|
2024-02-22 15:38:21 +01:00
|
|
|
import android.util.Log;
|
2020-06-11 15:31:11 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2020-05-17 18:14:06 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2022-04-25 07:42:52 +02:00
|
|
|
import java.util.Objects;
|
2020-05-17 18:14:06 +02:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
/**
|
2022-05-15 13:12:36 +02:00
|
|
|
* @author 6543
|
2020-05-17 18:14:06 +02:00
|
|
|
*/
|
|
|
|
public class Version {
|
|
|
|
|
|
|
|
// the raw String
|
|
|
|
private String raw;
|
|
|
|
// the version numbers in its order (dot separated)
|
|
|
|
private List<Integer> values;
|
2022-04-25 07:42:52 +02:00
|
|
|
private boolean dev;
|
2020-05-17 18:14:06 +02:00
|
|
|
|
|
|
|
public Version(String value) {
|
|
|
|
|
|
|
|
raw = value;
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
2020-09-04 16:33:37 +02:00
|
|
|
/**
|
|
|
|
* valid return true if string is a valid version
|
|
|
|
*
|
2024-03-28 13:02:17 +01:00
|
|
|
* @param value String
|
|
|
|
* @return true/false
|
2020-09-04 16:33:37 +02:00
|
|
|
*/
|
|
|
|
public static boolean valid(String value) {
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (value == null) {
|
2020-09-04 16:33:37 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
final Pattern patternValid =
|
2024-03-28 13:02:17 +01:00
|
|
|
Pattern.compile("^[vV]?(\\d+)+(\\.(\\d+))*([_\\-+][\\w\\-+.]*)?$");
|
2022-04-25 07:42:52 +02:00
|
|
|
return value.equals("main") || patternValid.matcher(value).find();
|
2020-09-04 16:33:37 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 18:14:06 +02:00
|
|
|
/**
|
2022-09-21 07:43:00 +02:00
|
|
|
* init parse and store values for other functions of an Version instance it use the raw
|
|
|
|
* variable as base
|
2020-05-17 18:14:06 +02:00
|
|
|
*/
|
|
|
|
private void init() {
|
|
|
|
|
2022-04-25 07:42:52 +02:00
|
|
|
final Pattern patternNumberDotNumber = Pattern.compile("^\\d+(\\.(\\d)+)*");
|
2020-05-17 18:14:06 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (raw.isEmpty()) {
|
2022-07-28 16:23:33 +02:00
|
|
|
raw = "0";
|
|
|
|
}
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (!valid(raw)) {
|
2024-02-22 15:38:21 +01:00
|
|
|
// throw new IllegalArgumentException("Invalid version format: " + raw);
|
|
|
|
Log.e("Version", "Invalid version format: " + raw);
|
2022-04-30 13:47:15 +02:00
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
if (raw.equals("main")) {
|
2022-04-25 07:42:52 +02:00
|
|
|
dev = true;
|
|
|
|
values = new ArrayList<>();
|
|
|
|
return;
|
2020-05-17 18:14:06 +02:00
|
|
|
}
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (raw.charAt(0) == 'v' || raw.charAt(0) == 'V') {
|
2020-05-17 18:14:06 +02:00
|
|
|
raw = raw.substring(1);
|
|
|
|
}
|
|
|
|
|
2022-04-25 07:42:52 +02:00
|
|
|
values = new ArrayList<>();
|
|
|
|
Matcher match = patternNumberDotNumber.matcher(raw);
|
2022-09-21 07:43:00 +02:00
|
|
|
if (!match.find()) {
|
2022-04-25 07:42:52 +02:00
|
|
|
dev = true;
|
|
|
|
values = new ArrayList<>();
|
|
|
|
return;
|
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
for (String i : match.group().split("\\.")) {
|
2020-05-17 18:14:06 +02:00
|
|
|
values.add(Integer.parseInt(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* equal return true if version is the same
|
|
|
|
*
|
2024-03-28 13:02:17 +01:00
|
|
|
* @param value String
|
2020-05-17 18:14:06 +02:00
|
|
|
* @return true/false
|
|
|
|
*/
|
|
|
|
public boolean equal(String value) {
|
|
|
|
|
|
|
|
return this.equal(new Version(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* equal return true if version is the same
|
|
|
|
*
|
2024-03-28 13:02:17 +01:00
|
|
|
* @param v Version
|
|
|
|
* @return true/false
|
2020-05-17 18:14:06 +02:00
|
|
|
*/
|
2020-06-11 15:31:11 +02:00
|
|
|
public boolean equal(@NonNull Version v) {
|
2020-05-17 18:14:06 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (dev || v.dev) { // equal if raw is equal
|
2022-04-25 07:42:52 +02:00
|
|
|
return Objects.equals(raw, v.raw);
|
|
|
|
}
|
|
|
|
|
2020-05-17 18:14:06 +02:00
|
|
|
int rounds = Math.min(this.values.size(), v.values.size());
|
2022-09-21 07:43:00 +02:00
|
|
|
for (int i = 0; i < rounds; i++) {
|
|
|
|
if (!Objects.equals(this.values.get(i), v.values.get(i))) {
|
2020-05-17 18:14:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* less return true if version is less
|
|
|
|
*
|
2024-03-28 13:02:17 +01:00
|
|
|
* @param value String
|
2020-05-17 18:14:06 +02:00
|
|
|
* @return true/false
|
|
|
|
*/
|
|
|
|
public boolean less(String value) {
|
|
|
|
|
|
|
|
return this.less(new Version(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* less return true if version is less
|
|
|
|
*
|
2024-03-28 13:02:17 +01:00
|
|
|
* @param v Version
|
|
|
|
* @return true/false
|
2020-05-17 18:14:06 +02:00
|
|
|
*/
|
2020-06-11 15:31:11 +02:00
|
|
|
public boolean less(@NonNull Version v) {
|
2020-05-17 18:14:06 +02:00
|
|
|
|
2020-07-14 18:20:00 +02:00
|
|
|
return v.higher(this);
|
2020-05-17 18:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* higher return true if version is higher
|
|
|
|
*
|
2024-03-28 13:02:17 +01:00
|
|
|
* @param value String
|
2020-05-17 18:14:06 +02:00
|
|
|
* @return true/false
|
|
|
|
*/
|
|
|
|
public boolean higher(String value) {
|
|
|
|
|
|
|
|
return this.higher(new Version(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* higher return true if version is higher
|
|
|
|
*
|
2024-03-28 13:02:17 +01:00
|
|
|
* @param v Version
|
|
|
|
* @return true/false
|
2020-05-17 18:14:06 +02:00
|
|
|
*/
|
2020-06-11 15:31:11 +02:00
|
|
|
public boolean higher(@NonNull Version v) {
|
2020-05-17 18:14:06 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (dev) {
|
2022-04-25 07:42:52 +02:00
|
|
|
return !v.dev;
|
2022-09-21 07:43:00 +02:00
|
|
|
} else if (v.dev) {
|
2022-04-25 07:42:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-17 18:14:06 +02:00
|
|
|
int rounds = Math.min(this.values.size(), v.values.size());
|
2022-09-21 07:43:00 +02:00
|
|
|
for (int i = 0; i < rounds; i++) {
|
|
|
|
if (i + 1 == rounds) {
|
|
|
|
if (this.values.get(i) <= v.values.get(i)) {
|
2020-05-17 18:14:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
} else {
|
|
|
|
if (this.values.get(i) < v.values.get(i)) {
|
2020-05-17 18:14:06 +02:00
|
|
|
return false;
|
2022-09-21 07:43:00 +02:00
|
|
|
} else if (this.values.get(i) > v.values.get(i)) {
|
2020-05-21 16:13:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-05-17 18:14:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lessOrEqual return true if version is less or equal
|
|
|
|
*
|
2024-03-28 13:02:17 +01:00
|
|
|
* @param value String
|
2020-05-17 18:14:06 +02:00
|
|
|
* @return true/false
|
|
|
|
*/
|
|
|
|
public boolean lessOrEqual(String value) {
|
|
|
|
|
|
|
|
return this.lessOrEqual(new Version(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lessOrEqual return true if version is less or equal
|
|
|
|
*
|
2024-03-28 13:02:17 +01:00
|
|
|
* @param v Version
|
|
|
|
* @return true/false
|
2020-05-17 18:14:06 +02:00
|
|
|
*/
|
2020-06-11 15:31:11 +02:00
|
|
|
public boolean lessOrEqual(@NonNull Version v) {
|
2020-05-17 18:14:06 +02:00
|
|
|
|
2020-07-14 18:20:00 +02:00
|
|
|
return v.higherOrEqual(this);
|
2020-05-17 18:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* higherOrEqual return true if version is higher or equal
|
|
|
|
*
|
2024-03-28 13:02:17 +01:00
|
|
|
* @param value String
|
2020-05-17 18:14:06 +02:00
|
|
|
* @return true/false
|
|
|
|
*/
|
|
|
|
public boolean higherOrEqual(String value) {
|
|
|
|
|
|
|
|
return this.higherOrEqual(new Version(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* higherOrEqual return true if version is higher or equal
|
|
|
|
*
|
2024-03-28 13:02:17 +01:00
|
|
|
* @param v Version
|
|
|
|
* @return true/false
|
2020-05-17 18:14:06 +02:00
|
|
|
*/
|
2020-06-11 15:31:11 +02:00
|
|
|
public boolean higherOrEqual(@NonNull Version v) {
|
2020-05-17 18:14:06 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (dev || v.dev) { // if one is a dev version, only true if both are dev
|
2022-04-25 07:42:52 +02:00
|
|
|
return v.dev && dev;
|
|
|
|
}
|
|
|
|
|
2020-05-17 18:14:06 +02:00
|
|
|
int rounds = Math.min(this.values.size(), v.values.size());
|
2022-09-21 07:43:00 +02:00
|
|
|
for (int i = 0; i < rounds; i++) {
|
|
|
|
if (this.values.get(i) > v.values.get(i)) {
|
2020-07-14 18:20:00 +02:00
|
|
|
return true;
|
2022-09-21 07:43:00 +02:00
|
|
|
} else if (this.values.get(i) < v.values.get(i)) {
|
2020-05-17 18:14:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@NonNull @Override
|
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
|
|
|
public String toString() {
|
|
|
|
|
|
|
|
return raw;
|
|
|
|
}
|
2020-06-11 15:31:11 +02:00
|
|
|
}
|