FurryChat/lib/components/encryption_button.dart

91 lines
2.9 KiB
Dart
Raw Permalink Normal View History

2020-02-22 08:03:44 +00:00
import 'dart:async';
import 'package:famedlysdk/famedlysdk.dart';
2020-05-07 05:52:40 +00:00
import 'package:fluffychat/l10n/l10n.dart';
2020-02-22 08:03:44 +00:00
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/views/chat_encryption_settings.dart';
import 'package:flutter/material.dart';
2020-05-13 08:45:50 +00:00
import 'package:bot_toast/bot_toast.dart';
2020-02-22 08:03:44 +00:00
2020-02-23 08:31:44 +00:00
import 'dialogs/simple_dialogs.dart';
2020-02-22 08:03:44 +00:00
import 'matrix.dart';
class EncryptionButton extends StatefulWidget {
final Room room;
const EncryptionButton(this.room, {Key key}) : super(key: key);
@override
_EncryptionButtonState createState() => _EncryptionButtonState();
}
class _EncryptionButtonState extends State<EncryptionButton> {
StreamSubscription _onSyncSub;
2020-02-23 08:31:44 +00:00
void _enableEncryptionAction() async {
if (widget.room.encrypted) {
2020-05-13 08:45:50 +00:00
BotToast.showText(text: L10n.of(context).warningEncryptionInBeta);
2020-02-23 08:31:44 +00:00
await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
ChatEncryptionSettingsView(widget.room.id),
),
);
return;
}
if (!widget.room.client.encryptionEnabled) {
2020-05-13 08:45:50 +00:00
BotToast.showText(text: L10n.of(context).needPantalaimonWarning);
2020-02-23 08:31:44 +00:00
return;
}
if (await SimpleDialogs(context).askConfirmation(
2020-05-07 05:52:40 +00:00
titleText: L10n.of(context).enableEncryptionWarning,
2020-02-23 08:31:44 +00:00
contentText: widget.room.client.encryptionEnabled
2020-05-07 05:52:40 +00:00
? L10n.of(context).warningEncryptionInBeta
: L10n.of(context).needPantalaimonWarning,
confirmText: L10n.of(context).yes,
2020-02-23 08:31:44 +00:00
) ==
true) {
2020-04-27 11:36:39 +00:00
await SimpleDialogs(context).tryRequestWithLoadingDialog(
2020-02-23 08:31:44 +00:00
widget.room.enableEncryption(),
);
}
}
2020-02-22 08:03:44 +00:00
@override
void dispose() {
_onSyncSub?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
_onSyncSub ??= Matrix.of(context)
.client
.onSync
.stream
.listen((s) => setState(() => null));
return FutureBuilder<List<DeviceKeys>>(
2020-03-29 10:45:39 +00:00
future: widget.room.encrypted ? widget.room.getUserDeviceKeys() : null,
2020-02-22 08:03:44 +00:00
builder: (BuildContext context, snapshot) {
Color color;
if (widget.room.encrypted && snapshot.hasData) {
2020-05-13 13:58:59 +00:00
var data = snapshot.data;
final deviceKeysList = data;
2020-02-22 08:03:44 +00:00
color = Colors.orange;
if (deviceKeysList.indexWhere((DeviceKeys deviceKeys) =>
deviceKeys.verified == false &&
deviceKeys.blocked == false) ==
-1) {
color = Colors.black.withGreen(220).withOpacity(0.75);
}
} else if (!widget.room.encrypted &&
widget.room.joinRules != JoinRules.public) {
color = null;
}
return IconButton(
icon: Icon(widget.room.encrypted ? Icons.lock : Icons.lock_open,
size: 20, color: color),
2020-02-23 08:31:44 +00:00
onPressed: _enableEncryptionAction,
2020-02-22 08:03:44 +00:00
);
});
}
}