add battery memory

This commit is contained in:
ImUrX authored and Uriel committed 2023-02-02 03:58:17 -03:00
1 parent 5e88f1fe48
commit afc6ff1e4a
4 files changed
+65 -6

No files matched your search

+43 -5
View File
@@ -1,3 +1,4 @@
import { useLocaleConfig } from '../../i18n/config';
import { BatteryIcon } from '../commons/icon/BatteryIcon';
import { Typography } from '../commons/Typography';
@@ -6,28 +7,60 @@ export function TrackerBattery({
voltage,
disabled,
textColor = 'secondary',
deviceId,
}: {
/**
* a [0, 1] value range is expected
*/
value: number;
voltage?: number | null;
disabled?: boolean;
textColor?: string;
deviceId: number | null;
}) {
const percent = value * 100;
const { currentLocales } = useLocaleConfig();
const percentFormatter = new Intl.NumberFormat(currentLocales, {
style: 'percent',
});
const voltageFormatter = new Intl.NumberFormat(currentLocales, {
minimumFractionDigits: 2,
});
const oldBatteryInfo = localStorage.getItem(`deviceBattery_${deviceId}`);
if (!disabled) {
const batteryInfo: BatteryInfo = { value, voltage };
localStorage.setItem(
`deviceBattery_${deviceId}`,
JSON.stringify(batteryInfo)
);
} else if (oldBatteryInfo) {
const batteryInfo: BatteryInfo = JSON.parse(oldBatteryInfo);
value = batteryInfo.value;
voltage = batteryInfo.voltage;
}
// Why did I name it like this
const actuallyDisabled = disabled && !oldBatteryInfo;
return (
<div className="flex gap-2">
<div className="flex flex-col justify-around">
<BatteryIcon value={value} disabled={disabled} />
</div>
{!disabled && (
{!actuallyDisabled && (
<div className="w-10">
<Typography color={textColor}>{percent.toFixed(0)} %</Typography>
<Typography color={textColor}>
{percentFormatter.format(value)}
</Typography>
{voltage && (
<Typography color={textColor}>{voltage.toFixed(2)} V</Typography>
<Typography color={textColor}>
{voltageFormatter.format(voltage)} V
</Typography>
)}
</div>
)}
{disabled && (
{actuallyDisabled && (
<div className="flex flex-col justify-center w-10">
<div className="w-7 h-1 bg-background-30 rounded-full"></div>
</div>
@@ -35,3 +68,8 @@ export function TrackerBattery({
</div>
);
}
interface BatteryInfo {
value: number;
voltage?: number | null;
}
@@ -41,6 +41,7 @@ function TrackerBig({
<TrackerBattery
value={device.hardwareStatus.batteryPctEstimate / 100}
disabled={tracker.status === TrackerStatusEnum.DISCONNECTED}
deviceId={device.id?.id || null}
/>
)}
<div className="flex gap-2">
@@ -87,6 +88,7 @@ function TrackerSmol({
<TrackerBattery
value={device.hardwareStatus.batteryPctEstimate / 100}
disabled={tracker.status === TrackerStatusEnum.DISCONNECTED}
deviceId={device.id?.id || null}
/>
)}
</div>
@@ -268,6 +268,7 @@ export function TrackersTable({
voltage={device.hardwareStatus?.batteryVoltage}
disabled={tracker.status === TrackerStatusEnum.DISCONNECTED}
textColor={fontColor}
deviceId={device.id?.id || null}
/>
),
})}
+19 -1
View File
@@ -1,7 +1,15 @@
import { match } from '@formatjs/intl-localematcher';
import { FluentBundle, FluentResource } from '@fluent/bundle';
import { LocalizationProvider, ReactLocalization } from '@fluent/react';
import { Children, ReactNode, useEffect, useState, createContext } from 'react';
import {
Children,
ReactNode,
useEffect,
useState,
createContext,
useContext,
} from 'react';
import { ConfigContext, ConfigContextC } from '../hooks/config';
export const defaultNS = 'translation';
export const DEFAULT_LOCALE = 'en';
@@ -153,3 +161,13 @@ export function AppLocalizationProvider(props: AppLocalizationProviderProps) {
</>
);
}
export function useLocaleConfig() {
const context = useContext<i18n>(LangContext);
if (!context) {
throw new Error(
'useLocaleConfig must be within a AppLocalization Provider'
);
}
return context;
}