fluffychat/lib/pages/device_settings/device_settings.dart

144 lines
4.3 KiB
Dart
Raw Permalink Normal View History

2021-10-26 18:50:34 +02:00
import 'package:flutter/material.dart';
2021-04-15 09:46:43 +02:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2022-01-29 12:35:03 +01:00
import 'package:collection/collection.dart' show IterableExtension;
2021-10-26 18:50:34 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/encryption/utils/key_verification.dart';
import 'package:matrix/matrix.dart';
2021-04-15 09:46:43 +02:00
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/device_settings/device_settings_view.dart';
import 'package:fluffychat/pages/key_verification/key_verification_dialog.dart';
import '../../widgets/matrix.dart';
2021-04-15 09:46:43 +02:00
class DevicesSettings extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const DevicesSettings({Key? key}) : super(key: key);
2021-10-14 18:09:30 +02:00
2021-04-15 09:46:43 +02:00
@override
DevicesSettingsController createState() => DevicesSettingsController();
}
class DevicesSettingsController extends State<DevicesSettings> {
2022-01-29 12:35:03 +01:00
List<Device>? devices;
2021-04-15 09:46:43 +02:00
Future<bool> loadUserDevices(BuildContext context) async {
if (devices != null) return true;
2021-05-20 13:59:55 +02:00
devices = await Matrix.of(context).client.getDevices();
2021-04-15 09:46:43 +02:00
return true;
}
void reload() => setState(() => devices = null);
bool loadingDeletingDevices = false;
2022-01-29 12:35:03 +01:00
String? errorDeletingDevices;
2021-04-15 09:46:43 +02:00
void removeDevicesAction(List<Device> devices) async {
if (await showOkCancelAlertDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
2021-04-15 09:46:43 +02:00
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.areYouSure,
okLabel: L10n.of(context)!.yes,
cancelLabel: L10n.of(context)!.cancel,
2021-04-15 09:46:43 +02:00
) ==
OkCancelResult.cancel) return;
final matrix = Matrix.of(context);
final deviceIds = <String>[];
for (final userDevice in devices) {
deviceIds.add(userDevice.deviceId);
}
try {
setState(() {
loadingDeletingDevices = true;
errorDeletingDevices = null;
});
await matrix.client.uiaRequestBackground(
(auth) => matrix.client.deleteDevices(
deviceIds,
auth: auth,
),
);
reload();
} catch (e, s) {
Logs().v('Error while deleting devices', e, s);
setState(() => errorDeletingDevices = e.toString());
} finally {
setState(() => loadingDeletingDevices = false);
}
}
void renameDeviceAction(Device device) async {
final displayName = await showTextInputDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
2021-04-15 09:46:43 +02:00
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.changeDeviceName,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
2021-04-15 09:46:43 +02:00
textFields: [
DialogTextField(
hintText: device.displayName,
)
],
);
if (displayName == null) return;
final success = await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context)
.client
2021-05-20 13:59:55 +02:00
.updateDevice(device.deviceId, displayName: displayName.single),
2021-04-15 09:46:43 +02:00
);
if (success.error == null) {
reload();
}
}
void verifyDeviceAction(Device device) async {
final req = Matrix.of(context)
.client
2022-01-29 12:35:03 +01:00
.userDeviceKeys[Matrix.of(context).client.userID!]!
.deviceKeys[device.deviceId]!
2021-04-15 09:46:43 +02:00
.startVerification();
req.onUpdate = () {
if ({KeyVerificationState.error, KeyVerificationState.done}
.contains(req.state)) {
2022-01-29 12:35:03 +01:00
setState(() {});
2021-04-15 09:46:43 +02:00
}
};
await KeyVerificationDialog(request: req).show(context);
}
void blockDeviceAction(Device device) async {
final key = Matrix.of(context)
.client
2022-01-29 12:35:03 +01:00
.userDeviceKeys[Matrix.of(context).client.userID!]!
.deviceKeys[device.deviceId]!;
2021-04-15 09:46:43 +02:00
if (key.directVerified) {
await key.setVerified(false);
}
await key.setBlocked(true);
2022-01-29 12:35:03 +01:00
setState(() {});
2021-04-15 09:46:43 +02:00
}
void unblockDeviceAction(Device device) async {
final key = Matrix.of(context)
.client
2022-01-29 12:35:03 +01:00
.userDeviceKeys[Matrix.of(context).client.userID!]!
.deviceKeys[device.deviceId]!;
2021-04-15 09:46:43 +02:00
await key.setBlocked(false);
2022-01-29 12:35:03 +01:00
setState(() {});
2021-04-15 09:46:43 +02:00
}
bool _isOwnDevice(Device userDevice) =>
userDevice.deviceId == Matrix.of(context).client.deviceID;
2022-01-29 12:35:03 +01:00
Device? get thisDevice => devices!.firstWhereOrNull(
2021-04-15 09:46:43 +02:00
_isOwnDevice,
);
2022-01-29 12:35:03 +01:00
List<Device> get notThisDevice => List<Device>.from(devices!)
2021-04-15 09:46:43 +02:00
..removeWhere(_isOwnDevice)
2021-08-27 16:56:12 +02:00
..sort((a, b) => (b.lastSeenTs ?? 0).compareTo(a.lastSeenTs ?? 0));
2021-04-15 09:46:43 +02:00
@override
2021-05-22 09:13:47 +02:00
Widget build(BuildContext context) => DevicesSettingsView(this);
2021-04-15 09:46:43 +02:00
}