mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-11-30 01:34:57 +01:00
Implement persistent ignored devices feature
Co-authored-by: jonasbark <1151304+jonasbark@users.noreply.github.com>
This commit is contained in:
@@ -52,8 +52,6 @@ class Connection {
|
||||
|
||||
Timer? _gamePadSearchTimer;
|
||||
|
||||
final _dontAllowReconnectDevices = <String>{};
|
||||
|
||||
void initialize() {
|
||||
actionStream.listen((log) {
|
||||
lastLogEntries.add((date: DateTime.now(), entry: log.toString()));
|
||||
@@ -203,9 +201,19 @@ class Connection {
|
||||
}
|
||||
|
||||
void addDevices(List<BaseDevice> 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();
|
||||
|
||||
@@ -182,4 +182,48 @@ class Settings {
|
||||
Future<void> setMiuiWarningDismissed(bool dismissed) async {
|
||||
await prefs.setBool('miui_warning_dismissed', dismissed);
|
||||
}
|
||||
|
||||
List<String> getIgnoredDeviceIds() {
|
||||
return prefs.getStringList('ignored_device_ids') ?? [];
|
||||
}
|
||||
|
||||
List<String> getIgnoredDeviceNames() {
|
||||
return prefs.getStringList('ignored_device_names') ?? [];
|
||||
}
|
||||
|
||||
Future<void> 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<void> 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;
|
||||
}
|
||||
}
|
||||
|
||||
75
lib/widgets/ignored_devices_dialog.dart
Normal file
75
lib/widgets/ignored_devices_dialog.dart
Normal file
@@ -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<IgnoredDevicesDialog> createState() => _IgnoredDevicesDialogState();
|
||||
}
|
||||
|
||||
class _IgnoredDevicesDialogState extends State<IgnoredDevicesDialog> {
|
||||
List<({String id, String name})> _ignoredDevices = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadIgnoredDevices();
|
||||
}
|
||||
|
||||
void _loadIgnoredDevices() {
|
||||
setState(() {
|
||||
_ignoredDevices = settings.getIgnoredDevices();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _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'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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<Widget> 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: () {
|
||||
|
||||
Reference in New Issue
Block a user