From ef23280493bbb335b7aa4891d12a6abe1d3fdc32 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Thu, 16 Apr 2020 22:34:17 +0200 Subject: [PATCH] Greatly increased performance --- src/components/Lists/GroupListAccordion.js | 69 ++++++++++------------ src/components/Lists/GroupListItem.js | 66 +++++++++++++++++++++ src/screens/GroupSelectionScreen.js | 26 ++++---- 3 files changed, 109 insertions(+), 52 deletions(-) create mode 100644 src/components/Lists/GroupListItem.js diff --git a/src/components/Lists/GroupListAccordion.js b/src/components/Lists/GroupListAccordion.js index 012f0b9..3b2b4e5 100644 --- a/src/components/Lists/GroupListAccordion.js +++ b/src/components/Lists/GroupListAccordion.js @@ -1,11 +1,12 @@ // @flow import * as React from 'react'; -import {IconButton, List, withTheme} from 'react-native-paper'; +import {List, withTheme} from 'react-native-paper'; import {FlatList, View} from "react-native"; import {stringMatchQuery} from "../../utils/Search"; import Collapsible from "react-native-collapsible"; import * as Animatable from "react-native-animatable"; +import GroupListItem from "./GroupListItem"; type Props = { item: Object, @@ -42,45 +43,27 @@ class GroupListAccordion extends React.Component { return (nextProps.currentSearchString !== this.props.currentSearchString) || (nextSate.expanded !== this.state.expanded) - || (nextProps.favoriteNumber !== this.props.favoriteNumber); + || (nextProps.favoriteNumber !== this.props.favoriteNumber) + || (nextProps.item.content.length !== this.props.item.content.length); } onPress = () => { - this.chevronRef.current.transitionTo({ rotate: this.state.expanded ? '0deg' : '180deg' }); + this.chevronRef.current.transitionTo({rotate: this.state.expanded ? '0deg' : '180deg'}); this.setState({expanded: !this.state.expanded}) }; keyExtractor = (item: Object) => item.id.toString(); - isItemFavorite(item: Object) { - return item.isFav !== undefined && item.isFav; - } - renderItem = ({item}: Object) => { if (stringMatchQuery(item.name, this.props.currentSearchString)) { - const onPress = () => this.props.onGroupPress(item); const onStartPress = () => this.props.onFavoritePress(item); return ( - - } - right={props => - } - style={{ - height: LIST_ITEM_HEIGHT, - justifyContent: 'center', - }} - /> + onStartPress={onStartPress}/> ); } else return null; @@ -90,16 +73,20 @@ class GroupListAccordion extends React.Component { render() { const item = this.props.item; + const accordionColor = this.state.expanded + ? this.props.theme.colors.primary + : this.props.theme.colors.text; + // console.log(item.id); return ( item.id === "0" ? { ref={this.chevronRef} {...props} icon={"chevron-down"} + color={this.state.expanded + ? this.props.theme.colors.primary + : props.color + } useNativeDriver />} /> - + {this.state.expanded // Only render list if expanded for increased performance + ? + : null} + ); diff --git a/src/components/Lists/GroupListItem.js b/src/components/Lists/GroupListItem.js new file mode 100644 index 0000000..fa36f41 --- /dev/null +++ b/src/components/Lists/GroupListItem.js @@ -0,0 +1,66 @@ +// @flow + +import * as React from 'react'; +import {IconButton, List, withTheme} from 'react-native-paper'; + +type Props = { + theme: Object, + onPress: Function, + onStartPress: Function, + item: Object, + height: number, +} + +type State = { + isFav: boolean, +} + +class GroupListItem extends React.Component { + + colors: Object; + + constructor(props) { + super(props); + this.colors = props.theme.colors; + this.state = { + isFav: (props.item.isFav !== undefined && props.item.isFav), + } + } + + shouldComponentUpdate(prevProps: Props, prevState: State) { + return (prevState.isFav !== this.state.isFav); + } + + onStarPress = () => { + this.setState({isFav: !this.state.isFav}); + this.props.onStartPress(); + } + + render() { + return ( + + } + right={props => + } + style={{ + height: this.props.height, + justifyContent: 'center', + }} + /> + ); + } +} + +export default withTheme(GroupListItem); diff --git a/src/screens/GroupSelectionScreen.js b/src/screens/GroupSelectionScreen.js index fbe98a5..c934b66 100644 --- a/src/screens/GroupSelectionScreen.js +++ b/src/screens/GroupSelectionScreen.js @@ -1,7 +1,7 @@ // @flow import * as React from 'react'; -import {Platform, View} from "react-native"; +import {Platform} from "react-native"; import i18n from "i18n-js"; import {Searchbar, withTheme} from "react-native-paper"; import {stringMatchQuery} from "../utils/Search"; @@ -206,20 +206,16 @@ class GroupSelectionScreen extends React.Component { render() { return ( - - - + ); } }