Skip to content

Commit

Permalink
FIX: Alert on android should use long strings as mesage (BlueWallet#7132
Browse files Browse the repository at this point in the history
)
  • Loading branch information
marcosrdz authored Oct 10, 2024
1 parent a9e988b commit 16482a9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 42 deletions.
28 changes: 13 additions & 15 deletions blue_modules/BlueElectrum.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import BigNumber from 'bignumber.js';
import * as bitcoin from 'bitcoinjs-lib';
import { Alert } from 'react-native';
import DefaultPreference from 'react-native-default-preference';
import RNFS from 'react-native-fs';
import Realm from 'realm';
Expand Down Expand Up @@ -299,13 +298,13 @@ const presentNetworkErrorAlert = async (usingPeer?: Peer) => {
);
return;
}
Alert.alert(
loc.errors.network,
loc.formatString(
presentAlert({
title: loc.errors.network,
message: loc.formatString(
usingPeer ? loc.settings.electrum_unable_to_connect : loc.settings.electrum_error_connect,
usingPeer ? { server: `${usingPeer.host}:${usingPeer.ssl ?? usingPeer.tcp}` } : {},
),
[
buttons: [
{
text: loc.wallets.list_tryagain,
onPress: () => {
Expand All @@ -318,10 +317,10 @@ const presentNetworkErrorAlert = async (usingPeer?: Peer) => {
{
text: loc.settings.electrum_reset,
onPress: () => {
Alert.alert(
loc.settings.electrum_reset,
loc.settings.electrum_reset_to_default,
[
presentAlert({
title: loc.settings.electrum_reset,
message: loc.settings.electrum_reset_to_default,
buttons: [
{
text: loc._.cancel,
style: 'cancel',
Expand All @@ -340,16 +339,15 @@ const presentNetworkErrorAlert = async (usingPeer?: Peer) => {
await DefaultPreference.clear(ELECTRUM_SSL_PORT);
await DefaultPreference.clear(ELECTRUM_TCP_PORT);
} catch (e) {
// Must be running on Android
console.log(e);
console.log(e); // Must be running on Android
}
presentAlert({ message: loc.settings.electrum_saved });
setTimeout(connectMain, 500);
},
},
],
{ cancelable: true },
);
options: { cancelable: true },
});
connectionAttempt = 0;
mainClient.close() && mainClient.close();
},
Expand All @@ -364,8 +362,8 @@ const presentNetworkErrorAlert = async (usingPeer?: Peer) => {
style: 'cancel',
},
],
{ cancelable: false },
);
options: { cancelable: false },
});
};

/**
Expand Down
111 changes: 84 additions & 27 deletions components/Alert.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,92 @@
import { Alert as RNAlert, Platform, ToastAndroid } from 'react-native';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../blue_modules/hapticFeedback';
import loc from '../loc';

export enum AlertType {
Alert,
Toast,
}
const presentAlert = ({
title,
message,
type = AlertType.Alert,
hapticFeedback,
}: {
title?: string;
message: string;
type?: AlertType;
hapticFeedback?: HapticFeedbackTypes;
}) => {
if (hapticFeedback) {
triggerHapticFeedback(hapticFeedback);
}

if (Platform.OS !== 'android') {
type = AlertType.Alert;
}
switch (type) {
case AlertType.Toast:
ToastAndroid.show(message, ToastAndroid.LONG);
break;
default:
RNAlert.alert(title ?? message, title && message ? message : undefined);
break;
}
};

interface AlertButton {
text: string;
onPress?: () => void;
style?: 'default' | 'cancel' | 'destructive';
}

interface AlertOptions {
cancelable?: boolean;
}

const presentAlert = (() => {
let lastAlertParams: {
title?: string;
message: string;
type?: AlertType;
hapticFeedback?: HapticFeedbackTypes;
buttons?: AlertButton[];
options?: AlertOptions;
} | null = null;

const clearCache = () => {
lastAlertParams = null;
};

return ({
title,
message,
type = AlertType.Alert,
hapticFeedback,
buttons = [],
options = { cancelable: false },
}: {
title?: string;
message: string;
type?: AlertType;
hapticFeedback?: HapticFeedbackTypes;
buttons?: AlertButton[];
options?: AlertOptions;
}) => {
if (
lastAlertParams &&
lastAlertParams.title === title &&
lastAlertParams.message === message &&
lastAlertParams.type === type &&
lastAlertParams.hapticFeedback === hapticFeedback &&
JSON.stringify(lastAlertParams.buttons) === JSON.stringify(buttons) &&
JSON.stringify(lastAlertParams.options) === JSON.stringify(options)
) {
return; // Skip showing the alert if the content is the same as the last one
}

lastAlertParams = { title, message, type, hapticFeedback, buttons, options };

if (hapticFeedback) {
triggerHapticFeedback(hapticFeedback);
}

// Ensure that there's at least one button (required for both iOS and Android)
const wrappedButtons =
buttons.length > 0
? buttons
: [
{
text: loc._.ok,
onPress: () => {},
},
];

switch (type) {
case AlertType.Toast:
if (Platform.OS === 'android') {
ToastAndroid.show(message, ToastAndroid.LONG);
clearCache();
}
break;
default:
RNAlert.alert(title ?? message, title && message ? message : undefined, wrappedButtons, options);
break;
}
};
})();

export default presentAlert;

0 comments on commit 16482a9

Please sign in to comment.