Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jon/detect-android-batt-saver #5262

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause the alert to be dismissed if isPowerSavingModeOn and the modal is showing. While you shouldn't get this case, you are relying on distributed logic to ensure this doesn't break. Logic in the SDK in addition to logic in your code.

This should be

if (isPowerSavingModeOn && airshipBridge == null) {
   // showmodal
} else if (!isPowerSavingModeOn && airshipBridge != null) {
    // close modal
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I originally had it like this but assumed you'd prefer it less verbose since it shouldn't happen

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verbose isn't bad if it makes the logic crystal clear.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to hear, that's my preference too

// 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
Loading