You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.8 KiB
Dart
116 lines
3.8 KiB
Dart
5 years ago
|
import 'dart:async';
|
||
|
|
||
4 years ago
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
||
4 years ago
|
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
|
||
5 years ago
|
import 'package:famedlysdk/famedlysdk.dart';
|
||
|
import 'package:flutter/material.dart';
|
||
4 years ago
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||
5 years ago
|
|
||
4 years ago
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
||
5 years ago
|
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;
|
||
|
|
||
5 years ago
|
void _enableEncryptionAction() async {
|
||
|
if (widget.room.encrypted) {
|
||
4 years ago
|
await AdaptivePageLayout.of(context)
|
||
|
.pushNamed('/rooms/${widget.room.id}/encryption');
|
||
5 years ago
|
return;
|
||
|
}
|
||
4 years ago
|
if (widget.room.joinRules == JoinRules.public) {
|
||
|
await showOkAlertDialog(
|
||
|
context: context,
|
||
|
useRootNavigator: false,
|
||
|
okLabel: L10n.of(context).ok,
|
||
|
message: L10n.of(context).noEncryptionForPublicRooms,
|
||
|
);
|
||
5 years ago
|
return;
|
||
|
}
|
||
4 years ago
|
if (await showOkCancelAlertDialog(
|
||
|
context: context,
|
||
4 years ago
|
useRootNavigator: false,
|
||
4 years ago
|
title: L10n.of(context).enableEncryption,
|
||
4 years ago
|
message: widget.room.client.encryptionEnabled
|
||
4 years ago
|
? L10n.of(context).enableEncryptionWarning
|
||
5 years ago
|
: L10n.of(context).needPantalaimonWarning,
|
||
4 years ago
|
okLabel: L10n.of(context).yes,
|
||
4 years ago
|
cancelLabel: L10n.of(context).cancel,
|
||
5 years ago
|
) ==
|
||
4 years ago
|
OkCancelResult.ok) {
|
||
4 years ago
|
await showFutureLoadingDialog(
|
||
|
context: context,
|
||
|
future: () => widget.room.enableEncryption(),
|
||
5 years ago
|
);
|
||
4 years ago
|
// we want to enable the lock icon
|
||
|
setState(() => null);
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
@override
|
||
|
void dispose() {
|
||
|
_onSyncSub?.cancel();
|
||
|
super.dispose();
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
4 years ago
|
if (widget.room.encrypted) {
|
||
|
_onSyncSub ??= Matrix.of(context)
|
||
|
.client
|
||
|
.onSync
|
||
|
.stream
|
||
|
.where((s) => s.deviceLists != null)
|
||
|
.listen((s) => setState(() => null));
|
||
|
}
|
||
4 years ago
|
return FutureBuilder<List<User>>(
|
||
|
future:
|
||
|
widget.room.encrypted ? widget.room.requestParticipants() : null,
|
||
5 years ago
|
builder: (BuildContext context, snapshot) {
|
||
|
Color color;
|
||
|
if (widget.room.encrypted && snapshot.hasData) {
|
||
4 years ago
|
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;
|
||
|
}
|
||
5 years ago
|
}
|
||
4 years ago
|
color = oneUserInvalid
|
||
|
? Colors.red
|
||
|
: (allUsersValid ? Colors.green : Colors.orange);
|
||
5 years ago
|
} else if (!widget.room.encrypted &&
|
||
|
widget.room.joinRules != JoinRules.public) {
|
||
|
color = null;
|
||
|
}
|
||
|
return IconButton(
|
||
4 years ago
|
tooltip: widget.room.encrypted
|
||
|
? L10n.of(context).encrypted
|
||
|
: L10n.of(context).encryptionNotEnabled,
|
||
4 years ago
|
icon: Icon(
|
||
|
widget.room.encrypted
|
||
|
? Icons.lock_outlined
|
||
|
: Icons.lock_open_outlined,
|
||
|
size: 20,
|
||
|
color: color),
|
||
5 years ago
|
onPressed: _enableEncryptionAction,
|
||
5 years ago
|
);
|
||
|
});
|
||
|
}
|
||
|
}
|