import React, { useState, useContext, useEffect } from "react"; import Screen from "../components/Screen"; import { StyleSheet, View, Text, TouchableNativeFeedback } from "react-native"; import { dimensions, screen_width } from "../config/dimensions"; import colors from "../config/colors"; import getWeatherForecast from "../api/weather_forecast"; import assets from "../config/assets"; import { KeyboardAwareScrollView } from "react-native-keyboard-aware-scroll-view"; import Swipeable from "react-native-gesture-handler/Swipeable"; import HeaderBarMenu from "../components/HeaderBarMenu"; function forecastDay(day, setDay) { return ( setDay(0)}> HOJE setDay(1)}> PRÓXIMO ); } function weatherIndexToString(weather_index) { return [ "Sol", "Sol entre nuvens", "Chuva", "Chuva forte", "Nuvens", "Céu limpo", ][weather_index]; } function WeatherInput(weather_index, weather_title) { const SvgImage = assets.weather_icons[weather_index]; return ( {weather_title} {weatherIndexToString(weather_index)} ); } function renderTodayForecast(forecast) { return ( {WeatherInput(forecast.today_forecast.morning_weather_index, "Manhã")} Probabilidade de chuva {forecast.today_forecast.rain_probability}% {WeatherInput(forecast.today_forecast.evening_weather_index, "Tarde")} {WeatherInput(forecast.today_forecast.night_weather_index, "Noite")} ); } function border() { return ( ); } function weekDayList(day_forecast) { const SvgImage = assets.weather_icons[day_forecast.weather_index]; return ( {border()} {day_forecast.week_day} ({day_forecast.date}) {day_forecast.rain_fall_mm} mm ); } function renderWeekForecast(forecast) { return ( {forecast.next_forecast.map(weekDayList)} {border()} ); } function currentLocationAndDate(forecast, day) { return ( {forecast.city}, {forecast.state} {day == 0 ? forecast.today_forecast.date : "Próximos dias"} ); } function renderScreen(forecast) { const [day, setDay] = useState(0); const [swipePosition, setSwipePosition] = useState(null); useEffect(() => { if (swipePosition) { day == 0 ? swipePosition.openLeft() : swipePosition.openRight(); } }, [day]); return ( {forecastDay(day, setDay)} {currentLocationAndDate(forecast, day)} setSwipePosition(ref)} renderRightActions={() => renderWeekForecast(forecast)} onSwipeableOpen={() => { setDay(1); }} onSwipeableClose={() => { setDay(0); }} > {renderTodayForecast(forecast)} ); } function renderErrorScreen() { return ( Ops, algo deu errado... Não conseguimos encontrar a previsão do tempo para sua localização ); } function ForecastScreen(props) { HeaderBarMenu(props.navigation); const forecast = getWeatherForecast(); return ( {forecast ? renderScreen(forecast) : renderErrorScreen()} ); } const styles = StyleSheet.create({ container: { padding: dimensions.spacing.normal_padding, }, centered: { backgroundColor: "white", flexDirection: "column", alignItems: "center", }, forecastDays: { flexDirection: "row", justifyContent: "space-between", alignContent: "center", }, forecastDayBtn: { borderBottomWidth: 3, width: "48%", height: 30, justifyContent: "center", alignItems: "center", }, }); export default ForecastScreen;