@@ -10,10 +10,10 @@ import { StatusBar, View } from 'react-native'
1010import { ScrollView } from 'react-native-gesture-handler'
1111import { useDerivedValue , useSharedValue , withTiming } from 'react-native-reanimated'
1212import { useSafeAreaInsets } from 'react-native-safe-area-context'
13- import { getWeather } from '../api'
14- import type { Weather } from '../types'
13+ import { getAQI , getWeather } from '../api'
14+ import type { AQI , Weather } from '../types'
1515import { calculatePressurePercentage , getAQIStatus , getRainStatus , getVisibilityStatusString } from '../utils'
16- import AQI from './components/AQI '
16+ import AirQualityIndex from './components/AirQualityIndex '
1717import Cloudiness from './components/Cloudiness'
1818import DailyForecast from './components/DailyForecast'
1919import FeelsLike , { getFeelsLikeStatusString } from './components/FeelsLike'
@@ -53,36 +53,39 @@ export default function WeatherScreen({ navigation }: NavProp) {
5353 // eslint-disable-next-line react-hooks/exhaustive-deps
5454 } , [ currentWeather ?. current . weather [ 0 ] ! . icon ] )
5555
56- const { isPending, data, mutate } = useMutation ( {
56+ const { isPending, error , data, mutate } = useMutation ( {
5757 mutationKey : [ 'currentWeather' ] ,
5858 mutationFn : ( ) => fetchResult ( ) ,
5959 onError : ( err ) => console . log ( err ) ,
60- onSuccess : setCurrentWeather ,
60+ onSuccess : ( d ) => {
61+ setCurrentWeather ( d )
62+ setLastUpdated ( new Date ( ) . getTime ( ) )
63+ } ,
6164 } )
6265 const w = data || currentWeather
6366
67+ useEffect ( ( ) => {
68+ console . log ( currentCity )
69+ } , [ currentCity ] )
70+
6471 useEffect ( ( ) => {
6572 if ( currentCity ) mutate ( )
6673 // eslint-disable-next-line react-hooks/exhaustive-deps
6774 } , [ currentCity ] )
6875
69- const bottom = useSafeAreaInsets ( ) . bottom
70- const top = useSafeAreaInsets ( ) . top
71- const height = H + bottom + top
72- const width = W
73-
7476 const fetchResult = useCallback ( async ( ) : Promise < Weather > => {
7577 const now = new Date ( ) . getTime ( )
7678 if ( now - lastUpdated > weatherCacheTime ) {
77- setLastUpdated ( now )
78- console . log ( 'fetching weather' )
7979 return ( await getWeather ( currentCity ?. lat || 0 , currentCity ?. lon || 0 ) ) as Weather
8080 }
81- console . log ( 'using cache' )
8281 return currentWeather
83- // eslint-disable-next-line react-hooks/exhaustive-deps
8482 } , [ currentWeather , lastUpdated , weatherCacheTime , currentCity ] )
8583
84+ const bottom = useSafeAreaInsets ( ) . bottom
85+ const top = useSafeAreaInsets ( ) . top
86+ const height = H + bottom + top
87+ const width = W
88+
8689 return (
8790 < >
8891 < StatusBar backgroundColor = 'transparent' barStyle = { 'light-content' } />
@@ -107,12 +110,45 @@ export default function WeatherScreen({ navigation }: NavProp) {
107110}
108111
109112function Boxes ( { w, theme } : { w : Weather ; theme : Theme } ) {
113+ const { currentCity, lastUpdatedAQI, currentAQI, setLastUpdatedAQI, weatherCacheTime, setCurrentAQI } = weatherStore (
114+ ( state ) => ( {
115+ currentCity : state . currentCity ,
116+ currentUnit : state . temperatureUnit ,
117+ lastUpdatedAQI : state . lastUpdatedAQI ,
118+ currentAQI : state . currentAQI ,
119+ setCurrentWeather : state . setCurrentWeather ,
120+ setLastUpdatedAQI : state . setLastUpdatedAQI ,
121+ weatherCacheTime : state . weatherCacheTime ,
122+ setCurrentAQI : state . setCurrentAQI ,
123+ } ) ,
124+ )
125+
126+ const { mutate } = useMutation ( {
127+ mutationKey : [ 'currentWeatherAQI' ] ,
128+ mutationFn : ( ) => fetchResult ( ) ,
129+ onError : ( err ) => console . log ( err ) ,
130+ onSuccess : ( d : AQI ) => {
131+ setCurrentAQI ( d )
132+ } ,
133+ } )
134+ const aqiStatus = useMemo ( ( ) => getAQIStatus ( currentAQI ?. list ?. [ 0 ] ?. main ?. aqi || 0 ) , [ currentAQI ] )
110135 const pressurePercent = useMemo ( ( ) => calculatePressurePercentage ( w ) , [ w ] )
111136 const feelsLikeStatus = useMemo ( ( ) => getFeelsLikeStatusString ( w ?. current . feels_like || 0 , w ?. current . temp || 0 ) , [ w ] )
112137 const visibilityStatus = useMemo ( ( ) => getVisibilityStatusString ( w ?. current . visibility || 10000 ) , [ w ] )
113- const aqiStatus = useMemo ( ( ) => getAQIStatus ( 140 ) , [ ] )
114138 const rainStatus = useMemo ( ( ) => getRainStatus ( w ?. daily [ 0 ] ?. rain ) , [ w ] )
115139
140+ const fetchResult = useCallback ( async ( ) : Promise < AQI > => {
141+ const now = new Date ( ) . getTime ( )
142+ if ( now - lastUpdatedAQI > weatherCacheTime ) {
143+ setLastUpdatedAQI ( now )
144+ return ( await getAQI ( currentCity ?. lat || 0 , currentCity ?. lon || 0 ) ) as AQI
145+ }
146+ return currentAQI
147+ } , [ lastUpdatedAQI , weatherCacheTime , currentAQI , setLastUpdatedAQI , currentCity ?. lat , currentCity ?. lon ] )
148+
149+ // eslint-disable-next-line react-hooks/exhaustive-deps
150+ useEffect ( ( ) => mutate , [ currentCity ] )
151+
116152 return (
117153 < View style = { { flexDirection : 'row' , justifyContent : 'space-between' , rowGap : 12 } } className = 'flex-wrap px-4' >
118154 < FeelsLike theme = { theme } feelsLike = { w ?. current . feels_like || 0 } feelsLikeStatus = { feelsLikeStatus } />
@@ -125,7 +161,7 @@ function Boxes({ w, theme }: { w: Weather; theme: Theme }) {
125161 visibilityStatus = { visibilityStatus }
126162 />
127163 < Wind theme = { theme } w = { w } />
128- < AQI aqi = { 140 } theme = { theme } aqiStatus = { aqiStatus } />
164+ < AirQualityIndex aqi = { currentAQI ?. list ?. [ 0 ] ?. main ?. aqi } theme = { theme } aqiStatus = { aqiStatus } />
129165 < Cloudiness theme = { theme } clouds = { w ?. current . clouds || 0 } />
130166 < Precipitation theme = { theme } rain = { w ?. daily [ 0 ] ?. rain } snow = { w ?. daily [ 0 ] ?. snow } status = { rainStatus } />
131167 < SunRiseSet w = { w } theme = { theme } now = { w ?. current ?. dt } sunrise = { w ?. current ?. sunrise } sunset = { w ?. current ?. sunset } />
0 commit comments