If no wger API key is present launch a browser so they can register manually

This commit is contained in:
Roland Geider
2021-09-19 20:30:54 +02:00
parent 18265bde39
commit adb5b6317e
4 changed files with 37 additions and 14 deletions

View File

@@ -17,6 +17,7 @@
*/
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/models/workouts/repetition_unit.dart';
import 'package:wger/models/workouts/weight_unit.dart';
@@ -90,3 +91,11 @@ extension TimeOfDayExtension on TimeOfDay {
return (hour * 60) + minute;
}
}
void launchURL(String url, BuildContext context) async {
await canLaunch(url)
? await launch(url)
: ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Could not open $url.")),
);
}

View File

@@ -16,12 +16,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:android_metadata/android_metadata.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
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 '../providers/auth.dart';
@@ -90,6 +92,7 @@ class AuthCard extends StatefulWidget {
class _AuthCardState extends State<AuthCard> {
final GlobalKey<FormState> _formKey = GlobalKey();
bool _canRegister = true;
AuthMode _authMode = AuthMode.Login;
bool _hideCustomServer = true;
final Map<String, String> _authData = {
@@ -107,11 +110,23 @@ class _AuthCardState extends State<AuthCard> {
@override
void initState() {
//_serverUrlController.text
super.initState();
context.read<AuthProvider>().getServerUrlFromPrefs().then((value) {
_serverUrlController.text = value;
});
// Check if the API key is set
//
// If not, the user will not be able to register via the app
try {
AndroidMetadata.metaDataAsMap.then((data) {
if (!data!.containsKey('wger.api_key') || data['wger.api_key'] == '') {
_canRegister = false;
}
});
} on PlatformException {
_canRegister = false;
}
}
void _submit(BuildContext context) async {
@@ -123,6 +138,7 @@ class _AuthCardState extends State<AuthCard> {
setState(() {
_isLoading = true;
});
try {
// Login existing user
if (_authMode == AuthMode.Login) {
@@ -159,6 +175,11 @@ class _AuthCardState extends State<AuthCard> {
}
void _switchAuthMode() {
if (!_canRegister) {
launchURL(DEFAULT_SERVER, context);
return;
}
if (_authMode == AuthMode.Login) {
setState(() {
_authMode = AuthMode.Signup;

View File

@@ -22,6 +22,7 @@ class SplashScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).primaryColor,
body: Center(
child: Text('Loading...'),
),

View File

@@ -19,18 +19,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:wger/helpers/misc.dart';
import 'package:wger/providers/auth.dart';
class WgerAboutListTile extends StatelessWidget {
void _launchURL(String url, BuildContext context) async {
await canLaunch(url)
? await launch(url)
: ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Could not open $url.')),
);
}
@override
Widget build(BuildContext context) {
final authProvider = Provider.of<AuthProvider>(context, listen: false);
@@ -68,7 +60,7 @@ class WgerAboutListTile extends StatelessWidget {
],
),
contentPadding: EdgeInsets.zero,
onTap: () async => _launchURL('https://github.com/wger-project', context),
onTap: () async => launchURL('https://github.com/wger-project', context),
),
SizedBox(height: 10),
ListTile(
@@ -86,7 +78,7 @@ class WgerAboutListTile extends StatelessWidget {
),
contentPadding: EdgeInsets.zero,
onTap: () async =>
_launchURL('https://github.com/wger-project/flutter/issues/new/choose', context),
launchURL('https://github.com/wger-project/flutter/issues/new/choose', context),
),
SizedBox(height: 10),
ListTile(
@@ -103,7 +95,7 @@ class WgerAboutListTile extends StatelessWidget {
],
),
contentPadding: EdgeInsets.zero,
onTap: () async => _launchURL('https://discord.gg/rPWFv6W', context),
onTap: () async => launchURL('https://discord.gg/rPWFv6W', context),
),
SizedBox(height: 10),
ListTile(
@@ -120,7 +112,7 @@ class WgerAboutListTile extends StatelessWidget {
],
),
contentPadding: EdgeInsets.zero,
onTap: () async => _launchURL('https://hosted.weblate.org/engage/wger/', context),
onTap: () async => launchURL('https://hosted.weblate.org/engage/wger/', context),
),
],
);