Skip to content

Commit

Permalink
Add a Battery Saver warning to Services
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-edge committed Sep 18, 2024
1 parent da293f2 commit 5a11e7d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- added: Battery Saver warning message for Android

## 4.14.0

- added: `ExpandableList` component, replacing the address hint dropdown in `AddressFormScene`
Expand Down
60 changes: 59 additions & 1 deletion src/components/services/Services.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { asDate, asJSON, asObject, uncleaner } from 'cleaners'
import { EdgeAccount } from 'edge-core-js'
import * as React from 'react'
import { EmitterSubscription } from 'react-native'
import { powerSavingModeChanged, powerSavingOn } from 'react-native-power-saving-mode'

import { updateExchangeInfo } from '../../actions/ExchangeInfoActions'
import { refreshConnectedWallets } from '../../actions/FioActions'
Expand All @@ -11,16 +13,18 @@ import { ENV } from '../../env'
import { useAsyncEffect } from '../../hooks/useAsyncEffect'
import { useHandler } from '../../hooks/useHandler'
import { useRefresher } from '../../hooks/useRefresher'
import { lstrings } from '../../locales/strings'
import { defaultAccount } from '../../reducers/CoreReducer'
import { FooterAccordionEventService } from '../../state/SceneFooterState'
import { useDispatch, useSelector } from '../../types/reactRedux'
import { NavigationBase } from '../../types/routerTypes'
import { height, ratioHorizontal, ratioVertical, width } from '../../util/scaling'
import { snooze } from '../../util/utils'
import { FioCreateHandleModal } from '../modals/FioCreateHandleModal'
import { AlertDropdown } from '../navigation/AlertDropdown'
import { AccountCallbackManager } from './AccountCallbackManager'
import { ActionQueueService } from './ActionQueueService'
import { Airship } from './AirshipInstance'
import { Airship, showDevError } from './AirshipInstance'
import { AutoLogout } from './AutoLogout'
import { ContactsLoader } from './ContactsLoader'
import { EdgeContextCallbackManager } from './EdgeContextCallbackManager'
Expand Down Expand Up @@ -123,6 +127,60 @@ export function Services(props: Props) {
'Services 2'
)

// Subscribe to Android Power Saver state, and show a warning only if it
// changes from off to on:
useAsyncEffect(
async () => {
let previousState = false
let isWarningShowing = false

const showBatterySaverWarning = async () => {
if (isWarningShowing) return

isWarningShowing = true
await Airship.show(bridge => <AlertDropdown bridge={bridge} message={lstrings.warning_battery_saver} warning persistent />)
isWarningShowing = false
}

const checkPowerSavingMode = async () => {
// This method is only available for Android
if (powerSavingOn != null) {
try {
previousState = await powerSavingOn()
if (previousState) {
// Show warning if power-saving mode is on initially
await showBatterySaverWarning()
}
} catch (error) {
showDevError(`Error checking power-saving mode: ${String(error)}`)
}
}
}

const handlePowerSavingModeChanged = async (state: boolean) => {
if (state && !previousState) {
await showBatterySaverWarning()
}
previousState = state
}

await checkPowerSavingMode()

let subscription: EmitterSubscription | undefined
if (powerSavingModeChanged != null) {
subscription = powerSavingModeChanged(handlePowerSavingModeChanged)
}

return () => {
if (subscription != null) {
subscription.remove()
}
}
},
[],
'Services 3'
)

// Methods to call periodically
useRefresher(
async () => {
Expand Down
1 change: 1 addition & 0 deletions src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const strings = {
'The entered token %1$s exists as a built-in token %2$s with the same contract address. Please proceed with caution and verify the contract is legitimate as use of this token can result in loss of funds. If you have questions about this feature please contact %3$s.',
warning_token_exists_1s: 'The entered token already exists as a built-in token %1$s',
warning_uk_risk: `Don't invest unless you're prepared to lose all the money you invest. This is a high-risk investment and you should not expect to be protected if something goes wrong. Take 2 min to learn more.`,
warning_battery_saver: `Battery Saver mode detected. Balances and transactions may be inaccurate`,

// Alert component:
alert_dropdown_alert: 'Alert! ',
Expand Down
1 change: 1 addition & 0 deletions src/locales/strings/enUS.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"warning_token_contract_override_3s": "The entered token %1$s exists as a built-in token %2$s with the same contract address. Please proceed with caution and verify the contract is legitimate as use of this token can result in loss of funds. If you have questions about this feature please contact %3$s.",
"warning_token_exists_1s": "The entered token already exists as a built-in token %1$s",
"warning_uk_risk": "Don't invest unless you're prepared to lose all the money you invest. This is a high-risk investment and you should not expect to be protected if something goes wrong. Take 2 min to learn more.",
"warning_battery_saver": "Battery Saver mode detected. Balances and transactions may be inaccurate",
"alert_dropdown_alert": "Alert! ",
"alert_dropdown_warning": "Warning! ",
"azteco_success": "You've redeemed an Azteco bitcoin card. Funds should arrive shortly.",
Expand Down

0 comments on commit 5a11e7d

Please sign in to comment.