Skip to content

Commit

Permalink
Merge pull request #5262 from EdgeApp/jon/detect-android-batt-saver
Browse files Browse the repository at this point in the history
Jon/detect-android-batt-saver
  • Loading branch information
Jon-edge authored Sep 27, 2024
2 parents 009af11 + 071f1f3 commit 15f5719
Show file tree
Hide file tree
Showing 8 changed files with 3,802 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- added: Battery Saver warning message for Android
- added: Buy conversion tracking for Moonpay
- added: Error tracking for failure to report conversions to referral server
- changed: Remove whitespaces from custom token contract address input
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"react-native-patina": "^0.1.6",
"react-native-permissions": "^4.1.5",
"react-native-piratechain": "^0.5.1",
"react-native-power-saving-mode": "^0.1.1",
"react-native-reanimated": "^3.14.0",
"react-native-safari-view": "^2.1.0",
"react-native-safe-area-context": "^4.10.1",
Expand Down
3,735 changes: 3,735 additions & 0 deletions patches/react-native-power-saving-mode+0.1.1.patch

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions src/components/services/Services.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { asDate, asJSON, asObject, uncleaner } from 'cleaners'
import { EdgeAccount } from 'edge-core-js'
import * as React from 'react'
import { EmitterSubscription } from 'react-native'
import { AirshipBridge } from 'react-native-airship'
import { powerSavingModeChanged, powerSavingOn } from 'react-native-power-saving-mode'

import { updateExchangeInfo } from '../../actions/ExchangeInfoActions'
import { refreshConnectedWallets } from '../../actions/FioActions'
Expand All @@ -11,13 +14,15 @@ 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'
Expand Down Expand Up @@ -123,6 +128,52 @@ 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 () => {
// This method is only available for Android
if (powerSavingOn == null) return

let airshipBridge: AirshipBridge<void> | undefined
const handlePowerSavingModeChanged = async (isPowerSavingModeOn: boolean) => {
if (isPowerSavingModeOn && airshipBridge == null) {
await Airship.show(bridge => {
airshipBridge = bridge // Capture the bridge here
return <AlertDropdown bridge={bridge} message={lstrings.warning_battery_saver} warning persistent />
}).then(() => {
airshipBridge = undefined
})
} else if (!isPowerSavingModeOn && airshipBridge != null) {
// Dismiss the alert when power-saving mode turns off and there's an
// active warning that wasn't dismissed
airshipBridge.resolve()
}
}

// Show warning if Power Saver mode is on initially on app boot
await handlePowerSavingModeChanged(await powerSavingOn())

// Subscribe to Power Saver mode changes
let subscription: EmitterSubscription | undefined
if (powerSavingModeChanged != null) {
subscription = powerSavingModeChanged(handlePowerSavingModeChanged)
}

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

// Methods to call periodically
useRefresher(
async () => {
Expand Down
7 changes: 7 additions & 0 deletions src/declare-modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ declare module 'csv-stringify/lib/browser/sync' {
export default function stringify(input: any[], options?: any): string
}

declare module 'react-native-power-saving-mode' {
import { EmitterSubscription } from 'react-native'

export const powerSavingOn: (() => Promise<boolean>) | null
export const powerSavingModeChanged: ((callback: (state: boolean) => void) => EmitterSubscription) | null
}

declare module 'edge-currency-monero/lib/react-native-io'
declare module 'react-native-smart-splash-screen'
declare module 'rn-id-blurview'
1 change: 1 addition & 0 deletions src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const strings = {
'The entered contract address differs from the contract address of built-in token %1$s. 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 or contract please contact %2$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 @@ -81,6 +81,7 @@
"warning_token_code_override_2s": "The entered contract address differs from the contract address of built-in token %1$s. 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 or contract please contact %2$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
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16277,6 +16277,11 @@ react-native-piratechain@^0.5.1:
dependencies:
rfc4648 "^1.3.0"

react-native-power-saving-mode@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/react-native-power-saving-mode/-/react-native-power-saving-mode-0.1.1.tgz#8ec758971979f11c81410751a6ea228abfce269d"
integrity sha512-aWRtlpW55fqjLQDTb/dEw6LqlG0iWx8NZxlhu6QWp5DcvQMzx3EDxCX7RC1h/ofrISCoNisd8Lb7YSa+9SY5VQ==

react-native-reanimated@^3.14.0:
version "3.14.0"
resolved "https://registry.yarnpkg.com/react-native-reanimated/-/react-native-reanimated-3.14.0.tgz#2118265f3a5cad8c142633e36d76d0f0cc8cc9e8"
Expand Down

0 comments on commit 15f5719

Please sign in to comment.