2022-03-31 22:04:57 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:desktopapp/classes/routes.dart';
|
|
|
|
import 'package:desktopapp/utils/logger.dart';
|
|
|
|
import 'package:dio/dio.dart';
|
2022-03-31 21:34:28 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-03-31 22:04:57 +02:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-03-31 21:34:28 +02:00
|
|
|
|
|
|
|
class HomePage extends StatelessWidget {
|
|
|
|
const HomePage({Key? key}) : super(key: key);
|
|
|
|
|
2022-03-31 22:04:57 +02:00
|
|
|
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')),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-03-31 21:34:28 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-03-31 22:04:57 +02:00
|
|
|
return Scaffold(
|
|
|
|
body: FutureBuilder(
|
|
|
|
future: SharedPreferences.getInstance(),
|
|
|
|
builder: (BuildContext context,
|
|
|
|
AsyncSnapshot<SharedPreferences> 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());
|
|
|
|
}
|
|
|
|
}));
|
2022-03-31 21:34:28 +02:00
|
|
|
}
|
|
|
|
}
|