From ae6ccd7f53179f1673c464193140d817bb30fdce Mon Sep 17 00:00:00 2001 From: Mekan1206 Date: Sat, 20 Sep 2025 13:39:40 +0500 Subject: [PATCH] Implement activity fetching and display in HomeScreen - Added useEffect to fetch today's activities from the getPrograms function. - Introduced logic to determine the next activity based on current time. - Updated the HomeScreen to conditionally render the FeatureCard with today's activity details. --- app/(tabs)/home.tsx | 52 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/app/(tabs)/home.tsx b/app/(tabs)/home.tsx index 688976e..cc1d91d 100644 --- a/app/(tabs)/home.tsx +++ b/app/(tabs)/home.tsx @@ -7,12 +7,46 @@ import i18n from '@/i18n'; import * as Updates from 'expo-updates'; import AsyncStorage from '@react-native-async-storage/async-storage'; import Colors from '@/constants/Colors'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; - +import { getPrograms, ProgramActivity } from '@/utils/programs'; export default function HomeScreen() { const insets = useSafeAreaInsets(); + const [todaysActivity, setTodaysActivity] = useState(null); + + useEffect(() => { + const fetchAndSetActivities = async () => { + try { + const { activities } = await getPrograms(); + const now = new Date(); + + const formatDate = (date: Date) => { + const day = String(date.getDate()).padStart(2, '0'); + const month = String(date.getMonth() + 1).padStart(2, '0'); + const year = date.getFullYear(); + return `${day}.${month}.${year}`; + }; + + const todayStr = formatDate(now); + const todaysActivities = activities[todayStr] || []; + + const nextActivity = todaysActivities.find(activity => { + if (!activity.time) return false; + const [hours, minutes] = activity.time.split(':').map(Number); + const activityTime = new Date(now); + activityTime.setHours(hours, minutes, 0, 0); + return activityTime > now; + }); + + setTodaysActivity(nextActivity || todaysActivities[0] || null); + } catch (error) { + console.error('Failed to load activities for home screen:', error); + } + }; + + fetchAndSetActivities(); + }, []); const changeLanguage = async (lang: 'en' | 'tk' | 'ru') => { if (!lang) return; @@ -45,12 +79,14 @@ export default function HomeScreen() { - {/* */} + {todaysActivity && ( + + )}