mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2025-12-10 07:25:37 -05:00
bluetooth: cleanup service
This commit is contained in:
@@ -203,6 +203,9 @@ shell.qml # Main entry point (minimal orchestration)
|
|||||||
- Services expose properties and functions
|
- Services expose properties and functions
|
||||||
- Widgets bind to service properties for reactive updates
|
- Widgets bind to service properties for reactive updates
|
||||||
- Use service functions for actions: `ServiceName.performAction(value)`
|
- Use service functions for actions: `ServiceName.performAction(value)`
|
||||||
|
- **CRITICAL**: DO NOT create wrapper functions for everything - bind directly to underlying APIs when possible
|
||||||
|
- Example: Use `BluetoothService.adapter.discovering = true` instead of `BluetoothService.startScan()`
|
||||||
|
- Example: Use `device.connect()` directly instead of `BluetoothService.connect(device.address)`
|
||||||
|
|
||||||
### Error Handling and Debugging
|
### Error Handling and Debugging
|
||||||
|
|
||||||
@@ -320,3 +323,4 @@ When modifying the shell:
|
|||||||
- **Robustness**: Implement feature detection and graceful degradation
|
- **Robustness**: Implement feature detection and graceful degradation
|
||||||
- **Consistency**: Follow Material Design 3 principles via Theme singleton
|
- **Consistency**: Follow Material Design 3 principles via Theme singleton
|
||||||
- **Performance**: Minimize expensive operations and use appropriate data structures
|
- **Performance**: Minimize expensive operations and use appropriate data structures
|
||||||
|
- **NO WRAPPER HELL**: Avoid creating unnecessary wrapper functions - bind directly to underlying APIs for better reactivity and performance
|
||||||
|
|||||||
@@ -21,15 +21,6 @@ Singleton {
|
|||||||
return dev && dev.paired && isValidDevice(dev);
|
return dev && dev.paired && isValidDevice(dev);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
readonly property var availableDevices: {
|
|
||||||
if (!adapter || !adapter.discovering || !Bluetooth.devices)
|
|
||||||
return [];
|
|
||||||
|
|
||||||
var filtered = Bluetooth.devices.values.filter((dev) => {
|
|
||||||
return dev && !dev.paired && !dev.pairing && !dev.blocked && isValidDevice(dev) && (dev.signalStrength === undefined || dev.signalStrength > 0);
|
|
||||||
});
|
|
||||||
return sortBySignalStrength(filtered);
|
|
||||||
}
|
|
||||||
readonly property var allDevicesWithBattery: {
|
readonly property var allDevicesWithBattery: {
|
||||||
if (!adapter || !adapter.devices)
|
if (!adapter || !adapter.devices)
|
||||||
return [];
|
return [];
|
||||||
@@ -39,8 +30,17 @@ Singleton {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortBySignalStrength(devices) {
|
function sortDevices(devices) {
|
||||||
return devices.sort((a, b) => {
|
return devices.sort((a, b) => {
|
||||||
|
var aName = a.name || a.deviceName || "";
|
||||||
|
var bName = b.name || b.deviceName || "";
|
||||||
|
|
||||||
|
var aHasRealName = aName.includes(" ") && aName.length > 3;
|
||||||
|
var bHasRealName = bName.includes(" ") && bName.length > 3;
|
||||||
|
|
||||||
|
if (aHasRealName && !bHasRealName) return -1;
|
||||||
|
if (!aHasRealName && bHasRealName) return 1;
|
||||||
|
|
||||||
var aSignal = (a.signalStrength !== undefined && a.signalStrength > 0) ? a.signalStrength : 0;
|
var aSignal = (a.signalStrength !== undefined && a.signalStrength > 0) ? a.signalStrength : 0;
|
||||||
var bSignal = (b.signalStrength !== undefined && b.signalStrength > 0) ? b.signalStrength : 0;
|
var bSignal = (b.signalStrength !== undefined && b.signalStrength > 0) ? b.signalStrength : 0;
|
||||||
return bSignal - aSignal;
|
return bSignal - aSignal;
|
||||||
@@ -121,7 +121,7 @@ Singleton {
|
|||||||
return "bluetooth";
|
return "bluetooth";
|
||||||
}
|
}
|
||||||
|
|
||||||
function canPair(device) {
|
function canConnect(device) {
|
||||||
if (!device)
|
if (!device)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -132,21 +132,6 @@ Singleton {
|
|||||||
console.log("Device:", device.name, "paired:", device.paired, "connected:", device.connected, "signalStrength:", device.signalStrength);
|
console.log("Device:", device.name, "paired:", device.paired, "connected:", device.connected, "signalStrength:", device.signalStrength);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPairingStatus(device) {
|
|
||||||
if (!device)
|
|
||||||
return "unknown";
|
|
||||||
|
|
||||||
if (device.pairing)
|
|
||||||
return "pairing";
|
|
||||||
|
|
||||||
if (device.paired)
|
|
||||||
return "paired";
|
|
||||||
|
|
||||||
if (device.blocked)
|
|
||||||
return "blocked";
|
|
||||||
|
|
||||||
return "available";
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSignalStrength(device) {
|
function getSignalStrength(device) {
|
||||||
if (!device || device.signalStrength === undefined || device.signalStrength <= 0)
|
if (!device || device.signalStrength === undefined || device.signalStrength <= 0)
|
||||||
@@ -188,71 +173,5 @@ Singleton {
|
|||||||
return "signal_cellular_0_bar";
|
return "signal_cellular_0_bar";
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleAdapter() {
|
|
||||||
if (adapter)
|
|
||||||
adapter.enabled = !adapter.enabled;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function startScan() {
|
|
||||||
if (adapter)
|
|
||||||
adapter.discovering = true;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function stopScan() {
|
|
||||||
if (adapter)
|
|
||||||
adapter.discovering = false;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function connect(address) {
|
|
||||||
var device = _findDevice(address);
|
|
||||||
if (device)
|
|
||||||
device.connect();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function disconnect(address) {
|
|
||||||
var device = _findDevice(address);
|
|
||||||
if (device)
|
|
||||||
device.disconnect();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function pair(address) {
|
|
||||||
var device = _findDevice(address);
|
|
||||||
if (device && canPair(device))
|
|
||||||
device.pair();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function forget(address) {
|
|
||||||
var device = _findDevice(address);
|
|
||||||
if (device)
|
|
||||||
device.forget();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function toggle(address) {
|
|
||||||
var device = _findDevice(address);
|
|
||||||
if (device) {
|
|
||||||
if (device.connected)
|
|
||||||
device.disconnect();
|
|
||||||
else
|
|
||||||
device.connect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function _findDevice(address) {
|
|
||||||
if (!adapter)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return adapter.devices.values.find((d) => {
|
|
||||||
return d && d.address === address;
|
|
||||||
}) || (Bluetooth.devices ? Bluetooth.devices.values.find((d) => {
|
|
||||||
return d && d.address === address;
|
|
||||||
}) : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,9 @@ Item {
|
|||||||
hoverEnabled: true
|
hoverEnabled: true
|
||||||
cursorShape: Qt.PointingHandCursor
|
cursorShape: Qt.PointingHandCursor
|
||||||
onClicked: {
|
onClicked: {
|
||||||
BluetoothService.toggleAdapter();
|
if (BluetoothService.adapter) {
|
||||||
|
BluetoothService.adapter.enabled = !BluetoothService.adapter.enabled;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,7 +210,11 @@ Item {
|
|||||||
cursorShape: Qt.PointingHandCursor
|
cursorShape: Qt.PointingHandCursor
|
||||||
onClicked: {
|
onClicked: {
|
||||||
BluetoothService.debugDevice(modelData);
|
BluetoothService.debugDevice(modelData);
|
||||||
BluetoothService.toggle(modelData.address);
|
if (modelData.connected) {
|
||||||
|
modelData.disconnect();
|
||||||
|
} else {
|
||||||
|
modelData.connect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -279,10 +285,9 @@ Item {
|
|||||||
hoverEnabled: true
|
hoverEnabled: true
|
||||||
cursorShape: Qt.PointingHandCursor
|
cursorShape: Qt.PointingHandCursor
|
||||||
onClicked: {
|
onClicked: {
|
||||||
if (BluetoothService.adapter && BluetoothService.adapter.discovering)
|
if (BluetoothService.adapter) {
|
||||||
BluetoothService.stopScan();
|
BluetoothService.adapter.discovering = !BluetoothService.adapter.discovering;
|
||||||
else
|
}
|
||||||
BluetoothService.startScan();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,11 +296,18 @@ Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Repeater {
|
Repeater {
|
||||||
model: BluetoothService.availableDevices
|
model: {
|
||||||
|
if (!BluetoothService.adapter || !BluetoothService.adapter.discovering || !Bluetooth.devices)
|
||||||
|
return [];
|
||||||
|
|
||||||
|
var filtered = Bluetooth.devices.values.filter((dev) => {
|
||||||
|
return dev && !dev.paired && !dev.pairing && !dev.blocked && BluetoothService.isValidDevice(dev) && (dev.signalStrength === undefined || dev.signalStrength > 0);
|
||||||
|
});
|
||||||
|
return BluetoothService.sortDevices(filtered);
|
||||||
|
}
|
||||||
|
|
||||||
Rectangle {
|
Rectangle {
|
||||||
property bool canPair: BluetoothService.canPair(modelData)
|
property bool canConnect: BluetoothService.canConnect(modelData)
|
||||||
property string pairingStatus: BluetoothService.getPairingStatus(modelData)
|
|
||||||
|
|
||||||
width: parent.width
|
width: parent.width
|
||||||
height: 70
|
height: 70
|
||||||
@@ -372,14 +384,11 @@ Item {
|
|||||||
|
|
||||||
Text {
|
Text {
|
||||||
text: {
|
text: {
|
||||||
switch (pairingStatus) {
|
if (modelData.pairing)
|
||||||
case "pairing":
|
|
||||||
return "Pairing...";
|
return "Pairing...";
|
||||||
case "blocked":
|
if (modelData.blocked)
|
||||||
return "Blocked";
|
return "Blocked";
|
||||||
default:
|
return BluetoothService.getSignalStrength(modelData);
|
||||||
return BluetoothService.getSignalStrength(modelData);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: {
|
color: {
|
||||||
@@ -398,14 +407,14 @@ Item {
|
|||||||
font.family: Theme.iconFont
|
font.family: Theme.iconFont
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: Qt.rgba(Theme.surfaceText.r, Theme.surfaceText.g, Theme.surfaceText.b, 0.7)
|
color: Qt.rgba(Theme.surfaceText.r, Theme.surfaceText.g, Theme.surfaceText.b, 0.7)
|
||||||
visible: modelData.signalStrength !== undefined && modelData.signalStrength > 0 && pairingStatus === "available"
|
visible: modelData.signalStrength !== undefined && modelData.signalStrength > 0 && !modelData.pairing && !modelData.blocked
|
||||||
}
|
}
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
text: (modelData.signalStrength !== undefined && modelData.signalStrength > 0) ? modelData.signalStrength + "%" : ""
|
text: (modelData.signalStrength !== undefined && modelData.signalStrength > 0) ? modelData.signalStrength + "%" : ""
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: Qt.rgba(Theme.surfaceText.r, Theme.surfaceText.g, Theme.surfaceText.b, 0.5)
|
color: Qt.rgba(Theme.surfaceText.r, Theme.surfaceText.g, Theme.surfaceText.b, 0.5)
|
||||||
visible: modelData.signalStrength !== undefined && modelData.signalStrength > 0 && pairingStatus === "available"
|
visible: modelData.signalStrength !== undefined && modelData.signalStrength > 0 && !modelData.pairing && !modelData.blocked
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -424,7 +433,7 @@ Item {
|
|||||||
anchors.rightMargin: Theme.spacingM
|
anchors.rightMargin: Theme.spacingM
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
color: {
|
color: {
|
||||||
if (!canPair && !modelData.pairing)
|
if (!canConnect && !modelData.pairing)
|
||||||
return Qt.rgba(Theme.surfaceVariant.r, Theme.surfaceVariant.g, Theme.surfaceVariant.b, 0.3);
|
return Qt.rgba(Theme.surfaceVariant.r, Theme.surfaceVariant.g, Theme.surfaceVariant.b, 0.3);
|
||||||
|
|
||||||
if (actionButtonArea.containsMouse)
|
if (actionButtonArea.containsMouse)
|
||||||
@@ -432,9 +441,9 @@ Item {
|
|||||||
|
|
||||||
return "transparent";
|
return "transparent";
|
||||||
}
|
}
|
||||||
border.color: canPair || modelData.pairing ? Theme.primary : Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.2)
|
border.color: canConnect || modelData.pairing ? Theme.primary : Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.2)
|
||||||
border.width: 1
|
border.width: 1
|
||||||
opacity: canPair || modelData.pairing ? 1 : 0.5
|
opacity: canConnect || modelData.pairing ? 1 : 0.5
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
anchors.centerIn: parent
|
anchors.centerIn: parent
|
||||||
@@ -445,10 +454,10 @@ Item {
|
|||||||
if (modelData.blocked)
|
if (modelData.blocked)
|
||||||
return "Blocked";
|
return "Blocked";
|
||||||
|
|
||||||
return "Pair";
|
return "Connect";
|
||||||
}
|
}
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: canPair || modelData.pairing ? Theme.primary : Qt.rgba(Theme.surfaceText.r, Theme.surfaceText.g, Theme.surfaceText.b, 0.5)
|
color: canConnect || modelData.pairing ? Theme.primary : Qt.rgba(Theme.surfaceText.r, Theme.surfaceText.g, Theme.surfaceText.b, 0.5)
|
||||||
font.weight: Font.Medium
|
font.weight: Font.Medium
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -457,12 +466,15 @@ Item {
|
|||||||
|
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
hoverEnabled: true
|
hoverEnabled: true
|
||||||
cursorShape: canPair ? Qt.PointingHandCursor : Qt.ArrowCursor
|
cursorShape: canConnect ? Qt.PointingHandCursor : Qt.ArrowCursor
|
||||||
enabled: canPair
|
enabled: canConnect
|
||||||
onClicked: {
|
onClicked: {
|
||||||
if (canPair)
|
if (canConnect) {
|
||||||
BluetoothService.pair(modelData.address);
|
if (BluetoothService.adapter && BluetoothService.adapter.discovering) {
|
||||||
|
BluetoothService.adapter.discovering = false;
|
||||||
|
}
|
||||||
|
modelData.connect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -474,12 +486,15 @@ Item {
|
|||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
anchors.rightMargin: 90 // Don't overlap with action button
|
anchors.rightMargin: 90 // Don't overlap with action button
|
||||||
hoverEnabled: true
|
hoverEnabled: true
|
||||||
cursorShape: canPair ? Qt.PointingHandCursor : Qt.ArrowCursor
|
cursorShape: canConnect ? Qt.PointingHandCursor : Qt.ArrowCursor
|
||||||
enabled: canPair
|
enabled: canConnect
|
||||||
onClicked: {
|
onClicked: {
|
||||||
if (canPair)
|
if (canConnect) {
|
||||||
BluetoothService.pair(modelData.address);
|
if (BluetoothService.adapter && BluetoothService.adapter.discovering) {
|
||||||
|
BluetoothService.adapter.discovering = false;
|
||||||
|
}
|
||||||
|
modelData.connect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -490,7 +505,16 @@ Item {
|
|||||||
Column {
|
Column {
|
||||||
width: parent.width
|
width: parent.width
|
||||||
spacing: Theme.spacingM
|
spacing: Theme.spacingM
|
||||||
visible: BluetoothService.adapter && BluetoothService.adapter.discovering && BluetoothService.availableDevices.length === 0
|
visible: {
|
||||||
|
if (!BluetoothService.adapter || !BluetoothService.adapter.discovering || !Bluetooth.devices)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var availableCount = Bluetooth.devices.values.filter((dev) => {
|
||||||
|
return dev && !dev.paired && !dev.pairing && !dev.blocked && BluetoothService.isValidDevice(dev) && (dev.signalStrength === undefined || dev.signalStrength > 0);
|
||||||
|
}).length;
|
||||||
|
|
||||||
|
return availableCount === 0;
|
||||||
|
}
|
||||||
|
|
||||||
Row {
|
Row {
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
anchors.horizontalCenter: parent.horizontalCenter
|
||||||
@@ -536,7 +560,16 @@ Item {
|
|||||||
text: "No devices found. Put your device in pairing mode and click Start Scanning."
|
text: "No devices found. Put your device in pairing mode and click Start Scanning."
|
||||||
font.pixelSize: Theme.fontSizeMedium
|
font.pixelSize: Theme.fontSizeMedium
|
||||||
color: Qt.rgba(Theme.surfaceText.r, Theme.surfaceText.g, Theme.surfaceText.b, 0.7)
|
color: Qt.rgba(Theme.surfaceText.r, Theme.surfaceText.g, Theme.surfaceText.b, 0.7)
|
||||||
visible: BluetoothService.availableDevices.length === 0 && (!BluetoothService.adapter || !BluetoothService.adapter.discovering)
|
visible: {
|
||||||
|
if (!BluetoothService.adapter || !Bluetooth.devices)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
var availableCount = Bluetooth.devices.values.filter((dev) => {
|
||||||
|
return dev && !dev.paired && !dev.pairing && !dev.blocked && BluetoothService.isValidDevice(dev) && (dev.signalStrength === undefined || dev.signalStrength > 0);
|
||||||
|
}).length;
|
||||||
|
|
||||||
|
return availableCount === 0 && !BluetoothService.adapter.discovering;
|
||||||
|
}
|
||||||
wrapMode: Text.WordWrap
|
wrapMode: Text.WordWrap
|
||||||
width: parent.width
|
width: parent.width
|
||||||
horizontalAlignment: Text.AlignHCenter
|
horizontalAlignment: Text.AlignHCenter
|
||||||
@@ -641,9 +674,13 @@ Item {
|
|||||||
hoverEnabled: true
|
hoverEnabled: true
|
||||||
cursorShape: Qt.PointingHandCursor
|
cursorShape: Qt.PointingHandCursor
|
||||||
onClicked: {
|
onClicked: {
|
||||||
if (bluetoothContextMenuWindow.deviceData)
|
if (bluetoothContextMenuWindow.deviceData) {
|
||||||
BluetoothService.toggle(bluetoothContextMenuWindow.deviceData.address);
|
if (bluetoothContextMenuWindow.deviceData.connected) {
|
||||||
|
bluetoothContextMenuWindow.deviceData.disconnect();
|
||||||
|
} else {
|
||||||
|
bluetoothContextMenuWindow.deviceData.connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
bluetoothContextMenuWindow.hide();
|
bluetoothContextMenuWindow.hide();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -711,9 +748,9 @@ Item {
|
|||||||
hoverEnabled: true
|
hoverEnabled: true
|
||||||
cursorShape: Qt.PointingHandCursor
|
cursorShape: Qt.PointingHandCursor
|
||||||
onClicked: {
|
onClicked: {
|
||||||
if (bluetoothContextMenuWindow.deviceData)
|
if (bluetoothContextMenuWindow.deviceData) {
|
||||||
BluetoothService.forget(bluetoothContextMenuWindow.deviceData.address);
|
bluetoothContextMenuWindow.deviceData.forget();
|
||||||
|
}
|
||||||
bluetoothContextMenuWindow.hide();
|
bluetoothContextMenuWindow.hide();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ PanelWindow {
|
|||||||
onVisibleChanged: {
|
onVisibleChanged: {
|
||||||
// Enable/disable WiFi auto-refresh based on control center visibility
|
// Enable/disable WiFi auto-refresh based on control center visibility
|
||||||
WifiService.autoRefreshEnabled = visible && NetworkService.wifiEnabled;
|
WifiService.autoRefreshEnabled = visible && NetworkService.wifiEnabled;
|
||||||
|
// Stop bluetooth scanning when control center is closed
|
||||||
|
if (!visible && BluetoothService.adapter && BluetoothService.adapter.discovering) {
|
||||||
|
BluetoothService.adapter.discovering = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
implicitWidth: 600
|
implicitWidth: 600
|
||||||
implicitHeight: 500
|
implicitHeight: 500
|
||||||
|
|||||||
Reference in New Issue
Block a user