Skip to content

Instantly share code, notes, and snippets.

@loonix
Created March 8, 2021 08:57
Show Gist options
  • Save loonix/30a2330fe94bbfd105000517db316253 to your computer and use it in GitHub Desktop.
Save loonix/30a2330fe94bbfd105000517db316253 to your computer and use it in GitHub Desktop.
Control version from api and local version.
import 'package:package_info/package_info.dart';
/// Helper functions for version handling.
/// Note: For functions that only manipulate version conversions/formatting
class VersionHandler {
/// Validates a version object and returns true or false, e.g. major: 1, minor: 0, patch: 1 returns true
static bool isValidVersionObject(obj) {
return (obj.major is int && obj.minor is int && obj.patch is int && (obj.major != null && obj.minor != null && obj.patch != null) && (obj.major != 0 || obj.minor != 0 || obj.patch != 0));
}
/// Converts a version string
/// Example:
/// '2.4.1' to an object with:
/// - major (2)
/// - minor (4)
/// - patch (1)
static AppVersion parseVersionString(str) {
if (!(str is String)) {
debugPrint('version string $str has invalid format');
throw 'version string ' + str + ' has invalid format';
}
var x = str.split('.');
var maj = int.parse(x[0], radix: 10);
var min = int.parse(x[1], radix: 10);
var pat = int.parse(x[2], radix: 10);
var versionObj = new AppVersion(major: maj, minor: min, patch: pat);
if (!isValidVersionObject(versionObj)) {
debugPrint('version string $str has invalid format');
throw 'version string ' + str + ' has invalid format';
}
return versionObj;
}
/// Gets the [appVersion] and the [apiVersion] and checks if there is a new version to trigger the out of date warning
static bool outdatedVersion(String appVersion, String apiVersion) {
AppVersion runningVersion = parseVersionString(appVersion);
AppVersion latestVersion = parseVersionString(apiVersion);
if (runningVersion.major > latestVersion.major) {
return false;
}
// 'A major new update is available!'
if (runningVersion.major < latestVersion.major) {
debugPrint('A major new update is available!');
return true;
}
// Running version is a minor upgrade over the minimum version
if (runningVersion.minor > latestVersion.minor) {
debugPrint('Running version is a minor upgrade over the minimum version');
return false;
}
// 'A new minor update is available.'
if (runningVersion.minor < latestVersion.minor) {
debugPrint('A new minor update is available.');
return true;
}
// Running version is a patch upgrade over the minimum version
if (runningVersion.patch > latestVersion.patch) {
debugPrint('Running version is a patch upgrade over the minimum version');
return false;
}
// 'A new patch update is available.'
if (runningVersion.patch < latestVersion.patch) {
debugPrint('A new patch update is available.');
return true;
}
// 'We are running the latest version! No need to update.'
debugPrint('We are running the latest version! No need to update.');
return false;
}
/// Deals with the version number if there are special versions with dashes
/// Examples:
/// [number] = 1.0.0-dev -> output: 1.0.0
static String formatVersionNumber(number) {
var splitString = number.split('-');
return splitString[0];
}
/// Gets the app current version number
static Future<String> getCurrentAppVersion() async {
var package = await PackageInfo.fromPlatform();
return formatVersionNumber(package.version);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment