From 798617d8810d10335be11374e1d99741f29a84e4 Mon Sep 17 00:00:00 2001 From: keplyx Date: Sun, 8 Mar 2020 11:27:05 +0100 Subject: [PATCH] Fixed watchlist fetch + use modal instead of alert --- components/FeedItem.js | 2 +- screens/About/AboutScreen.js | 13 ++-- screens/Proxiwash/ProxiwashScreen.js | 96 +++++++++++++++++++++------- utils/NotificationsManager.js | 5 +- 4 files changed, 87 insertions(+), 29 deletions(-) diff --git a/components/FeedItem.js b/components/FeedItem.js index d77b101..a7de279 100644 --- a/components/FeedItem.js +++ b/components/FeedItem.js @@ -9,7 +9,7 @@ const ICON_AMICALE = require('../assets/amicale.png'); function FeedItem(props) { const {colors} = props.theme; return ( - + { return ( } - right={props => } + left={props => } + right={props => } onPress={item.onPressCallback} /> ); @@ -280,7 +280,7 @@ export default class AboutScreen extends React.Component { return ( } + left={props => } onPress={item.onPressCallback} /> ); @@ -311,7 +311,10 @@ export default class AboutScreen extends React.Component { return ( + handlePosition={'inside'} + modalStyle={{backgroundColor: ThemeManager.getCurrentThemeVariables().surface}} + handleStyle={{backgroundColor: ThemeManager.getCurrentThemeVariables().text}} + > , bannerVisible: boolean, }; @@ -38,6 +41,8 @@ type State = { */ export default class ProxiwashScreen extends React.Component { + modalRef: { current: null | Modalize }; + onAboutPress: Function; getRenderItem: Function; getRenderSectionHeader: Function; @@ -52,6 +57,7 @@ export default class ProxiwashScreen extends React.Component { fetchedData: {}, // machinesWatched: JSON.parse(dataString), machinesWatched: [], + modalCurrentDisplayItem: null, bannerVisible: AsyncStorageManager.getInstance().preferences.proxiwashShowBanner.current === '1', }; @@ -60,6 +66,7 @@ export default class ProxiwashScreen extends React.Component { */ constructor() { super(); + this.modalRef = React.createRef(); stateStrings[ProxiwashConstants.machineStates.TERMINE] = i18n.t('proxiwashScreen.states.finished'); stateStrings[ProxiwashConstants.machineStates.DISPONIBLE] = i18n.t('proxiwashScreen.states.ready'); stateStrings[ProxiwashConstants.machineStates["EN COURS"]] = i18n.t('proxiwashScreen.states.running'); @@ -238,29 +245,40 @@ export default class ProxiwashScreen extends React.Component { ]; } - /** - * Show an alert fo a machine, allowing to enable/disable notifications if running - * - * @param title - * @param item - * @param isDryer - */ - showAlert(title: string, item: Object, isDryer: boolean) { - let buttons = [{text: i18n.t("proxiwashScreen.modal.ok")}]; + showModal(title: string, item: Object, isDryer: boolean) { + this.setState({ + modalCurrentDisplayItem: this.getModalContent(title, item, isDryer) + }); + if (this.modalRef.current) { + this.modalRef.current.open(); + } + } + + onSetupNotificationsPress(machineId: string) { + if (this.modalRef.current) { + this.modalRef.current.close(); + } + this.setupNotifications(machineId) + } + + getModalContent(title: string, item: Object, isDryer: boolean) { + let button = { + text: i18n.t("proxiwashScreen.modal.ok"), + icon: '', + onPress: undefined + }; let message = modalStateStrings[ProxiwashConstants.machineStates[item.state]]; - const onPress = this.setupNotifications.bind(this, item.number); + const onPress = this.onSetupNotificationsPress.bind(this, item.number); if (ProxiwashConstants.machineStates[item.state] === ProxiwashConstants.machineStates["EN COURS"]) { - buttons = [ + button = { text: this.isMachineWatched(item.number) ? i18n.t("proxiwashScreen.modal.disableNotifications") : i18n.t("proxiwashScreen.modal.enableNotifications"), + icon: '', onPress: onPress - }, - { - text: i18n.t("proxiwashScreen.modal.cancel") } - ]; + ; message = i18n.t('proxiwashScreen.modal.running', { start: item.startTime, @@ -273,13 +291,39 @@ export default class ProxiwashScreen extends React.Component { else message += '\n' + i18n.t('proxiwashScreen.washersTariff'); } - Alert.alert( - title, - message, - buttons + return ( + + } + + /> + + {message} + + + {button.onPress !== undefined ? + + + : null} + ); } + onAboutPress() { this.props.navigation.navigate('ProxiwashAboutScreen'); } @@ -305,10 +349,18 @@ export default class ProxiwashScreen extends React.Component { icon={() => } + />} > {i18n.t('proxiwashScreen.enableNotificationsTip')} + + {this.state.modalCurrentDisplayItem} + { const isMachineRunning = ProxiwashConstants.machineStates[item.state] === ProxiwashConstants.machineStates["EN COURS"]; const machineName = (section.title === i18n.t('proxiwashScreen.dryers') ? i18n.t('proxiwashScreen.dryer') : i18n.t('proxiwashScreen.washer')) + ' n°' + item.number; const isDryer = section.title === i18n.t('proxiwashScreen.dryers'); - const onPress = this.showAlert.bind(this, machineName, item, isDryer); + const onPress = this.showModal.bind(this, machineName, item, isDryer); let width = item.donePercent !== '' ? (parseInt(item.donePercent)).toString() + '%' : 0; if (ProxiwashConstants.machineStates[item.state] === '0') width = '100%'; diff --git a/utils/NotificationsManager.js b/utils/NotificationsManager.js index c424429..da83e20 100644 --- a/utils/NotificationsManager.js +++ b/utils/NotificationsManager.js @@ -123,7 +123,10 @@ export default class NotificationsManager { 'Content-Type': 'application/json', }), body: JSON.stringify(data) // <-- Post parameters - }); + }).then((response) => response.json()) + .then((responseJson) => { + callback(responseJson); + }); } }