mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 01:18:35 +01:00
Improve server version parsing and add UI feedback
- Enhanced version parsing to handle Python/Django version formats more reliably - Added serverUpdateRequired AuthState for proper UI routing - Improved server version sanitization with better regex patterns - Added UpdateServerScreen with localized user feedback - Integrated into auth flow routing to show appropriate update messages
This commit is contained in:
@@ -762,6 +762,18 @@
|
||||
"@appUpdateTitle": {},
|
||||
"appUpdateContent": "This version of the app is not compatible with the server, please update your application.",
|
||||
"@appUpdateContent": {},
|
||||
"serverUpdateTitle": "Server update required",
|
||||
"@serverUpdateTitle": {},
|
||||
"serverUpdateContent": "The connected server is too old for this version of the app. Please ask the server administrator to update to at least version {minVersion}.",
|
||||
"@serverUpdateContent": {
|
||||
"description": "Message shown when the server version is too old. minVersion is the minimum required server version string.",
|
||||
"type": "text",
|
||||
"placeholders": {
|
||||
"minVersion": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
},
|
||||
"productFound": "Product found",
|
||||
"@productFound": {
|
||||
"description": "Header label for dialog when product is found with barcode"
|
||||
|
||||
@@ -61,6 +61,7 @@ import 'package:wger/screens/settings_plates_screen.dart';
|
||||
import 'package:wger/screens/splash_screen.dart';
|
||||
import 'package:wger/screens/trophy_screen.dart';
|
||||
import 'package:wger/screens/update_app_screen.dart';
|
||||
import 'package:wger/screens/update_server_screen.dart';
|
||||
import 'package:wger/screens/weight_screen.dart';
|
||||
import 'package:wger/theme/theme.dart';
|
||||
import 'package:wger/widgets/core/about.dart';
|
||||
@@ -144,6 +145,8 @@ class MainApp extends StatelessWidget {
|
||||
return HomeTabsScreen();
|
||||
case AuthState.updateRequired:
|
||||
return const UpdateAppScreen();
|
||||
case AuthState.serverUpdateRequired:
|
||||
return const UpdateServerScreen();
|
||||
default:
|
||||
return FutureBuilder(
|
||||
future: auth.tryAutoLogin(),
|
||||
|
||||
@@ -40,6 +40,7 @@ enum LoginActions {
|
||||
|
||||
enum AuthState {
|
||||
updateRequired,
|
||||
serverUpdateRequired,
|
||||
loggedIn,
|
||||
loggedOut,
|
||||
}
|
||||
@@ -118,31 +119,31 @@ class AuthProvider with ChangeNotifier {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Strip common non-semver suffixes emitted by Python/Django backends,
|
||||
// e.g. '2.5.0a2' → '2.5.0', '2.3.0 (git-abc1234)' → '2.3.0'.
|
||||
final sanitized = rawVersion
|
||||
.replaceFirst(RegExp(r'\s.*$'), '') // strip trailing space + anything after
|
||||
.replaceFirst(RegExp(r'[a-zA-Z].*$'), ''); // strip alpha/beta/rc suffix
|
||||
|
||||
final Version current;
|
||||
try {
|
||||
// Server versions may use Python-style pre-release suffixes such as
|
||||
// "2.5.0a2", "2.5.0b1", "2.5.0rc1", or appended build metadata like
|
||||
// "-beta" / " (git-abc123)". Extract only the leading numeric X.Y.Z part.
|
||||
final match = RegExp(r'^\d+\.\d+(\.\d+)?').firstMatch(rawVersion);
|
||||
if (match == null) {
|
||||
_logger.warning(
|
||||
'serverUpdateRequired: no numeric version found in "$rawVersion", skipping check',
|
||||
);
|
||||
return false;
|
||||
}
|
||||
final sanitized = match.group(0)!;
|
||||
final current = Version.parse(sanitized);
|
||||
final required = Version.parse(MIN_SERVER_VERSION);
|
||||
|
||||
final needUpdate = current < required;
|
||||
if (needUpdate) {
|
||||
_logger.fine('Server update required: server $current < minimum $required');
|
||||
}
|
||||
|
||||
return needUpdate;
|
||||
} on FormatException catch (e) {
|
||||
_logger.warning('serverUpdateRequired: could not parse server version "$rawVersion": $e');
|
||||
current = Version.parse(sanitized);
|
||||
} on FormatException {
|
||||
// Completely unparseable version string — be lenient and allow login.
|
||||
_logger.warning(
|
||||
'serverUpdateRequired: could not parse server version "$rawVersion" '
|
||||
'(sanitized: "$sanitized"), skipping check',
|
||||
);
|
||||
return false;
|
||||
}
|
||||
final required = Version.parse(MIN_SERVER_VERSION);
|
||||
|
||||
final needUpdate = current < required;
|
||||
if (needUpdate) {
|
||||
_logger.fine('Server update required: server $current < minimum $required');
|
||||
}
|
||||
|
||||
return needUpdate;
|
||||
}
|
||||
|
||||
/// Registers a new user
|
||||
@@ -226,10 +227,10 @@ class AuthProvider with ChangeNotifier {
|
||||
|
||||
await initVersions(serverUrl);
|
||||
|
||||
// If the server is too old for this app version, log out silently so the
|
||||
// user lands back on the auth screen and can connect to a different server.
|
||||
// If the server is too old for this app version, surface the dedicated
|
||||
// screen so the user knows what to do (ask their server admin to upgrade).
|
||||
if (serverUpdateRequired()) {
|
||||
await logout(shouldNotify: false);
|
||||
state = AuthState.serverUpdateRequired;
|
||||
return LoginActions.update;
|
||||
}
|
||||
|
||||
@@ -310,10 +311,11 @@ class AuthProvider with ChangeNotifier {
|
||||
|
||||
await initVersions(serverUrl!);
|
||||
|
||||
// If the server is too old for this app version, log out silently so the
|
||||
// user lands back on the auth screen and can connect to a different server.
|
||||
// If the server is too old for this app version, surface the dedicated
|
||||
// screen so the user knows what to do (ask their server admin to upgrade).
|
||||
if (serverUpdateRequired()) {
|
||||
await logout();
|
||||
state = AuthState.serverUpdateRequired;
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/helpers/errors.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/screens/update_app_screen.dart';
|
||||
import 'package:wger/screens/update_server_screen.dart';
|
||||
import 'package:wger/theme/theme.dart';
|
||||
import 'package:wger/widgets/auth/api_token_field.dart';
|
||||
import 'package:wger/widgets/auth/email_field.dart';
|
||||
@@ -199,16 +200,20 @@ class _AuthCardState extends State<AuthCard> {
|
||||
);
|
||||
}
|
||||
|
||||
// Only push the app-update screen for a genuine app update requirement.
|
||||
// If the server was too old, login() already logged out silently and the
|
||||
// user stays here to connect to a different server — no screen push needed.
|
||||
if (res == LoginActions.update &&
|
||||
context.read<AuthProvider>().state == AuthState.updateRequired &&
|
||||
mounted) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (context) => const UpdateAppScreen()),
|
||||
);
|
||||
return;
|
||||
// Navigate to the appropriate "update required" screen.
|
||||
if (res == LoginActions.update && mounted) {
|
||||
final authState = context.read<AuthProvider>().state;
|
||||
if (authState == AuthState.updateRequired) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (context) => const UpdateAppScreen()),
|
||||
);
|
||||
return;
|
||||
} else if (authState == AuthState.serverUpdateRequired) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (context) => const UpdateServerScreen()),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
|
||||
52
lib/screens/update_server_screen.dart
Normal file
52
lib/screens/update_server_screen.dart
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
|
||||
/// Screen shown when the connected server's version is below the minimum
|
||||
/// required by this build of the app. Mirrors [UpdateAppScreen] but surfaces
|
||||
/// to the user that they need to ask their server administrator to upgrade,
|
||||
/// rather than updating the app itself.
|
||||
class UpdateServerScreen extends StatelessWidget {
|
||||
const UpdateServerScreen({super.key});
|
||||
|
||||
static const routeName = '/update-server';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final i18n = AppLocalizations.of(context);
|
||||
|
||||
return Scaffold(
|
||||
body: AlertDialog(
|
||||
title: Text(
|
||||
i18n.serverUpdateTitle,
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
content: Text(i18n.serverUpdateContent(MIN_SERVER_VERSION)),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: Text(MaterialLocalizations.of(context).closeButtonLabel),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user