2019-07-26 14:14:01 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
|
|
|
import WebDataManager from "../utils/WebDataManager";
|
2019-07-31 17:41:07 +02:00
|
|
|
import {Container, H3, Tab, TabHeading, Tabs, Text} from "native-base";
|
2019-07-26 14:14:01 +02:00
|
|
|
import CustomHeader from "./CustomHeader";
|
2019-07-31 14:34:38 +02:00
|
|
|
import {RefreshControl, SectionList, View} from "react-native";
|
2019-07-28 12:56:20 +02:00
|
|
|
import CustomMaterialIcon from "./CustomMaterialIcon";
|
2019-07-31 17:41:07 +02:00
|
|
|
import i18n from 'i18n-js';
|
|
|
|
import ThemeManager from "../utils/ThemeManager";
|
2019-07-26 14:14:01 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
navigation: Object,
|
|
|
|
}
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
refreshing: boolean,
|
|
|
|
firstLoading: boolean,
|
|
|
|
fetchedData: Object,
|
2019-07-30 20:40:17 +02:00
|
|
|
machinesWatched: Array<Object>,
|
2019-07-26 14:14:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class FetchedDataSectionList extends React.Component<Props, State> {
|
|
|
|
|
2019-07-28 12:40:35 +02:00
|
|
|
webDataManager: WebDataManager;
|
2019-07-26 14:14:01 +02:00
|
|
|
|
2019-07-30 20:40:17 +02:00
|
|
|
constructor(fetchUrl: string) {
|
2019-07-26 14:14:01 +02:00
|
|
|
super();
|
2019-07-30 20:40:17 +02:00
|
|
|
this.webDataManager = new WebDataManager(fetchUrl);
|
2019-07-26 14:14:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
|
|
|
refreshing: false,
|
|
|
|
firstLoading: true,
|
|
|
|
fetchedData: {},
|
2019-07-28 12:40:35 +02:00
|
|
|
machinesWatched: [],
|
2019-07-26 14:14:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
getHeaderTranslation() {
|
|
|
|
return "Header";
|
|
|
|
}
|
|
|
|
|
2019-07-28 12:40:35 +02:00
|
|
|
getUpdateToastTranslations() {
|
2019-07-26 14:14:01 +02:00
|
|
|
return ["whoa", "nah"];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh the FetchedData on first screen load
|
|
|
|
*/
|
|
|
|
componentDidMount() {
|
|
|
|
this._onRefresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onRefresh = () => {
|
|
|
|
this.setState({refreshing: true});
|
|
|
|
this.webDataManager.readData().then((fetchedData) => {
|
|
|
|
this.setState({
|
|
|
|
fetchedData: fetchedData,
|
|
|
|
refreshing: false,
|
|
|
|
firstLoading: false
|
|
|
|
});
|
2019-07-31 14:34:38 +02:00
|
|
|
this.webDataManager.showUpdateToast(this.getUpdateToastTranslations()[0], this.getUpdateToastTranslations()[1]);
|
2019-07-26 14:14:01 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-07-28 12:40:35 +02:00
|
|
|
getRenderItem(item: Object, section: Object, data: Object) {
|
|
|
|
return <View/>;
|
2019-07-26 14:14:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getRenderSectionHeader(title: String) {
|
2019-07-28 12:40:35 +02:00
|
|
|
return <View/>;
|
2019-07-26 14:14:01 +02:00
|
|
|
}
|
|
|
|
|
2019-07-31 17:41:07 +02:00
|
|
|
getEmptyRenderItem(text: string, icon: string) {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<View style={{
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
width: '100%',
|
|
|
|
height: 100,
|
|
|
|
marginBottom: 20
|
|
|
|
}}>
|
|
|
|
<CustomMaterialIcon
|
|
|
|
icon={icon}
|
|
|
|
fontSize={100}
|
|
|
|
width={100}
|
|
|
|
color={ThemeManager.getCurrentThemeVariables().fetchedDataSectionListErrorText}/>
|
|
|
|
</View>
|
|
|
|
|
|
|
|
<H3 style={{
|
|
|
|
textAlign: 'center',
|
|
|
|
marginRight: 20,
|
|
|
|
marginLeft: 20,
|
|
|
|
color: ThemeManager.getCurrentThemeVariables().fetchedDataSectionListErrorText
|
|
|
|
}}>
|
|
|
|
{text}
|
|
|
|
</H3>
|
|
|
|
</View>);
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:14:01 +02:00
|
|
|
/**
|
|
|
|
* Create the dataset to be used in the list from the data fetched
|
|
|
|
* @param fetchedData {Object}
|
|
|
|
* @return {Array}
|
|
|
|
*/
|
2019-07-28 12:40:35 +02:00
|
|
|
createDataset(fetchedData: Object): Array<Object> {
|
2019-07-26 14:14:01 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-07-31 17:41:07 +02:00
|
|
|
createEmptyDataset() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
title: '',
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
text: this.state.refreshing ?
|
|
|
|
i18n.t('general.loading') :
|
|
|
|
i18n.t('general.networkError'),
|
|
|
|
icon: this.state.refreshing ?
|
|
|
|
'refresh' :
|
|
|
|
'access-point-network-off'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
keyExtractor: (item: Object) => item.text,
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-07-28 12:40:35 +02:00
|
|
|
hasTabs() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
getSectionList(dataset: Array<Object>) {
|
2019-07-31 17:41:07 +02:00
|
|
|
let isEmpty = dataset[0].data.length === 0;
|
|
|
|
if (isEmpty)
|
|
|
|
dataset = this.createEmptyDataset();
|
2019-07-28 12:40:35 +02:00
|
|
|
return (
|
|
|
|
<SectionList
|
|
|
|
sections={dataset}
|
|
|
|
refreshControl={
|
|
|
|
<RefreshControl
|
|
|
|
refreshing={this.state.refreshing}
|
|
|
|
onRefresh={this._onRefresh}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
renderSectionHeader={({section: {title}}) =>
|
2019-07-31 17:41:07 +02:00
|
|
|
isEmpty ?
|
|
|
|
<View/> :
|
|
|
|
this.getRenderSectionHeader(title)
|
2019-07-28 12:40:35 +02:00
|
|
|
}
|
|
|
|
renderItem={({item, section}) =>
|
2019-07-31 17:41:07 +02:00
|
|
|
isEmpty ?
|
|
|
|
this.getEmptyRenderItem(item.text, item.icon) :
|
|
|
|
this.getRenderItem(item, section, dataset)
|
2019-07-28 12:40:35 +02:00
|
|
|
}
|
|
|
|
style={{minHeight: 300, width: '100%'}}
|
2019-07-31 17:41:07 +02:00
|
|
|
contentContainerStyle={
|
|
|
|
isEmpty ?
|
|
|
|
{flexGrow: 1, justifyContent: 'center', alignItems: 'center'} : {}
|
|
|
|
}
|
2019-07-28 12:40:35 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getTabbedView(dataset: Array<Object>) {
|
|
|
|
let tabbedView = [];
|
|
|
|
for (let i = 0; i < dataset.length; i++) {
|
2019-07-31 14:34:38 +02:00
|
|
|
tabbedView.push(
|
2019-07-28 12:56:20 +02:00
|
|
|
<Tab heading={
|
|
|
|
<TabHeading>
|
|
|
|
<CustomMaterialIcon icon={dataset[i].icon}
|
|
|
|
color={'#fff'}
|
|
|
|
fontSize={20}
|
|
|
|
/>
|
|
|
|
<Text>{dataset[i].title}</Text>
|
2019-07-31 14:22:36 +02:00
|
|
|
</TabHeading>}
|
2019-07-31 14:34:38 +02:00
|
|
|
key={dataset[i].title}>
|
|
|
|
{this.getSectionList(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
title: dataset[i].title,
|
|
|
|
data: dataset[i].data,
|
|
|
|
extraData: dataset[i].extraData,
|
|
|
|
keyExtractor: dataset[i].keyExtractor
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)}
|
2019-07-28 12:40:35 +02:00
|
|
|
</Tab>);
|
|
|
|
}
|
|
|
|
return tabbedView;
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:14:01 +02:00
|
|
|
render() {
|
|
|
|
const nav = this.props.navigation;
|
|
|
|
const dataset = this.createDataset(this.state.fetchedData);
|
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<CustomHeader navigation={nav} title={this.getHeaderTranslation()}/>
|
2019-07-28 12:40:35 +02:00
|
|
|
{this.hasTabs() ?
|
|
|
|
<Tabs>
|
|
|
|
{this.getTabbedView(dataset)}
|
|
|
|
</Tabs>
|
|
|
|
:
|
2019-07-31 14:34:38 +02:00
|
|
|
this.getSectionList(dataset)
|
2019-07-28 12:40:35 +02:00
|
|
|
}
|
2019-07-26 14:14:01 +02:00
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|