Skip to content
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
40 changes: 20 additions & 20 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ windows = { version = "0.62", features = [
tauri-plugin-single-instance = { version = "2.4.3", features = ["deep-link"] }

[target.'cfg(any(windows, target_os = "linux"))'.dependencies]
tauri-plugin-notifications = { git = "https://github.com/SableClient/tauri-plugin-notifications.git", rev = "308a0e8f3bb9513c3a1fd431fce98312b52c1d40" }
tauri-plugin-notifications = { git = "https://github.com/SableClient/tauri-plugin-notifications.git", rev = "658491c87be246740216f97cc367e29e31e690af" }

# default-features = false drops notify-rust so macOS uses the native
# UNUserNotificationCenter backend (needs a signed .app to deliver).
[target.'cfg(target_os = "macos")'.dependencies]
tauri-plugin-notifications = { git = "https://github.com/SableClient/tauri-plugin-notifications.git", rev = "308a0e8f3bb9513c3a1fd431fce98312b52c1d40", default-features = false }
tauri-plugin-notifications = { git = "https://github.com/SableClient/tauri-plugin-notifications.git", rev = "658491c87be246740216f97cc367e29e31e690af", default-features = false }

[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
tauri-plugin-updater = { version = "2", optional = true }
Expand All @@ -139,7 +139,7 @@ libloading = "0.9"
zbus = "5"

[target.'cfg(any(target_os = "android", target_os = "ios"))'.dependencies]
tauri-plugin-notifications = { git = "https://github.com/SableClient/tauri-plugin-notifications.git", rev = "308a0e8f3bb9513c3a1fd431fce98312b52c1d40", features = [
tauri-plugin-notifications = { git = "https://github.com/SableClient/tauri-plugin-notifications.git", rev = "658491c87be246740216f97cc367e29e31e690af", features = [
"push-notifications",
] }
tauri-plugin-edge-to-edge = { git = "https://github.com/SableClient/tauri-plugin-edge-to-edge.git", rev = "33c6116c27be28c06df5a9d02231ecc5fdeb93c5" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { BatteryOptimizationSetting } from './BatteryOptimization';

const { isIgnoringBatteryOptimizations, requestIgnoreBatteryOptimizations } = vi.hoisted(() => ({
isIgnoringBatteryOptimizations: vi.fn<() => Promise<boolean | null>>(),
requestIgnoreBatteryOptimizations: vi.fn<() => Promise<void>>(),
}));

vi.mock('./UnifiedPushNotifications', () => ({
isIgnoringBatteryOptimizations,
requestIgnoreBatteryOptimizations,
}));

describe('BatteryOptimizationSetting', () => {
beforeEach(() => {
vi.clearAllMocks();
requestIgnoreBatteryOptimizations.mockResolvedValue(undefined);
});

afterEach(() => {
vi.restoreAllMocks();
});

it('prompts when the embedded distributor runs without an exemption', async () => {
isIgnoringBatteryOptimizations.mockResolvedValue(false);

render(<BatteryOptimizationSetting active />);

const allow = await screen.findByRole('button', { name: /allow/i });
await userEvent.click(allow);

expect(requestIgnoreBatteryOptimizations).toHaveBeenCalledOnce();
});

it('stays hidden once the exemption is granted', async () => {
isIgnoringBatteryOptimizations.mockResolvedValue(true);

render(<BatteryOptimizationSetting active />);

await waitFor(() => expect(isIgnoringBatteryOptimizations).toHaveBeenCalled());
expect(screen.queryByRole('button', { name: /allow/i })).not.toBeInTheDocument();
});

it('stays hidden for other distributors', async () => {
render(<BatteryOptimizationSetting active={false} />);

await Promise.resolve();
expect(isIgnoringBatteryOptimizations).not.toHaveBeenCalled();
expect(screen.queryByRole('button', { name: /allow/i })).not.toBeInTheDocument();
});

it('re-checks when the user comes back from the system dialog', async () => {
isIgnoringBatteryOptimizations.mockResolvedValue(false);

render(<BatteryOptimizationSetting active />);
await screen.findByRole('button', { name: /allow/i });

isIgnoringBatteryOptimizations.mockResolvedValue(true);
document.dispatchEvent(new Event('visibilitychange'));

await waitFor(() =>
expect(screen.queryByRole('button', { name: /allow/i })).not.toBeInTheDocument()
);
});
});
70 changes: 70 additions & 0 deletions src/app/features/settings/notifications/BatteryOptimization.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { useCallback, useEffect, useState } from 'react';
import { color, Text } from 'folds';
import { Button } from '$components/button';
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { SettingTile } from '../../../components/setting-tile';
import {
isIgnoringBatteryOptimizations,
requestIgnoreBatteryOptimizations,
} from './UnifiedPushNotifications';

type BatteryOptimizationSettingProps = {
active: boolean;
};

export function BatteryOptimizationSetting({ active }: BatteryOptimizationSettingProps) {
const [ignoring, setIgnoring] = useState<boolean | null>(null);

const refresh = useCallback(() => {
void isIgnoringBatteryOptimizations().then(setIgnoring);
}, []);

const [requestState, requestExemption] = useAsyncCallback(
useCallback(async () => {
await requestIgnoreBatteryOptimizations();
}, [])
);

useEffect(() => {
if (!active) return undefined;

refresh();
const onVisibilityChange = () => {
if (document.visibilityState === 'visible') refresh();
};
document.addEventListener('visibilitychange', onVisibilityChange);

return () => {
document.removeEventListener('visibilitychange', onVisibilityChange);
};
}, [active, refresh]);

if (!active || ignoring !== false) return null;

return (
<SettingTile
title="Battery optimization"
focusId="embedded-push-battery-optimization"
description="Android suspends the built-in distributor's connection while the device sleeps. Allow unrestricted battery use to keep notifications arriving."
after={
<Button
size="300"
radii="300"
variant="Primary"
fill="Soft"
onClick={requestExemption}
loading={requestState.status === AsyncStatus.Loading}
>
<Text size="B300">Allow</Text>
</Button>
}
>
{requestState.status === AsyncStatus.Error && (
<Text as="span" style={{ color: color.Critical.Main }} size="T200">
<br />
Could not open the battery settings. Allow unrestricted battery use for Sable manually.
</Text>
)}
</SettingTile>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
enablePushNotifications,
disablePushNotifications,
} from './PushNotifications';
import { BatteryOptimizationSetting } from './BatteryOptimization';
import { DeregisterAllPushersSetting } from './DeregisterPushNotifications';
import {
disableNativePush,
Expand Down Expand Up @@ -912,6 +913,9 @@ function BackgroundPushNotificationSetting() {
</Text>
)}
</SettingTile>
<BatteryOptimizationSetting
active={upEndpoint?.distributor === EMBEDDED_WEBSOCKET_DISTRIBUTOR}
/>
<NotificationTransportOverrideInput
focusId="unified-push-gateway-url"
title="UnifiedPush Gateway URL"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,23 @@ export async function setEncryptedContentAllowed(allowed: boolean): Promise<void
}

const TAKE_PUSH_DIAGNOSTICS = 'plugin:notifications|take_push_diagnostics';
const IS_IGNORING_BATTERY_OPTIMIZATIONS = 'plugin:notifications|is_ignoring_battery_optimizations';
const REQUEST_IGNORE_BATTERY_OPTIMIZATIONS =
'plugin:notifications|request_ignore_battery_optimizations';

export async function isIgnoringBatteryOptimizations(): Promise<boolean | null> {
if (!isTauri()) return null;
try {
return await invoke<boolean>(IS_IGNORING_BATTERY_OPTIMIZATIONS);
} catch {
return null;
}
}

export async function requestIgnoreBatteryOptimizations(): Promise<void> {
if (!isTauri()) return;
await invoke(REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
}

export type PushDiagnostics = {
counts: Record<string, number>;
Expand Down
Loading