diff --git a/lib/classes/routes.dart b/lib/classes/routes.dart index 3bb4fd7..b6e17ec 100644 --- a/lib/classes/routes.dart +++ b/lib/classes/routes.dart @@ -20,4 +20,4 @@ var routes = { Routes.home.path: (context) => const HomePage(), }; -var initialRoute = Routes.streamlabsTutorial.path; +var initialRoute = Routes.home.path; diff --git a/lib/widgets/components/tutorials/step3.dart b/lib/widgets/components/tutorials/step3.dart index 0268c13..3c5e1ca 100644 --- a/lib/widgets/components/tutorials/step3.dart +++ b/lib/widgets/components/tutorials/step3.dart @@ -103,7 +103,8 @@ class Step3 extends StatelessWidget { await dio.post('https://streamlabs.com/api/v1.0/token', data: data); if (response.statusCode == HttpStatus.ok) { - prefs.setString('credentials', response.data.toString()); + jsonDecode(response.data).forEach((key, value) => + prefs.setString('credentials.$key', value.toString())); Logger.log(LoggerType.info, this, 'Authorization success : ${response.data}'); showDialog( diff --git a/lib/widgets/pages/home.dart b/lib/widgets/pages/home.dart index 6ab2b1e..f70086a 100644 --- a/lib/widgets/pages/home.dart +++ b/lib/widgets/pages/home.dart @@ -1,10 +1,108 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:desktopapp/classes/routes.dart'; +import 'package:desktopapp/utils/logger.dart'; +import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; class HomePage extends StatelessWidget { const HomePage({Key? key}) : super(key: key); + Widget buildContent(BuildContext context, SharedPreferences prefs) { + return Column(children: [ + const Padding( + padding: EdgeInsets.symmetric(vertical: 20), + child: Text('Appuyez sur le bouton pour envoyer une alerte de test', + style: TextStyle(fontSize: 20)), + ), + TextButton( + onPressed: () async { + Dio dio = Dio(BaseOptions( + contentType: Headers.jsonContentType, + responseType: ResponseType.json, + validateStatus: (_) => true)); + + var accessToken = prefs.getString('credentials.access_token'); + + var data = jsonEncode({ + 'type': 'donation', + 'message': '*MrDev023* *Break* Test !', + 'access_token': accessToken, + 'special_text_color': '#ff00ff', + 'image_href': 'https://postimgs.org/img/logo.png', + 'sound_href': '' + }); + + var response = await dio + .post('https://streamlabs.com/api/v1.0/alerts', data: data); + + if (response.statusCode == HttpStatus.ok) { + prefs.setString('credentials', response.data.toString()); + Logger.log(LoggerType.info, this, + 'Authorization success : ${response.data}'); + showDialog( + context: context, + builder: (_) => AlertDialog( + title: const Text('Info'), + content: const Text('Envoie réussie'), + actions: [ + TextButton( + onPressed: () { + Navigator.pop(context); + }, + child: const Text('OK')) + ], + )); + } else { + Logger.log(LoggerType.error, this, + 'Authorization error : ${response.data} with $data and ${response.requestOptions.headers}'); + showDialog( + context: context, + builder: (_) => AlertDialog( + title: const Text('Erreur'), + content: const Text('Impossible de se connecter.'), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context), + child: const Text('OK')) + ], + )); + } + }, + child: const Text('Envoyer une alerte de test')), + ]); + } + @override Widget build(BuildContext context) { - return const Scaffold(body: Text('Test')); + return Scaffold( + body: FutureBuilder( + future: SharedPreferences.getInstance(), + builder: (BuildContext context, + AsyncSnapshot snapshot) { + if (snapshot.hasData) { + final prefs = snapshot.data!; + + Future(() async { + if (!prefs.containsKey('credentials.access_token')) { + Navigator.of(context) + .pushReplacementNamed(Routes.streamlabsTutorial.path); + } + }); + + return Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [buildContent(context, prefs)], + ) + ]); + } else { + return const Center(child: CircularProgressIndicator()); + } + })); } }