2019-06-29 13:37:21 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2019-07-30 13:59:28 +02:00
|
|
|
import {Image, Linking, TouchableOpacity, View} from 'react-native';
|
2019-10-06 10:54:01 +02:00
|
|
|
import {Body, Button, Card, CardItem, Left, Right, Text, Thumbnail, H1, H3} from 'native-base';
|
2019-06-25 22:20:24 +02:00
|
|
|
import i18n from "i18n-js";
|
2019-07-25 17:52:16 +02:00
|
|
|
import CustomMaterialIcon from '../components/CustomMaterialIcon';
|
2019-07-26 14:14:01 +02:00
|
|
|
import FetchedDataSectionList from "../components/FetchedDataSectionList";
|
2019-07-30 13:59:28 +02:00
|
|
|
import Autolink from 'react-native-autolink';
|
2019-08-05 14:52:18 +02:00
|
|
|
import ThemeManager from "../utils/ThemeManager";
|
2019-10-06 10:54:01 +02:00
|
|
|
import PlatformTouchable from "react-native-platform-touchable";
|
2019-07-25 17:52:16 +02:00
|
|
|
|
|
|
|
const ICON_AMICALE = require('../assets/amicale.png');
|
2019-07-26 14:14:01 +02:00
|
|
|
const NAME_AMICALE = 'Amicale INSA Toulouse';
|
2019-10-06 10:54:01 +02:00
|
|
|
const DATA_URL = "https://srv-falcon.etud.insa-toulouse.fr/~amicale_app/dashboard/dashboard_data.json";
|
|
|
|
|
|
|
|
const SECTIONS_ID = [
|
|
|
|
'dashboard',
|
|
|
|
'news_feed'
|
|
|
|
];
|
2019-07-28 12:40:35 +02:00
|
|
|
|
2019-07-25 17:52:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a link in the device's browser
|
|
|
|
* @param link The link to open
|
|
|
|
*/
|
|
|
|
function openWebLink(link) {
|
|
|
|
Linking.openURL(link).catch((err) => console.error('Error opening link', err));
|
|
|
|
}
|
|
|
|
|
2019-06-29 15:43:57 +02:00
|
|
|
/**
|
|
|
|
* Class defining the app's home screen
|
|
|
|
*/
|
2019-07-26 14:14:01 +02:00
|
|
|
export default class HomeScreen extends FetchedDataSectionList {
|
2019-07-25 17:52:16 +02:00
|
|
|
|
2019-07-30 20:40:17 +02:00
|
|
|
constructor() {
|
2019-07-31 19:11:30 +02:00
|
|
|
super(DATA_URL, 0);
|
2019-07-30 20:40:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-26 14:14:01 +02:00
|
|
|
getHeaderTranslation() {
|
|
|
|
return i18n.t("screens.home");
|
|
|
|
}
|
2019-07-25 17:52:16 +02:00
|
|
|
|
2019-07-31 14:34:38 +02:00
|
|
|
getUpdateToastTranslations() {
|
|
|
|
return [i18n.t("homeScreen.listUpdated"), i18n.t("homeScreen.listUpdateFail")];
|
2019-07-25 17:52:16 +02:00
|
|
|
}
|
|
|
|
|
2019-07-30 13:59:28 +02:00
|
|
|
getKeyExtractor(item: Object) {
|
2019-07-28 12:40:35 +02:00
|
|
|
return item !== undefined ? item.id : undefined;
|
2019-07-25 17:52:16 +02:00
|
|
|
}
|
|
|
|
|
2019-07-30 13:59:28 +02:00
|
|
|
createDataset(fetchedData: Object) {
|
2019-10-06 10:54:01 +02:00
|
|
|
let newsData = [];
|
|
|
|
let dashboardData = [];
|
|
|
|
if (fetchedData['news_feed'] !== undefined)
|
|
|
|
newsData = fetchedData['news_feed']['data'];
|
|
|
|
if (fetchedData['dashboard'] !== undefined)
|
|
|
|
dashboardData = this.generateDashboardDataset(fetchedData['dashboard']);
|
2019-07-26 14:14:01 +02:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
title: '',
|
2019-10-06 10:54:01 +02:00
|
|
|
data: dashboardData,
|
|
|
|
extraData: super.state,
|
|
|
|
keyExtractor: this.getKeyExtractor,
|
|
|
|
id: SECTIONS_ID[0]
|
|
|
|
},
|
|
|
|
{
|
2019-10-06 12:30:29 +02:00
|
|
|
title: i18n.t('homeScreen.newsFeed'),
|
2019-10-06 10:54:01 +02:00
|
|
|
data: newsData,
|
2019-07-31 14:22:36 +02:00
|
|
|
extraData: super.state,
|
2019-10-06 10:54:01 +02:00
|
|
|
keyExtractor: this.getKeyExtractor,
|
|
|
|
id: SECTIONS_ID[1]
|
2019-07-25 17:52:16 +02:00
|
|
|
}
|
2019-07-26 14:14:01 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-10-06 10:54:01 +02:00
|
|
|
generateDashboardDataset(dashboardData: Object) {
|
|
|
|
let dataset = [];
|
|
|
|
for (let [key, value] of Object.entries(dashboardData)) {
|
|
|
|
dataset.push(
|
|
|
|
{
|
|
|
|
id: key,
|
|
|
|
data: value
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return dataset
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-26 14:14:01 +02:00
|
|
|
/**
|
|
|
|
* Converts a dateString using Unix Timestamp to a formatted date
|
|
|
|
* @param dateString {string} The Unix Timestamp representation of a date
|
|
|
|
* @return {string} The formatted output date
|
|
|
|
*/
|
|
|
|
static getFormattedDate(dateString: string) {
|
|
|
|
let date = new Date(Number.parseInt(dateString) * 1000);
|
|
|
|
return date.toLocaleString();
|
|
|
|
}
|
2019-07-25 17:52:16 +02:00
|
|
|
|
2019-10-06 10:54:01 +02:00
|
|
|
getRenderSectionHeader(title: string) {
|
|
|
|
if (title === '') {
|
|
|
|
return <View/>;
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<View style={{
|
|
|
|
backgroundColor: ThemeManager.getCurrentThemeVariables().containerBgColor
|
|
|
|
}}>
|
|
|
|
<H1 style={{
|
|
|
|
marginLeft: 'auto',
|
|
|
|
marginRight: 'auto',
|
|
|
|
marginTop: 10,
|
|
|
|
marginBottom: 10
|
|
|
|
}}>{title}</H1>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getDashboardItemData(item: Object) {
|
|
|
|
let icon = '';
|
|
|
|
let title = '';
|
2019-10-06 12:30:29 +02:00
|
|
|
let subtitle;
|
2019-10-06 10:54:01 +02:00
|
|
|
let clickAction;
|
2019-10-06 12:30:29 +02:00
|
|
|
let isAvailable = false;
|
|
|
|
let color = ThemeManager.getCurrentThemeVariables().disabledTextColor;
|
2019-10-06 10:54:01 +02:00
|
|
|
switch (item['id']) {
|
|
|
|
case 'today_events':
|
|
|
|
icon = 'calendar-range';
|
2019-10-06 12:30:29 +02:00
|
|
|
color = ThemeManager.getCurrentThemeVariables().planningColor;
|
|
|
|
title = i18n.t('homeScreen.dashboard.todayEventsTitle');
|
|
|
|
isAvailable = item['data'].length > 0;
|
|
|
|
if (isAvailable) {
|
|
|
|
subtitle =
|
|
|
|
<Text>
|
|
|
|
<Text style={{fontWeight: "bold"}}>{item['data'].length}</Text>
|
|
|
|
<Text>{i18n.t('homeScreen.dashboard.todayEventsSubtitle')}</Text>
|
|
|
|
</Text>;
|
|
|
|
} else
|
|
|
|
subtitle = i18n.t('homeScreen.dashboard.todayEventsSubtitleNA');
|
2019-10-06 10:54:01 +02:00
|
|
|
clickAction = () => this.props.navigation.navigate('Planning');
|
|
|
|
break;
|
|
|
|
case 'proximo_articles':
|
|
|
|
icon = 'shopping';
|
2019-10-06 12:30:29 +02:00
|
|
|
color = ThemeManager.getCurrentThemeVariables().proximoColor;
|
|
|
|
title = i18n.t('homeScreen.dashboard.proximoTitle');
|
|
|
|
isAvailable = parseInt(item['data']) > 0;
|
|
|
|
if (isAvailable) {
|
|
|
|
subtitle =
|
|
|
|
<Text>
|
|
|
|
<Text style={{fontWeight: "bold"}}>{item['data']}</Text>
|
|
|
|
<Text>{i18n.t('homeScreen.dashboard.proximoSubtitle')}</Text>
|
|
|
|
</Text>;
|
|
|
|
} else
|
|
|
|
subtitle = i18n.t('homeScreen.dashboard.proximoSubtitleNA');
|
2019-10-06 10:54:01 +02:00
|
|
|
clickAction = () => this.props.navigation.navigate('Proximo');
|
|
|
|
break;
|
|
|
|
case 'available_machines':
|
|
|
|
icon = 'washing-machine';
|
2019-10-06 12:30:29 +02:00
|
|
|
color = ThemeManager.getCurrentThemeVariables().proxiwashColor;
|
|
|
|
title = i18n.t('homeScreen.dashboard.proxiwashTitle');
|
|
|
|
isAvailable = parseInt(item['data']['dryers']) > 0 || parseInt(item['data']['washers']) > 0;
|
|
|
|
if (isAvailable) {
|
|
|
|
subtitle =
|
|
|
|
<Text>
|
|
|
|
<Text style={{
|
|
|
|
fontWeight: parseInt(item['data']['dryers']) > 0 ?
|
|
|
|
'bold' :
|
|
|
|
'normal',
|
|
|
|
color: parseInt(item['data']['dryers']) > 0 ?
|
|
|
|
ThemeManager.getCurrentThemeVariables().textColor :
|
|
|
|
ThemeManager.getCurrentThemeVariables().listNoteColor
|
|
|
|
}}>
|
|
|
|
{item['data']['dryers']}
|
|
|
|
</Text>
|
|
|
|
<Text>{i18n.t('homeScreen.dashboard.proxiwashSubtitle1')}</Text>
|
|
|
|
<Text style={{
|
|
|
|
fontWeight: parseInt(item['data']['washers']) > 0 ?
|
|
|
|
'bold' :
|
|
|
|
'normal',
|
|
|
|
color: parseInt(item['data']['washers']) > 0 ?
|
|
|
|
ThemeManager.getCurrentThemeVariables().textColor :
|
|
|
|
ThemeManager.getCurrentThemeVariables().listNoteColor
|
|
|
|
}}>
|
|
|
|
{item['data']['washers']}
|
|
|
|
</Text>
|
|
|
|
<Text>{i18n.t('homeScreen.dashboard.proxiwashSubtitle2')}</Text>
|
|
|
|
</Text>;
|
|
|
|
} else
|
|
|
|
subtitle = i18n.t('homeScreen.dashboard.proxiwashSubtitleNA');
|
2019-10-06 10:54:01 +02:00
|
|
|
clickAction = () => this.props.navigation.navigate('Proxiwash');
|
|
|
|
break;
|
|
|
|
case 'today_menu':
|
|
|
|
icon = 'silverware-fork-knife';
|
2019-10-06 12:30:29 +02:00
|
|
|
color = ThemeManager.getCurrentThemeVariables().menuColor;
|
|
|
|
title = i18n.t('homeScreen.dashboard.menuTitle');
|
|
|
|
isAvailable = item['data'].length > 0;
|
|
|
|
if (isAvailable) {
|
|
|
|
subtitle = i18n.t('homeScreen.dashboard.menuSubtitle');
|
|
|
|
} else
|
|
|
|
subtitle = i18n.t('homeScreen.dashboard.menuSubtitleNA');
|
2019-10-06 10:54:01 +02:00
|
|
|
clickAction = () => this.props.navigation.navigate('SelfMenuScreen');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
icon: icon,
|
2019-10-06 12:30:29 +02:00
|
|
|
color: color,
|
2019-10-06 10:54:01 +02:00
|
|
|
title: title,
|
|
|
|
subtitle: subtitle,
|
2019-10-06 12:30:29 +02:00
|
|
|
clickAction: clickAction,
|
|
|
|
isAvailable: isAvailable
|
2019-10-06 10:54:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-30 13:59:28 +02:00
|
|
|
getRenderItem(item: Object, section: Object, data: Object) {
|
2019-10-06 10:54:01 +02:00
|
|
|
if (section['id'] === SECTIONS_ID[0]) {
|
|
|
|
let itemData = this.getDashboardItemData(item);
|
|
|
|
return (
|
|
|
|
<Card style={{
|
|
|
|
flex: 0,
|
|
|
|
marginLeft: 10,
|
2019-10-06 12:30:29 +02:00
|
|
|
marginRight: 10,
|
|
|
|
borderRadius: 50,
|
|
|
|
backgroundColor: ThemeManager.getCurrentThemeVariables().cardDefaultBg
|
2019-10-06 10:54:01 +02:00
|
|
|
}}>
|
|
|
|
<PlatformTouchable
|
|
|
|
onPress={itemData['clickAction']}
|
2019-10-06 12:30:29 +02:00
|
|
|
style={{
|
|
|
|
zIndex: 100,
|
|
|
|
borderRadius: 50
|
|
|
|
}}
|
2019-10-06 10:54:01 +02:00
|
|
|
>
|
2019-10-06 12:30:29 +02:00
|
|
|
<CardItem style={{
|
|
|
|
borderRadius: 50,
|
|
|
|
backgroundColor: 'transparent'
|
|
|
|
}}>
|
2019-10-06 10:54:01 +02:00
|
|
|
<Left>
|
|
|
|
<CustomMaterialIcon
|
|
|
|
icon={itemData['icon']}
|
2019-10-06 12:30:29 +02:00
|
|
|
color={
|
|
|
|
itemData['isAvailable'] ?
|
|
|
|
itemData['color'] :
|
|
|
|
ThemeManager.getCurrentThemeVariables().textDisabledColor
|
|
|
|
}
|
2019-10-06 10:54:01 +02:00
|
|
|
fontSize={40}
|
|
|
|
width={40}/>
|
|
|
|
<Body>
|
2019-10-06 12:30:29 +02:00
|
|
|
<H3 style={{
|
|
|
|
color: itemData['isAvailable'] ?
|
|
|
|
ThemeManager.getCurrentThemeVariables().textColor :
|
|
|
|
ThemeManager.getCurrentThemeVariables().listNoteColor
|
|
|
|
}}>
|
|
|
|
{itemData['title']}
|
|
|
|
</H3>
|
|
|
|
<Text style={{
|
|
|
|
color: itemData['isAvailable'] ?
|
|
|
|
ThemeManager.getCurrentThemeVariables().listNoteColor :
|
|
|
|
ThemeManager.getCurrentThemeVariables().textDisabledColor
|
|
|
|
}}>
|
|
|
|
{itemData['subtitle']}
|
|
|
|
</Text>
|
2019-10-06 10:54:01 +02:00
|
|
|
</Body>
|
|
|
|
</Left>
|
|
|
|
</CardItem>
|
|
|
|
</PlatformTouchable>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Card style={{
|
|
|
|
flex: 0,
|
|
|
|
marginLeft: 10,
|
|
|
|
marginRight: 10
|
|
|
|
}}>
|
|
|
|
<CardItem>
|
|
|
|
<Left>
|
|
|
|
<Thumbnail source={ICON_AMICALE} square/>
|
|
|
|
<Body>
|
|
|
|
<Text>{NAME_AMICALE}</Text>
|
|
|
|
<Text note>{HomeScreen.getFormattedDate(item.created_time)}</Text>
|
|
|
|
</Body>
|
|
|
|
</Left>
|
|
|
|
</CardItem>
|
|
|
|
<CardItem>
|
2019-07-25 17:52:16 +02:00
|
|
|
<Body>
|
2019-10-06 10:54:01 +02:00
|
|
|
{item.full_picture !== '' && item.full_picture !== undefined ?
|
|
|
|
<TouchableOpacity onPress={() => openWebLink(item.full_picture)}
|
|
|
|
style={{width: '100%', height: 250}}>
|
|
|
|
<Image source={{uri: item.full_picture}}
|
|
|
|
style={{flex: 1, resizeMode: "contain"}}
|
|
|
|
resizeMode="contain"
|
|
|
|
/>
|
|
|
|
</TouchableOpacity>
|
|
|
|
: <View/>}
|
|
|
|
{item.message !== undefined ?
|
|
|
|
<Autolink
|
|
|
|
text={item.message}
|
|
|
|
hashtag="facebook"
|
|
|
|
style={{color: ThemeManager.getCurrentThemeVariables().textColor}}
|
|
|
|
/> : <View/>
|
|
|
|
}
|
2019-07-25 17:52:16 +02:00
|
|
|
</Body>
|
2019-10-06 10:54:01 +02:00
|
|
|
</CardItem>
|
|
|
|
<CardItem>
|
|
|
|
<Left>
|
|
|
|
<Button transparent
|
|
|
|
onPress={() => openWebLink(item.permalink_url)}>
|
|
|
|
<CustomMaterialIcon
|
|
|
|
icon="facebook"
|
|
|
|
color="#57aeff"
|
|
|
|
width={20}/>
|
|
|
|
<Text>En savoir plus</Text>
|
|
|
|
</Button>
|
|
|
|
</Left>
|
|
|
|
</CardItem>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
2019-07-25 17:52:16 +02:00
|
|
|
}
|
|
|
|
|
2019-06-25 22:20:24 +02:00
|
|
|
}
|