-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The app now notifies the user on the homescreen when a new version is available.
- Loading branch information
Showing
8 changed files
with
246 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'github_tag.g.dart'; | ||
|
||
// To build the code generateion run: | ||
// flutter pub run build_runner build | ||
@JsonSerializable() | ||
class Tag { | ||
String name; | ||
|
||
Tag({required this.name}); | ||
|
||
factory Tag.fromJSON(Map<String, dynamic> json) => _$TagFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$TagToJson(this); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Model for the very popular semantic versioning | ||
// https://semver.org/ | ||
class Semver implements Comparable { | ||
int major; | ||
int minor; | ||
int patch; | ||
|
||
Semver(this.major, this.minor, this.patch); | ||
|
||
static Semver parse(String text) { | ||
List<String> parts = text.split("."); | ||
|
||
if (parts.length != 3) | ||
throw "Semanticversining Validation Error (not enough numbers)"; | ||
|
||
return Semver( | ||
int.parse(parts[0]), | ||
int.parse(parts[1]), | ||
int.parse(parts[2]), | ||
); | ||
} | ||
|
||
@override | ||
int compareTo(dynamic other) { | ||
if (this.major < other.major) return -1; | ||
if (this.major > other.major) return 1; | ||
|
||
if (this.minor < other.minor) return -1; | ||
if (this.minor > other.minor) return 1; | ||
|
||
if (this.patch < other.patch) return -1; | ||
if (this.patch > other.patch) return 1; | ||
|
||
return 0; | ||
} | ||
|
||
@override | ||
String toString() { | ||
return "$major.$minor.$patch"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:package_info_plus/package_info_plus.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
import 'package:tu_wien_addressbook/models/github_tag.dart'; | ||
import 'package:tu_wien_addressbook/models/semver.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
/// This class is used to check with GitHub if there is a new version available. | ||
/// To achive its goal the class gets the name of the latest | ||
/// tag (semantic versioning) and compares it to its own tag. If the tag on | ||
/// GitHub is higher, it means that there is a new version. | ||
class UpdateManager { | ||
UpdateManager(); | ||
|
||
Future<bool> hasNewerVersion() async { | ||
PackageInfo packageInfo = await PackageInfo.fromPlatform(); | ||
Semver currentVersion = Semver.parse(packageInfo.version); | ||
|
||
print("Downloading version from GitHub..."); | ||
Uri url = Uri.parse( | ||
"https://api.github.com/repos/flofriday/TU_Wien_Addressbook/tags"); | ||
var response = await http.get(url); | ||
if (response.statusCode != 200) { | ||
// Ok the either the internet / service is currently not availabel, or | ||
// we got ratelimited whatever. Just do not update the cache and | ||
// say that there is no version available. | ||
print( | ||
'Requesting the version from GitHub returned ${response.statusCode}'); | ||
return false; | ||
} | ||
|
||
List<Tag> tags = (jsonDecode(response.body) as List) | ||
.map((data) => Tag.fromJSON(data)) | ||
.toList(); | ||
|
||
// Check if there are maybe just no versions. | ||
if (tags.isEmpty) return false; | ||
|
||
// Parse the newest available version and add it to the cache | ||
Semver updateVersion = Semver.parse(tags[0].name); | ||
print("GitHub version: ${updateVersion.toString()}"); | ||
print("Current version: ${currentVersion.toString()}"); | ||
|
||
return currentVersion.compareTo(updateVersion) < 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters