From e73d85de040e528126530499ffe406eba7db260f Mon Sep 17 00:00:00 2001 From: ItsLemmy Date: Sat, 27 Sep 2025 17:33:09 -0400 Subject: [PATCH] Bluetooth: Removed the copy of the adapter's state in Settings, makes code much simpler and robust by always relying on the actual adapter's state. --- Assets/settings-default.json | 5 +-- Commons/Settings.qml | 3 +- Modules/Bar/Bluetooth/BluetoothPanel.qml | 4 +- Modules/Bar/Widgets/Bluetooth.qml | 2 +- Modules/Settings/Tabs/NetworkTab.qml | 2 +- Services/BluetoothService.qml | 47 +++--------------------- 6 files changed, 13 insertions(+), 50 deletions(-) diff --git a/Assets/settings-default.json b/Assets/settings-default.json index 08d0199a..3fddf7cd 100644 --- a/Assets/settings-default.json +++ b/Assets/settings-default.json @@ -1,5 +1,5 @@ { - "settingsVersion": 11, + "settingsVersion": 12, "bar": { "position": "top", "backgroundOpacity": 1, @@ -118,8 +118,7 @@ "pinnedApps": [] }, "network": { - "wifiEnabled": true, - "bluetoothEnabled": true + "wifiEnabled": true }, "notifications": { "doNotDisturb": false, diff --git a/Commons/Settings.qml b/Commons/Settings.qml index b5fc1751..bed06a98 100644 --- a/Commons/Settings.qml +++ b/Commons/Settings.qml @@ -114,7 +114,7 @@ Singleton { JsonAdapter { id: adapter - property int settingsVersion: 11 + property int settingsVersion: 12 // bar property JsonObject bar: JsonObject { @@ -240,7 +240,6 @@ Singleton { // network property JsonObject network: JsonObject { property bool wifiEnabled: true - property bool bluetoothEnabled: true } // notifications diff --git a/Modules/Bar/Bluetooth/BluetoothPanel.qml b/Modules/Bar/Bluetooth/BluetoothPanel.qml index 3f4773ab..c625f1f1 100644 --- a/Modules/Bar/Bluetooth/BluetoothPanel.qml +++ b/Modules/Bar/Bluetooth/BluetoothPanel.qml @@ -44,13 +44,13 @@ NPanel { NToggle { id: bluetoothSwitch - checked: Settings.data.network.bluetoothEnabled + checked: BluetoothService.enabled onToggled: checked => BluetoothService.setBluetoothEnabled(checked) baseSize: Style.baseWidgetSize * 0.65 * scaling } NIconButton { - enabled: Settings.data.network.bluetoothEnabled + enabled: BluetoothService.enabled icon: BluetoothService.adapter && BluetoothService.adapter.discovering ? "stop" : "refresh" tooltipText: I18n.tr("tooltips.refresh-devices") baseSize: Style.baseWidgetSize * 0.8 diff --git a/Modules/Bar/Widgets/Bluetooth.qml b/Modules/Bar/Widgets/Bluetooth.qml index 04f2757d..ce374bd4 100644 --- a/Modules/Bar/Widgets/Bluetooth.qml +++ b/Modules/Bar/Widgets/Bluetooth.qml @@ -20,7 +20,7 @@ NIconButton { colorBorder: Color.transparent colorBorderHover: Color.transparent - icon: Settings.data.network.bluetoothEnabled ? "bluetooth" : "bluetooth-off" + icon: BluetoothService.enabled ? "bluetooth" : "bluetooth-off" tooltipText: I18n.tr("tooltips.bluetooth-devices") onClicked: PanelService.getPanel("bluetoothPanel")?.toggle(this) onRightClicked: PanelService.getPanel("bluetoothPanel")?.toggle(this) diff --git a/Modules/Settings/Tabs/NetworkTab.qml b/Modules/Settings/Tabs/NetworkTab.qml index 104ac149..b19a0819 100644 --- a/Modules/Settings/Tabs/NetworkTab.qml +++ b/Modules/Settings/Tabs/NetworkTab.qml @@ -23,7 +23,7 @@ ColumnLayout { NToggle { label: I18n.tr("settings.network.bluetooth.label") - checked: Settings.data.network.bluetoothEnabled + checked: BluetoothService.enabled onToggled: checked => BluetoothService.setBluetoothEnabled(checked) } diff --git a/Services/BluetoothService.qml b/Services/BluetoothService.qml index ccfc0439..8820592c 100644 --- a/Services/BluetoothService.qml +++ b/Services/BluetoothService.qml @@ -21,6 +21,7 @@ Singleton { return dev && (dev.paired || dev.trusted) }) } + readonly property var allDevicesWithBattery: { if (!adapter || !adapter.devices) { return [] @@ -30,30 +31,8 @@ Singleton { }) } - property bool lastAdapterState: false - property bool restoringState: false - function init() { Logger.log("Bluetooth", "Service initialized") - // Try to restore saved state if needed - if (Settings.data.network.bluetoothEnabled !== undefined && adapter) { - restoringState = true - adapter.enabled = Settings.data.network.bluetoothEnabled - restoringState = false - } - - // Need to delay a bit for the adapter to be ready - initTimer.running = true - } - - Timer { - id: initTimer - interval: 1000 - repeat: false - onTriggered: { - lastAdapterState = adapter?.enabled ?? false - Logger.log("Bluetooth", "LastAdapterState:", lastAdapterState) - } } Timer { @@ -72,21 +51,11 @@ Singleton { } Logger.log("Bluetooth", "onEnableChanged", adapter.enabled) - - // Only save to settings if this is a user-initiated change - if (!restoringState) { - Settings.data.network.bluetoothEnabled = adapter.enabled - } - - // Show toast only for actual state changes - if (lastAdapterState !== adapter.enabled) { - lastAdapterState = adapter.enabled - if (adapter.enabled) { - ToastService.showNotice(I18n.tr("bluetooth.panel.title"), I18n.tr("toast.bluetooth.enabled")) - discoveryTimer.running = true - } else { - ToastService.showNotice(I18n.tr("bluetooth.panel.title"), I18n.tr("toast.bluetooth.disabled")) - } + if (adapter.enabled) { + ToastService.showNotice(I18n.tr("bluetooth.panel.title"), I18n.tr("toast.bluetooth.enabled")) + discoveryTimer.running = true + } else { + ToastService.showNotice(I18n.tr("bluetooth.panel.title"), I18n.tr("toast.bluetooth.disabled")) } } } @@ -146,14 +115,10 @@ Singleton { if (!device) return false - /* Paired - Means you’ve successfully exchanged keys with the device. - The devices remember each other and can authenticate without repeating the pairing process. - Example: once your headphones are paired, you don’t need to type a PIN every time. Hence, instead of !device.paired, should be device.connected */