fluffychat/lib/widgets/connection_status_header.dart

78 lines
2.2 KiB
Dart
Raw Normal View History

2020-07-04 10:22:51 +02:00
import 'dart:async';
import 'package:flutter/material.dart';
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;
bool get _connected =>
DateTime.now().millisecondsSinceEpoch -
_lastSyncReceived.millisecondsSinceEpoch <
1000 * 30;
static DateTime _lastSyncReceived = DateTime(0);
SdkError _error;
2020-07-04 10:22:51 +02:00
@override
void dispose() {
_onSyncSub?.cancel();
_onSyncErrorSub?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
_onSyncSub ??= Matrix.of(context).client.onSync.stream.listen(
(_) => setState(
() {
_lastSyncReceived = DateTime.now();
_error = null;
},
),
2020-07-04 10:22:51 +02:00
);
_onSyncErrorSub ??= Matrix.of(context).client.onSyncError.stream.listen(
(error) => setState(
() {
_lastSyncReceived = DateTime(0);
_error = error;
},
),
2020-07-04 10:22:51 +02:00
);
return AnimatedContainer(
2021-06-20 12:57:46 +02:00
duration: Duration(milliseconds: 250),
curve: Curves.bounceInOut,
height: _connected ? 0 : 36,
clipBehavior: Clip.hardEdge,
2021-06-20 12:57:46 +02:00
decoration: BoxDecoration(color: Theme.of(context).secondaryHeaderColor),
padding: EdgeInsets.symmetric(horizontal: 12),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(strokeWidth: 2),
),
SizedBox(width: 12),
Text(
_error != null
? (_error.exception as Object).toLocalizedString(context)
: L10n.of(context).loadingPleaseWait,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
2020-07-04 10:22:51 +02:00
);
}
}