Skip to content

Commit

Permalink
fixup! 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 26, 2024
1 parent 5a11e7d commit 34eaf37
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions src/components/services/Services.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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'
Expand All @@ -24,7 +25,7 @@ import { FioCreateHandleModal } from '../modals/FioCreateHandleModal'
import { AlertDropdown } from '../navigation/AlertDropdown'
import { AccountCallbackManager } from './AccountCallbackManager'
import { ActionQueueService } from './ActionQueueService'
import { Airship, showDevError } from './AirshipInstance'
import { Airship } from './AirshipInstance'
import { AutoLogout } from './AutoLogout'
import { ContactsLoader } from './ContactsLoader'
import { EdgeContextCallbackManager } from './EdgeContextCallbackManager'
Expand Down Expand Up @@ -131,50 +132,42 @@ export function Services(props: Props) {
// 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)}`)
}
// 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 (airshipBridge != null) {
// Dismiss the alert when power-saving mode turns off and there's an
// active warning that wasn't dismissed
airshipBridge.resolve()
}
}

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

await checkPowerSavingMode()
// 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()
}
}
},
[],
Expand Down

0 comments on commit 34eaf37

Please sign in to comment.