import { Image } from 'expo-image';
import { Pressable, StyleSheet, View } from 'react-native';
import { useCallback, useState } from 'react';
import { Link, useFocusEffect } from 'expo-router';
import ParallaxScrollView from '@/components/parallax-scroll-view';
import { ThemedText } from '@/components/themed-text';
import { ThemedView } from '@/components/themed-view';
import { ThemedButton } from '@/components/themed-button';
import { IconSymbol } from '@/components/ui/icon-symbol';
import { Colors, Fonts } from '@/constants/theme';
import { useTranslation } from '@/localization/i18n';
import { dbPromise, initCoreTables } from '@/services/db';
import { useColorScheme } from '@/hooks/use-color-scheme';
export default function HomeScreen() {
const { t } = useTranslation();
const theme = useColorScheme() ?? 'light';
const palette = Colors[theme];
const iconColor = palette.tint;
const [counts, setCounts] = useState({
fields: 0,
crops: 0,
observations: 0,
tasks: 0,
history: 0,
harvests: 0,
sales: 0,
costs: 0,
});
useFocusEffect(
useCallback(() => {
let isActive = true;
async function loadCounts() {
try {
await initCoreTables();
const db = await dbPromise;
const fieldCount = await db.getFirstAsync<{ count: number }>(
'SELECT COUNT(*) as count FROM fields;'
);
const cropCount = await db.getFirstAsync<{ count: number }>(
'SELECT COUNT(*) as count FROM crops;'
);
const observationCount = await db.getFirstAsync<{ count: number }>(
'SELECT COUNT(*) as count FROM observations;'
);
const taskCount = await db.getFirstAsync<{ count: number }>(
'SELECT COUNT(*) as count FROM daily_tasks WHERE is_active = 1;'
);
const historyCount = await db.getFirstAsync<{ count: number }>(
'SELECT COUNT(*) as count FROM daily_task_entries;'
);
const harvestCount = await db.getFirstAsync<{ count: number }>(
'SELECT COUNT(*) as count FROM harvests;'
);
const salesCount = await db.getFirstAsync<{ count: number }>(
'SELECT COUNT(*) as count FROM sales;'
);
const costCount = await db.getFirstAsync<{ count: number }>(
'SELECT COUNT(*) as count FROM costs;'
);
if (!isActive) return;
setCounts({
fields: fieldCount?.count ?? 0,
crops: cropCount?.count ?? 0,
observations: observationCount?.count ?? 0,
tasks: taskCount?.count ?? 0,
history: historyCount?.count ?? 0,
harvests: harvestCount?.count ?? 0,
sales: salesCount?.count ?? 0,
costs: costCount?.count ?? 0,
});
} catch {
if (!isActive) return;
setCounts((prev) => prev);
}
}
loadCounts();
return () => {
isActive = false;
};
}, [])
);
return (
}>
{t('home.badge')}
{t('home.title')}
{t('home.subtitle')}
{t('home.quickActions')}
{t('home.fields')}
{counts.fields}
{t('home.fieldsHint')}
{t('home.crops')}
{counts.crops}
{t('home.cropsHint')}
{t('home.observations')}
{counts.observations}
{t('home.observationsHint')}
{t('home.onnx')}
{t('home.onnxHint')}
{t('home.harvests')}
{counts.harvests}
{t('home.harvestsHint')}
{t('home.sales')}
{counts.sales}
{t('home.salesHint')}
{t('home.costs')}
{counts.costs}
{t('home.costsHint')}
{t('home.todayTitle')}
{t('home.todayCardTitle')}
{t('home.count.tasks')} {counts.tasks}
{t('home.count.history')} {counts.history}
{t('home.todayCardBody')}
{t('home.learnAnalyze')}
{t('home.blogs')}
{t('home.blogsHint')}
{t('home.profile')}
{t('home.profileHint')}
);
}
const styles = StyleSheet.create({
heroImage: {
height: '100%',
width: '100%',
position: 'absolute',
},
heroCopy: {
gap: 12,
paddingHorizontal: 4,
},
heroBadge: {
alignSelf: 'flex-start',
flexDirection: 'row',
gap: 6,
paddingHorizontal: 10,
paddingVertical: 6,
borderRadius: 999,
borderWidth: 1,
borderColor: '#D2C9B8',
backgroundColor: '#F6F1E8',
},
heroBadgeText: {
fontSize: 12,
fontWeight: '600',
},
heroTitle: {
fontFamily: Fonts.rounded,
fontSize: 28,
},
heroSubtitle: {
opacity: 0.8,
fontSize: 15,
lineHeight: 22,
},
heroActions: {
flexDirection: 'row',
flexWrap: 'wrap',
gap: 12,
},
section: {
gap: 12,
},
actionGrid: {
flexDirection: 'row',
flexWrap: 'wrap',
columnGap: 20,
rowGap: 16,
},
actionCard: {
flexBasis: '49%',
flexGrow: 1,
gap: 6,
borderRadius: 14,
borderWidth: 1,
borderColor: '#D9D1C2',
padding: 12,
backgroundColor: '#FBF8F1',
alignItems: 'center',
},
actionCardWide: {
flexDirection: 'row',
alignItems: 'center',
gap: 12,
borderRadius: 14,
borderWidth: 1,
borderColor: '#D9D1C2',
padding: 12,
backgroundColor: '#FBF8F1',
flexBasis: '100%',
},
actionCopy: {
gap: 4,
},
actionHeader: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
gap: 8,
},
actionTitle: {
fontSize: 16,
fontWeight: '600',
},
actionHint: {
fontSize: 12,
opacity: 0.7,
},
highlightCard: {
borderRadius: 16,
borderWidth: 1,
borderColor: '#D6CCB9',
backgroundColor: '#EFE7D6',
padding: 16,
gap: 10,
},
highlightHeader: {
gap: 8,
},
highlightCounts: {
flexDirection: 'row',
flexWrap: 'wrap',
gap: 8,
},
highlightTitle: {
fontSize: 18,
fontWeight: '700',
},
highlightBody: {
fontSize: 14,
lineHeight: 20,
opacity: 0.8,
},
highlightActions: {
flexDirection: 'row',
flexWrap: 'wrap',
gap: 12,
},
countBadge: {
minWidth: 28,
borderRadius: 999,
borderWidth: 1,
borderColor: '#D9D1C2',
backgroundColor: '#F6F1E8',
alignItems: 'center',
justifyContent: 'center',
paddingHorizontal: 8,
paddingVertical: 4,
},
countText: {
fontSize: 11,
fontWeight: '600',
},
cardLightShadow: {
shadowColor: '#1E1A12',
shadowOpacity: 0.08,
shadowRadius: 10,
shadowOffset: { width: 0, height: 6 },
elevation: 2,
},
});