Solian/lib/widgets/provider_init.dart

91 lines
2.4 KiB
Dart
Raw Normal View History

import 'dart:async';
2024-05-12 12:59:33 +00:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/chat.dart';
import 'package:solian/providers/keypair.dart';
import 'package:solian/providers/notify.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:solian/widgets/exts.dart';
2024-05-12 12:59:33 +00:00
class ProviderInitializer extends StatefulWidget {
final Widget child;
const ProviderInitializer({super.key, required this.child});
@override
State<ProviderInitializer> createState() => _ProviderInitializerState();
}
class _ProviderInitializerState extends State<ProviderInitializer> {
2024-05-13 14:52:41 +00:00
void showConnectionStatus(bool status) {
if (status) {
showConnectionSnackbar();
} else {
ScaffoldMessenger.of(context).clearSnackBars();
}
}
void showConnectionSnackbar() {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(AppLocalizations.of(context)!.connectingServer),
duration: const Duration(minutes: 1),
));
}
2024-05-12 12:59:33 +00:00
void connect() async {
final auth = context.read<AuthProvider>();
final nty = context.read<NotifyProvider>();
final chat = context.read<ChatProvider>();
final keypair = context.read<KeypairProvider>();
final notify = ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.connectingServer),
2024-05-13 14:52:41 +00:00
duration: const Duration(seconds: 3),
2024-05-12 12:59:33 +00:00
),
);
try {
if (await auth.isAuthorized()) {
if (auth.client == null) {
await auth.loadClient();
}
2024-05-12 12:59:33 +00:00
2024-05-13 14:52:41 +00:00
nty.connect(
auth,
onKexRequest: keypair.provideKeypair,
onKexProvide: keypair.receiveKeypair,
2024-05-13 14:52:41 +00:00
onStateUpdated: showConnectionStatus
).then((value) {
keypair.channel = value;
});
chat.connect(auth, onStateUpdated: showConnectionStatus);
nty.fetch(auth);
Timer.periodic(const Duration(seconds: 1), (timer) {
2024-05-13 14:52:41 +00:00
nty.connect(auth, onStateUpdated: showConnectionStatus);
chat.connect(auth, onStateUpdated: showConnectionStatus);
});
}
} catch (e) {
context.showErrorDialog(e);
2024-05-12 12:59:33 +00:00
}
notify.close();
}
@override
void initState() {
super.initState();
Future.delayed(Duration.zero, () => connect());
}
@override
Widget build(BuildContext context) {
return widget.child;
}
}