fluffychat/lib/components/encryption_button.dart

114 lines
3.6 KiB
Dart
Raw Normal View History

2020-02-22 09:03:44 +01:00
import 'dart:async';
2020-11-14 10:08:13 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flushbar/flushbar_helper.dart';
2020-02-22 09:03:44 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/views/chat_encryption_settings.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-02-22 09:03:44 +01:00
2020-12-25 09:58:34 +01:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2020-02-22 09:03:44 +01: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 09:31:44 +01:00
void _enableEncryptionAction() async {
if (widget.room.encrypted) {
await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
ChatEncryptionSettingsView(widget.room.id),
),
);
return;
}
if (!widget.room.client.encryptionEnabled) {
await FlushbarHelper.createInformation(
message: L10n.of(context).needPantalaimonWarning)
.show(context);
2020-02-23 09:31:44 +01:00
return;
}
2020-11-14 10:08:13 +01:00
if (await showOkCancelAlertDialog(
context: context,
title: L10n.of(context).enableEncryptionWarning,
message: widget.room.client.encryptionEnabled
2020-05-07 07:52:40 +02:00
? L10n.of(context).warningEncryptionInBeta
: L10n.of(context).needPantalaimonWarning,
2020-11-14 10:08:13 +01:00
okLabel: L10n.of(context).yes,
2020-02-23 09:31:44 +01:00
) ==
2020-11-14 10:08:13 +01:00
OkCancelResult.ok) {
2020-12-25 09:58:34 +01:00
await showFutureLoadingDialog(
context: context,
future: () => widget.room.enableEncryption(),
2020-02-23 09:31:44 +01:00
);
// we want to enable the lock icon
setState(() => null);
2020-02-23 09:31:44 +01:00
}
}
2020-02-22 09:03:44 +01:00
@override
void dispose() {
_onSyncSub?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
if (widget.room.encrypted) {
_onSyncSub ??= Matrix.of(context)
.client
.onSync
.stream
.where((s) => s.deviceLists != null)
.listen((s) => setState(() => null));
}
2020-10-31 10:39:38 +01:00
return FutureBuilder<List<User>>(
future:
widget.room.encrypted ? widget.room.requestParticipants() : null,
2020-02-22 09:03:44 +01:00
builder: (BuildContext context, snapshot) {
Color color;
if (widget.room.encrypted && snapshot.hasData) {
2020-10-31 10:39:38 +01:00
final users = snapshot.data;
users.removeWhere((u) =>
!{Membership.invite, Membership.join}.contains(u.membership) ||
!widget.room.client.userDeviceKeys.containsKey(u.id));
var allUsersValid = true;
var oneUserInvalid = false;
for (final u in users) {
final status = widget.room.client.userDeviceKeys[u.id].verified;
if (status != UserVerifiedStatus.verified) {
allUsersValid = false;
}
if (status == UserVerifiedStatus.unknownDevice) {
oneUserInvalid = true;
}
2020-02-22 09:03:44 +01:00
}
2020-10-31 10:39:38 +01:00
color = oneUserInvalid
? Colors.red
: (allUsersValid ? Colors.green : Colors.orange);
2020-02-22 09:03:44 +01:00
} else if (!widget.room.encrypted &&
widget.room.joinRules != JoinRules.public) {
color = null;
}
return IconButton(
2020-12-06 10:31:35 +01:00
icon: Icon(
widget.room.encrypted
? Icons.lock_outlined
: Icons.lock_open_outlined,
size: 20,
color: color),
2020-02-23 09:31:44 +01:00
onPressed: _enableEncryptionAction,
2020-02-22 09:03:44 +01:00
);
});
}
}