2020-11-14 10:08:13 +01:00
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
2021-02-20 10:06:43 +01:00
|
|
|
import 'package:famedlysdk/encryption/utils/key_verification.dart';
|
2020-02-19 16:23:13 +01:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2021-04-09 16:15:03 +02:00
|
|
|
import 'package:fluffychat/views/widgets/dialogs/key_verification_dialog.dart';
|
2021-04-09 18:26:44 +02:00
|
|
|
import 'package:fluffychat/views/widgets/max_width_body.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';
|
2020-10-03 13:11:07 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2020-02-19 16:23:13 +01:00
|
|
|
|
2021-04-09 16:15:03 +02:00
|
|
|
import '../views/widgets/matrix.dart';
|
2020-10-03 13:11:07 +02:00
|
|
|
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,
|
2021-02-24 12:17:23 +01:00
|
|
|
useRootNavigator: false,
|
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,
|
2021-02-24 12:17:23 +01:00
|
|
|
useRootNavigator: false,
|
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
|
|
|
}
|
|
|
|
|
2021-02-20 10:06:43 +01: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);
|
|
|
|
}
|
|
|
|
};
|
2021-02-24 12:17:23 +01:00
|
|
|
await KeyVerificationDialog(request: req).show(context);
|
2021-02-20 10:06:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
),
|
2021-04-09 18:26:44 +02:00
|
|
|
body: MaxWidthBody(
|
|
|
|
child: FutureBuilder<bool>(
|
|
|
|
future: _loadUserDevices(context),
|
|
|
|
builder: (BuildContext context, snapshot) {
|
|
|
|
if (snapshot.hasError) {
|
|
|
|
return Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Icon(Icons.error_outlined),
|
|
|
|
Text(snapshot.error.toString()),
|
|
|
|
],
|
2020-02-19 16:23:13 +01:00
|
|
|
),
|
2021-04-09 18:26:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!snapshot.hasData || this.devices == null) {
|
|
|
|
return Center(child: CircularProgressIndicator());
|
|
|
|
}
|
|
|
|
Function isOwnDevice = (Device userDevice) =>
|
|
|
|
userDevice.deviceId == Matrix.of(context).client.deviceID;
|
|
|
|
final devices = List<Device>.from(this.devices);
|
|
|
|
var thisDevice =
|
|
|
|
devices.firstWhere(isOwnDevice, orElse: () => null);
|
|
|
|
devices.removeWhere(isOwnDevice);
|
|
|
|
devices.sort((a, b) => b.lastSeenTs.compareTo(a.lastSeenTs));
|
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
|
|
|
if (thisDevice != null)
|
|
|
|
UserDeviceListItem(
|
|
|
|
thisDevice,
|
|
|
|
rename: (d) => _renameDeviceAction(context, d),
|
|
|
|
remove: (d) => _removeDevicesAction(context, [d]),
|
|
|
|
verify: (d) => _verifyDeviceAction(context, d),
|
|
|
|
block: (d) => _blockDeviceAction(context, d),
|
|
|
|
unblock: (d) => _unblockDeviceAction(context, d),
|
2020-02-19 16:23:13 +01:00
|
|
|
),
|
2021-04-09 18:26:44 +02:00
|
|
|
Divider(height: 1),
|
|
|
|
if (devices.isNotEmpty)
|
|
|
|
ListTile(
|
|
|
|
title: Text(
|
|
|
|
_errorDeletingDevices ??
|
|
|
|
L10n.of(context).removeAllOtherDevices,
|
|
|
|
style: TextStyle(color: Colors.red),
|
|
|
|
),
|
|
|
|
trailing: _loadingDeletingDevices
|
|
|
|
? CircularProgressIndicator()
|
|
|
|
: Icon(Icons.delete_outline),
|
|
|
|
onTap: _loadingDeletingDevices
|
|
|
|
? null
|
|
|
|
: () => _removeDevicesAction(context, devices),
|
|
|
|
),
|
|
|
|
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],
|
|
|
|
rename: (d) => _renameDeviceAction(context, d),
|
|
|
|
remove: (d) => _removeDevicesAction(context, [d]),
|
|
|
|
verify: (d) => _verifyDeviceAction(context, d),
|
|
|
|
block: (d) => _blockDeviceAction(context, d),
|
|
|
|
unblock: (d) => _unblockDeviceAction(context, d),
|
|
|
|
),
|
2020-02-19 16:23:13 +01:00
|
|
|
),
|
2021-04-09 18:26:44 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2020-02-19 16:23:13 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 10:06:43 +01:00
|
|
|
enum UserDeviceListItemAction {
|
|
|
|
rename,
|
|
|
|
remove,
|
|
|
|
verify,
|
|
|
|
block,
|
2021-02-20 10:28:04 +01:00
|
|
|
unblock,
|
2021-02-20 10:06:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-19 16:23:13 +01:00
|
|
|
class UserDeviceListItem extends StatelessWidget {
|
2020-06-10 10:07:01 +02:00
|
|
|
final Device userDevice;
|
2021-02-20 10:06:43 +01:00
|
|
|
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
|
|
|
|
2021-02-20 10:06:43 +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,
|
2021-02-20 10:06:43 +01:00
|
|
|
Key key,
|
|
|
|
}) : super(key: key);
|
2020-02-19 16:23:13 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-02-20 10:06:43 +01:00
|
|
|
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,
|
2021-02-20 10:06:43 +01:00
|
|
|
),
|
|
|
|
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,
|
|
|
|
),
|
2021-02-20 10:06:43 +01:00
|
|
|
SheetAction(
|
2021-02-20 10:28:04 +01:00
|
|
|
key: UserDeviceListItemAction.remove,
|
2021-02-20 10:06:43 +01:00
|
|
|
label: L10n.of(context).delete,
|
|
|
|
isDestructiveAction: true,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
2020-10-06 09:01:49 +02:00
|
|
|
switch (action) {
|
2021-02-20 10:06:43 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
2021-02-20 10:06:43 +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),
|
2021-02-20 10:06:43 +01:00
|
|
|
),
|
|
|
|
title: Row(
|
|
|
|
children: <Widget>[
|
|
|
|
Text(
|
2021-02-20 10:28:04 +01:00
|
|
|
userDevice.displayname,
|
2021-02-20 10:06:43 +01:00
|
|
|
maxLines: 1,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
2020-10-06 09:01:49 +02:00
|
|
|
),
|
2021-02-20 10:06:43 +01: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
|
|
|
),
|
|
|
|
),
|
2021-02-20 10:06:43 +01:00
|
|
|
],
|
2020-02-19 16:23:13 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|