Skip to content

Commit

Permalink
Merge pull request #450 from FineFindus/refactor/version-checking
Browse files Browse the repository at this point in the history
refactor(Instance): improve compatible version checking
  • Loading branch information
LucasGGamerM authored Jul 6, 2024
2 parents 857d0ce + 31a52c2 commit 3265cfe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void clearImage(int index){
public void onClick(){
//TODO: enable timeline for all servers once 4.3.0 is released
if(getInstance().isEmpty() ||
!getInstance().get().version.contains("4.3.0")){
!getInstance().get().checkVersion(4,3,0)){
UiUtils.launchWebBrowser(getActivity(), item.url);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.joinmastodon.android.model;

import android.text.Html;
import android.util.Log;

import org.joinmastodon.android.api.ObjectValidationException;
import org.joinmastodon.android.api.RequiredField;
import org.joinmastodon.android.model.catalog.CatalogInstance;
import org.parceler.Parcel;

import java.net.IDN;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -166,6 +168,31 @@ public boolean hasFeature(Feature feature) {
.orElse(false);
};
}
/**
* Returns true if the instance version is the same as or newer than the passed in version.
* @param major: The major version to check for.
* @param minor: the minor version to check for.
* @param patch: The patch version to check for.
*/
public boolean checkVersion(int major, int minor, int patch) {
try{
String[] parts=version.split("-", 2);
String[] numbers=parts[0].split("\\.", 3);
if(numbers.length < 3)
throw new IllegalArgumentException("Invalid version format. Expected format: major.minor.micro");

int majorVersion=Integer.parseInt(numbers[0]);
int minorVersion=Integer.parseInt(numbers[1]);
int patchVersion=Integer.parseInt(numbers[2]);
return (majorVersion > major ||
(majorVersion == major && minorVersion > minor) ||
(majorVersion == major && minorVersion == minor &&
patchVersion>= patch));
} catch(Exception e) {
Log.w("Instance", "checkVersion: failed to parse " + version + ", " + e);
return false;
}
}

public enum Feature {
BUBBLE_TIMELINE,
Expand Down

0 comments on commit 3265cfe

Please sign in to comment.