diff --git a/lib/bluetooth/connection.dart b/lib/bluetooth/connection.dart index 29175fe2..8e4619b2 100644 --- a/lib/bluetooth/connection.dart +++ b/lib/bluetooth/connection.dart @@ -52,8 +52,6 @@ class Connection { Timer? _gamePadSearchTimer; - final _dontAllowReconnectDevices = {}; - void initialize() { actionStream.listen((log) { lastLogEntries.add((date: DateTime.now(), entry: log.toString())); @@ -203,9 +201,19 @@ class Connection { } void addDevices(List dev) { - final newDevices = dev - .where((device) => !devices.contains(device) && !_dontAllowReconnectDevices.contains(device.name)) - .toList(); + final ignoredDeviceIds = settings.getIgnoredDeviceIds(); + final newDevices = dev.where((device) { + if (devices.contains(device)) return false; + + // Check if device is in the ignored list + if (device is BluetoothDevice) { + if (ignoredDeviceIds.contains(device.device.deviceId)) { + return false; + } + } + + return true; + }).toList(); devices.addAll(newDevices); _connectionQueue.addAll(newDevices); @@ -330,6 +338,11 @@ class Connection { if (device.isConnected) { await device.disconnect(); } + if (forget && device is BluetoothDevice) { + // Add device to ignored list when forgetting + await settings.addIgnoredDevice(device.device.deviceId, device.name); + _actionStreams.add(LogNotification('Device ignored: ${device.name}')); + } if (!forget && device is BluetoothDevice) { _lastScanResult.removeWhere((b) => b.deviceId == device.device.deviceId); _streamSubscriptions[device]?.cancel(); diff --git a/lib/utils/settings/settings.dart b/lib/utils/settings/settings.dart index 683cad4b..ef709e1a 100644 --- a/lib/utils/settings/settings.dart +++ b/lib/utils/settings/settings.dart @@ -182,4 +182,48 @@ class Settings { Future setMiuiWarningDismissed(bool dismissed) async { await prefs.setBool('miui_warning_dismissed', dismissed); } + + List getIgnoredDeviceIds() { + return prefs.getStringList('ignored_device_ids') ?? []; + } + + List getIgnoredDeviceNames() { + return prefs.getStringList('ignored_device_names') ?? []; + } + + Future addIgnoredDevice(String deviceId, String deviceName) async { + final ids = getIgnoredDeviceIds(); + final names = getIgnoredDeviceNames(); + + if (!ids.contains(deviceId)) { + ids.add(deviceId); + names.add(deviceName); + await prefs.setStringList('ignored_device_ids', ids); + await prefs.setStringList('ignored_device_names', names); + } + } + + Future removeIgnoredDevice(String deviceId) async { + final ids = getIgnoredDeviceIds(); + final names = getIgnoredDeviceNames(); + + final index = ids.indexOf(deviceId); + if (index != -1) { + ids.removeAt(index); + names.removeAt(index); + await prefs.setStringList('ignored_device_ids', ids); + await prefs.setStringList('ignored_device_names', names); + } + } + + List<({String id, String name})> getIgnoredDevices() { + final ids = getIgnoredDeviceIds(); + final names = getIgnoredDeviceNames(); + + final result = <({String id, String name})>[]; + for (int i = 0; i < ids.length && i < names.length; i++) { + result.add((id: ids[i], name: names[i])); + } + return result; + } } diff --git a/lib/widgets/ignored_devices_dialog.dart b/lib/widgets/ignored_devices_dialog.dart new file mode 100644 index 00000000..27f384ed --- /dev/null +++ b/lib/widgets/ignored_devices_dialog.dart @@ -0,0 +1,75 @@ +import 'package:flutter/material.dart'; +import 'package:swift_control/main.dart'; + +class IgnoredDevicesDialog extends StatefulWidget { + const IgnoredDevicesDialog({super.key}); + + @override + State createState() => _IgnoredDevicesDialogState(); +} + +class _IgnoredDevicesDialogState extends State { + List<({String id, String name})> _ignoredDevices = []; + + @override + void initState() { + super.initState(); + _loadIgnoredDevices(); + } + + void _loadIgnoredDevices() { + setState(() { + _ignoredDevices = settings.getIgnoredDevices(); + }); + } + + Future _removeDevice(String deviceId) async { + await settings.removeIgnoredDevice(deviceId); + _loadIgnoredDevices(); + } + + @override + Widget build(BuildContext context) { + return AlertDialog( + title: Text('Ignored Devices'), + content: SizedBox( + width: double.maxFinite, + child: _ignoredDevices.isEmpty + ? Padding( + padding: const EdgeInsets.all(16.0), + child: Text( + 'No ignored devices.', + style: TextStyle( + color: Theme.of(context).colorScheme.onSurfaceVariant, + ), + ), + ) + : ListView.builder( + shrinkWrap: true, + itemCount: _ignoredDevices.length, + itemBuilder: (context, index) { + final device = _ignoredDevices[index]; + return ListTile( + title: Text(device.name), + subtitle: Text( + device.id, + style: TextStyle(fontSize: 12), + ), + trailing: IconButton( + icon: Icon(Icons.delete_outline), + tooltip: 'Remove from ignored list', + onPressed: () => _removeDevice(device.id), + ), + ); + }, + ), + ), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(), + child: Text('Close'), + ), + ], + ); + } +} diff --git a/lib/widgets/menu.dart b/lib/widgets/menu.dart index 08fcc967..e671b75f 100644 --- a/lib/widgets/menu.dart +++ b/lib/widgets/menu.dart @@ -13,6 +13,7 @@ import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher_string.dart'; import '../pages/device.dart'; +import 'ignored_devices_dialog.dart'; List buildMenuButtons() { return [ @@ -174,7 +175,22 @@ class MenuButton extends StatelessWidget { Navigator.push(context, MaterialPageRoute(builder: (c) => MarkdownPage(assetPath: 'CHANGELOG.md'))); }, ), - + PopupMenuItem( + child: Text('Ignored Devices'), + onTap: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (c) => Scaffold( + appBar: AppBar(), + body: Center( + child: IgnoredDevicesDialog(), + ), + ), + ), + ); + }, + ), PopupMenuItem( child: Text('License'), onTap: () {