From cfaf900f3f7fe5975b6adbe6cb9cafca7497f3da Mon Sep 17 00:00:00 2001 From: cbxcvl Date: Tue, 3 Mar 2026 16:19:52 -0300 Subject: [PATCH] refactor(bluetooth): address code review feedback - Replace mirrored autoConnectEnabled property with Connections block - Remove no-op _autoConnectInProgress flag (device.connect() is async) - Remove redundant count variable, use devList.length directly --- Services/Networking/BluetoothService.qml | 32 ++++++++++-------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/Services/Networking/BluetoothService.qml b/Services/Networking/BluetoothService.qml index 1e98105d0..36415c0b1 100644 --- a/Services/Networking/BluetoothService.qml +++ b/Services/Networking/BluetoothService.qml @@ -67,15 +67,15 @@ Singleton { // Internal: temporarily pause discovery during pair/connect to reduce HCI churn property bool _discoveryWasRunning: false property bool _ctlInit: false - property bool _autoConnectInProgress: false - // Mirror the setting so we can react to changes via onAutoConnectEnabledChanged - property bool autoConnectEnabled: Settings?.data?.network?.bluetoothAutoConnect ?? true - onAutoConnectEnabledChanged: { - if (autoConnectEnabled && adapter && adapter.enabled) { - autoConnectTimer.restart(); - } else { - autoConnectTimer.stop(); + Connections { + target: Settings.data.network + function onBluetoothAutoConnectChanged() { + if ((Settings?.data?.network?.bluetoothAutoConnect ?? true) && adapter && adapter.enabled) { + autoConnectTimer.restart(); + } else { + autoConnectTimer.stop(); + } } } @@ -105,7 +105,7 @@ Singleton { Quickshell.execDetached(["rfkill", "block", "bluetooth"]); } // Auto-connect on startup if BT is already enabled - if (root.autoConnectEnabled && adapter && adapter.enabled) { + if ((Settings?.data?.network?.bluetoothAutoConnect ?? true) && adapter && adapter.enabled) { autoConnectTimer.restart(); } } @@ -129,7 +129,7 @@ Singleton { checkAirplaneMode.running = true; } function onEnabledChanged() { - if (adapter && adapter.enabled && root.autoConnectEnabled) { + if (adapter && adapter.enabled && (Settings?.data?.network?.bluetoothAutoConnect ?? true)) { autoConnectTimer.restart(); } } @@ -625,30 +625,24 @@ Singleton { } function attemptAutoConnect() { - if (_autoConnectInProgress) return; if (airplaneModeEnabled) return; if (!adapter || !adapter.enabled) return; - if (!autoConnectEnabled) return; - - _autoConnectInProgress = true; + if (!(Settings?.data?.network?.bluetoothAutoConnect ?? true)) return; var devList = adapter.devices.values.filter(function(dev) { return dev && dev.paired && !dev.connected && !dev.blocked; }); - var count = devList.length; for (var i = 0; i < devList.length; i++) { Logger.i("Bluetooth", "Auto-connecting to:", devList[i].name || devList[i].deviceName); connectDeviceWithTrust(devList[i]); } - if (count > 0) { + if (devList.length > 0) { ToastService.showNotice(I18n.tr("common.bluetooth"), I18n.tr("toast.bluetooth.auto-connecting", { - count: count + count: devList.length }), "bluetooth"); } - - _autoConnectInProgress = false; } function connectDeviceWithTrust(device) {