fluffychat/lib/views/settings_devices.dart

342 lines
11 KiB
Dart
Raw Normal View History

2020-11-14 10:08:13 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:famedlysdk/encryption/utils/key_verification.dart';
2020-02-19 16:23:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/dialogs/key_verification_dialog.dart';
2020-12-25 09:58:34 +01:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2020-02-19 16:23:13 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-02-19 16:23:13 +01:00
import '../components/matrix.dart';
import '../utils/date_time_extension.dart';
2021-02-20 10:28:04 +01:00
import '../utils/device_extension.dart';
2020-02-19 16:23:13 +01:00
class DevicesSettings extends StatefulWidget {
@override
DevicesSettingsState createState() => DevicesSettingsState();
}
class DevicesSettingsState extends State<DevicesSettings> {
2020-06-10 10:07:01 +02:00
List<Device> devices;
2020-02-19 16:23:13 +01:00
Future<bool> _loadUserDevices(BuildContext context) async {
if (devices != null) return true;
2020-08-16 12:54:43 +02:00
devices = await Matrix.of(context).client.requestDevices();
2020-02-19 16:23:13 +01:00
return true;
}
void reload() => setState(() => devices = null);
2021-01-18 22:59:02 +01:00
bool _loadingDeletingDevices = false;
String _errorDeletingDevices;
2020-06-10 10:07:01 +02:00
void _removeDevicesAction(BuildContext context, List<Device> devices) async {
2020-11-14 10:08:13 +01:00
if (await showOkCancelAlertDialog(
context: context,
title: L10n.of(context).areYouSure,
2021-02-18 14:23:22 +01:00
okLabel: L10n.of(context).yes,
cancelLabel: L10n.of(context).cancel,
2020-11-14 10:08:13 +01:00
) ==
OkCancelResult.cancel) return;
2020-05-13 15:58:59 +02:00
var matrix = Matrix.of(context);
var deviceIds = <String>[];
for (var userDevice in devices) {
2020-02-19 16:23:13 +01:00
deviceIds.add(userDevice.deviceId);
}
2020-06-10 10:07:01 +02:00
2021-01-18 22:59:02 +01:00
try {
setState(() {
_loadingDeletingDevices = true;
_errorDeletingDevices = null;
});
await matrix.client.uiaRequestBackground(
(auth) => matrix.client.deleteDevices(
deviceIds,
auth: auth,
2020-12-11 10:27:38 +01:00
),
2021-01-18 22:59:02 +01:00
);
2020-02-19 16:23:13 +01:00
reload();
2021-01-18 22:59:02 +01:00
} catch (e, s) {
Logs().v('Error while deleting devices', e, s);
setState(() => _errorDeletingDevices = e.toString());
} finally {
setState(() => _loadingDeletingDevices = false);
2020-02-19 16:23:13 +01:00
}
}
2020-10-06 09:01:49 +02:00
void _renameDeviceAction(BuildContext context, Device device) async {
2020-11-14 10:08:13 +01:00
final displayName = await showTextInputDialog(
context: context,
title: L10n.of(context).changeDeviceName,
2021-02-23 11:03:54 +01:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
2020-11-14 10:08:13 +01:00
textFields: [
DialogTextField(
hintText: device.displayName,
)
],
2020-10-06 09:01:49 +02:00
);
if (displayName == null) return;
2020-12-25 09:58:34 +01:00
final success = await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context)
2020-10-06 09:01:49 +02:00
.client
2020-11-14 10:08:13 +01:00
.setDeviceMetadata(device.deviceId, displayName: displayName.single),
2020-10-06 09:01:49 +02:00
);
2020-12-25 09:58:34 +01:00
if (success.error == null) {
2020-10-15 09:34:40 +02:00
reload();
}
2020-10-06 09:01:49 +02:00
}
void _verifyDeviceAction(BuildContext context, Device device) async {
final req = Matrix.of(context)
.client
.userDeviceKeys[Matrix.of(context).client.userID]
.deviceKeys[device.deviceId]
.startVerification();
req.onUpdate = () {
if ({KeyVerificationState.error, KeyVerificationState.done}
.contains(req.state)) {
setState(() => null);
}
};
await KeyVerificationDialog(
request: req,
l10n: L10n.of(context),
).show(context);
}
void _blockDeviceAction(BuildContext context, Device device) async {
final key = Matrix.of(context)
.client
.userDeviceKeys[Matrix.of(context).client.userID]
.deviceKeys[device.deviceId];
if (key.directVerified) {
await key.setVerified(false);
}
await key.setBlocked(true);
setState(() => null);
}
2021-02-20 10:28:04 +01:00
void _unblockDeviceAction(BuildContext context, Device device) async {
final key = Matrix.of(context)
.client
.userDeviceKeys[Matrix.of(context).client.userID]
.deviceKeys[device.deviceId];
await key.setBlocked(false);
setState(() => null);
}
2020-02-19 16:23:13 +01:00
@override
Widget build(BuildContext context) {
return Scaffold(
2021-01-16 14:24:52 +01:00
appBar: AppBar(
leading: BackButton(),
title: Text(L10n.of(context).devices),
),
2020-02-19 16:23:13 +01:00
body: FutureBuilder<bool>(
future: _loadUserDevices(context),
builder: (BuildContext context, snapshot) {
if (snapshot.hasError) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
2020-12-06 10:31:35 +01:00
Icon(Icons.error_outlined),
2020-02-19 16:23:13 +01:00
Text(snapshot.error.toString()),
],
),
);
}
if (!snapshot.hasData || this.devices == null) {
return Center(child: CircularProgressIndicator());
}
2020-06-10 10:07:01 +02:00
Function isOwnDevice = (Device userDevice) =>
2020-02-19 16:23:13 +01:00
userDevice.deviceId == Matrix.of(context).client.deviceID;
2020-06-10 10:07:01 +02:00
final devices = List<Device>.from(this.devices);
2020-05-13 15:58:59 +02:00
var thisDevice = devices.firstWhere(isOwnDevice, orElse: () => null);
2020-02-19 16:23:13 +01:00
devices.removeWhere(isOwnDevice);
2020-03-29 12:06:25 +02:00
devices.sort((a, b) => b.lastSeenTs.compareTo(a.lastSeenTs));
2020-02-19 16:23:13 +01:00
return Column(
children: <Widget>[
if (thisDevice != null)
UserDeviceListItem(
thisDevice,
2020-10-15 09:34:40 +02:00
rename: (d) => _renameDeviceAction(context, d),
2020-02-19 16:23:13 +01:00
remove: (d) => _removeDevicesAction(context, [d]),
verify: (d) => _verifyDeviceAction(context, d),
block: (d) => _blockDeviceAction(context, d),
2021-02-20 10:28:04 +01:00
unblock: (d) => _unblockDeviceAction(context, d),
2020-02-19 16:23:13 +01:00
),
Divider(height: 1),
if (devices.isNotEmpty)
ListTile(
title: Text(
2021-01-18 22:59:02 +01:00
_errorDeletingDevices ??
L10n.of(context).removeAllOtherDevices,
2020-02-19 16:23:13 +01:00
style: TextStyle(color: Colors.red),
),
2021-01-18 22:59:02 +01:00
trailing: _loadingDeletingDevices
? CircularProgressIndicator()
: Icon(Icons.delete_outline),
onTap: _loadingDeletingDevices
? null
: () => _removeDevicesAction(context, devices),
2020-02-19 16:23:13 +01:00
),
Divider(height: 1),
Expanded(
child: devices.isEmpty
? Center(
child: Icon(
Icons.devices_other,
size: 60,
color: Theme.of(context).secondaryHeaderColor,
),
)
: ListView.separated(
separatorBuilder: (BuildContext context, int i) =>
Divider(height: 1),
itemCount: devices.length,
itemBuilder: (BuildContext context, int i) =>
UserDeviceListItem(
devices[i],
2020-10-06 09:01:49 +02:00
rename: (d) => _renameDeviceAction(context, d),
2020-02-19 16:23:13 +01:00
remove: (d) => _removeDevicesAction(context, [d]),
verify: (d) => _verifyDeviceAction(context, d),
block: (d) => _blockDeviceAction(context, d),
2021-02-20 10:28:04 +01:00
unblock: (d) => _unblockDeviceAction(context, d),
2020-02-19 16:23:13 +01:00
),
),
),
],
);
},
),
);
}
}
enum UserDeviceListItemAction {
rename,
remove,
verify,
block,
2021-02-20 10:28:04 +01:00
unblock,
}
2020-02-19 16:23:13 +01:00
class UserDeviceListItem extends StatelessWidget {
2020-06-10 10:07:01 +02:00
final Device userDevice;
final void Function(Device) remove;
final void Function(Device) rename;
final void Function(Device) verify;
final void Function(Device) block;
2021-02-20 10:28:04 +01:00
final void Function(Device) unblock;
2020-02-19 16:23:13 +01:00
const UserDeviceListItem(
this.userDevice, {
@required this.remove,
@required this.rename,
@required this.verify,
@required this.block,
2021-02-20 10:28:04 +01:00
@required this.unblock,
Key key,
}) : super(key: key);
2020-02-19 16:23:13 +01:00
@override
Widget build(BuildContext context) {
final keys = Matrix.of(context)
.client
.userDeviceKeys[Matrix.of(context).client.userID]
?.deviceKeys[userDevice.deviceId];
return ListTile(
onTap: () async {
final action = await showModalActionSheet<UserDeviceListItemAction>(
context: context,
actions: [
SheetAction(
key: UserDeviceListItemAction.rename,
label: L10n.of(context).changeDeviceName,
),
SheetAction(
key: UserDeviceListItemAction.verify,
2021-02-20 10:28:04 +01:00
label: L10n.of(context).verifyStart,
),
if (keys != null) ...{
2021-02-20 10:28:04 +01:00
if (!keys.blocked)
SheetAction(
key: UserDeviceListItemAction.block,
label: L10n.of(context).blockDevice,
isDestructiveAction: true,
),
if (keys.blocked)
SheetAction(
key: UserDeviceListItemAction.unblock,
label: L10n.of(context).unblockDevice,
isDestructiveAction: true,
),
SheetAction(
2021-02-20 10:28:04 +01:00
key: UserDeviceListItemAction.remove,
label: L10n.of(context).delete,
isDestructiveAction: true,
),
},
],
);
2020-10-06 09:01:49 +02:00
switch (action) {
case UserDeviceListItemAction.rename:
rename(userDevice);
break;
case UserDeviceListItemAction.remove:
remove(userDevice);
break;
case UserDeviceListItemAction.verify:
verify(userDevice);
break;
case UserDeviceListItemAction.block:
block(userDevice);
2020-10-06 09:01:49 +02:00
break;
2021-02-20 10:28:04 +01:00
case UserDeviceListItemAction.unblock:
unblock(userDevice);
break;
2020-02-19 16:23:13 +01:00
}
},
leading: CircleAvatar(
foregroundColor: Theme.of(context).textTheme.bodyText1.color,
backgroundColor: Theme.of(context).secondaryHeaderColor,
2021-02-20 10:28:04 +01:00
child: Icon(userDevice.icon),
),
title: Row(
children: <Widget>[
Text(
2021-02-20 10:28:04 +01:00
userDevice.displayname,
maxLines: 1,
overflow: TextOverflow.ellipsis,
2020-10-06 09:01:49 +02:00
),
Spacer(),
Text(userDevice.lastSeenTs.localizedTimeShort(context)),
],
),
subtitle: Row(
children: <Widget>[
Text(userDevice.deviceId),
Spacer(),
if (keys != null)
Text(
keys.blocked
? L10n.of(context).blocked
: keys.verified
? L10n.of(context).verified
: L10n.of(context).unknownDevice,
style: TextStyle(
color: keys.blocked
? Colors.red
: keys.verified
? Colors.green
: Colors.orange,
2020-03-29 12:06:25 +02:00
),
),
],
2020-02-19 16:23:13 +01:00
),
);
}
}