Refresh lists on focus and every minute for proxiwash

This commit is contained in:
keplyx 2019-07-31 19:11:30 +02:00
parent 0201b9a389
commit 054d8f4a33
4 changed files with 42 additions and 5 deletions

View file

@ -24,9 +24,14 @@ export default class FetchedDataSectionList extends React.Component<Props, State
webDataManager: WebDataManager; webDataManager: WebDataManager;
constructor(fetchUrl: string) { willFocusSubscription : function;
willBlurSubscription : function;
refreshInterval: IntervalID;
constructor(fetchUrl: string, refreshTime : number) {
super(); super();
this.webDataManager = new WebDataManager(fetchUrl); this.webDataManager = new WebDataManager(fetchUrl);
this.refreshTime = refreshTime;
} }
state = { state = {
@ -48,10 +53,40 @@ export default class FetchedDataSectionList extends React.Component<Props, State
* Refresh the FetchedData on first screen load * Refresh the FetchedData on first screen load
*/ */
componentDidMount() { componentDidMount() {
this._onRefresh(); this.willFocusSubscription = this.props.navigation.addListener(
'willFocus',
payload => {
this.onScreenFocus();
}
);
this.willBlurSubscription = this.props.navigation.addListener(
'willBlur',
payload => {
this.onScreenBlur();
}
);
} }
onScreenFocus() {
this._onRefresh();
if (this.refreshTime > 0)
this.refreshInterval = setInterval(() => this._onRefresh(), this.refreshTime)
}
onScreenBlur() {
clearInterval(this.refreshInterval);
}
componentWillUnmount() {
this.willBlurSubscription.remove();
this.willFocusSubscription.remove();
}
_onRefresh = () => { _onRefresh = () => {
console.log('refresh');
this.setState({refreshing: true}); this.setState({refreshing: true});
this.webDataManager.readData().then((fetchedData) => { this.webDataManager.readData().then((fetchedData) => {
this.setState({ this.setState({

View file

@ -27,7 +27,7 @@ function openWebLink(link) {
export default class HomeScreen extends FetchedDataSectionList { export default class HomeScreen extends FetchedDataSectionList {
constructor() { constructor() {
super(DATA_URL); super(DATA_URL, 0);
} }
getHeaderTranslation() { getHeaderTranslation() {

View file

@ -24,7 +24,7 @@ const typesIcons = {
export default class ProximoMainScreen extends FetchedDataSectionList { export default class ProximoMainScreen extends FetchedDataSectionList {
constructor() { constructor() {
super(DATA_URL); super(DATA_URL, 0);
} }
getHeaderTranslation() { getHeaderTranslation() {

View file

@ -35,11 +35,13 @@ let stateColors = {};
*/ */
export default class ProxiwashScreen extends FetchedDataSectionList { export default class ProxiwashScreen extends FetchedDataSectionList {
refreshInterval : IntervalID;
/** /**
* Creates machine state parameters using current theme and translations * Creates machine state parameters using current theme and translations
*/ */
constructor() { constructor() {
super(DATA_URL); super(DATA_URL, 1000 * 60); // Refresh every minute
let colors = ThemeManager.getCurrentThemeVariables(); let colors = ThemeManager.getCurrentThemeVariables();
stateColors[MACHINE_STATES.TERMINE] = colors.proxiwashFinishedColor; stateColors[MACHINE_STATES.TERMINE] = colors.proxiwashFinishedColor;
stateColors[MACHINE_STATES.DISPONIBLE] = colors.proxiwashReadyColor; stateColors[MACHINE_STATES.DISPONIBLE] = colors.proxiwashReadyColor;