diff --git a/lib/screens/send_data.dart b/lib/screens/send_data.dart index 02357b5..c270a12 100644 --- a/lib/screens/send_data.dart +++ b/lib/screens/send_data.dart @@ -5,6 +5,7 @@ import 'package:flutter/services.dart'; import 'package:qr_flutter/qr_flutter.dart'; import 'package:gsheets/gsheets.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; +import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; class SendData extends StatefulWidget { @@ -39,7 +40,8 @@ class _SendDataState extends State { void initState() { super.initState(); getLocalGames().then((_) { - setState(() {}); //refresh, very unpracitical but this is a hotfix after all :P ... + setState( + () {}); //refresh, very unpracitical but this is a hotfix after all :P ... }); } @@ -51,35 +53,33 @@ class _SendDataState extends State { } } - Future loadEnv() async { - String envString; - try { - envString = await rootBundle.loadString('.env'); - } catch (e) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('The .env file could not be found')), - ); - return; + Future fetchApi(String key) async { + var a = await http.get(Uri.parse( + 'https://ryanidkproductions.com/api/mergedata?key=$key')); // temp api host + if (a.statusCode == 200) { + return jsonDecode(a.body); + } else { + return {"Error": "Invalid API key"}; } + } - try { - await dotenv.load(fileName: ".env").timeout(Duration(seconds: 5)); - _gsheets = GSheets(dotenv.env['GOOGLE_SHEETS_DATA']!); - _spreadsheetId = dotenv.env['SPREADSHEET_ID']!; - _gameWorksheetName = dotenv.env['GAME_WORKSHEET_NAME']!; - _pitWorksheetName = dotenv.env['PIT_WORKSHEET_NAME']!; - _passcode = dotenv.env['PASSCODE']!; - } on TimeoutException catch (e) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Loading environment variables timed out')), - ); - } catch (e) { + Future loadApi() async { + final prefs = await SharedPreferences.getInstance(); + String apiKey = prefs.getString('apiKey') ?? ''; + Map apiResponse = await fetchApi(apiKey); + + if (apiResponse.containsKey("Error")) { ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('An error occurred while loading environment variables')), + SnackBar(content: Text('Invalid or no API key entered')), ); + } else { + _gsheets = GSheets(apiResponse["GOOGLE_SHEETS_DATA"]); + _spreadsheetId = apiResponse["SPREADSHEET_ID"]; + _gameWorksheetName = apiResponse["GAME_WORKSHEET_NAME"]; + _pitWorksheetName = apiResponse["PIT_WORKSHEET_NAME"]; } } - + Future saveDataLocally() async { final prefs = await SharedPreferences.getInstance(); final savedGames = prefs.getStringList('savedGames') ?? []; @@ -87,49 +87,6 @@ class _SendDataState extends State { await prefs.setStringList('savedGames', savedGames); } - Future validate() async { - await loadEnv(); - SharedPreferences prefs = await SharedPreferences.getInstance(); - bool isAuthenticated = prefs.getBool('isAuthenticated') ?? false; - - /*if (!isAuthenticated) { - String? passcode; - await showDialog( - context: context, - builder: (BuildContext context) { - return AlertDialog( - title: const Text('Enter passcode'), - content: TextField( - obscureText: true, - onChanged: (value) { - passcode = value; // Update passcode - }, - ), - actions: [ - TextButton( - onPressed: () { - Navigator.of(context).pop(); - }, - child: const Text('OK'), - ), - ], - ); - }, - ); - - if (passcode == dotenv.env['PASSCODE']) { - await prefs.setBool('isAuthenticated', true); - sendDataToGoogleSheets(); - } else { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text("Wrong passcode!")), - ); - } - } else { // already authenticated - sendDataToGoogleSheets(); - }*/ - } - Future sendDataToGoogleSheets() async { String message = ''; @@ -171,7 +128,7 @@ class _SendDataState extends State { } } } catch (e) { - message = 'Shep Error!: $e'; + message = 'Could not send data to sheets!'; print(e); } @@ -183,7 +140,7 @@ class _SendDataState extends State { @override Widget build(BuildContext context) { return FutureBuilder( - future: loadEnv(), + future: loadApi(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { return Scaffold( @@ -241,7 +198,8 @@ class _SendDataState extends State { padding: EdgeInsets.all(15), child: Text( savedGamesArray.join('\n\n\n'), - style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + style: const TextStyle( + fontSize: 18, fontWeight: FontWeight.bold), ), ), if (showQR) @@ -358,7 +316,8 @@ class _SendDataState extends State { ); }, ); - validate().then((_) { //send to google sheets, but authenticate first + sendDataToGoogleSheets().then((_) { + //send to google sheets, but authenticate first if (!isCancelled) { Navigator.of(context).pop(); } diff --git a/lib/screens/start.dart b/lib/screens/start.dart index 2dfc85a..d704943 100644 --- a/lib/screens/start.dart +++ b/lib/screens/start.dart @@ -52,8 +52,7 @@ class _StartState extends State { Navigator.push( context, MaterialPageRoute( - builder: (context) => - SendData(data: {}, isGame: true, justSend: true), + builder: (context) => SendData(data: {}, isGame: true, justSend: true), ), ); } @@ -98,7 +97,8 @@ class _StartState extends State { decoration: InputDecoration( hintText: "API Key", suffixIcon: IconButton( - icon: Icon(obscureText ? Icons.visibility_off : Icons.visibility), + icon: Icon( + obscureText ? Icons.visibility_off : Icons.visibility), onPressed: () { setState(() { obscureText = !obscureText; @@ -152,7 +152,8 @@ class _StartState extends State { builder: (BuildContext context) { return AlertDialog( title: Text('Delete all local games?'), - content: Text('This action cannot be undone!!', style: TextStyle(color: Colors.red)), + content: Text('This action cannot be undone!!', + style: TextStyle(color: Colors.red)), actions: [ TextButton( child: Text('Yes'), @@ -160,7 +161,8 @@ class _StartState extends State { deleteAllGames(); Navigator.of(context).pop(); ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('All local games deleted.')), + SnackBar( + content: Text('All local games deleted.')), ); }, ), @@ -212,7 +214,8 @@ class _StartState extends State { children: [ ElevatedButton( onPressed: matchScouting, - child: const Text("Match Scouting", style: TextStyle(fontSize: 28)), + child: + const Text("Match Scouting", style: TextStyle(fontSize: 28)), ), const Padding(padding: EdgeInsets.all(10)), ElevatedButton( @@ -226,7 +229,9 @@ class _StartState extends State { ), const Padding(padding: EdgeInsets.all(10)), StreamBuilder>( - stream: Stream.periodic(Duration(seconds: 1)).asyncMap((_) => SharedPreferences.getInstance().then((prefs) => prefs.getStringList('savedGames') ?? [])), + stream: Stream.periodic(Duration(seconds: 1)).asyncMap((_) => + SharedPreferences.getInstance().then( + (prefs) => prefs.getStringList('savedGames') ?? [])), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return ElevatedButton( @@ -236,7 +241,8 @@ class _StartState extends State { } else { return ElevatedButton( onPressed: sendData, - child: Text('Upload Local Saves (${snapshot.data!.length})', style: TextStyle(fontSize: 28)), + child: Text('Upload Local Saves (${snapshot.data!.length})', + style: TextStyle(fontSize: 28)), ); } },