forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: Alert on android should use long strings as mesage (BlueWallet#7132
- Loading branch information
Showing
2 changed files
with
97 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |