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.

This commit is contained in:
ItsLemmy
2025-09-27 17:33:09 -04:00
parent fafd7a518b
commit e73d85de04
6 changed files with 13 additions and 50 deletions
+2 -3
View File
@@ -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,
+1 -2
View File
@@ -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
+2 -2
View File
@@ -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
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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)
}
+6 -41
View File
@@ -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 youve 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 dont need to type a PIN every time.
Hence, instead of !device.paired, should be device.connected
*/