fluffychat/lib/views/settings_devices.dart

234 lines
7.3 KiB
Dart
Raw Normal View History

2020-11-14 10:08:13 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2020-02-19 16:23:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-02-19 16:23:13 +01:00
import '../components/adaptive_page_layout.dart';
import '../components/matrix.dart';
import '../utils/date_time_extension.dart';
2020-02-19 16:23:13 +01:00
import 'chat_list.dart';
class DevicesSettingsView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AdaptivePageLayout(
primaryPage: FocusPage.SECOND,
firstScaffold: ChatList(),
secondScaffold: DevicesSettings(),
);
}
}
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);
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,
) ==
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-11-14 10:08:13 +01:00
final password = await showTextInputDialog(
title: L10n.of(context).pleaseEnterYourPassword,
context: context,
textFields: [
DialogTextField(
hintText: '******',
obscureText: true,
minLines: 1,
maxLines: 1,
2020-11-14 10:08:13 +01:00
)
],
);
2020-06-10 10:07:01 +02:00
if (password == null) return;
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
2020-12-11 10:27:38 +01:00
matrix.client.deleteDevices(
deviceIds,
auth: AuthenticationPassword(
password: password.single,
user: matrix.client.userID,
identifier: AuthenticationUserIdentifier(user: matrix.client.userID),
),
),
);
2020-02-19 16:23:13 +01:00
if (success != false) {
reload();
}
}
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,
textFields: [
DialogTextField(
hintText: device.displayName,
)
],
2020-10-06 09:01:49 +02:00
);
if (displayName == null) return;
2020-10-15 09:34:40 +02:00
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
2020-10-06 09:01:49 +02:00
Matrix.of(context)
.client
2020-11-14 10:08:13 +01:00
.setDeviceMetadata(device.deviceId, displayName: displayName.single),
2020-10-06 09:01:49 +02:00
);
2020-10-15 09:34:40 +02:00
if (success != false) {
reload();
}
2020-10-06 09:01:49 +02:00
}
2020-02-19 16:23:13 +01:00
@override
Widget build(BuildContext context) {
return Scaffold(
2020-05-07 07:52:40 +02:00
appBar: AppBar(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]),
),
Divider(height: 1),
if (devices.isNotEmpty)
ListTile(
title: Text(
2020-05-07 07:52:40 +02:00
L10n.of(context).removeAllOtherDevices,
2020-02-19 16:23:13 +01:00
style: TextStyle(color: Colors.red),
),
trailing: Icon(Icons.delete_outline),
onTap: () => _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],
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]),
),
),
),
],
);
},
),
);
}
}
class UserDeviceListItem extends StatelessWidget {
2020-06-10 10:07:01 +02:00
final Device userDevice;
2020-02-19 16:23:13 +01:00
final Function remove;
2020-10-06 09:01:49 +02:00
final Function rename;
2020-02-19 16:23:13 +01:00
2020-10-06 09:01:49 +02:00
const UserDeviceListItem(this.userDevice, {this.remove, this.rename, Key key})
2020-02-19 16:23:13 +01:00
: super(key: key);
@override
Widget build(BuildContext context) {
return PopupMenuButton(
onSelected: (String action) {
2020-10-06 09:01:49 +02:00
switch (action) {
case 'remove':
if (remove != null) remove(userDevice);
break;
case 'rename':
if (rename != null) rename(userDevice);
2020-02-19 16:23:13 +01:00
}
},
itemBuilder: (BuildContext context) => [
2020-10-06 09:01:49 +02:00
PopupMenuItem<String>(
value: 'rename',
child: Text(L10n.of(context).changeDeviceName),
),
2020-02-19 16:23:13 +01:00
PopupMenuItem<String>(
2020-05-13 15:58:59 +02:00
value: 'remove',
2020-10-06 09:01:49 +02:00
child: Text(
L10n.of(context).removeDevice,
style: TextStyle(color: Colors.red),
),
2020-02-19 16:23:13 +01:00
),
],
child: ListTile(
contentPadding: EdgeInsets.all(16.0),
title: Row(
children: <Widget>[
2020-03-29 12:06:25 +02:00
Expanded(
child: Text(
(userDevice.displayName?.isNotEmpty ?? false)
? userDevice.displayName
2020-05-07 07:52:40 +02:00
: L10n.of(context).unknownDevice,
2020-03-29 12:06:25 +02:00
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
2020-02-19 16:23:13 +01:00
Text(userDevice.lastSeenTs.localizedTimeShort(context)),
],
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
2020-05-13 15:58:59 +02:00
Text('${L10n.of(context).id}: ${userDevice.deviceId}'),
Text('${L10n.of(context).lastSeenIp}: ${userDevice.lastSeenIp}'),
2020-02-19 16:23:13 +01:00
],
),
),
);
}
}