feat: Implement change device name

This commit is contained in:
Christian Pauly 2020-10-06 09:01:49 +02:00
parent 9653d0dd7e
commit bfd3888607
2 changed files with 36 additions and 6 deletions

View file

@ -159,6 +159,11 @@
"type": "text", "type": "text",
"placeholders": {} "placeholders": {}
}, },
"changeDeviceName": "Change device name",
"@changeDeviceName": {
"type": "text",
"placeholders": {}
},
"changedTheChatAvatar": "{username} changed the chat avatar", "changedTheChatAvatar": "{username} changed the chat avatar",
"@changedTheChatAvatar": { "@changedTheChatAvatar": {
"type": "text", "type": "text",
@ -1718,4 +1723,4 @@
"type": "text", "type": "text",
"placeholders": {} "placeholders": {}
} }
} }

View file

@ -56,6 +56,19 @@ class DevicesSettingsState extends State<DevicesSettings> {
} }
} }
void _renameDeviceAction(BuildContext context, Device device) async {
final displayName = await SimpleDialogs(context).enterText(
hintText: device.displayName,
labelText: L10n.of(context).changeDeviceName,
);
if (displayName == null) return;
await SimpleDialogs(context).tryRequestWithLoadingDialog(
Matrix.of(context)
.client
.setDeviceMetadata(device.deviceId, displayName: displayName),
);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -117,6 +130,7 @@ class DevicesSettingsState extends State<DevicesSettings> {
itemBuilder: (BuildContext context, int i) => itemBuilder: (BuildContext context, int i) =>
UserDeviceListItem( UserDeviceListItem(
devices[i], devices[i],
rename: (d) => _renameDeviceAction(context, d),
remove: (d) => _removeDevicesAction(context, [d]), remove: (d) => _removeDevicesAction(context, [d]),
), ),
), ),
@ -132,23 +146,34 @@ class DevicesSettingsState extends State<DevicesSettings> {
class UserDeviceListItem extends StatelessWidget { class UserDeviceListItem extends StatelessWidget {
final Device userDevice; final Device userDevice;
final Function remove; final Function remove;
final Function rename;
const UserDeviceListItem(this.userDevice, {this.remove, Key key}) const UserDeviceListItem(this.userDevice, {this.remove, this.rename, Key key})
: super(key: key); : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return PopupMenuButton( return PopupMenuButton(
onSelected: (String action) { onSelected: (String action) {
if (action == 'remove' && remove != null) { switch (action) {
remove(userDevice); case 'remove':
if (remove != null) remove(userDevice);
break;
case 'rename':
if (rename != null) rename(userDevice);
} }
}, },
itemBuilder: (BuildContext context) => [ itemBuilder: (BuildContext context) => [
PopupMenuItem<String>(
value: 'rename',
child: Text(L10n.of(context).changeDeviceName),
),
PopupMenuItem<String>( PopupMenuItem<String>(
value: 'remove', value: 'remove',
child: Text(L10n.of(context).removeDevice, child: Text(
style: TextStyle(color: Colors.red)), L10n.of(context).removeDevice,
style: TextStyle(color: Colors.red),
),
), ),
], ],
child: ListTile( child: ListTile(