115 lines
3.9 KiB
Dart
115 lines
3.9 KiB
Dart
import 'dart:async';
|
|
import 'dart:math';
|
|
|
|
import 'package:desktopapp/classes/routes.dart';
|
|
import 'package:desktopapp/classes/stream_element.dart';
|
|
import 'package:desktopapp/utils/mailer.dart';
|
|
import 'package:desktopapp/widgets/components/stream_element_card.dart';
|
|
import 'package:desktopapp/widgets/forms/email_form.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
_HomePageState createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
bool connected = false;
|
|
List<StreamElement> streamElements = [];
|
|
final StreamController<StreamElement> controller =
|
|
StreamController<StreamElement>();
|
|
|
|
Widget buildContent(BuildContext context, SharedPreferences prefs) {
|
|
return Column(children: [
|
|
if (!connected)
|
|
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
|
EmailForm(
|
|
prefs: prefs,
|
|
onValid: (String email, String password) async {
|
|
var value = await Mailer.connect(
|
|
email: email,
|
|
password: password,
|
|
streamController: controller);
|
|
setState(() {
|
|
connected = value;
|
|
});
|
|
})
|
|
]),
|
|
if (connected)
|
|
Row(
|
|
children: [
|
|
TextButton(
|
|
onPressed: () async {
|
|
await Mailer.disconnect();
|
|
setState(() {
|
|
connected = false;
|
|
});
|
|
},
|
|
child: const Text('Se déconnecter')),
|
|
],
|
|
),
|
|
Expanded(
|
|
child: StreamBuilder<StreamElement>(
|
|
stream: controller.stream,
|
|
builder:
|
|
(BuildContext context, AsyncSnapshot<StreamElement> snapshot) {
|
|
switch (snapshot.connectionState) {
|
|
case ConnectionState.none:
|
|
return const Text('Nothing');
|
|
case ConnectionState.waiting:
|
|
return const Center(child: CircularProgressIndicator());
|
|
case ConnectionState.active:
|
|
if (snapshot.hasData) {
|
|
int skipNumber = max(0, streamElements.length - 10);
|
|
bool alreadyAdded = streamElements
|
|
.skip(skipNumber)
|
|
.cast<StreamElement?>()
|
|
.firstWhere(
|
|
(element) => element!.uuid == snapshot.data!.uuid,
|
|
orElse: () => null) !=
|
|
null;
|
|
if (!alreadyAdded) streamElements.add(snapshot.data!);
|
|
}
|
|
return ListView.builder(
|
|
itemCount: streamElements.length,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
return StreamElementCard(
|
|
streamElement: streamElements[index]);
|
|
},
|
|
);
|
|
case ConnectionState.done:
|
|
return const Text('Terminate');
|
|
}
|
|
},
|
|
),
|
|
)
|
|
]);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
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 buildContent(context, prefs);
|
|
} else {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
}));
|
|
}
|
|
}
|