Skip to content

Commit

Permalink
Fixed problems with updater
Browse files Browse the repository at this point in the history
* since it opened the url in the app instead of in the browser and this seems to not work
* fixed versioning check, because latest tag might not be latest published release, so we should extract the version of the tagname from the release
  • Loading branch information
susch19 committed Oct 23, 2023
1 parent f5bfc4c commit 7416bbf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lib/helper/helper_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import 'package:url_launcher/url_launcher_string.dart';

class HelperMethods {
static void openUrl(final String url) async {
await launchUrlString(url);
await launchUrlString(url, mode: LaunchMode.externalApplication);
}
}
51 changes: 18 additions & 33 deletions lib/helper/update_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final versionAndUrlProvider = StateNotifierProvider<UpdateManager, VersionAndUrl
});

class UpdateManager extends StateNotifier<VersionAndUrl?> {
static final Version version = Version(1, 2, 2);
static final Version version = Version(1, 2, 3);
static const int checkEveryHours = 16;
static final GitHub gitHub = GitHub();
static final RepositorySlug repositorySlug = RepositorySlug("susch19", "SmartHome");
Expand Down Expand Up @@ -115,51 +115,36 @@ class UpdateManager extends StateNotifier<VersionAndUrl?> {
return v;
}

static Future<bool> isNewVersionAvailable() async {
final newVersion = await _getNewVersion();
return _isNewVersionAvailable(newVersion);
}

static Future<bool> _isNewVersionAvailable(final Version? newVersion) async {
return newVersion != null && newVersion > version;
}

static Future<VersionAndUrl?> getVersionAndUrl() async {
final v = await _getNewVersion();
if (v == null) return null;
final release = await gitHub.repositories.getLatestRelease(repositorySlug);
Version latestVersion;
try {
latestVersion = Version.parse(release.tagName!.replaceFirst(versionRegExp, ''));
} catch (e) {
return null;
}

final newVersionAvailable = await _isNewVersionAvailable(v);
final asset = _extractUrl(release);

if (newVersionAvailable) {
return VersionAndUrl(v, false, await _newVersionUrl());
if (asset != null && latestVersion > version) {
return VersionAndUrl(latestVersion, false, asset.browserDownloadUrl);
} else {
return VersionAndUrl(v, true, null);
return VersionAndUrl(latestVersion, true, null);
}
}

static Future<Version?> _getNewVersion() {
return gitHub.repositories.listTags(repositorySlug).asyncMap((final element) {
try {
return Version.parse(element.name.replaceFirst(versionRegExp, ''));
} catch (e) {
// TODO: log
return null;
}
}).firstWhere((final element) => (element ?? Version(0, 0, 0)) > version, orElse: () => version);
}

static Future<String?> _newVersionUrl() async {
final release = await gitHub.repositories.getLatestRelease(repositorySlug);
static ReleaseAsset? _extractUrl(final Release release) {
return release.assets?.firstWhere((final element) {
if (element.name == null) return false;
if (Platform.isAndroid) {
return element.contentType == "application/vnd.android.package-archive";
return element.name!.contains("Android");
} else if (Platform.isWindows) {
return element.contentType == "application/zip";
return element.name!.contains("Windows");
} else if (Platform.isLinux) {
return element.contentType == "application/gzip";
return element.name!.contains("Linux");
} else {
return false;
}
}).browserDownloadUrl;
});
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: Controlling Smarthome devices with AppBroker.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# Read more about versioning at semver.org.
version: 1.2.2+0
version: 1.2.3+0

environment:
sdk: ">=2.17.0"
Expand Down

0 comments on commit 7416bbf

Please sign in to comment.