Skip to content

Commit

Permalink
fix(mobile): Battery fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
voloshinskii committed Jan 16, 2024
1 parent 4462f01 commit 169bcd7
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 20 deletions.
4 changes: 4 additions & 0 deletions packages/@core-js/src/Tonkeeper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { BatteryAPI } from './BatteryAPI';
import { signProofForTonkeeper } from './utils/tonProof';
import { WalletContractV4 } from '@ton/ton';
import nacl from 'tweetnacl';
import { batteryState } from './managers/BatteryManager';

class PermissionsManager {
public notifications = true;
Expand Down Expand Up @@ -183,6 +184,7 @@ export class Tonkeeper {
private preload() {
this.wallet.activityList.preload();
this.wallet.tonInscriptions.preload();
this.wallet.battery.fetchBalance();
// TODO:
this.wallet.subscriptions.prefetch();
this.wallet.balances.prefetch();
Expand All @@ -193,6 +195,7 @@ export class Tonkeeper {
this.wallet.jettonActivityList.rehydrate();
this.wallet.tonActivityList.rehydrate();
this.wallet.activityList.rehydrate();
this.wallet.battery.rehydrate();
}

public async lock() {
Expand Down Expand Up @@ -235,6 +238,7 @@ export class Tonkeeper {
}

public destroy() {
batteryState.clear();
this.wallet?.destroy();
this.queryClient.clear();
this.wallet = null!;
Expand Down
2 changes: 1 addition & 1 deletion packages/@core-js/src/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class Wallet {
this.balances = new BalancesManager(context);
this.nfts = new NftsManager(context);
this.tronService = new TronService(context);
this.battery = new BatteryManager(context, this.identity);
this.battery = new BatteryManager(context, this.identity, this.storage);

this.listenTransactions();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const InsufficientFundsModal = memo<InsufficientFundsParams>((props) => {
fee,
isStakingDeposit,
} = props;
const { data: batteryBalance } = useBatteryBalance();
const { batteryBalance } = useBatteryBalance();
const nav = useNavigation();
const formattedAmount = useMemo(
() => formatter.format(fromNano(totalAmount, decimals), { decimals }),
Expand Down
1 change: 1 addition & 0 deletions packages/mobile/src/store/wallet/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ function* refreshBalancesPageWorker(action: RefreshBalancesPageAction) {
yield call(setLastRefreshedAt, Date.now());
yield put(subscriptionsActions.loadSubscriptions());
yield call([tk.wallet.tonInscriptions, 'getInscriptions']);
yield call([tk.wallet.battery, 'fetchBalance']);
} catch (e) {
yield put(walletActions.endRefreshBalancesPage());
}
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/components/BatteryIcon/BatteryIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const iconNames: { [key: string]: IconNames } = {
const hitSlop = { top: 8, bottom: 8, right: 8, left: 8 };

export const BatteryIcon = memo(() => {
const { data: balance } = useBatteryBalance();
const { balance } = useBatteryBalance();
if (!balance || balance === '0' || config.get('disable_battery')) return null;

const iconName = iconNames[getBatteryState(balance)];
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/components/RefillBattery/RefillBattery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const iconNames: { [key: string]: IconNames } = {
};

export const RefillBattery = memo(() => {
const { data: balance } = useBatteryBalance();
const { balance } = useBatteryBalance();
const batteryState = getBatteryState(balance ?? '0');
const iconName = iconNames[batteryState];

Expand Down
18 changes: 9 additions & 9 deletions packages/shared/query/hooks/useBatteryBalance.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { tk } from '../../tonkeeper';
import { useQuery } from 'react-query';
import { useExternalState } from '../../hooks/useExternalState';
import { batteryState } from '@tonkeeper/core/src/managers/BatteryManager';

export const useBatteryBalance = () => {
return useQuery({
enabled: !!tk.wallet?.address.ton,
queryKey: tk.wallet?.battery.cacheKey,
cacheTime: Infinity,
queryFn: async () => {
return tk.wallet?.battery.getBalance();
},
});
const state = useExternalState(batteryState);

return {
reload: () => tk.wallet?.battery.fetchBalance(),
isLoading: state.isLoading,
balance: state.balance,
};
};
2 changes: 1 addition & 1 deletion packages/shared/query/hooks/useBatteryState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { useBatteryBalance } from './useBatteryBalance';
import { getBatteryState } from '../../utils/battery';

export const useBatteryState = () => {
const { data: balance } = useBatteryBalance();
const { balance } = useBatteryBalance();
return getBatteryState(balance ?? '0');
};
2 changes: 1 addition & 1 deletion packages/shared/query/hooks/useTonInscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const useTonInscriptions = () => {
tk.wallet?.tonInscriptions.state ??
new State({
isLoading: false,
inscriptions: [],
items: [],
}),
);

Expand Down
5 changes: 3 additions & 2 deletions packages/uikit/src/components/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TouchableOpacity } from './TouchableOpacity';
import { corners } from '../styles/constants';
import { Steezy, useTheme } from '../styles';
import { Font } from './Text/TextStyles';
import { isAndroid } from '../utils';
import { isAndroid, isIOS } from '../utils';
import { Icon } from './Icon';
import { View } from './View';
import { Text } from './Text';
Expand Down Expand Up @@ -73,6 +73,7 @@ enum InputState {

const LABEL_SCALE_RATIO = 0.8;
const ANIM_DURATION = 100;
const LABEL_TRANSLATE_X_TO = isIOS ? -32 : -30;

export const Input = forwardRef<InputRef, InputProps>((props, ref) => {
const {
Expand Down Expand Up @@ -248,7 +249,7 @@ export const Input = forwardRef<InputRef, InputProps>((props, ref) => {
const labelContainerStyle = useAnimatedStyle(() => ({
transform: [
{
translateX: interpolate(hasValueAnim.value, [0, 1], [0, -30]),
translateX: interpolate(hasValueAnim.value, [0, 1], [0, LABEL_TRANSLATE_X_TO]),
},
{
translateY: interpolate(hasValueAnim.value, [0, 1], [0, -13]),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { TextInput } from 'react-native-gesture-handler';
import { TextInputProps, TextInput as TextInputNative } from 'react-native';
import { TextInputProps } from 'react-native';
import { useBottomSheetInternal } from '@gorhom/bottom-sheet';
import { isAndroid, useMergeRefs } from '../../../utils';

Expand Down Expand Up @@ -65,11 +65,11 @@ const SheetModalInputComponentAndroid = React.forwardRef<
if (autoFocus) {
setTimeout(() => {
inputRef.current?.focus();
}, 75);
}, 500);
}
}, []);

return <TextInputNative ref={setRef} {...rest} />;
return <TextInput ref={setRef} {...rest} />;
});

export const SheetModalInput = isAndroid
Expand Down

0 comments on commit 169bcd7

Please sign in to comment.