import React, { Component } from 'react';
import { View, ActivityIndicator, Image, Text } from 'react-native';
import { SafeBlueArea, BlueNavigationStyle } from '../../BlueComponents';
import SortableList from 'react-native-sortable-list';
import LinearGradient from 'react-native-linear-gradient';
import PropTypes from 'prop-types';
import { PlaceholderWallet, LightningCustodianWallet } from '../../class';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import WalletGradient from '../../class/walletGradient';
let EV = require('../../events');
/** @type {AppStorage} */
let BlueApp = require('../../BlueApp');
let loc = require('../../loc/');
export default class ReorderWallets extends Component {
static navigationOptions = ({ navigation }) => ({
...BlueNavigationStyle(
navigation,
true,
navigation.getParam('customCloseButtonFunction') ? navigation.state.params.customCloseButtonFunction : undefined,
),
title: loc.wallets.reorder.title,
});
constructor(props) {
super(props);
this.state = {
isLoading: true,
data: [],
hasMovedARow: false,
};
}
componentDidMount() {
this.props.navigation.setParams({
customCloseButtonFunction: async () => {
if (this.sortableList.state.data.length === this.state.data.length && this.state.hasMovedARow) {
let newWalletsOrderArray = [];
this.sortableList.state.order.forEach(element => {
newWalletsOrderArray.push(this.state.data[element]);
});
BlueApp.wallets = newWalletsOrderArray;
await BlueApp.saveToDisk();
setTimeout(function() {
EV(EV.enum.WALLETS_COUNT_CHANGED);
}, 500); // adds some animaton
this.props.navigation.dismiss();
} else {
this.props.navigation.dismiss();
}
},
});
const wallets = BlueApp.getWallets().filter(wallet => wallet.type !== PlaceholderWallet.type);
this.setState({
data: wallets,
isLoading: false,
});
}
_renderItem = (item, _active) => {
if (!item.data) {
return;
}
item = item.data;
return (
{item.getLabel()}
{loc.formatBalance(Number(item.getBalance()), item.getPreferredBalanceUnit(), true)}
{loc.wallets.list.latest_transaction}
{loc.transactionTimeToReadable(item.getLatestTransactionTime())}
);
};
render() {
if (this.state.isLoading) {
return (
);
}
return (
(this.sortableList = ref)}
style={{ flex: 1 }}
data={this.state.data}
renderRow={this._renderItem}
onChangeOrder={() => {
ReactNativeHapticFeedback.trigger('impactMedium', { ignoreAndroidSystemSettings: false });
this.setState({ hasMovedARow: true });
}}
onActivateRow={() => {
ReactNativeHapticFeedback.trigger('selection', { ignoreAndroidSystemSettings: false });
}}
onReleaseRow={() => {
ReactNativeHapticFeedback.trigger('impactLight', { ignoreAndroidSystemSettings: false });
}}
/>
);
}
}
ReorderWallets.propTypes = {
navigation: PropTypes.shape({
navigate: PropTypes.func,
setParams: PropTypes.func,
dismiss: PropTypes.func,
}),
};