Browse Source

Greatly increased performance

Arnaud Vergnet 4 years ago
parent
commit
ef23280493

+ 32
- 37
src/components/Lists/GroupListAccordion.js View File

@@ -1,11 +1,12 @@
1 1
 // @flow
2 2
 
3 3
 import * as React from 'react';
4
-import {IconButton, List, withTheme} from 'react-native-paper';
4
+import {List, withTheme} from 'react-native-paper';
5 5
 import {FlatList, View} from "react-native";
6 6
 import {stringMatchQuery} from "../../utils/Search";
7 7
 import Collapsible from "react-native-collapsible";
8 8
 import * as Animatable from "react-native-animatable";
9
+import GroupListItem from "./GroupListItem";
9 10
 
10 11
 type Props = {
11 12
     item: Object,
@@ -42,45 +43,27 @@ class GroupListAccordion extends React.Component<Props, State> {
42 43
 
43 44
         return (nextProps.currentSearchString !== this.props.currentSearchString)
44 45
             || (nextSate.expanded !== this.state.expanded)
45
-            || (nextProps.favoriteNumber !== this.props.favoriteNumber);
46
+            || (nextProps.favoriteNumber !== this.props.favoriteNumber)
47
+            || (nextProps.item.content.length !== this.props.item.content.length);
46 48
     }
47 49
 
48 50
     onPress = () => {
49
-        this.chevronRef.current.transitionTo({ rotate: this.state.expanded ? '0deg' : '180deg' });
51
+        this.chevronRef.current.transitionTo({rotate: this.state.expanded ? '0deg' : '180deg'});
50 52
         this.setState({expanded: !this.state.expanded})
51 53
     };
52 54
 
53 55
     keyExtractor = (item: Object) => item.id.toString();
54 56
 
55
-    isItemFavorite(item: Object) {
56
-        return item.isFav !== undefined && item.isFav;
57
-    }
58
-
59 57
     renderItem = ({item}: Object) => {
60 58
         if (stringMatchQuery(item.name, this.props.currentSearchString)) {
61
-
62 59
             const onPress = () => this.props.onGroupPress(item);
63 60
             const onStartPress = () => this.props.onFavoritePress(item);
64 61
             return (
65
-                <List.Item
66
-                    title={item.name}
62
+                <GroupListItem
63
+                    height={LIST_ITEM_HEIGHT}
64
+                    item={item}
67 65
                     onPress={onPress}
68
-                    left={props =>
69
-                        <List.Icon
70
-                            {...props}
71
-                            icon={"chevron-right"}/>}
72
-                    right={props =>
73
-                        <IconButton
74
-                            {...props}
75
-                            icon={"star"}
76
-                            onPress={onStartPress}
77
-                            color={this.isItemFavorite(item) ? this.props.theme.colors.tetrisScore : props.color}
78
-                        />}
79
-                    style={{
80
-                        height: LIST_ITEM_HEIGHT,
81
-                        justifyContent: 'center',
82
-                    }}
83
-                />
66
+                    onStartPress={onStartPress}/>
84 67
             );
85 68
         } else
86 69
             return null;
@@ -90,16 +73,20 @@ class GroupListAccordion extends React.Component<Props, State> {
90 73
 
91 74
     render() {
92 75
         const item = this.props.item;
76
+        const accordionColor = this.state.expanded
77
+            ? this.props.theme.colors.primary
78
+            : this.props.theme.colors.text;
79
+        // console.log(item.id);
93 80
         return (
94 81
             <View>
95 82
                 <List.Item
96 83
                     title={item.name}
97
-                    expanded={this.state.expanded}
98 84
                     onPress={this.onPress}
99 85
                     style={{
100 86
                         height: this.props.height,
101 87
                         justifyContent: 'center',
102 88
                     }}
89
+                    titleStyle={{color: accordionColor}}
103 90
                     left={props =>
104 91
                         item.id === "0"
105 92
                             ? <List.Icon
@@ -112,22 +99,30 @@ class GroupListAccordion extends React.Component<Props, State> {
112 99
                         ref={this.chevronRef}
113 100
                         {...props}
114 101
                         icon={"chevron-down"}
102
+                        color={this.state.expanded
103
+                            ? this.props.theme.colors.primary
104
+                            : props.color
105
+                        }
115 106
                         useNativeDriver
116 107
                     />}
117 108
                 />
118 109
                 <Collapsible
119 110
                     collapsed={!this.state.expanded}
111
+                    ease={"easeInOut"}
120 112
                 >
121
-                    <FlatList
122
-                        data={item.content}
123
-                        extraData={this.props.currentSearchString}
124
-                        renderItem={this.renderItem}
125
-                        keyExtractor={this.keyExtractor}
126
-                        listKey={item.id}
127
-                        // Performance props, see https://reactnative.dev/docs/optimizing-flatlist-configuration
128
-                        getItemLayout={this.itemLayout} // Broken with search
129
-                        removeClippedSubviews={true}
130
-                    />
113
+                    {this.state.expanded // Only render list if expanded for increased performance
114
+                        ? <FlatList
115
+                            data={item.content}
116
+                            extraData={this.props.currentSearchString}
117
+                            renderItem={this.renderItem}
118
+                            keyExtractor={this.keyExtractor}
119
+                            listKey={item.id}
120
+                            // Performance props, see https://reactnative.dev/docs/optimizing-flatlist-configuration
121
+                            getItemLayout={this.itemLayout} // Broken with search
122
+                            removeClippedSubviews={true}
123
+                        />
124
+                        : null}
125
+
131 126
                 </Collapsible>
132 127
             </View>
133 128
         );

+ 66
- 0
src/components/Lists/GroupListItem.js View File

@@ -0,0 +1,66 @@
1
+// @flow
2
+
3
+import * as React from 'react';
4
+import {IconButton, List, withTheme} from 'react-native-paper';
5
+
6
+type Props = {
7
+    theme: Object,
8
+    onPress: Function,
9
+    onStartPress: Function,
10
+    item: Object,
11
+    height: number,
12
+}
13
+
14
+type State = {
15
+    isFav: boolean,
16
+}
17
+
18
+class GroupListItem extends React.Component<Props, State> {
19
+
20
+    colors: Object;
21
+
22
+    constructor(props) {
23
+        super(props);
24
+        this.colors = props.theme.colors;
25
+        this.state = {
26
+            isFav: (props.item.isFav !== undefined && props.item.isFav),
27
+        }
28
+    }
29
+
30
+    shouldComponentUpdate(prevProps: Props, prevState: State) {
31
+        return (prevState.isFav !== this.state.isFav);
32
+    }
33
+
34
+    onStarPress = () => {
35
+        this.setState({isFav: !this.state.isFav});
36
+        this.props.onStartPress();
37
+    }
38
+
39
+    render() {
40
+        return (
41
+            <List.Item
42
+                title={this.props.item.name}
43
+                onPress={this.props.onPress}
44
+                left={props =>
45
+                    <List.Icon
46
+                        {...props}
47
+                        icon={"chevron-right"}/>}
48
+                right={props =>
49
+                    <IconButton
50
+                        {...props}
51
+                        icon={"star"}
52
+                        onPress={this.onStarPress}
53
+                        color={this.state.isFav
54
+                            ? this.props.theme.colors.tetrisScore
55
+                            : props.color}
56
+                    />}
57
+                style={{
58
+                    height: this.props.height,
59
+                    justifyContent: 'center',
60
+                }}
61
+            />
62
+        );
63
+    }
64
+}
65
+
66
+export default withTheme(GroupListItem);

+ 11
- 15
src/screens/GroupSelectionScreen.js View File

@@ -1,7 +1,7 @@
1 1
 // @flow
2 2
 
3 3
 import * as React from 'react';
4
-import {Platform, View} from "react-native";
4
+import {Platform} from "react-native";
5 5
 import i18n from "i18n-js";
6 6
 import {Searchbar, withTheme} from "react-native-paper";
7 7
 import {stringMatchQuery} from "../utils/Search";
@@ -206,20 +206,16 @@ class GroupSelectionScreen extends React.Component<Props, State> {
206 206
 
207 207
     render() {
208 208
         return (
209
-            <View style={{
210
-                height: '100%'
211
-            }}>
212
-                <WebSectionList
213
-                    {...this.props}
214
-                    createDataset={this.createDataset}
215
-                    autoRefreshTime={0}
216
-                    refreshOnFocus={false}
217
-                    fetchUrl={GROUPS_URL}
218
-                    renderItem={this.renderItem}
219
-                    updateData={this.state.currentSearchString + this.state.favoriteGroups.length}
220
-                    itemHeight={LIST_ITEM_HEIGHT}
221
-                />
222
-            </View>
209
+            <WebSectionList
210
+                {...this.props}
211
+                createDataset={this.createDataset}
212
+                autoRefreshTime={0}
213
+                refreshOnFocus={false}
214
+                fetchUrl={GROUPS_URL}
215
+                renderItem={this.renderItem}
216
+                updateData={this.state.currentSearchString + this.state.favoriteGroups.length}
217
+                itemHeight={LIST_ITEM_HEIGHT}
218
+            />
223 219
         );
224 220
     }
225 221
 }

Loading…
Cancel
Save