2019-07-26 14:14:01 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
|
|
|
import WebDataManager from "../utils/WebDataManager";
|
2019-08-07 10:24:35 +02:00
|
|
|
import {H3, Spinner, Tab, TabHeading, Tabs, Text} from "native-base";
|
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-08-07 10:24:35 +02:00
|
|
|
import BaseContainer from "./BaseContainer";
|
2019-07-26 14:14:01 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
navigation: Object,
|
|
|
|
}
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
refreshing: boolean,
|
|
|
|
firstLoading: boolean,
|
|
|
|
fetchedData: Object,
|
2019-08-13 12:20:03 +02:00
|
|
|
machinesWatched: Array<string>,
|
2019-07-26 14:14:01 +02:00
|
|
|
};
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Class used to create a basic list view using online json data.
|
|
|
|
* Used by inheriting from it and redefining getters.
|
|
|
|
*/
|
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-08-06 13:28:11 +02:00
|
|
|
willFocusSubscription: function;
|
|
|
|
willBlurSubscription: function;
|
2019-07-31 19:11:30 +02:00
|
|
|
refreshInterval: IntervalID;
|
2019-08-01 11:04:27 +02:00
|
|
|
refreshTime: number;
|
2019-08-08 18:40:42 +02:00
|
|
|
lastRefresh: Date;
|
2019-07-31 19:11:30 +02:00
|
|
|
|
2019-08-13 12:20:03 +02:00
|
|
|
minTimeBetweenRefresh = 60;
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
constructor(fetchUrl: string, refreshTime: number) {
|
2019-07-26 14:14:01 +02:00
|
|
|
super();
|
2019-07-30 20:40:17 +02:00
|
|
|
this.webDataManager = new WebDataManager(fetchUrl);
|
2019-07-31 19:11:30 +02:00
|
|
|
this.refreshTime = refreshTime;
|
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
|
|
|
};
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Get the translation for the header in the current language
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2019-08-06 14:06:05 +02:00
|
|
|
getHeaderTranslation(): string {
|
2019-07-26 14:14:01 +02:00
|
|
|
return "Header";
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Get the translation for the toasts in the current language
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
getUpdateToastTranslations(): Array<string> {
|
2019-07-26 14:14:01 +02:00
|
|
|
return ["whoa", "nah"];
|
|
|
|
}
|
|
|
|
|
2019-08-13 12:20:03 +02:00
|
|
|
setMinTimeRefresh(value: number) {
|
|
|
|
this.minTimeBetweenRefresh = value;
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:14:01 +02:00
|
|
|
/**
|
2019-08-06 13:28:11 +02:00
|
|
|
* Register react navigation events on first screen load.
|
|
|
|
* Allows to detect when the screen is focused
|
2019-07-26 14:14:01 +02:00
|
|
|
*/
|
|
|
|
componentDidMount() {
|
2019-07-31 19:11:30 +02:00
|
|
|
this.willFocusSubscription = this.props.navigation.addListener(
|
|
|
|
'willFocus',
|
|
|
|
payload => {
|
|
|
|
this.onScreenFocus();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
this.willBlurSubscription = this.props.navigation.addListener(
|
|
|
|
'willBlur',
|
|
|
|
payload => {
|
|
|
|
this.onScreenBlur();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Refresh data when focusing the screen and setup a refresh interval if asked to
|
|
|
|
*/
|
2019-07-31 19:11:30 +02:00
|
|
|
onScreenFocus() {
|
2019-07-26 14:14:01 +02:00
|
|
|
this._onRefresh();
|
2019-07-31 19:11:30 +02:00
|
|
|
if (this.refreshTime > 0)
|
|
|
|
this.refreshInterval = setInterval(() => this._onRefresh(), this.refreshTime)
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Remove any interval on un-focus
|
|
|
|
*/
|
2019-07-31 19:11:30 +02:00
|
|
|
onScreenBlur() {
|
|
|
|
clearInterval(this.refreshInterval);
|
2019-07-26 14:14:01 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Unregister from event when un-mounting components
|
|
|
|
*/
|
2019-07-31 19:11:30 +02:00
|
|
|
componentWillUnmount() {
|
2019-08-05 14:52:18 +02:00
|
|
|
if (this.willBlurSubscription !== undefined)
|
|
|
|
this.willBlurSubscription.remove();
|
|
|
|
if (this.willFocusSubscription !== undefined)
|
|
|
|
this.willFocusSubscription.remove();
|
2019-07-31 19:11:30 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Refresh data and show a toast if any error occurred
|
|
|
|
* @private
|
|
|
|
*/
|
2019-07-26 14:14:01 +02:00
|
|
|
_onRefresh = () => {
|
2019-08-08 18:40:42 +02:00
|
|
|
let canRefresh;
|
|
|
|
if (this.lastRefresh !== undefined)
|
2019-09-16 19:13:01 +02:00
|
|
|
canRefresh = (new Date().getTime() - this.lastRefresh.getTime()) / 1000 > this.minTimeBetweenRefresh;
|
2019-08-08 18:40:42 +02:00
|
|
|
else
|
|
|
|
canRefresh = true;
|
|
|
|
|
|
|
|
if (canRefresh) {
|
|
|
|
this.setState({refreshing: true});
|
|
|
|
this.webDataManager.readData()
|
|
|
|
.then((fetchedData) => {
|
|
|
|
this.setState({
|
|
|
|
fetchedData: fetchedData,
|
|
|
|
refreshing: false,
|
|
|
|
firstLoading: false
|
|
|
|
});
|
|
|
|
this.lastRefresh = new Date();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.setState({
|
|
|
|
fetchedData: {},
|
|
|
|
refreshing: false,
|
|
|
|
firstLoading: false
|
|
|
|
});
|
|
|
|
this.webDataManager.showUpdateToast(this.getUpdateToastTranslations()[0], this.getUpdateToastTranslations()[1]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:14:01 +02:00
|
|
|
};
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Get the render item to be used for display in the list.
|
|
|
|
* Must be overridden by inheriting class.
|
|
|
|
*
|
|
|
|
* @param item
|
|
|
|
* @param section
|
|
|
|
* @param data
|
|
|
|
* @return {*}
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Get the render item to be used for the section title in the list.
|
|
|
|
* Must be overridden by inheriting class.
|
|
|
|
*
|
|
|
|
* @param title
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2019-10-06 10:54: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-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Get the render item to be used when the list is empty.
|
|
|
|
* No need to be overridden, has good defaults.
|
|
|
|
*
|
|
|
|
* @param text
|
2019-08-06 14:06:05 +02:00
|
|
|
* @param isSpinner
|
2019-08-06 13:28:11 +02:00
|
|
|
* @param icon
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2019-08-06 14:06:05 +02:00
|
|
|
getEmptyRenderItem(text: string, isSpinner: boolean, icon: string) {
|
2019-07-31 17:41:07 +02:00
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<View style={{
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
width: '100%',
|
|
|
|
height: 100,
|
|
|
|
marginBottom: 20
|
|
|
|
}}>
|
2019-08-06 14:06:05 +02:00
|
|
|
{isSpinner ?
|
|
|
|
<Spinner/>
|
|
|
|
:
|
|
|
|
<CustomMaterialIcon
|
|
|
|
icon={icon}
|
|
|
|
fontSize={100}
|
|
|
|
width={100}
|
|
|
|
color={ThemeManager.getCurrentThemeVariables().fetchedDataSectionListErrorText}/>}
|
2019-07-31 17:41:07 +02:00
|
|
|
</View>
|
|
|
|
|
|
|
|
<H3 style={{
|
|
|
|
textAlign: 'center',
|
|
|
|
marginRight: 20,
|
|
|
|
marginLeft: 20,
|
|
|
|
color: ThemeManager.getCurrentThemeVariables().fetchedDataSectionListErrorText
|
|
|
|
}}>
|
|
|
|
{text}
|
|
|
|
</H3>
|
|
|
|
</View>);
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:14:01 +02:00
|
|
|
/**
|
2019-08-06 13:28:11 +02:00
|
|
|
* Create the dataset to be used in the list from the data fetched.
|
|
|
|
* Must be overridden.
|
|
|
|
*
|
2019-07-26 14:14:01 +02:00
|
|
|
* @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-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Create the dataset when no fetched data is available.
|
|
|
|
* No need to be overridden, has good defaults.
|
|
|
|
*
|
|
|
|
* @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'),
|
2019-08-06 14:06:05 +02:00
|
|
|
isSpinner: this.state.refreshing,
|
2019-07-31 17:41:07 +02:00
|
|
|
icon: this.state.refreshing ?
|
|
|
|
'refresh' :
|
|
|
|
'access-point-network-off'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
keyExtractor: (item: Object) => item.text,
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Should the app use a tab layout instead of a section list ?
|
|
|
|
* If yes, each section will be rendered in a new tab.
|
|
|
|
* Can be overridden.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2019-07-28 12:40:35 +02:00
|
|
|
hasTabs() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-19 22:19:47 +02:00
|
|
|
hasBackButton() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-08 18:05:18 +02:00
|
|
|
getRightButton() {
|
|
|
|
return <View/>
|
|
|
|
}
|
|
|
|
|
2019-10-06 10:54:01 +02:00
|
|
|
hasStickyHeader() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Get the section list render using the generated dataset
|
|
|
|
*
|
|
|
|
* @param dataset
|
|
|
|
* @return
|
|
|
|
*/
|
2019-07-28 12:40:35 +02:00
|
|
|
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}
|
2019-10-06 10:54:01 +02:00
|
|
|
stickySectionHeadersEnabled={this.hasStickyHeader()}
|
2019-07-28 12:40:35 +02:00
|
|
|
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 ?
|
2019-08-06 14:06:05 +02:00
|
|
|
this.getEmptyRenderItem(item.text, item.isSpinner, item.icon) :
|
2019-07-31 17:41:07 +02:00
|
|
|
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
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:28:11 +02:00
|
|
|
/**
|
|
|
|
* Generate the tabs containing the lists
|
|
|
|
*
|
|
|
|
* @param dataset
|
|
|
|
* @return
|
|
|
|
*/
|
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>
|
2019-09-16 19:13:01 +02:00
|
|
|
<CustomMaterialIcon
|
|
|
|
icon={dataset[i].icon}
|
|
|
|
color={ThemeManager.getCurrentThemeVariables().tabIconColor}
|
|
|
|
fontSize={20}
|
2019-07-28 12:56:20 +02:00
|
|
|
/>
|
|
|
|
<Text>{dataset[i].title}</Text>
|
2019-07-31 14:22:36 +02:00
|
|
|
</TabHeading>}
|
2019-08-05 14:52:18 +02:00
|
|
|
key={dataset[i].title}
|
2019-08-06 13:28:11 +02:00
|
|
|
style={{backgroundColor: ThemeManager.getCurrentThemeVariables().containerBgColor}}>
|
2019-07-31 14:34:38 +02:00
|
|
|
{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 (
|
2019-09-17 10:37:52 +02:00
|
|
|
<BaseContainer
|
|
|
|
navigation={nav} headerTitle={this.getHeaderTranslation()}
|
|
|
|
headerRightButton={this.getRightButton()}
|
2019-09-19 22:19:47 +02:00
|
|
|
hasTabs={this.hasTabs()}
|
|
|
|
hasBackButton={this.hasBackButton()}
|
|
|
|
>
|
2019-07-28 12:40:35 +02:00
|
|
|
{this.hasTabs() ?
|
2019-09-17 10:37:52 +02:00
|
|
|
<Tabs
|
|
|
|
tabContainerStyle={{
|
|
|
|
elevation: 0, // Fix for android shadow
|
|
|
|
}}
|
|
|
|
>
|
2019-07-28 12:40:35 +02:00
|
|
|
{this.getTabbedView(dataset)}
|
|
|
|
</Tabs>
|
|
|
|
:
|
2019-07-31 14:34:38 +02:00
|
|
|
this.getSectionList(dataset)
|
2019-07-28 12:40:35 +02:00
|
|
|
}
|
2019-08-07 10:24:35 +02:00
|
|
|
</BaseContainer>
|
2019-07-26 14:14:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|