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
|
|
|
|
2021-06-20 09:03:22 +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
|
|
|
|
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
|
|
|
|
_ConnectionStatusHeaderState createState() => _ConnectionStatusHeaderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ConnectionStatusHeaderState extends State<ConnectionStatusHeader> {
|
2021-11-19 20:38:16 +01:00
|
|
|
StreamSubscription? _onSyncSub;
|
2021-11-23 19:34:55 +01:00
|
|
|
static bool _anySyncReceived = false;
|
|
|
|
|
2021-10-14 18:09:30 +02:00
|
|
|
SyncStatusUpdate _status =
|
|
|
|
const SyncStatusUpdate(SyncStatus.waitingForResponse);
|
2020-07-04 10:22:51 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_onSyncSub?.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-06-22 15:46:52 +02:00
|
|
|
_onSyncSub ??= Matrix.of(context).client.onSyncStatus.stream.listen(
|
|
|
|
(status) => setState(
|
2021-06-20 09:03:22 +02:00
|
|
|
() {
|
2021-06-22 15:46:52 +02:00
|
|
|
_status = status;
|
2021-11-23 19:34:55 +01:00
|
|
|
if (status.status == SyncStatus.finished) {
|
|
|
|
_anySyncReceived = true;
|
|
|
|
}
|
2021-06-20 09:03:22 +02:00
|
|
|
},
|
|
|
|
),
|
2020-07-04 10:22:51 +02:00
|
|
|
);
|
|
|
|
|
2021-11-23 19:34:55 +01:00
|
|
|
final hide = _anySyncReceived &&
|
|
|
|
_status.status != SyncStatus.error &&
|
|
|
|
Matrix.of(context).client.prevBatch != null;
|
|
|
|
|
2020-07-04 10:22:51 +02:00
|
|
|
return AnimatedContainer(
|
2021-10-14 18:09:30 +02:00
|
|
|
duration: const Duration(milliseconds: 200),
|
2021-06-20 12:57:46 +02:00
|
|
|
curve: Curves.bounceInOut,
|
2021-11-23 19:34:55 +01:00
|
|
|
height: hide ? 0 : 36,
|
2021-06-20 09:03:22 +02:00
|
|
|
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),
|
2021-06-20 09:03:22 +02:00
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
SizedBox(
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
2021-10-10 13:38:06 +02:00
|
|
|
child: CircularProgressIndicator.adaptive(
|
2021-06-22 15:46:52 +02:00
|
|
|
strokeWidth: 2,
|
2021-11-23 19:34:55 +01:00
|
|
|
value: hide ? 1.0 : _status.progress,
|
2021-06-22 15:46:52 +02:00
|
|
|
),
|
2021-06-20 09:03:22 +02:00
|
|
|
),
|
2021-10-14 18:09:30 +02:00
|
|
|
const SizedBox(width: 12),
|
2021-06-20 09:03:22 +02:00
|
|
|
Text(
|
2021-06-22 15:46:52 +02:00
|
|
|
_status.toLocalizedString(context),
|
2021-06-20 09:03:22 +02:00
|
|
|
maxLines: 1,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
2022-06-04 20:06:57 +02:00
|
|
|
style: TextStyle(color: Theme.of(context).colorScheme.onSurface),
|
2021-06-20 09:03:22 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2020-07-04 10:22:51 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-06-22 15:46:52 +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;
|
2021-06-22 15:46:52 +02:00
|
|
|
case SyncStatus.error:
|
2021-11-19 20:38:16 +01:00
|
|
|
return ((error?.exception ?? Object()) as Object)
|
|
|
|
.toLocalizedString(context);
|
2021-06-22 15:46:52 +02:00
|
|
|
case SyncStatus.processing:
|
|
|
|
case SyncStatus.cleaningUp:
|
|
|
|
case SyncStatus.finished:
|
|
|
|
default:
|
2021-11-19 20:38:16 +01:00
|
|
|
return L10n.of(context)!.synchronizingPleaseWait;
|
2021-06-22 15:46:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|