Skip to content

Commit 4e46495

Browse files
committed
rework the android edithome view
1 parent 9ef3557 commit 4e46495

File tree

2 files changed

+91
-61
lines changed

2 files changed

+91
-61
lines changed

source/views/home/edit/list.android.js

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// @flow
22

33
import React from 'react'
4-
import {Dimensions, StyleSheet} from 'react-native'
4+
import {StyleSheet, FlatList} from 'react-native'
55

66
import {saveHomescreenOrder} from '../../../flux/parts/homescreen'
77
import {connect} from 'react-redux'
88
import * as c from '../../components/colors'
9-
import fromPairs from 'lodash/fromPairs'
109
import sortBy from 'lodash/sortBy'
1110

1211
import type {ViewType} from '../../views'
@@ -22,28 +21,54 @@ const styles = StyleSheet.create({
2221
})
2322

2423
type Props = {
25-
onSaveOrder: (ViewType[]) => any,
24+
onChangeOrder: (string[]) => any,
2625
order: string[],
2726
}
28-
;[]
2927

30-
type State = {
31-
width: number,
32-
}
33-
;[]
34-
35-
class EditHomeView extends React.PureComponent<void, Props, State> {
28+
class EditHomeView extends React.PureComponent<void, Props, void> {
3629
static navigationOptions = {
3730
title: 'Edit Home',
3831
}
3932

40-
renderRow = ({data, active}: {data: ViewType, active: boolean}) => (
41-
<EditHomeRow data={data} active={active} width={this.state.width} />
42-
)
33+
handleMoveUp = (currentOrder: string[], viewName: string) => {
34+
const currentIndex = currentOrder.indexOf(viewName)
35+
const newIndex = Math.max(0, currentIndex - 1)
36+
this.onChangeOrder(currentOrder, viewName, newIndex)
37+
}
38+
39+
handleMoveDown = (currentOrder: string[], viewName: string) => {
40+
const currentIndex = currentOrder.indexOf(viewName)
41+
const newIndex = Math.min(currentOrder.length - 1, currentIndex + 1)
42+
this.onChangeOrder(currentOrder, viewName, newIndex)
43+
}
44+
45+
onChangeOrder = (
46+
currentOrder: string[],
47+
viewName: string,
48+
newIndex: number,
49+
) => {
50+
const newOrder = currentOrder.filter(v => v !== viewName)
51+
newOrder.splice(newIndex, 0, viewName)
52+
53+
this.props.onChangeOrder(newOrder)
54+
}
4355

44-
onChangeOrder = (order: ViewType[]) => this.props.onSaveOrder(order)
56+
renderItem = ({item}: {item: ViewType}) => {
57+
const index = this.props.order.indexOf(item.view)
58+
const last = this.props.order.length - 1
59+
return (
60+
<EditHomeRow
61+
item={item}
62+
isFirst={index === 0}
63+
isLast={index === last}
64+
order={this.props.order}
65+
onMoveUp={this.handleMoveUp}
66+
onMoveDown={this.handleMoveDown}
67+
/>
68+
)
69+
}
4570

46-
keyExtractor = (item: ViewType) => view.view
71+
keyExtractor = (item: ViewType) => item.view
4772

4873
render() {
4974
const data = sortBy(allViews, (item: ViewType) =>
@@ -53,24 +78,22 @@ class EditHomeView extends React.PureComponent<void, Props, State> {
5378
return (
5479
<FlatList
5580
contentContainerStyle={styles.contentContainer}
56-
data={allViews}
81+
data={data}
5782
keyExtractor={this.keyExtractor}
5883
onChangeOrder={this.onChangeOrder}
59-
renderRow={this.renderRow}
84+
renderItem={this.renderItem}
6085
/>
6186
)
6287
}
6388
}
6489

6590
function mapState(state) {
66-
return {
67-
order: state.homescreen.order,
68-
}
91+
return {order: state.homescreen.order}
6992
}
7093

7194
function mapDispatch(dispatch) {
7295
return {
73-
onSaveOrder: newOrder => dispatch(saveHomescreenOrder(newOrder)),
96+
onChangeOrder: newOrder => dispatch(saveHomescreenOrder(newOrder)),
7497
}
7598
}
7699

source/views/home/edit/row.android.js

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,101 @@
22

33
import React from 'react'
44
import {View, StyleSheet, Text} from 'react-native'
5-
6-
import * as c from '../../components/colors'
7-
85
import EntypoIcon from 'react-native-vector-icons/Entypo'
96
import IonIcon from 'react-native-vector-icons/Ionicons'
10-
7+
import * as c from '../../components/colors'
118
import {Touchable} from '../../components/touchable'
129
import type {ViewType} from '../../views'
1310

1411
const ROW_HORIZONTAL_MARGIN = 15
1512
const styles = StyleSheet.create({
1613
row: {
1714
flex: 1,
18-
1915
flexDirection: 'row',
2016
alignItems: 'center',
21-
2217
backgroundColor: c.white,
2318

2419
marginVertical: 5,
2520
marginHorizontal: ROW_HORIZONTAL_MARGIN,
2621
paddingVertical: 12,
2722
paddingRight: 10,
28-
2923
borderRadius: 4,
30-
3124
elevation: 1,
3225
},
3326
icon: {
34-
paddingLeft: 10,
35-
paddingRight: 10,
36-
color: c.black,
27+
paddingHorizontal: 10,
28+
color: c.androidTextColor,
3729
},
38-
viewIcon: {
39-
// marginRight: 20,
30+
disabledIcon: {
31+
color: c.androidDisabledIcon,
4032
},
4133
text: {
4234
flex: 1,
4335
flexShrink: 0,
4436
fontSize: 18,
45-
color: c.white,
37+
color: c.black,
4638
},
4739
})
4840

4941
const MenuIcon = ({icon, tint}: {icon: string, tint: string}) => (
50-
<EntypoIcon
51-
name={icon}
52-
size={32}
53-
style={[styles.icon, styles.viewIcon, {color: tint}]}
54-
/>
42+
<EntypoIcon name={icon} size={32} style={[styles.icon, {color: tint}]} />
5543
)
5644

5745
type Props = {
58-
data: ViewType,
59-
width: number,
60-
onMoveUp: string => any,
61-
onMoveDown: string => any,
46+
item: ViewType,
47+
isFirst: boolean,
48+
isLast: boolean,
49+
order: string[],
50+
onMoveUp: (string[], string) => any,
51+
onMoveDown: (string[], string) => any,
6252
}
63-
;[]
6453

6554
export class EditHomeRow extends React.PureComponent<void, Props, void> {
6655
onMoveUp = () => {
67-
this.props.onMoveUp(this.props.data.view)
56+
this.props.onMoveUp(this.props.order, this.props.item.view)
6857
}
6958

7059
onMoveDown = () => {
71-
this.props.onMoveDown(this.props.data.view)
60+
this.props.onMoveDown(this.props.order, this.props.item.view)
7261
}
7362

7463
render() {
75-
const width = this.props.width - ROW_HORIZONTAL_MARGIN * 2
76-
64+
const {item, isFirst, isLast} = this.props
7765
return (
78-
<View style={[styles.row, {width}]}>
79-
<MenuIcon icon={this.props.data.icon} tint={this.props.data.tint} />
80-
81-
<Text style={[styles.text, {color: this.props.data.tint}]}>
82-
{this.props.data.title}
83-
</Text>
66+
<View style={styles.row}>
67+
<MenuIcon icon={this.props.item.icon} tint={item.tint} />
8468

85-
<Touchable onPress={this.onMoveUp}>
86-
<IonIcon name="md-arrow-up" size={32} style={styles.icon} />
87-
</Touchable>
69+
<Text style={[styles.text, {color: item.tint}]}>{item.title}</Text>
8870

89-
<Touchable onPress={this.onMoveDown}>
90-
<IonIcon name="md-arrow-down" size={32} style={styles.icon} />
91-
</Touchable>
71+
<ArrowIcon dir="up" disabled={isFirst} onPress={this.onMoveUp} />
72+
<ArrowIcon dir="down" disabled={isLast} onPress={this.onMoveDown} />
9273
</View>
9374
)
9475
}
9576
}
77+
78+
type ArrowIconProps = {
79+
dir: 'up' | 'down',
80+
disabled: boolean,
81+
onPress: () => any,
82+
}
83+
const ArrowIcon = ({dir, disabled, onPress}: ArrowIconProps) => {
84+
const icon = `md-arrow-${dir}`
85+
const size = 24
86+
87+
if (disabled) {
88+
return (
89+
<IonIcon
90+
name={icon}
91+
size={size}
92+
style={[styles.icon, styles.disabledIcon]}
93+
/>
94+
)
95+
}
96+
97+
return (
98+
<Touchable borderless={true} onPress={onPress}>
99+
<IonIcon name={icon} size={size} style={styles.icon} />
100+
</Touchable>
101+
)
102+
}

0 commit comments

Comments
 (0)