|
| 1 | +// @flow |
| 2 | + |
| 3 | +import React from 'react' |
| 4 | +import {StyleSheet, FlatList} from 'react-native' |
| 5 | + |
| 6 | +import {saveHomescreenOrder} from '../../../flux/parts/homescreen' |
| 7 | +import {connect} from 'react-redux' |
| 8 | +import * as c from '../../components/colors' |
| 9 | +import sortBy from 'lodash/sortBy' |
| 10 | + |
| 11 | +import type {ViewType} from '../../views' |
| 12 | +import {allViews} from '../../views' |
| 13 | +import {EditHomeRow} from './row' |
| 14 | + |
| 15 | +const styles = StyleSheet.create({ |
| 16 | + contentContainer: { |
| 17 | + backgroundColor: c.androidLightBackground, |
| 18 | + paddingTop: 10, |
| 19 | + paddingBottom: 20, |
| 20 | + }, |
| 21 | +}) |
| 22 | + |
| 23 | +type Props = { |
| 24 | + onChangeOrder: (string[]) => any, |
| 25 | + order: string[], |
| 26 | +} |
| 27 | + |
| 28 | +class EditHomeView extends React.PureComponent<void, Props, void> { |
| 29 | + static navigationOptions = { |
| 30 | + title: 'Edit Home', |
| 31 | + } |
| 32 | + |
| 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 | + } |
| 55 | + |
| 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 | + } |
| 70 | + |
| 71 | + keyExtractor = (item: ViewType) => item.view |
| 72 | + |
| 73 | + render() { |
| 74 | + const data = sortBy(allViews, (item: ViewType) => |
| 75 | + this.props.order.indexOf(item.view), |
| 76 | + ) |
| 77 | + |
| 78 | + return ( |
| 79 | + <FlatList |
| 80 | + contentContainerStyle={styles.contentContainer} |
| 81 | + data={data} |
| 82 | + keyExtractor={this.keyExtractor} |
| 83 | + onChangeOrder={this.onChangeOrder} |
| 84 | + renderItem={this.renderItem} |
| 85 | + /> |
| 86 | + ) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function mapState(state) { |
| 91 | + return {order: state.homescreen.order} |
| 92 | +} |
| 93 | + |
| 94 | +function mapDispatch(dispatch) { |
| 95 | + return { |
| 96 | + onChangeOrder: newOrder => dispatch(saveHomescreenOrder(newOrder)), |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export const ConnectedEditHomeView = connect(mapState, mapDispatch)( |
| 101 | + EditHomeView, |
| 102 | +) |
0 commit comments