From e7620ec4195ecf4b8e95f87eaa33ef5bc827c871 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Thu, 10 Sep 2026 22:33:05 +0200 Subject: [PATCH] feat(notifications): prompt for a battery exemption on the built-in distributor --- .../BatteryOptimization.test.tsx | 67 ++++++++++++++++++ .../notifications/BatteryOptimization.tsx | 70 +++++++++++++++++++ .../notifications/SystemNotification.tsx | 4 ++ .../notifications/UnifiedPushNotifications.ts | 17 +++++ 4 files changed, 158 insertions(+) create mode 100644 src/app/features/settings/notifications/BatteryOptimization.test.tsx create mode 100644 src/app/features/settings/notifications/BatteryOptimization.tsx diff --git a/src/app/features/settings/notifications/BatteryOptimization.test.tsx b/src/app/features/settings/notifications/BatteryOptimization.test.tsx new file mode 100644 index 000000000..8650ddee2 --- /dev/null +++ b/src/app/features/settings/notifications/BatteryOptimization.test.tsx @@ -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>(), + requestIgnoreBatteryOptimizations: vi.fn<() => Promise>(), +})); + +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(); + + 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(); + + await waitFor(() => expect(isIgnoringBatteryOptimizations).toHaveBeenCalled()); + expect(screen.queryByRole('button', { name: /allow/i })).not.toBeInTheDocument(); + }); + + it('stays hidden for other distributors', async () => { + render(); + + 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(); + 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() + ); + }); +}); diff --git a/src/app/features/settings/notifications/BatteryOptimization.tsx b/src/app/features/settings/notifications/BatteryOptimization.tsx new file mode 100644 index 000000000..1c4537653 --- /dev/null +++ b/src/app/features/settings/notifications/BatteryOptimization.tsx @@ -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(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 ( + + Allow + + } + > + {requestState.status === AsyncStatus.Error && ( + +
+ Could not open the battery settings. Allow unrestricted battery use for Sable manually. +
+ )} +
+ ); +} diff --git a/src/app/features/settings/notifications/SystemNotification.tsx b/src/app/features/settings/notifications/SystemNotification.tsx index 54301c166..a71ad7e18 100644 --- a/src/app/features/settings/notifications/SystemNotification.tsx +++ b/src/app/features/settings/notifications/SystemNotification.tsx @@ -37,6 +37,7 @@ import { enablePushNotifications, disablePushNotifications, } from './PushNotifications'; +import { BatteryOptimizationSetting } from './BatteryOptimization'; import { DeregisterAllPushersSetting } from './DeregisterPushNotifications'; import { disableNativePush, @@ -912,6 +913,9 @@ function BackgroundPushNotificationSetting() { )} + { + if (!isTauri()) return null; + try { + return await invoke(IS_IGNORING_BATTERY_OPTIMIZATIONS); + } catch { + return null; + } +} + +export async function requestIgnoreBatteryOptimizations(): Promise { + if (!isTauri()) return; + await invoke(REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); +} export type PushDiagnostics = { counts: Record;