2019-07-26 14:14:01 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
|
|
|
import WebDataManager from "../utils/WebDataManager";
|
2019-07-28 12:40:35 +02:00
|
|
|
import {Container, Content, Tab, TabHeading, Tabs, Text} from "native-base";
|
2019-07-26 14:14:01 +02:00
|
|
|
import CustomHeader from "./CustomHeader";
|
2019-07-30 20:40:17 +02:00
|
|
|
import {RefreshControl, SectionList, View, TouchableHighlight} from "react-native";
|
2019-07-28 12:56:20 +02:00
|
|
|
import CustomMaterialIcon from "./CustomMaterialIcon";
|
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"];
|
|
|
|
}
|
|
|
|
|
2019-07-28 12:56:20 +02:00
|
|
|
shouldShowUpdateToast() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:14:01 +02:00
|
|
|
/**
|
|
|
|
* 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-28 12:56:20 +02:00
|
|
|
if (this.shouldShowUpdateToast())
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-28 12:40:35 +02:00
|
|
|
hasTabs() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
getSectionList(dataset: Array<Object>) {
|
|
|
|
return (
|
|
|
|
<SectionList
|
|
|
|
sections={dataset}
|
|
|
|
refreshControl={
|
|
|
|
<RefreshControl
|
|
|
|
refreshing={this.state.refreshing}
|
|
|
|
onRefresh={this._onRefresh}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
renderSectionHeader={({section: {title}}) =>
|
|
|
|
this.getRenderSectionHeader(title)
|
|
|
|
}
|
|
|
|
renderItem={({item, section}) =>
|
|
|
|
this.getRenderItem(item, section, dataset)
|
|
|
|
}
|
|
|
|
style={{minHeight: 300, width: '100%'}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getTabbedView(dataset: Array<Object>) {
|
|
|
|
let tabbedView = [];
|
|
|
|
for (let i = 0; i < dataset.length; i++) {
|
2019-07-31 14:22:36 +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>}
|
|
|
|
key={dataset[i].title}>
|
2019-07-28 12:40:35 +02:00
|
|
|
<Content padder>
|
|
|
|
{this.getSectionList(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
title: dataset[i].title,
|
|
|
|
data: dataset[i].data,
|
|
|
|
extraData: dataset[i].extraData,
|
2019-07-31 14:22:36 +02:00
|
|
|
keyExtractor: dataset[i].keyExtractor
|
2019-07-28 12:40:35 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
)}
|
|
|
|
</Content>
|
|
|
|
</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>
|
|
|
|
:
|
|
|
|
<Content padder>
|
|
|
|
{this.getSectionList(dataset)}
|
|
|
|
</Content>
|
|
|
|
}
|
2019-07-26 14:14:01 +02:00
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|