Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Toothpick] Add commands for Toggling devices #12157

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extensions/toothpick/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Toothpick Changelog

## [QoL Improvements] - 2024-05-06

- Added so it's possible to toggle devices

## [Update] - 2024-05-02

- Fixed an error when using deeplink in background
Expand Down
40 changes: 39 additions & 1 deletion extensions/toothpick/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"briankosw",
"oranja",
"sxn",
"pernielsentikaer"
"pernielsentikaer",
"rspeicher"
],
"repository": {
"type": "git",
Expand Down Expand Up @@ -79,6 +80,22 @@
],
"disabledByDefault": true
},
{
"name": "toggle-device",
"title": "Toggle Device",
"subtitle": "Toothpick",
"description": "Shortcut for toggling a connection to a specific device.",
"mode": "no-view",
"arguments": [
{
"name": "nameOrMacAddress",
"placeholder": "Name or Mac Address",
"type": "text",
"required": true
}
],
"disabledByDefault": true
},
{
"name": "connect-favorite-device-1",
"title": "Connect Favorite Device #1",
Expand Down Expand Up @@ -121,6 +138,27 @@
"mode": "no-view",
"disabledByDefault": true
},
{
"name": "toggle-favorite-device-1",
"title": "Toggle Favorite Device #1",
"description": "Shortcut for toggling a connection to favorite device #1.",
"mode": "no-view",
"disabledByDefault": true
},
{
"name": "toggle-favorite-device-2",
"title": "Toggle Favorite Device #2",
"description": "Shortcut for toggling a connection to favorite device #2.",
"mode": "no-view",
"disabledByDefault": true
},
{
"name": "toggle-favorite-device-3",
"title": "Toggle Favorite Device #3",
"description": "Shortcut for toggling a connection to favorite device #3.",
"mode": "no-view",
"disabledByDefault": true
},
{
"name": "view-mapped-devices",
"title": "View Mapped Devices",
Expand Down
38 changes: 38 additions & 0 deletions extensions/toothpick/src/toggle-device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getPreferenceValues } from "@raycast/api";
import { ratio } from "fuzzball";
import { connectDevice } from "./core/devices/handlers/connect-device";
import { disconnectDevice } from "./core/devices/handlers/disconnect-device";
import { getDevicesService } from "./core/devices/devices.service";
import { showErrorMessage } from "./utils";

export default async (props: { arguments: { nameOrMacAddress: string | undefined } }) => {
const { fuzzyRatio, bluetoothBackend } = getPreferenceValues<ExtensionPreferences>();

if (props.arguments.nameOrMacAddress === undefined) {
await showErrorMessage("Undefined value. Check extension preferences.");
return;
}

if (isNaN(parseFloat(fuzzyRatio))) {
await showErrorMessage("Invalid fuzzy ratio. Check extension preferences.");
return;
}

const devices = getDevicesService(bluetoothBackend)?.getDevices() ?? [];

const device = devices.find(
(device) =>
ratio(device.name, props.arguments.nameOrMacAddress || "") > parseFloat(fuzzyRatio) ||
device.macAddress === props.arguments.nameOrMacAddress
);

if (!device) {
await showErrorMessage("Device not found");
} else {
if (device.connected) {
await disconnectDevice(device);
} else {
await connectDevice(device);
}
}
};
8 changes: 8 additions & 0 deletions extensions/toothpick/src/toggle-favorite-device-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getPreferenceValues, updateCommandMetadata } from "@raycast/api";
import toggleFavoriteDevice from "./toggle-device";

export default async () => {
const { favoriteDevice1 } = getPreferenceValues<ExtensionPreferences>();
await updateCommandMetadata({ subtitle: favoriteDevice1 });
await toggleFavoriteDevice({ arguments: { nameOrMacAddress: favoriteDevice1 } });
};
8 changes: 8 additions & 0 deletions extensions/toothpick/src/toggle-favorite-device-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getPreferenceValues, updateCommandMetadata } from "@raycast/api";
import toggleFavoriteDevice from "./toggle-device";

export default async () => {
const { favoriteDevice2 } = getPreferenceValues<ExtensionPreferences>();
await updateCommandMetadata({ subtitle: favoriteDevice2 });
await toggleFavoriteDevice({ arguments: { nameOrMacAddress: favoriteDevice2 } });
};
8 changes: 8 additions & 0 deletions extensions/toothpick/src/toggle-favorite-device-3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getPreferenceValues, updateCommandMetadata } from "@raycast/api";
import toggleFavoriteDevice from "./toggle-device";

export default async () => {
const { favoriteDevice3 } = getPreferenceValues<ExtensionPreferences>();
await updateCommandMetadata({ subtitle: favoriteDevice3 });
await toggleFavoriteDevice({ arguments: { nameOrMacAddress: favoriteDevice3 } });
};