From f48ac15f1e0ea04f7d8c091291dd2ae81d608a57 Mon Sep 17 00:00:00 2001 From: Petschko Date: Fri, 26 Feb 2021 17:23:57 +0100 Subject: [PATCH] Added check to not notify if newest version is lower than current (for example due to cache) --- src/org/petschko/lib/update/Update.java | 3 +- src/org/petschko/lib/update/Version.java | 47 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/org/petschko/lib/update/Update.java b/src/org/petschko/lib/update/Update.java index ad34c40..ce91dab 100644 --- a/src/org/petschko/lib/update/Update.java +++ b/src/org/petschko/lib/update/Update.java @@ -179,7 +179,8 @@ private void checkVersion() { if(this.newestVersion == null) return; - this.hasNewVersion = ! this.currentVersion.versionsEqual(this.newestVersion); + if(! this.currentVersion.versionsEqual(this.newestVersion)) + this.hasNewVersion = this.currentVersion.thisIsLowerThan(this.newestVersion); } /** diff --git a/src/org/petschko/lib/update/Version.java b/src/org/petschko/lib/update/Version.java index ad5a8dc..dd58036 100644 --- a/src/org/petschko/lib/update/Version.java +++ b/src/org/petschko/lib/update/Version.java @@ -42,4 +42,51 @@ public String getVersion() { public boolean versionsEqual(Version version) { return this.version.equals(version.getVersion()); } + + /** + * Checks if this Version is lower than the other Version + * + * @param version - Version to compare + * @return - This Version is lower than the other + */ + public boolean thisIsLowerThan(Version version) { + String[] thisVersion = this.version.split("\\."); + String[] otherVersion = version.version.split("\\."); + + // Ensure same length arrays + if(thisVersion.length > otherVersion.length) { + String[] tmp = new String[thisVersion.length]; + + for(int i = 0; i < tmp.length; i++) { + if(i < otherVersion.length) + tmp[i] = otherVersion[i]; + else + tmp[i] = "0"; + } + + otherVersion = tmp; + } else if(otherVersion.length > thisVersion.length) { + String[] tmp = new String[otherVersion.length]; + + for(int i = 0; i < tmp.length; i++) { + if(i < thisVersion.length) + tmp[i] = thisVersion[i]; + else + tmp[i] = "0"; + } + + thisVersion = tmp; + } + + // Compare Versions + for(int n = 0; n < thisVersion.length; n++) { + int thisNumber = Integer.parseInt(thisVersion[n]); + int otherNumber = Integer.parseInt(otherVersion[n]); + + if(thisNumber < otherNumber) + return true; + } + + return false; + } }