diff --git a/lib/helpers/consts.dart b/lib/helpers/consts.dart index c7cecfbd1..a37ab849f 100644 --- a/lib/helpers/consts.dart +++ b/lib/helpers/consts.dart @@ -70,3 +70,6 @@ const ENERGY_CARBOHYDRATES = 4; /// kcal per gram of fat (approx) const ENERGY_FAT = 9; + +/// Flag to check for updates to the new version. +const ENABLED_UPDATE = false; diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index eea7b645e..6c9235b75 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -495,5 +495,7 @@ "dataCopied": "Data copied to new entry", "@dataCopied": { "description": "Snackbar message to show on copying data to a new log entry" - } + }, + "appUpdateTitle" : "App Update Available", + "appUpdateContent" : "The newer version of the app is availibale. Please update your application." } diff --git a/lib/main.dart b/lib/main.dart index e5420b068..c58abe57c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -38,6 +38,7 @@ import 'package:wger/screens/nutritional_diary_screen.dart'; import 'package:wger/screens/nutritional_plan_screen.dart'; import 'package:wger/screens/nutritional_plans_screen.dart'; import 'package:wger/screens/splash_screen.dart'; +import 'package:wger/screens/update_app_screen.dart'; import 'package:wger/screens/weight_screen.dart'; import 'package:wger/screens/workout_plan_screen.dart'; import 'package:wger/screens/workout_plans_screen.dart'; @@ -103,7 +104,13 @@ class MyApp extends StatelessWidget { title: 'wger', theme: wgerTheme, home: auth.isAuth - ? HomeTabsScreen() + ? FutureBuilder( + future: auth.neededApplicationUpdate(), + builder: (ctx, snapshot) => + snapshot.connectionState == ConnectionState.done && snapshot.data == true + ? UpdateAppScreen() + : HomeTabsScreen(), + ) : FutureBuilder( future: auth.tryAutoLogin(), builder: (ctx, authResultSnapshot) => diff --git a/lib/providers/auth.dart b/lib/providers/auth.dart index 3322a9a11..0ec6958e6 100644 --- a/lib/providers/auth.dart +++ b/lib/providers/auth.dart @@ -28,6 +28,7 @@ import 'package:flutter/widgets.dart'; import 'package:http/http.dart' as http; import 'package:package_info/package_info.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:version/version.dart'; import 'package:wger/exceptions/http_exception.dart'; import 'package:wger/helpers/consts.dart'; @@ -76,6 +77,18 @@ class AuthProvider with ChangeNotifier { applicationVersion = packageInfo; } + /// Checking if there is a new version of the application. + Future neededApplicationUpdate() async { + if (!ENABLED_UPDATE) { + return false; + } + final response = await http.get(makeUri(serverUrl!, 'min-app-version')); + final applicationLatestVersion = json.decode(response.body); + final currentVersion = Version.parse(applicationVersion!.version); + final latestAppVersion = Version.parse(applicationLatestVersion); + return latestAppVersion > currentVersion; + } + /// Registers a new user Future register({ required String username, diff --git a/lib/screens/update_app_screen.dart b/lib/screens/update_app_screen.dart new file mode 100644 index 000000000..d0a6a560c --- /dev/null +++ b/lib/screens/update_app_screen.dart @@ -0,0 +1,36 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (C) 2020, 2021 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; + +class UpdateAppScreen extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Scaffold( + body: AlertDialog( + title: Text( + AppLocalizations.of(context).appUpdateTitle, + style: TextStyle(fontSize: 24), + ), + content: Text(AppLocalizations.of(context).appUpdateContent), + actions: null, + ), + ); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 18d72ef75..2aaf592c4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -44,6 +44,7 @@ dependencies: image_picker: ^0.8.4 intl: ^0.17.0 json_annotation: ^4.0.1 + version: ^2.0.0 package_info: ^2.0.2 provider: ^5.0.0 shared_preferences: ^2.0.7