Skip to content

Commit 638512e

Browse files
committed
Added Java SemVer for JS API
1 parent 939e6c1 commit 638512e

2 files changed

Lines changed: 9 additions & 72 deletions

File tree

AnkiDroid/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,7 @@ dependencies {
266266
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
267267
androidTestImplementation 'androidx.test:rules:1.2.0'
268268
androidTestImplementation 'org.smali:dexlib2:2.4.0'
269+
270+
//For AnkiDroid JS API Versioning
271+
implementation "com.github.zafarkhaja:java-semver:0.9.0"
269272
}

AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.java

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@
144144
import static com.ichi2.anki.cardviewer.ViewerCommand.*;
145145
import static com.ichi2.anki.reviewer.CardMarker.*;
146146

147+
import com.github.zafarkhaja.semver.Version;
148+
147149
@SuppressWarnings({"PMD.AvoidThrowingRawExceptionTypes","PMD.FieldDeclarationsShouldBeAtStartOfClass"})
148150
public abstract class AbstractFlashcardViewer extends NavigationDrawerActivity implements ReviewerUi, CommandProcessor {
149151

@@ -3492,85 +3494,17 @@ private void showDeveloperContact() {
34923494
snackbar.show();
34933495
}
34943496

3495-
/*
3496-
* AnkiDroid JavaScript API Class for major, minor, patch version eg: 1.2.3
3497-
* major version : 1
3498-
* minor version : 2
3499-
* patch version : 3
3500-
*
3497+
/**
35013498
* Supplied api version must be equal to current api version to call mark card, toggle flag functions etc.
3502-
* https://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java/27891752
35033499
*/
3504-
public class Version implements Comparable<Version> {
3505-
3506-
private String version;
3507-
3508-
public final String get() {
3509-
return this.version;
3510-
}
3511-
3512-
public Version(String version) {
3513-
if (version == null) {
3514-
throw new IllegalArgumentException("Version can not be null");
3515-
}
3516-
if (!version.matches("[0-9]+(\\.[0-9]+)*")) {
3517-
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.valid_js_api_version, sCurrentJsApiVersion), false);
3518-
throw new IllegalArgumentException("Invalid version format");
3519-
}
3520-
this.version = version;
3521-
}
3522-
3523-
@Override
3524-
public int compareTo(Version that) {
3525-
if (that == null) {
3526-
return 1;
3527-
}
3528-
3529-
String[] thisParts = this.get().split("\\.");
3530-
String[] thatParts = that.get().split("\\.");
3531-
int length = Math.max(thisParts.length, thatParts.length);
3532-
3533-
for (int i = 0; i < length; i++) {
3534-
int thisPart = i < thisParts.length ? Integer.parseInt(thisParts[i]) : 0;
3535-
int thatPart = i < thatParts.length ? Integer.parseInt(thatParts[i]) : 0;
3536-
3537-
if (thisPart < thatPart) {
3538-
return -1;
3539-
}
3540-
3541-
if (thisPart > thatPart) {
3542-
return 1;
3543-
}
3544-
}
3545-
return 0;
3546-
}
3547-
3548-
@Override
3549-
public boolean equals(Object that) {
3550-
if (this == that) {
3551-
return true;
3552-
}
3553-
3554-
if (that == null) {
3555-
return false;
3556-
}
3557-
3558-
if (this.getClass() != that.getClass()) {
3559-
return false;
3560-
}
3561-
3562-
return this.compareTo((Version) that) == 0;
3563-
}
3564-
}
3565-
35663500
private boolean requireApiVersion(String apiVer) {
35673501

3568-
Version mVersionCurrent = new Version(sCurrentJsApiVersion);
3569-
Version mVersionSupplied = new Version(apiVer);
3502+
Version mVersionCurrent = Version.valueOf(sCurrentJsApiVersion);
3503+
Version mVersionSupplied = Version.valueOf(apiVer);
35703504

35713505
if (mVersionCurrent.equals(mVersionSupplied)) {
35723506
return true;
3573-
} else if (mVersionCurrent.compareTo(mVersionSupplied) == 1) { // mCurrent > mSupplied
3507+
} else if (mVersionCurrent.greaterThan(mVersionSupplied)) {
35743508
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.update_js_api_version, sCurrentJsApiVersion), false);
35753509
return false;
35763510
} else {

0 commit comments

Comments
 (0)