mirror of
https://github.com/zapstore/zapstore.git
synced 2026-10-05 20:48:24 +00:00
Improve version comparison and add guard
This commit is contained in:
@@ -165,11 +165,11 @@ mixin LocalAppAdapter on Adapter<LocalApp> {
|
||||
? installedVersionCode + 2000
|
||||
: installedVersionCode;
|
||||
comp = app.latestMetadata!.versionCode!.compareTo(code);
|
||||
}
|
||||
|
||||
// If versionCode was not available or not enough, compare string version
|
||||
if (comp == 0) {
|
||||
comp = compareVersions(installedVersion, app.latestMetadata!.version!);
|
||||
} else {
|
||||
// If versionCode was not available, compare string version
|
||||
try {
|
||||
comp = compareVersions(installedVersion, app.latestMetadata!.version!);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
if (comp == 1) return AppInstallStatus.updatable;
|
||||
|
||||
+16
-16
@@ -1,36 +1,36 @@
|
||||
import 'dart:math';
|
||||
|
||||
int compareVersions(String version1, String version2) {
|
||||
int compareVersions(String installedVersion, String latestFromRemoteVersion) {
|
||||
// Split the versions into main and pre-release components
|
||||
final parts1 = version1.split('-');
|
||||
final parts2 = version2.split('-');
|
||||
final parts1 = installedVersion.split('-');
|
||||
final parts2 = latestFromRemoteVersion.split('-');
|
||||
|
||||
// Split the main version into numeric parts
|
||||
final mainVersion1 = parts1[0].split('.').map(int.parse).toList();
|
||||
final mainVersion2 = parts2[0].split('.').map(int.parse).toList();
|
||||
final mainVersion1 = parts1[0].split('.').map(int.tryParse).toList();
|
||||
final mainVersion2 = parts2[0].split('.').map(int.tryParse).toList();
|
||||
|
||||
// Compare the main version parts
|
||||
for (var i = 0; i < max(mainVersion1.length, mainVersion2.length); i++) {
|
||||
final v1 = i < mainVersion1.length ? mainVersion1[i] : 0;
|
||||
final v2 = i < mainVersion2.length ? mainVersion2[i] : 0;
|
||||
final v1 = i < mainVersion1.length ? (mainVersion1[i] ?? 0) : 0;
|
||||
final v2 = i < mainVersion2.length ? (mainVersion2[i] ?? 0) : 0;
|
||||
|
||||
if (v1 < v2) return 1; // version2 is higher
|
||||
if (v1 > v2) return -1; // version1 is higher
|
||||
if (v1 < v2) return 1; // upgrade
|
||||
if (v1 > v2) return -1; // downgrade
|
||||
}
|
||||
|
||||
// If main versions are equal, compare pre-release versions if they exist
|
||||
final preRelease1 = parts1.length > 1 ? parts1[1] : '';
|
||||
final preRelease2 = parts2.length > 1 ? parts2[1] : '';
|
||||
|
||||
if (preRelease1.isEmpty && preRelease2.isNotEmpty)
|
||||
return -1; // version2 is higher
|
||||
if (preRelease2.isEmpty && preRelease1.isNotEmpty)
|
||||
return 1; // version1 is higher
|
||||
|
||||
if (preRelease1.isEmpty && preRelease2.isEmpty) return 0; // both are equal
|
||||
if (preRelease1.isEmpty && preRelease2.isNotEmpty) {
|
||||
return -1; // downgrade
|
||||
}
|
||||
if (preRelease2.isEmpty && preRelease1.isNotEmpty) {
|
||||
return 1; // upgrade
|
||||
}
|
||||
|
||||
// Compare pre-release versions lexicographically
|
||||
final comparison = preRelease1.compareTo(preRelease2);
|
||||
final comparison = preRelease2.compareTo(preRelease1);
|
||||
|
||||
return comparison < 0
|
||||
? -1
|
||||
|
||||
@@ -6,13 +6,14 @@ void main() {
|
||||
test('returns correct comparison', () {
|
||||
expect(compareVersions("1.2.3", "1.2.3"), 0);
|
||||
expect(compareVersions("1.2.3", "1.2.3-alpha"), -1);
|
||||
expect(compareVersions("1.2.3-alpha", "1.2.3-beta"), -1);
|
||||
expect(compareVersions("1.2.3-beta", "1.2.3-alpha"), 1);
|
||||
expect(compareVersions("1.2.3-alpha", "1.2.3-beta"), 1);
|
||||
expect(compareVersions("1.2.3-beta", "1.2.3-alpha"), -1);
|
||||
expect(compareVersions("1.9.0", "1.10.0"), 1);
|
||||
expect(compareVersions("1.26.8", "1.27.2"), 1);
|
||||
expect(compareVersions("0.2.7", "0.2.11"), 1);
|
||||
expect(compareVersions("0.2.11", "0.2.7"), -1);
|
||||
expect(compareVersions("2024.9", "2024.10-beta2"), 1);
|
||||
expect(compareVersions("a.x", "b.y"), 0);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user