// @flow
import * as React from 'react';
import {Image, Linking, TouchableOpacity, View} from 'react-native';
import {Body, Button, Card, CardItem, Left, Text, Thumbnail, H1, H3} from 'native-base';
import i18n from "i18n-js";
import CustomMaterialIcon from '../components/CustomMaterialIcon';
import FetchedDataSectionList from "../components/FetchedDataSectionList";
import Autolink from 'react-native-autolink';
import ThemeManager from "../utils/ThemeManager";
import PlatformTouchable from "react-native-platform-touchable";
import HTML from 'react-native-render-html';
import {LinearGradient} from 'expo-linear-gradient';
const ICON_AMICALE = require('../assets/amicale.png');
const NAME_AMICALE = 'Amicale INSA Toulouse';
const DATA_URL = "https://srv-falcon.etud.insa-toulouse.fr/~amicale_app/dashboard/dashboard_data.json";
const SECTIONS_ID = [
'dashboard',
'news_feed'
];
const REFRESH_TIME = 1000 * 20; // Refresh every 20 seconds
/**
* 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));
}
/**
* Class defining the app's home screen
*/
export default class HomeScreen extends FetchedDataSectionList {
constructor() {
super(DATA_URL, REFRESH_TIME);
}
getHeaderTranslation() {
return i18n.t("screens.home");
}
getUpdateToastTranslations() {
return [i18n.t("homeScreen.listUpdated"), i18n.t("homeScreen.listUpdateFail")];
}
getKeyExtractor(item: Object) {
return item !== undefined ? item.id : undefined;
}
createDataset(fetchedData: Object) {
// fetchedData = DATA;
let newsData = [];
let dashboardData = [];
if (fetchedData['news_feed'] !== undefined)
newsData = fetchedData['news_feed']['data'];
if (fetchedData['dashboard'] !== undefined)
dashboardData = this.generateDashboardDataset(fetchedData['dashboard']);
return [
{
title: '',
data: dashboardData,
extraData: super.state,
keyExtractor: this.getKeyExtractor,
id: SECTIONS_ID[0]
},
{
title: i18n.t('homeScreen.newsFeed'),
data: newsData,
extraData: super.state,
keyExtractor: this.getKeyExtractor,
id: SECTIONS_ID[1]
}
];
}
generateDashboardDataset(dashboardData: Object) {
let dataset = [
{
id: 'top',
content: undefined
},
{
id: 'middle',
content: [{}, {}]
},
{
id: 'bottom',
content: undefined
},
];
for (let [key, value] of Object.entries(dashboardData)) {
switch (key) {
case 'today_events':
dataset[0]['content'] = value;
break;
case 'proximo_articles':
dataset[1]['content'][0] = {id: key, data: value};
break;
case 'today_menu':
dataset[1]['content'][1] = {id: key, data: value};
break;
case 'available_machines':
dataset[2]['content'] = value;
break;
}
}
return dataset
}
/**
* 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();
}
getRenderSectionHeader(title: string) {
if (title === '') {
return ;
} else {
return (
{title}
);
}
}
getDashboardItem(item: Object) {
let content = item['content'];
if (item['id'] === 'top')
return this.getDashboardTopItem(content);
else if (item['id'] === 'middle')
return this.getDashboardMiddleItem(content);
else
return this.getDashboardBottomItem(content);
}
/**
* Convert the date string given by in the event list json to a date object
* @param dateString
* @return {Date}
*/
stringToDate(dateString: ?string): ?Date {
let date = new Date();
if (dateString === undefined || dateString === null)
date = undefined;
else if (dateString.split(' ').length > 1) {
let timeStr = dateString.split(' ')[1];
date.setHours(parseInt(timeStr.split(':')[0]), parseInt(timeStr.split(':')[1]), 0);
} else
date = undefined;
return date;
}
/**
* Get the time limit depending on the current day:
* 17:30 for every day of the week except for thursday 11:30
* 00:00 on weekends
*/
getTodayEventTimeLimit() {
let now = new Date();
if (now.getDay() === 4) // Thursday
now.setHours(11, 30, 0);
else if (now.getDay() === 6 || now.getDay() === 0) // Weekend
now.setHours(0, 0, 0);
else
now.setHours(17, 30, 0);
return now;
}
/**
* Get the duration (in milliseconds) of an event
* @param event {Object}
* @return {number} The number of milliseconds
*/
getEventDuration(event: Object): number {
let start = this.stringToDate(event['date_begin']);
let end = this.stringToDate(event['date_end']);
let duration = 0;
if (start !== undefined && start !== null && end !== undefined && end !== null)
duration = end - start;
return duration;
}
/**
* Get events starting after the limit
*
* @param events
* @param limit
* @return {Array