fluffychat/lib/pages/device_settings/user_device_list_item.dart

146 lines
4.3 KiB
Dart
Raw Normal View History

2021-04-15 07:46:43 +00:00
import 'package:flutter/material.dart';
2021-10-26 16:50:34 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-04-15 07:46:43 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 16:50:34 +00:00
import 'package:matrix/matrix.dart';
2021-04-15 07:46:43 +00:00
2021-05-22 06:53:52 +00:00
import '../../utils/date_time_extension.dart';
2022-12-30 16:54:01 +00:00
import '../../utils/matrix_sdk_extensions/device_extension.dart';
2021-11-09 20:32:16 +00:00
import '../../widgets/matrix.dart';
2021-04-15 07:46:43 +00:00
enum UserDeviceListItemAction {
rename,
remove,
verify,
block,
unblock,
}
class UserDeviceListItem extends StatelessWidget {
final Device userDevice;
final void Function(Device) remove;
final void Function(Device) rename;
final void Function(Device) verify;
final void Function(Device) block;
final void Function(Device) unblock;
const UserDeviceListItem(
this.userDevice, {
required this.remove,
required this.rename,
required this.verify,
required this.block,
required this.unblock,
super.key,
});
2021-04-15 07:46:43 +00:00
@override
Widget build(BuildContext context) {
final client = Matrix.of(context).client;
final keys = client.userDeviceKeys[Matrix.of(context).client.userID]
2021-04-15 07:46:43 +00:00
?.deviceKeys[userDevice.deviceId];
final isOwnDevice = userDevice.deviceId == client.deviceID;
2021-04-15 07:46:43 +00:00
return ListTile(
onTap: () async {
final action = await showModalActionSheet<UserDeviceListItemAction>(
context: context,
2021-11-19 09:07:05 +00:00
title: '${userDevice.displayName} (${userDevice.deviceId})',
2021-04-15 07:46:43 +00:00
actions: [
SheetAction(
key: UserDeviceListItemAction.rename,
label: L10n.of(context)!.changeDeviceName,
2021-04-15 07:46:43 +00:00
),
if (!isOwnDevice && keys != null) ...{
2021-09-19 20:02:37 +00:00
SheetAction(
key: UserDeviceListItemAction.verify,
label: L10n.of(context)!.verifyStart,
2021-09-19 20:02:37 +00:00
),
2021-04-15 07:46:43 +00:00
if (!keys.blocked)
SheetAction(
key: UserDeviceListItemAction.block,
label: L10n.of(context)!.blockDevice,
2021-04-15 07:46:43 +00:00
isDestructiveAction: true,
),
if (keys.blocked)
SheetAction(
key: UserDeviceListItemAction.unblock,
label: L10n.of(context)!.unblockDevice,
2021-04-15 07:46:43 +00:00
isDestructiveAction: true,
),
},
if (!isOwnDevice)
SheetAction(
key: UserDeviceListItemAction.remove,
label: L10n.of(context)!.delete,
isDestructiveAction: true,
),
2021-04-15 07:46:43 +00:00
],
);
if (action == null) return;
2021-04-15 07:46:43 +00: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);
break;
case UserDeviceListItemAction.unblock:
unblock(userDevice);
break;
}
},
leading: CircleAvatar(
foregroundColor: Colors.white,
backgroundColor: keys == null
? Colors.grey[700]
: keys.blocked
? Colors.red
: keys.verified
? Colors.green
: Colors.orange,
2021-04-15 07:46:43 +00:00
child: Icon(userDevice.icon),
),
title: Row(
children: <Widget>[
2022-07-07 16:50:13 +00:00
Expanded(
child: Text(
userDevice.displayname,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
2021-04-15 07:46:43 +00:00
),
if (keys != null)
Text(
keys.blocked
? L10n.of(context)!.blocked
2021-04-15 07:46:43 +00:00
: keys.verified
? L10n.of(context)!.verified
: L10n.of(context)!.unverified,
2021-04-15 07:46:43 +00:00
style: TextStyle(
color: keys.blocked
? Colors.red
: keys.verified
? Colors.green
: Colors.orange,
),
),
],
),
2021-11-19 09:07:05 +00:00
subtitle: Text(
L10n.of(context)!.lastActiveAgo(
DateTime.fromMillisecondsSinceEpoch(userDevice.lastSeenTs ?? 0)
.localizedTimeShort(context),
),
2021-11-19 09:07:05 +00:00
style: const TextStyle(fontWeight: FontWeight.w300),
),
2021-04-15 07:46:43 +00:00
);
}
}