mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
feat(notifications): prompt for a battery exemption on the built-in distributor
This commit is contained in:
4 files changed
+158
No files matched your search
@@ -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()
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
@@ -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() {
|
||||
</Text>
|
||||
)}
|
||||
</SettingTile>
|
||||
<BatteryOptimizationSetting
|
||||
active={upEndpoint?.distributor === EMBEDDED_WEBSOCKET_DISTRIBUTOR}
|
||||
/>
|
||||
<NotificationTransportOverrideInput
|
||||
focusId="unified-push-gateway-url"
|
||||
title="UnifiedPush Gateway URL"
|
||||
|
||||
@@ -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>;
|
||||
|
||||
Reference in new issue
Block a user