From fdf0fffabc0992dfe45e1dfb954884ec646eb3ae Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Sat, 18 Jul 2020 21:28:15 +0200 Subject: [PATCH] Replaced game alert by paper dialog --- src/components/Dialogs/AlertDialog.js | 3 +- src/components/Dialogs/OptionsDialog.js | 56 +++++++++++ src/screens/Game/screens/GameMainScreen.js | 104 ++++++++++++++++----- 3 files changed, 137 insertions(+), 26 deletions(-) create mode 100644 src/components/Dialogs/OptionsDialog.js diff --git a/src/components/Dialogs/AlertDialog.js b/src/components/Dialogs/AlertDialog.js index a79efc7..d0564cc 100644 --- a/src/components/Dialogs/AlertDialog.js +++ b/src/components/Dialogs/AlertDialog.js @@ -2,6 +2,7 @@ import * as React from 'react'; import {Button, Dialog, Paragraph, Portal} from 'react-native-paper'; +import i18n from "i18n-js"; type Props = { visible: boolean, @@ -23,7 +24,7 @@ class AlertDialog extends React.PureComponent { {this.props.message} - + diff --git a/src/components/Dialogs/OptionsDialog.js b/src/components/Dialogs/OptionsDialog.js new file mode 100644 index 0000000..3503a44 --- /dev/null +++ b/src/components/Dialogs/OptionsDialog.js @@ -0,0 +1,56 @@ +// @flow + +import * as React from 'react'; +import {Button, Dialog, Paragraph, Portal} from 'react-native-paper'; +import {FlatList} from "react-native"; + +export type OptionsDialogButton = { + title: string, + onPress: () => void, +} + +type Props = { + visible: boolean, + title: string, + message: string, + buttons: Array, + onDismiss: () => void, +} + +class OptionsDialog extends React.PureComponent { + + getButtonRender = ({item}: { item: OptionsDialogButton }) => { + return ; + } + + keyExtractor = (item: OptionsDialogButton) => item.title; + + render() { + return ( + + + {this.props.title} + + {this.props.message} + + + + + + + ); + } +} + +export default OptionsDialog; diff --git a/src/screens/Game/screens/GameMainScreen.js b/src/screens/Game/screens/GameMainScreen.js index e2afa0a..1e56ad6 100644 --- a/src/screens/Game/screens/GameMainScreen.js +++ b/src/screens/Game/screens/GameMainScreen.js @@ -1,7 +1,7 @@ // @flow import * as React from 'react'; -import {Alert, View} from 'react-native'; +import {View} from 'react-native'; import {IconButton, Text, withTheme} from 'react-native-paper'; import MaterialCommunityIcons from "react-native-vector-icons/MaterialCommunityIcons"; import GameLogic from "../logic/GameLogic"; @@ -12,6 +12,8 @@ import i18n from "i18n-js"; import MaterialHeaderButtons, {Item} from "../../../components/Overrides/CustomHeaderButton"; import {StackNavigationProp} from "@react-navigation/stack"; import type {CustomTheme} from "../../../managers/ThemeManager"; +import type {OptionsDialogButton} from "../../../components/Dialogs/OptionsDialog"; +import OptionsDialog from "../../../components/Dialogs/OptionsDialog"; type Props = { navigation: StackNavigationProp, @@ -24,6 +26,12 @@ type State = { gameTime: number, gameScore: number, gameLevel: number, + + dialogVisible: boolean, + dialogTitle: string, + dialogMessage: string, + dialogButtons: Array, + onDialogDismiss: () => void, } class GameMainScreen extends React.Component { @@ -39,6 +47,11 @@ class GameMainScreen extends React.Component { gameTime: 0, gameScore: 0, gameLevel: 0, + dialogVisible: false, + dialogTitle: "", + dialogMessage: "", + dialogButtons: [], + onDialogDismiss: () => {}, }; this.props.navigation.addListener('blur', this.onScreenBlur); this.props.navigation.addListener('focus', this.onScreenFocus); @@ -120,43 +133,77 @@ class GameMainScreen extends React.Component { this.showPausePopup(); } + onDialogDismiss = () => this.setState({dialogVisible: false}); + showPausePopup = () => { - Alert.alert( - i18n.t("screens.game.pause"), - i18n.t("screens.game.pauseMessage"), - [ - {text: i18n.t("screens.game.restart.text"), onPress: this.showRestartConfirm}, - {text: i18n.t("screens.game.resume"), onPress: this.togglePause}, + const onDismiss = () => { + this.togglePause(); + this.onDialogDismiss(); + }; + this.setState({ + dialogVisible: true, + dialogTitle: i18n.t("screens.game.pause"), + dialogMessage: i18n.t("screens.game.pauseMessage"), + dialogButtons: [ + { + title: i18n.t("screens.game.restart.text"), + onPress: this.showRestartConfirm + }, + { + title: i18n.t("screens.game.resume"), + onPress: onDismiss + } ], - {cancelable: false}, - ); + onDialogDismiss: onDismiss, + }); } showRestartConfirm = () => { - Alert.alert( - i18n.t("screens.game.restart.confirm"), - i18n.t("screens.game.restart.confirmMessage"), - [ - {text: i18n.t("screens.game.restart.confirmNo"), onPress: this.showPausePopup}, - {text: i18n.t("screens.game.restart.confirmYes"), onPress: this.startGame}, + this.setState({ + dialogVisible: true, + dialogTitle: i18n.t("screens.game.restart.confirm"), + dialogMessage: i18n.t("screens.game.restart.confirmMessage"), + dialogButtons: [ + { + title: i18n.t("screens.game.restart.confirmYes"), + onPress: () => { + this.onDialogDismiss(); + this.startGame(); + } + }, + { + title: i18n.t("screens.game.restart.confirmNo"), + onPress: this.showPausePopup + } ], - {cancelable: false}, - ); + onDialogDismiss: this.showPausePopup, + }); } showGameOverConfirm() { let message = i18n.t("screens.game.gameOver.score") + this.state.gameScore + '\n'; message += i18n.t("screens.game.gameOver.level") + this.state.gameLevel + '\n'; message += i18n.t("screens.game.gameOver.time") + this.getFormattedTime(this.state.gameTime) + '\n'; - Alert.alert( - i18n.t("screens.game.gameOver.text"), - message, - [ - {text: i18n.t("screens.game.gameOver.exit"), onPress: () => this.props.navigation.goBack()}, - {text: i18n.t("screens.game.restart.text"), onPress: this.startGame}, + const onDismiss = () => { + this.onDialogDismiss(); + this.startGame(); + }; + this.setState({ + dialogVisible: true, + dialogTitle: i18n.t("screens.game.gameOver.text"), + dialogMessage: message, + dialogButtons: [ + { + title: i18n.t("screens.game.gameOver.exit"), + onPress: () => this.props.navigation.goBack() + }, + { + title: i18n.t("screens.game.resume"), + onPress: onDismiss + } ], - {cancelable: false}, - ); + onDialogDismiss: onDismiss, + }); } startGame = () => { @@ -281,6 +328,13 @@ class GameMainScreen extends React.Component { color={colors.tetrisScore} /> + ); }