Set sort menu with modalize

This commit is contained in:
keplyx 2020-03-07 22:31:08 +01:00
parent 8d914c97f5
commit 922f15f3d2
3 changed files with 110 additions and 144 deletions

View file

@ -3,15 +3,9 @@
import * as React from 'react'; import * as React from 'react';
import {FlatList, Image, ScrollView, View} from "react-native"; import {FlatList, Image, ScrollView, View} from "react-native";
import i18n from "i18n-js"; import i18n from "i18n-js";
import {MaterialCommunityIcons} from "@expo/vector-icons";
import ThemeManager from "../../utils/ThemeManager"; import ThemeManager from "../../utils/ThemeManager";
import {Modalize} from 'react-native-modalize'; import {Modalize} from 'react-native-modalize';
import {Avatar, Divider, IconButton, List, Menu, Searchbar, Subheading, Text, Title} from "react-native-paper"; import {Avatar, IconButton, List, RadioButton, Searchbar, Subheading, Text, Title} from "react-native-paper";
const sortMode = {
price: "0",
name: '1',
};
function sortPrice(a, b) { function sortPrice(a, b) {
return a.price - b.price; return a.price - b.price;
@ -22,17 +16,17 @@ function sortPriceReverse(a, b) {
} }
function sortName(a, b) { function sortName(a, b) {
if (a.name < b.name) if (a.name.toLowerCase() < b.name.toLowerCase())
return -1; return -1;
if (a.name > b.name) if (a.name.toLowerCase() > b.name.toLowerCase())
return 1; return 1;
return 0; return 0;
} }
function sortNameReverse(a, b) { function sortNameReverse(a, b) {
if (a.name < b.name) if (a.name.toLowerCase() < b.name.toLowerCase())
return 1; return 1;
if (a.name > b.name) if (a.name.toLowerCase() > b.name.toLowerCase())
return -1; return -1;
return 0; return 0;
} }
@ -43,13 +37,9 @@ type Props = {
} }
type State = { type State = {
currentSortMode: string, currentSortMode: number,
isSortReversed: boolean, modalCurrentDisplayItem: React.Node,
sortPriceIcon: React.Node,
sortNameIcon: React.Node,
modalCurrentDisplayItem: Object,
currentlyDisplayedData: Array<Object>, currentlyDisplayedData: Array<Object>,
menuVisible: boolean,
}; };
/** /**
@ -62,10 +52,7 @@ export default class ProximoListScreen extends React.Component<Props, State> {
shouldFocusSearchBar: boolean; shouldFocusSearchBar: boolean;
onSearchStringChange: Function; onSearchStringChange: Function;
onSelectSortModeName: Function;
onSelectSortModePrice: Function;
onSortMenuPress: Function; onSortMenuPress: Function;
onSortMenuDismiss: Function;
renderItem: Function; renderItem: Function;
constructor(props: any) { constructor(props: any) {
@ -75,67 +62,42 @@ export default class ProximoListScreen extends React.Component<Props, State> {
this.shouldFocusSearchBar = this.props.route.params['shouldFocusSearchBar']; this.shouldFocusSearchBar = this.props.route.params['shouldFocusSearchBar'];
this.state = { this.state = {
currentlyDisplayedData: this.originalData, currentlyDisplayedData: this.originalData,
currentSortMode: sortMode.price, currentSortMode: 1,
isSortReversed: false, modalCurrentDisplayItem: <View/>,
sortPriceIcon: '',
sortNameIcon: '',
modalCurrentDisplayItem: {},
menuVisible: false,
}; };
this.onSearchStringChange = this.onSearchStringChange.bind(this); this.onSearchStringChange = this.onSearchStringChange.bind(this);
this.onSelectSortModeName = this.onSelectSortModeName.bind(this);
this.onSelectSortModePrice = this.onSelectSortModePrice.bind(this);
this.onSortMenuPress = this.onSortMenuPress.bind(this); this.onSortMenuPress = this.onSortMenuPress.bind(this);
this.onSortMenuDismiss = this.onSortMenuPress.bind(this);
this.renderItem = this.renderItem.bind(this); this.renderItem = this.renderItem.bind(this);
} }
/**
* Sets the sort mode based on the one selected.
* If the selected mode is the current one, reverse it.
*
* @param mode The string representing the mode
*/
sortModeSelected(mode: string) {
let isReverse = this.state.isSortReversed;
if (mode === this.state.currentSortMode) // reverse mode
isReverse = !isReverse; // this.state not updating on this function cycle
else
isReverse = false;
this.setSortMode(mode, isReverse);
}
/** /**
* Set the current sort mode. * Set the current sort mode.
* *
* @param mode The string representing the mode * @param mode The number representing the mode
* @param isReverse Whether to use a reverse sort
*/ */
setSortMode(mode: string, isReverse: boolean) { setSortMode(mode: number) {
this.setState({ this.setState({
currentSortMode: mode, currentSortMode: mode,
isSortReversed: isReverse
}); });
let data = this.state.currentlyDisplayedData; let data = this.state.currentlyDisplayedData;
switch (mode) { switch (mode) {
case sortMode.price: case 1:
if (isReverse) { data.sort(sortPrice);
data.sort(sortPriceReverse);
} else {
data.sort(sortPrice);
}
break; break;
case sortMode.name: case 2:
if (isReverse) { data.sort(sortPriceReverse);
data.sort(sortNameReverse); break;
} else { case 3:
data.sort(sortName); data.sort(sortName);
} break;
case 4:
data.sort(sortNameReverse);
break; break;
} }
this.setupSortIcons(mode, isReverse); if (this.modalRef.current && mode !== this.state.currentSortMode) {
this.modalRef.current.close();
}
} }
/** /**
@ -148,7 +110,7 @@ export default class ProximoListScreen extends React.Component<Props, State> {
headerRight: button, headerRight: button,
headerTitle: title, headerTitle: title,
}); });
this.setSortMode(this.state.currentSortMode, this.state.isSortReversed); this.setSortMode(1);
} }
getSearchBar() { getSearchBar() {
@ -177,42 +139,6 @@ export default class ProximoListScreen extends React.Component<Props, State> {
return color; return color;
} }
/**
* Set the sort menu icon based on the given mode.
*
* @param mode The string representing the mode
* @param isReverse Whether to use a reversed icon
*/
setupSortIcons(mode: string, isReverse: boolean) {
const downSortIcon =
<MaterialCommunityIcons
name={'sort-descending'}
size={26}/>;
const upSortIcon =
<MaterialCommunityIcons
name={'sort-ascending'}
size={26}/>;
switch (mode) {
case sortMode.price:
this.setState({sortNameIcon: ''});
if (isReverse) {
this.setState({sortPriceIcon: upSortIcon});
} else {
this.setState({sortPriceIcon: downSortIcon});
}
break;
case sortMode.name:
this.setState({sortPriceIcon: ''});
if (isReverse) {
this.setState({sortNameIcon: upSortIcon});
} else {
this.setState({sortNameIcon: downSortIcon});
}
break;
}
}
sanitizeString(str: string) { sanitizeString(str: string) {
return str.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""); return str.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
} }
@ -241,81 +167,112 @@ export default class ProximoListScreen extends React.Component<Props, State> {
}) })
} }
getModalContent() { getModalItemContent(item: Object) {
return ( return (
<View style={{ <View style={{
flex: 1, flex: 1,
padding: 20 padding: 20
}}> }}>
<Title>{this.state.modalCurrentDisplayItem.name}</Title> <Title>{item.name}</Title>
<View style={{ <View style={{
flexDirection: 'row', flexDirection: 'row',
width: '100%', width: '100%',
marginTop: 10, marginTop: 10,
}}> }}>
<Subheading style={{ <Subheading style={{
color: this.getStockColor(parseInt(this.state.modalCurrentDisplayItem.quantity)), color: this.getStockColor(parseInt(item.quantity)),
}}> }}>
{this.state.modalCurrentDisplayItem.quantity + ' ' + i18n.t('proximoScreen.inStock')} {item.quantity + ' ' + i18n.t('proximoScreen.inStock')}
</Subheading> </Subheading>
<Subheading style={{marginLeft: 'auto'}}>{this.state.modalCurrentDisplayItem.price}</Subheading> <Subheading style={{marginLeft: 'auto'}}>{item.price}</Subheading>
</View> </View>
<ScrollView> <ScrollView>
<View style={{width: '100%', height: 150, marginTop: 20, marginBottom: 20}}> <View style={{width: '100%', height: 150, marginTop: 20, marginBottom: 20}}>
<Image style={{flex: 1, resizeMode: "contain"}} <Image style={{flex: 1, resizeMode: "contain"}}
source={{uri: this.state.modalCurrentDisplayItem.image}}/> source={{uri: item.image}}/>
</View> </View>
<Text>{this.state.modalCurrentDisplayItem.description}</Text> <Text>{item.description}</Text>
</ScrollView> </ScrollView>
</View> </View>
); );
} }
getModalSortMenu() {
console.log(this.state.currentSortMode);
return (
<View style={{
flex: 1,
padding: 20
}}>
<Title>Sort Order</Title>
<RadioButton.Group
onValueChange={value => this.setSortMode(value)}
value={this.state.currentSortMode}
>
<View style={{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
}}>
<Text>{i18n.t('proximoScreen.sortPrice')}</Text>
<RadioButton value={1}/>
</View>
<View style={{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
}}>
<Text>{i18n.t('proximoScreen.sortPriceReverse')}</Text>
<RadioButton value={2}/>
</View>
<View style={{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
}}>
<Text>{i18n.t('proximoScreen.sortName')}</Text>
<RadioButton value={3}/>
</View>
<View style={{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center'
}}>
<Text>{i18n.t('proximoScreen.sortNameReverse')}</Text>
<RadioButton value={4}/>
</View>
</RadioButton.Group>
</View>
);
}
onListItemPress(item: Object) { onListItemPress(item: Object) {
this.setState({ this.setState({
modalCurrentDisplayItem: item modalCurrentDisplayItem: this.getModalItemContent(item)
}); });
if (this.modalRef.current) { if (this.modalRef.current) {
this.modalRef.current.open(); this.modalRef.current.open();
} }
} }
onSelectSortModeName() {
this.sortModeSelected(sortMode.name);
}
onSelectSortModePrice() {
this.sortModeSelected(sortMode.price);
}
onSortMenuPress() { onSortMenuPress() {
this.setState({menuVisible: true}); this.setState({
console.log('pressed'); modalCurrentDisplayItem: this.getModalSortMenu()
} });
if (this.modalRef.current) {
onSortMenuDismiss() { this.modalRef.current.open();
this.setState({menuVisible: false}); }
} }
getSortMenu() { getSortMenu() {
return ( return (
<Menu <IconButton
visible={this.state.menuVisible} icon="sort"
onDismiss={this.onSortMenuDismiss} color={ThemeManager.getCurrentThemeVariables().text}
anchor={ size={26}
<IconButton onPress={this.onSortMenuPress}
icon="sort" />
color={ThemeManager.getCurrentThemeVariables().text}
size={26}
onPress={this.onSortMenuPress}
/>
}
>
<Menu.Item onPress={this.onSelectSortModeName} title={i18n.t('proximoScreen.sortName')}/>
<Divider/>
<Menu.Item onPress={this.onSelectSortModePrice} title={i18n.t('proximoScreen.sortPrice')}/>
</Menu>
); );
} }
@ -327,7 +284,8 @@ export default class ProximoListScreen extends React.Component<Props, State> {
description={item.quantity + ' ' + i18n.t('proximoScreen.inStock')} description={item.quantity + ' ' + i18n.t('proximoScreen.inStock')}
descriptionStyle={{color: this.getStockColor(parseInt(item.quantity))}} descriptionStyle={{color: this.getStockColor(parseInt(item.quantity))}}
onPress={onPress} onPress={onPress}
left={props => <Avatar.Image style={{backgroundColor: 'transparent'}} size={64} source={{uri: item.image}}/>} left={props => <Avatar.Image style={{backgroundColor: 'transparent'}} size={64}
source={{uri: item.image}}/>}
right={props => right={props =>
<Text style={{fontWeight: "bold"}}> <Text style={{fontWeight: "bold"}}>
{item.price} {item.price}
@ -344,10 +302,14 @@ export default class ProximoListScreen extends React.Component<Props, State> {
console.log("rendering ProximoListScreen"); console.log("rendering ProximoListScreen");
return ( return (
<View> <View>
<Modalize ref={this.modalRef} <Modalize
adjustToContentHeight ref={this.modalRef}
modalStyle={{backgroundColor: ThemeManager.getCurrentThemeVariables().card}}> adjustToContentHeight
{this.getModalContent()} handlePosition={'inside'}
modalStyle={{backgroundColor: ThemeManager.getCurrentThemeVariables().card}}
handleStyle={{backgroundColor: ThemeManager.getCurrentThemeVariables().text}}
>
{this.state.modalCurrentDisplayItem}
</Modalize> </Modalize>
<FlatList <FlatList
data={this.state.currentlyDisplayedData} data={this.state.currentlyDisplayedData}

View file

@ -65,7 +65,7 @@
"startScreen": "Start Screen", "startScreen": "Start Screen",
"startScreenSub": "Select which screen to start the app on", "startScreenSub": "Select which screen to start the app on",
"proxiwashNotifReminder": "Machine running reminder", "proxiwashNotifReminder": "Machine running reminder",
"proxiwashNotifReminderSub": "How many minutes before", "proxiwashNotifReminderSub": "How many minutes before"
}, },
"homeScreen": { "homeScreen": {
"listUpdated": "List updated!", "listUpdated": "List updated!",
@ -122,7 +122,9 @@
"article": "Article", "article": "Article",
"articles": "Articles", "articles": "Articles",
"sortName": "Sort by name", "sortName": "Sort by name",
"sortNameReverse": "Sort by name (reverse)",
"sortPrice": "Sort by price", "sortPrice": "Sort by price",
"sortPriceReverse": "Sort by price (reverse)",
"listUpdated": "Article list updated!", "listUpdated": "Article list updated!",
"listUpdateFail": "Error while updating article list", "listUpdateFail": "Error while updating article list",
"loading": "Loading...", "loading": "Loading...",

View file

@ -65,7 +65,7 @@
"startScreen": "Écran de démarrage", "startScreen": "Écran de démarrage",
"startScreenSub": "Choisissez l'écran utilisé au démarrage", "startScreenSub": "Choisissez l'écran utilisé au démarrage",
"proxiwashNotifReminder": "Rappel de machine en cours", "proxiwashNotifReminder": "Rappel de machine en cours",
"proxiwashNotifReminderSub": "Combien de minutes avant", "proxiwashNotifReminderSub": "Combien de minutes avant"
}, },
"homeScreen": { "homeScreen": {
"listUpdated": "List mise à jour!", "listUpdated": "List mise à jour!",
@ -122,7 +122,9 @@
"article": "Article", "article": "Article",
"articles": "Articles", "articles": "Articles",
"sortName": "Trier par nom", "sortName": "Trier par nom",
"sortNameReverse": "Trier par nom (inversé)",
"sortPrice": "Trier par prix", "sortPrice": "Trier par prix",
"sortPriceReverse": "Trier par prix (inversé)",
"listUpdated": "Liste d'articles mise à jour !", "listUpdated": "Liste d'articles mise à jour !",
"listUpdateFail": "Erreur lors de la mise à jour de la list d'articles", "listUpdateFail": "Erreur lors de la mise à jour de la list d'articles",
"loading": "Chargement...", "loading": "Chargement...",