Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Ogundoyin Toluwani - <https://github.com/Tolu007>
- Nenza Nurfirmansyah - <https://github.com/nenzan>
- Florian Schmitz - <https://github.com/floodoo>
- Aman Negi - <https://github.com/AmanNegi>
- Sandi Milohanic - <https://github.com/sandimilohanic>

## Translators
Expand Down
15 changes: 12 additions & 3 deletions lib/helpers/ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ void showHttpExceptionErrorDialog(WgerHttpException exception, BuildContext cont
final List<Widget> errorList = [];
for (final key in exception.errors!.keys) {
// Error headers
errorList.add(Text(key, style: const TextStyle(fontWeight: FontWeight.bold)));
// Ensure that the error heading first letter is capitalized.
final String errorHeaderMsg = key[0].toUpperCase() + key.substring(1, key.length);

errorList.add(
Text(
errorHeaderMsg,
style: const TextStyle(fontWeight: FontWeight.bold),
),
);

// Error messages
if (exception.errors![key] is String) {
Expand All @@ -74,6 +82,7 @@ void showHttpExceptionErrorDialog(WgerHttpException exception, BuildContext cont
builder: (ctx) => AlertDialog(
title: Text(AppLocalizations.of(ctx).anErrorOccurred),
content: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [...errorList],
),
Expand All @@ -94,7 +103,7 @@ void showHttpExceptionErrorDialog(WgerHttpException exception, BuildContext cont
}

dynamic showDeleteDialog(BuildContext context, String confirmDeleteName, Log log, Exercise exercise,
Map<Exercise, List<Log>> _exerciseData) async {
Map<Exercise, List<Log>> exerciseData) async {
final res = await showDialog(
context: context,
builder: (BuildContext contextDialog) {
Expand All @@ -113,7 +122,7 @@ dynamic showDeleteDialog(BuildContext context, String confirmDeleteName, Log log
style: TextStyle(color: Theme.of(context).errorColor),
),
onPressed: () {
_exerciseData[exercise]!.removeWhere((el) => el.id == log.id);
exerciseData[exercise]!.removeWhere((el) => el.id == log.id);
Provider.of<WorkoutPlansProvider>(context, listen: false).deleteLog(
log,
);
Expand Down
18 changes: 16 additions & 2 deletions lib/providers/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class AuthProvider with ChangeNotifier {
}

/// Registers a new user
Future<void> register(
Future<Map<String, String>> register(
{required String username,
required String password,
required String email,
Expand All @@ -124,14 +124,21 @@ class AuthProvider with ChangeNotifier {
throw WgerHttpException(responseData);
}

// If update is required don't log in user
final bool updateRequired = await applicationUpdateRequired();
if (updateRequired) {
return {'action': 'update'};
}

login(username, password, serverUrl);
return {'action': 'proceed'};
} catch (error) {
rethrow;
}
}

/// Authenticates a user
Future<void> login(String username, String password, String serverUrl) async {
Future<Map<String, String>> login(String username, String password, String serverUrl) async {
await logout(shouldNotify: false);

try {
Expand All @@ -149,6 +156,12 @@ class AuthProvider with ChangeNotifier {
throw WgerHttpException(responseData);
}

// If update is required don't log in user
final bool updateRequired = await applicationUpdateRequired();
if (updateRequired) {
return {'action': 'update'};
}

// Log user in
this.serverUrl = serverUrl;
token = responseData['token'];
Expand All @@ -169,6 +182,7 @@ class AuthProvider with ChangeNotifier {
await setApplicationVersion();
prefs.setString('userData', userData);
prefs.setString('lastServer', serverData);
return {'action': 'proceed'};
} catch (error) {
rethrow;
}
Expand Down
36 changes: 25 additions & 11 deletions lib/screens/auth_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:wger/exceptions/http_exception.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/helpers/misc.dart';
import 'package:wger/helpers/ui.dart';
import 'package:wger/screens/update_app_screen.dart';

import '../providers/auth.dart';

Expand Down Expand Up @@ -139,19 +140,30 @@ class _AuthCardState extends State<AuthCard> {

try {
// Login existing user
late Map<String, String> res;
if (_authMode == AuthMode.Login) {
await Provider.of<AuthProvider>(context, listen: false)
res = await Provider.of<AuthProvider>(context, listen: false)
.login(_authData['username']!, _authData['password']!, _authData['serverUrl']!);

// Register new user
} else {
await Provider.of<AuthProvider>(context, listen: false).register(
res = await Provider.of<AuthProvider>(context, listen: false).register(
username: _authData['username']!,
password: _authData['password']!,
email: _authData['email']!,
serverUrl: _authData['serverUrl']!);
}

// Check if update is required else continue normally
if (res.containsKey('action')) {
if (res['action'] == 'update' && mounted) {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => UpdateAppScreen()),
);
return;
}
}

setState(() {
_isLoading = false;
});
Expand All @@ -161,10 +173,12 @@ class _AuthCardState extends State<AuthCard> {
_isLoading = false;
});
} catch (error) {
showErrorDialog(error, context);
setState(() {
_isLoading = false;
});
if (mounted) {
showErrorDialog(error, context);
setState(() {
_isLoading = false;
});
}
}
}

Expand Down Expand Up @@ -221,7 +235,7 @@ class _AuthCardState extends State<AuthCard> {
}
return null;
},
inputFormatters: [FilteringTextInputFormatter.deny(new RegExp(r"\s\b|\b\s"))],
inputFormatters: [FilteringTextInputFormatter.deny(RegExp(r'\s\b|\b\s'))],
onSaved: (value) {
_authData['username'] = value!;
},
Expand Down Expand Up @@ -349,23 +363,23 @@ class _AuthCardState extends State<AuthCard> {
),
TextButton(
key: const Key('toggleActionButton'),
onPressed: _switchAuthMode,
child: Text(
_authMode == AuthMode.Login
? AppLocalizations.of(context).registerInstead.toUpperCase()
: AppLocalizations.of(context).loginInstead.toUpperCase(),
),
onPressed: _switchAuthMode,
),
TextButton(
child: Text(_hideCustomServer
? AppLocalizations.of(context).useCustomServer
: AppLocalizations.of(context).useDefaultServer),
key: const Key('toggleCustomServerButton'),
onPressed: () {
setState(() {
_hideCustomServer = !_hideCustomServer;
});
},
child: Text(_hideCustomServer
? AppLocalizations.of(context).useCustomServer
: AppLocalizations.of(context).useDefaultServer),
),
],
),
Expand Down