2020-07-04 10:22:51 +02:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
2021-06-20 09:03:22 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
import '../utils/localized_exception_extension.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2020-07-04 10:22:51 +02:00
|
|
|
|
|
|
|
import 'matrix.dart';
|
|
|
|
|
|
|
|
class ConnectionStatusHeader extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_ConnectionStatusHeaderState createState() => _ConnectionStatusHeaderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ConnectionStatusHeaderState extends State<ConnectionStatusHeader> {
|
|
|
|
StreamSubscription _onSyncSub;
|
|
|
|
StreamSubscription _onSyncErrorSub;
|
2021-06-20 09:03:22 +02:00
|
|
|
bool get _connected =>
|
|
|
|
DateTime.now().millisecondsSinceEpoch -
|
|
|
|
_lastSyncReceived.millisecondsSinceEpoch <
|
2021-08-18 17:24:59 +02:00
|
|
|
(Matrix.of(context).client.sendMessageTimeoutSeconds + 2) * 1000;
|
2021-06-20 09:03:22 +02:00
|
|
|
static DateTime _lastSyncReceived = DateTime(0);
|
2021-06-22 15:46:52 +02:00
|
|
|
SyncStatusUpdate _status = SyncStatusUpdate(SyncStatus.waitingForResponse);
|
2020-07-04 10:22:51 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_onSyncSub?.cancel();
|
|
|
|
_onSyncErrorSub?.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-23 11:40:12 +02:00
|
|
|
if ((status.status == SyncStatus.processing &&
|
|
|
|
Matrix.of(context).client.prevBatch != null) ||
|
|
|
|
status.status == SyncStatus.finished) {
|
2021-06-22 15:46:52 +02:00
|
|
|
_lastSyncReceived = DateTime.now();
|
|
|
|
}
|
|
|
|
_status = status;
|
2021-06-20 09:03:22 +02:00
|
|
|
},
|
|
|
|
),
|
2020-07-04 10:22:51 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return AnimatedContainer(
|
2021-06-22 15:46:52 +02:00
|
|
|
duration: Duration(milliseconds: 200),
|
2021-06-20 12:57:46 +02:00
|
|
|
curve: Curves.bounceInOut,
|
2021-06-20 09:03:22 +02:00
|
|
|
height: _connected ? 0 : 36,
|
|
|
|
clipBehavior: Clip.hardEdge,
|
2021-06-20 12:57:46 +02:00
|
|
|
decoration: BoxDecoration(color: Theme.of(context).secondaryHeaderColor),
|
2021-06-20 09:03:22 +02:00
|
|
|
padding: 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(
|
2021-06-22 15:46:52 +02:00
|
|
|
strokeWidth: 2,
|
2021-07-23 16:59:46 +02:00
|
|
|
value: _connected ? 1.0 : _status.progress,
|
2021-06-22 15:46:52 +02:00
|
|
|
),
|
2021-06-20 09:03:22 +02:00
|
|
|
),
|
|
|
|
SizedBox(width: 12),
|
|
|
|
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,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
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:
|
|
|
|
return L10n.of(context).loadingPleaseWait;
|
|
|
|
case SyncStatus.error:
|
|
|
|
return (error.exception as Object).toLocalizedString(context);
|
|
|
|
case SyncStatus.processing:
|
|
|
|
case SyncStatus.cleaningUp:
|
|
|
|
case SyncStatus.finished:
|
|
|
|
default:
|
|
|
|
return L10n.of(context).synchronizingPleaseWait;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|