Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
+ download nuova versione in base alla piattaforma;
  • Loading branch information
mikyll committed Aug 5, 2023
1 parent 08ad7d0 commit e42911a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
42 changes: 35 additions & 7 deletions app-mobile/flutter_application/lib/model/AppUpdater.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:roquiz/model/PlatformType.dart';

class AppUpdater {
static Future<(bool, String, String)> checkNewVersion(
String currentVersion) async {
bool newVersionPresent = false;
String newVersion = "";
String newVersionDownloadURL = "";

http.Response response = await http.get(Uri.parse(
'https://api.github.com/repos/mikyll/ROQuiz/releases/latest'));

try {
Map<String, dynamic> json = jsonDecode(response.body);
String tag_name = json['tag_name'];
List<String> repoVersion = tag_name.replaceAll("v", "").split(".");
String tagName = json['tag_name'];
List<String> repoVersion = tagName.replaceAll("v", "").split(".");

int repoMajor = int.parse(repoVersion[0]);
int repoMinor = int.parse(repoVersion[1]);
Expand All @@ -19,16 +25,38 @@ class AppUpdater {
int currentMajor = int.parse(splittedCurrentVersion[0]);
int currentMinor = int.parse(splittedCurrentVersion[1]);

if (currentMajor < repoMajor ||
(currentMajor == repoMajor && currentMinor < repoMinor)) {
// retrieve asset link from APIs
newVersionPresent = currentMajor < repoMajor ||
(currentMajor == repoMajor && currentMinor < repoMinor);

if (newVersionPresent) {
newVersion = tagName;

return (true, tag_name, "asset link");
// Retrieve asset link from APIs
List<dynamic> assets = json['assets'];
for (dynamic asset in assets) {
String name = asset['name'];
String downloadUrl = asset['browser_download_url'];
if (getPlatformType() != PlatformType.WEB) {
if (Platform.isAndroid && name.toLowerCase().contains('android')) {
newVersionDownloadURL = downloadUrl;
} else if (Platform.isWindows &&
name.toLowerCase().contains('windows')) {
newVersionDownloadURL = downloadUrl;
} else if (Platform.isLinux &&
name.toLowerCase().contains('linux')) {
newVersionDownloadURL = downloadUrl;
}
}
}
}
} catch (e) {
print("Error: $e");
rethrow;
}
return (false, "", "");

print(
"New version: $newVersionPresent\nVersion tag name: $newVersion\nVersion download URL: $newVersionDownloadURL");

return (newVersionPresent, newVersion, newVersionDownloadURL);
}
}
2 changes: 1 addition & 1 deletion app-mobile/flutter_application/lib/views/ViewMenu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ViewMenuState extends State<ViewMenu> {
context,
"Nuova Versione App",
"È stata trovata una versione più recente dell'applicazione.\n"
"Versione attuale: ${Settings.VERSION_NUMBER}\n"
"Versione attuale: v${Settings.VERSION_NUMBER}\n"
"Nuova versione: $newVersion\n"
"Scaricare la nuova versione?",
() {
Expand Down
2 changes: 1 addition & 1 deletion app-mobile/flutter_application/lib/views/ViewSettings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ViewSettingsState extends State<ViewSettings> {
context,
"Nuova Versione App",
"È stata trovata una versione più recente dell'applicazione.\n"
"Versione attuale: ${Settings.VERSION_NUMBER}\n"
"Versione attuale: v${Settings.VERSION_NUMBER}\n"
"Nuova versione: $newVersion\n"
"Scaricare la nuova versione?",
"Sì",
Expand Down

0 comments on commit e42911a

Please sign in to comment.