fluffychat/lib/widgets/connection_status_header.dart

92 lines
2.6 KiB
Dart
Raw Permalink Normal View History

2020-07-04 10:22:51 +02:00
import 'dart:async';
2021-10-26 18:50:34 +02:00
2020-07-04 10:22:51 +02:00
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 18:50:34 +02:00
import 'package:matrix/matrix.dart';
2020-07-04 10:22:51 +02:00
import '../config/themes.dart';
2021-10-26 18:50:34 +02:00
import '../utils/localized_exception_extension.dart';
2020-07-04 10:22:51 +02:00
import 'matrix.dart';
class ConnectionStatusHeader extends StatefulWidget {
2021-11-19 20:38:16 +01:00
const ConnectionStatusHeader({Key? key}) : super(key: key);
2021-10-14 18:09:30 +02:00
2020-07-04 10:22:51 +02:00
@override
2022-08-14 16:59:21 +02:00
ConnectionStatusHeaderState createState() => ConnectionStatusHeaderState();
2020-07-04 10:22:51 +02:00
}
2022-08-14 16:59:21 +02:00
class ConnectionStatusHeaderState extends State<ConnectionStatusHeader> {
2022-07-09 10:18:53 +02:00
late final StreamSubscription _onSyncSub;
2021-11-23 19:34:55 +01:00
2022-07-09 10:18:53 +02:00
@override
void initState() {
_onSyncSub = Matrix.of(context).client.onSyncStatus.stream.listen(
(_) => setState(() {}),
);
super.initState();
}
2020-07-04 10:22:51 +02:00
@override
void dispose() {
2022-07-09 10:18:53 +02:00
_onSyncSub.cancel();
2020-07-04 10:22:51 +02:00
super.dispose();
}
@override
Widget build(BuildContext context) {
final client = Matrix.of(context).client;
final status = client.onSyncStatus.value ??
2022-07-09 10:18:53 +02:00
const SyncStatusUpdate(SyncStatus.waitingForResponse);
final hide = client.onSync.value != null &&
2022-07-09 10:18:53 +02:00
status.status != SyncStatus.error &&
client.prevBatch != null;
2021-11-23 19:34:55 +01:00
2020-07-04 10:22:51 +02:00
return AnimatedContainer(
duration: FluffyThemes.animationDuration,
curve: FluffyThemes.animationCurve,
2021-11-23 19:34:55 +01:00
height: hide ? 0 : 36,
clipBehavior: Clip.hardEdge,
2022-06-04 20:06:57 +02:00
decoration: BoxDecoration(color: Theme.of(context).colorScheme.surface),
2021-10-14 18:09:30 +02:00
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 24,
height: 24,
2021-10-10 13:38:06 +02:00
child: CircularProgressIndicator.adaptive(
strokeWidth: 2,
2022-07-09 10:18:53 +02:00
value: hide ? 1.0 : status.progress,
),
),
2021-10-14 18:09:30 +02:00
const SizedBox(width: 12),
Text(
2022-07-09 10:18:53 +02:00
status.toLocalizedString(context),
maxLines: 1,
overflow: TextOverflow.ellipsis,
2022-06-04 20:06:57 +02:00
style: TextStyle(color: Theme.of(context).colorScheme.onSurface),
),
],
),
2020-07-04 10:22:51 +02:00
);
}
}
extension on SyncStatusUpdate {
String toLocalizedString(BuildContext context) {
switch (status) {
case SyncStatus.waitingForResponse:
2021-11-19 20:38:16 +01:00
return L10n.of(context)!.loadingPleaseWait;
case SyncStatus.error:
2021-11-19 20:38:16 +01:00
return ((error?.exception ?? Object()) as Object)
.toLocalizedString(context);
case SyncStatus.processing:
case SyncStatus.cleaningUp:
case SyncStatus.finished:
default:
2021-11-19 20:38:16 +01:00
return L10n.of(context)!.synchronizingPleaseWait;
}
}
}