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

Persist chosen units across app restarts #1913

Merged
merged 2 commits into from
Dec 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 2 additions & 10 deletions components/AmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ interface AmountInputProps {
FiatStore?: FiatStore;
SettingsStore?: SettingsStore;
UnitsStore?: UnitsStore;
// Quick fix for ZEUS-1580
// Prevents unit reset when Keysend is initiated from Keypad
// since unit depends on user selection and is not necessarily sats
preventUnitReset?: boolean;
}

interface AmountInputState {
Expand Down Expand Up @@ -85,13 +81,9 @@ export default class AmountInput extends React.Component<
constructor(props: any) {
super(props);

const { amount, onAmountChange, preventUnitReset } = props;
const { amount, onAmountChange } = props;
let satAmount = '0';
if (amount && !preventUnitReset) {
// reset units to sats if amount is passed in
this.props.UnitsStore?.resetUnits();
satAmount = getSatAmount(amount).toString();
}
if (amount) satAmount = getSatAmount(amount).toString();

onAmountChange(amount, satAmount);
this.state = {
Expand Down
17 changes: 15 additions & 2 deletions stores/UnitsStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { action, observable } from 'mobx';
import EncryptedStorage from 'react-native-encrypted-storage';

import SettingsStore from './SettingsStore';
import FiatStore from './FiatStore';
import FeeUtils from './../utils/FeeUtils';
Expand All @@ -8,6 +10,8 @@ type Units = 'sats' | 'BTC' | 'fiat';
// 100_000_000
export const SATS_PER_BTC = 100000000;

const UNIT_KEY = 'zeus-units';

interface ValueDisplayProps {
amount: string;
unit: Units;
Expand All @@ -19,17 +23,26 @@ interface ValueDisplayProps {
}

export default class UnitsStore {
@observable public units: Units = 'sats';
@observable public units: Units | string = 'sats';
settingsStore: SettingsStore;
fiatStore: FiatStore;

constructor(settingsStore: SettingsStore, fiatStore: FiatStore) {
this.settingsStore = settingsStore;
this.fiatStore = fiatStore;
this.getUnits();
}

getUnits = async () => {
const units = await EncryptedStorage.getItem(UNIT_KEY);
if (units) this.units = units;
};

@action
public changeUnits = () => (this.units = this.getNextUnit());
public changeUnits = async () => {
this.units = this.getNextUnit();
await EncryptedStorage.setItem(UNIT_KEY, this.units);
};

public getNextUnit = () => {
const { settings } = this.settingsStore;
Expand Down
7 changes: 7 additions & 0 deletions utils/AddressUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
jest.mock('react-native-encrypted-storage', () => ({
setItem: jest.fn(() => Promise.resolve()),
getItem: jest.fn(() => Promise.resolve()),
removeItem: jest.fn(() => Promise.resolve()),
clear: jest.fn(() => Promise.resolve())
}));

import AddressUtils from './AddressUtils';

describe('AddressUtils', () => {
Expand Down
3 changes: 2 additions & 1 deletion utils/handleAnything.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import NodeUriUtils from './NodeUriUtils';
import { localeString } from './LocaleUtils';
import BackendUtils from './BackendUtils';

const { nodeInfoStore, invoicesStore, settingsStore } = stores;
const { nodeInfoStore, invoicesStore, unitsStore, settingsStore } = stores;

const isClipboardValue = (data: string) =>
handleAnything(data, undefined, true);
Expand Down Expand Up @@ -56,6 +56,7 @@ const handleAnything = async (
AddressUtils.isValidBitcoinAddress(value, isTestNet || isRegTest)
) {
if (isClipboardValue) return true;
if (amount) unitsStore?.resetUnits();
return [
'Send',
{
Expand Down
1 change: 0 additions & 1 deletion views/LnurlPay/LnurlPay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ export default class LnurlPay extends React.Component<
<View style={{ marginTop: -20 }}>
<AmountInput
amount={amount}
preventUnitReset={true}
locked={
lnurl &&
lnurl.minSendable === lnurl.maxSendable
Expand Down
5 changes: 0 additions & 5 deletions views/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ interface SendState {
enableAtomicMultiPathPayment: boolean;
clipboard: string;
loading: boolean;
preventUnitReset: boolean;
contactName: string;
}

Expand All @@ -107,7 +106,6 @@ export default class Send extends React.Component<SendProps, SendState> {
const amount = navigation.getParam('amount', null);
const transactionType = navigation.getParam('transactionType', null);
const isValid = navigation.getParam('isValid', false);
const preventUnitReset = navigation.getParam('preventUnitReset', false);
const contactName = navigation.getParam('contactName', null);

if (transactionType === 'Lightning') {
Expand All @@ -132,7 +130,6 @@ export default class Send extends React.Component<SendProps, SendState> {
enableAtomicMultiPathPayment: false,
clipboard: '',
loading: false,
preventUnitReset,
contactName
};
}
Expand Down Expand Up @@ -405,7 +402,6 @@ export default class Send extends React.Component<SendProps, SendState> {
enableAtomicMultiPathPayment,
clipboard,
loading,
preventUnitReset,
contactName
} = this.state;
const {
Expand Down Expand Up @@ -724,7 +720,6 @@ export default class Send extends React.Component<SendProps, SendState> {
<React.Fragment>
<AmountInput
amount={amount}
preventUnitReset={preventUnitReset}
title={localeString('views.Send.amount')}
onAmountChange={(
amount: string,
Expand Down
3 changes: 1 addition & 2 deletions views/Wallet/KeypadPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,7 @@ export default class KeypadPane extends React.PureComponent<
noUppercase
onPress={() => {
navigation.navigate('Send', {
amount,
preventUnitReset: true
amount
});
}}
buttonStyle={{ height: 40 }}
Expand Down