Geen omschrijving

index.tsx 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. import { Pressable, ScrollView, StyleSheet, View } from 'react-native';
  2. import { useEffect, useState } from 'react';
  3. import { Image } from 'expo-image';
  4. import { ThemedText } from '@/components/themed-text';
  5. import { ThemedView } from '@/components/themed-view';
  6. import { IconSymbol } from '@/components/ui/icon-symbol';
  7. import { Colors, Fonts } from '@/constants/theme';
  8. import { Link } from 'expo-router';
  9. import { useTranslation } from '@/localization/i18n';
  10. import { dbPromise, initCoreTables } from '@/services/db';
  11. import { useColorScheme } from '@/hooks/use-color-scheme';
  12. export default function LogbookScreen() {
  13. const { t } = useTranslation();
  14. const theme = useColorScheme() ?? 'light';
  15. const palette = Colors[theme];
  16. const [counts, setCounts] = useState({
  17. fields: 0,
  18. crops: 0,
  19. observations: 0,
  20. tasks: 0,
  21. history: 0,
  22. harvests: 0,
  23. sales: 0,
  24. costs: 0,
  25. });
  26. useEffect(() => {
  27. let isActive = true;
  28. async function loadCounts() {
  29. try {
  30. await initCoreTables();
  31. const db = await dbPromise;
  32. const fieldCount = await db.getFirstAsync<{ count: number }>(
  33. 'SELECT COUNT(*) as count FROM fields;'
  34. );
  35. const cropCount = await db.getFirstAsync<{ count: number }>(
  36. 'SELECT COUNT(*) as count FROM crops;'
  37. );
  38. const observationCount = await db.getFirstAsync<{ count: number }>(
  39. 'SELECT COUNT(*) as count FROM observations;'
  40. );
  41. const taskCount = await db.getFirstAsync<{ count: number }>(
  42. 'SELECT COUNT(*) as count FROM daily_tasks WHERE is_active = 1;'
  43. );
  44. const historyCount = await db.getFirstAsync<{ count: number }>(
  45. 'SELECT COUNT(*) as count FROM daily_task_entries;'
  46. );
  47. const harvestCount = await db.getFirstAsync<{ count: number }>(
  48. 'SELECT COUNT(*) as count FROM harvests;'
  49. );
  50. const salesCount = await db.getFirstAsync<{ count: number }>(
  51. 'SELECT COUNT(*) as count FROM sales;'
  52. );
  53. const costCount = await db.getFirstAsync<{ count: number }>(
  54. 'SELECT COUNT(*) as count FROM costs;'
  55. );
  56. if (!isActive) return;
  57. setCounts({
  58. fields: fieldCount?.count ?? 0,
  59. crops: cropCount?.count ?? 0,
  60. observations: observationCount?.count ?? 0,
  61. tasks: taskCount?.count ?? 0,
  62. history: historyCount?.count ?? 0,
  63. harvests: harvestCount?.count ?? 0,
  64. sales: salesCount?.count ?? 0,
  65. costs: costCount?.count ?? 0,
  66. });
  67. } catch {
  68. if (!isActive) return;
  69. setCounts((prev) => prev);
  70. }
  71. }
  72. loadCounts();
  73. return () => {
  74. isActive = false;
  75. };
  76. }, []);
  77. return (
  78. <ThemedView style={styles.container}>
  79. <ScrollView contentContainerStyle={styles.scrollContent}>
  80. <ThemedView style={styles.hero}>
  81. <Image
  82. source={require('@/assets/images/logbook.jpg')}
  83. style={styles.heroImage}
  84. contentFit="cover"
  85. />
  86. </ThemedView>
  87. <ThemedView style={styles.titleContainer}>
  88. <ThemedText type="title" style={{ fontFamily: Fonts.rounded }}>
  89. {t('logbook.title')}
  90. </ThemedText>
  91. <ThemedText style={styles.subtitle}>{t('logbook.subtitle')}</ThemedText>
  92. </ThemedView>
  93. <View style={styles.grid}>
  94. <Link href="/logbook/fields" asChild>
  95. <Pressable
  96. style={[
  97. styles.card,
  98. {
  99. backgroundColor: palette.card,
  100. borderColor: palette.border,
  101. ...(theme === 'light' ? styles.cardLightShadow : null),
  102. },
  103. ]}>
  104. <View style={styles.cardHeader}>
  105. <View style={styles.cardTitleRow}>
  106. <View
  107. style={[
  108. styles.cardIcon,
  109. { backgroundColor: palette.surface },
  110. theme === 'light' ? styles.cardIconLight : null,
  111. ]}>
  112. <IconSymbol size={18} color={palette.tint} name="square.grid.2x2.fill" />
  113. </View>
  114. <ThemedText type="subtitle">{t('logbook.fields')}</ThemedText>
  115. </View>
  116. <View style={[styles.countBadge, { borderColor: palette.border, backgroundColor: palette.surface }]}>
  117. <ThemedText style={styles.countText}>{counts.fields}</ThemedText>
  118. </View>
  119. </View>
  120. <ThemedText style={styles.cardHint}>{t('logbook.fieldsHint')}</ThemedText>
  121. </Pressable>
  122. </Link>
  123. <Link href="/logbook/crops" asChild>
  124. <Pressable
  125. style={[
  126. styles.card,
  127. {
  128. backgroundColor: palette.card,
  129. borderColor: palette.border,
  130. ...(theme === 'light' ? styles.cardLightShadow : null),
  131. },
  132. ]}>
  133. <View style={styles.cardHeader}>
  134. <View style={styles.cardTitleRow}>
  135. <View
  136. style={[
  137. styles.cardIcon,
  138. { backgroundColor: palette.surface },
  139. theme === 'light' ? styles.cardIconLight : null,
  140. ]}>
  141. <IconSymbol size={18} color={palette.tint} name="leaf.fill" />
  142. </View>
  143. <ThemedText type="subtitle">{t('logbook.crops')}</ThemedText>
  144. </View>
  145. <View style={[styles.countBadge, { borderColor: palette.border, backgroundColor: palette.surface }]}>
  146. <ThemedText style={styles.countText}>{counts.crops}</ThemedText>
  147. </View>
  148. </View>
  149. <ThemedText style={styles.cardHint}>{t('logbook.cropsHint')}</ThemedText>
  150. </Pressable>
  151. </Link>
  152. <Link href={{ pathname: '/observations', params: { from: 'logbook' } }} asChild>
  153. <Pressable
  154. style={[
  155. styles.card,
  156. {
  157. backgroundColor: palette.card,
  158. borderColor: palette.border,
  159. ...(theme === 'light' ? styles.cardLightShadow : null),
  160. },
  161. ]}>
  162. <View style={styles.cardHeader}>
  163. <View style={styles.cardTitleRow}>
  164. <View
  165. style={[
  166. styles.cardIcon,
  167. { backgroundColor: palette.surface },
  168. theme === 'light' ? styles.cardIconLight : null,
  169. ]}>
  170. <IconSymbol size={18} color={palette.tint} name="eye.fill" />
  171. </View>
  172. <ThemedText type="subtitle">{t('logbook.observations')}</ThemedText>
  173. </View>
  174. <View style={[styles.countBadge, { borderColor: palette.border, backgroundColor: palette.surface }]}>
  175. <ThemedText style={styles.countText}>{counts.observations}</ThemedText>
  176. </View>
  177. </View>
  178. <ThemedText style={styles.cardHint}>{t('logbook.observationsHint')}</ThemedText>
  179. </Pressable>
  180. </Link>
  181. <Link href={{ pathname: '/tasks', params: { from: 'logbook' } }} asChild>
  182. <Pressable
  183. style={[
  184. styles.card,
  185. {
  186. backgroundColor: palette.card,
  187. borderColor: palette.border,
  188. ...(theme === 'light' ? styles.cardLightShadow : null),
  189. },
  190. ]}>
  191. <View style={styles.cardHeader}>
  192. <View style={styles.cardTitleRow}>
  193. <View
  194. style={[
  195. styles.cardIcon,
  196. { backgroundColor: palette.surface },
  197. theme === 'light' ? styles.cardIconLight : null,
  198. ]}>
  199. <IconSymbol size={18} color={palette.tint} name="checklist" />
  200. </View>
  201. <ThemedText type="subtitle">{t('logbook.tasks')}</ThemedText>
  202. </View>
  203. <View style={[styles.countBadge, { borderColor: palette.border, backgroundColor: palette.surface }]}>
  204. <ThemedText style={styles.countText}>{counts.tasks}</ThemedText>
  205. </View>
  206. </View>
  207. <ThemedText style={styles.cardHint}>{t('logbook.tasksHint')}</ThemedText>
  208. </Pressable>
  209. </Link>
  210. <Link href={{ pathname: '/task-history', params: { from: 'logbook' } }} asChild>
  211. <Pressable
  212. style={[
  213. styles.card,
  214. {
  215. backgroundColor: palette.card,
  216. borderColor: palette.border,
  217. ...(theme === 'light' ? styles.cardLightShadow : null),
  218. },
  219. ]}>
  220. <View style={styles.cardHeader}>
  221. <View style={styles.cardTitleRow}>
  222. <View
  223. style={[
  224. styles.cardIcon,
  225. { backgroundColor: palette.surface },
  226. theme === 'light' ? styles.cardIconLight : null,
  227. ]}>
  228. <IconSymbol size={18} color={palette.tint} name="clock.fill" />
  229. </View>
  230. <ThemedText type="subtitle">{t('logbook.history')}</ThemedText>
  231. </View>
  232. <View style={[styles.countBadge, { borderColor: palette.border, backgroundColor: palette.surface }]}>
  233. <ThemedText style={styles.countText}>{counts.history}</ThemedText>
  234. </View>
  235. </View>
  236. <ThemedText style={styles.cardHint}>{t('logbook.historyHint')}</ThemedText>
  237. </Pressable>
  238. </Link>
  239. <Link href={{ pathname: '/harvests', params: { from: 'logbook' } }} asChild>
  240. <Pressable
  241. style={[
  242. styles.card,
  243. {
  244. backgroundColor: palette.card,
  245. borderColor: palette.border,
  246. ...(theme === 'light' ? styles.cardLightShadow : null),
  247. },
  248. ]}>
  249. <View style={styles.cardHeader}>
  250. <View style={styles.cardTitleRow}>
  251. <View
  252. style={[
  253. styles.cardIcon,
  254. { backgroundColor: palette.surface },
  255. theme === 'light' ? styles.cardIconLight : null,
  256. ]}>
  257. <IconSymbol size={18} color={palette.tint} name="basket.fill" />
  258. </View>
  259. <ThemedText type="subtitle">{t('logbook.harvests')}</ThemedText>
  260. </View>
  261. <View style={[styles.countBadge, { borderColor: palette.border, backgroundColor: palette.surface }]}>
  262. <ThemedText style={styles.countText}>{counts.harvests}</ThemedText>
  263. </View>
  264. </View>
  265. <ThemedText style={styles.cardHint}>{t('logbook.harvestsHint')}</ThemedText>
  266. </Pressable>
  267. </Link>
  268. <Link href={{ pathname: '/sales', params: { from: 'logbook' } }} asChild>
  269. <Pressable
  270. style={[
  271. styles.card,
  272. {
  273. backgroundColor: palette.card,
  274. borderColor: palette.border,
  275. ...(theme === 'light' ? styles.cardLightShadow : null),
  276. },
  277. ]}>
  278. <View style={styles.cardHeader}>
  279. <View style={styles.cardTitleRow}>
  280. <View
  281. style={[
  282. styles.cardIcon,
  283. { backgroundColor: palette.surface },
  284. theme === 'light' ? styles.cardIconLight : null,
  285. ]}>
  286. <IconSymbol size={18} color={palette.tint} name="cart.fill" />
  287. </View>
  288. <ThemedText type="subtitle">{t('logbook.sales')}</ThemedText>
  289. </View>
  290. <View style={[styles.countBadge, { borderColor: palette.border, backgroundColor: palette.surface }]}>
  291. <ThemedText style={styles.countText}>{counts.sales}</ThemedText>
  292. </View>
  293. </View>
  294. <ThemedText style={styles.cardHint}>{t('logbook.salesHint')}</ThemedText>
  295. </Pressable>
  296. </Link>
  297. <Link href={{ pathname: '/costs', params: { from: 'logbook' } }} asChild>
  298. <Pressable
  299. style={[
  300. styles.card,
  301. {
  302. backgroundColor: palette.card,
  303. borderColor: palette.border,
  304. ...(theme === 'light' ? styles.cardLightShadow : null),
  305. },
  306. ]}>
  307. <View style={styles.cardHeader}>
  308. <View style={styles.cardTitleRow}>
  309. <View
  310. style={[
  311. styles.cardIcon,
  312. { backgroundColor: palette.surface },
  313. theme === 'light' ? styles.cardIconLight : null,
  314. ]}>
  315. <IconSymbol size={18} color={palette.tint} name="creditcard.fill" />
  316. </View>
  317. <ThemedText type="subtitle">{t('logbook.costs')}</ThemedText>
  318. </View>
  319. <View style={[styles.countBadge, { borderColor: palette.border, backgroundColor: palette.surface }]}>
  320. <ThemedText style={styles.countText}>{counts.costs}</ThemedText>
  321. </View>
  322. </View>
  323. <ThemedText style={styles.cardHint}>{t('logbook.costsHint')}</ThemedText>
  324. </Pressable>
  325. </Link>
  326. </View>
  327. </ScrollView>
  328. </ThemedView>
  329. );
  330. }
  331. const styles = StyleSheet.create({
  332. container: {
  333. flex: 1,
  334. },
  335. scrollContent: {
  336. paddingBottom: 24,
  337. },
  338. hero: {
  339. backgroundColor: '#E8E6DA',
  340. aspectRatio: 16 / 9,
  341. width: '100%',
  342. },
  343. heroImage: {
  344. width: '100%',
  345. height: '100%',
  346. },
  347. titleContainer: {
  348. gap: 8,
  349. paddingHorizontal: 16,
  350. paddingVertical: 12,
  351. },
  352. subtitle: {
  353. opacity: 0.7,
  354. },
  355. grid: {
  356. paddingHorizontal: 16,
  357. gap: 12,
  358. },
  359. card: {
  360. borderRadius: 12,
  361. borderWidth: 1,
  362. borderColor: '#C6C6C6',
  363. padding: 16,
  364. backgroundColor: '#FFFFFF',
  365. },
  366. cardHeader: {
  367. flexDirection: 'row',
  368. alignItems: 'center',
  369. justifyContent: 'space-between',
  370. gap: 12,
  371. },
  372. cardTitleRow: {
  373. flexDirection: 'row',
  374. alignItems: 'center',
  375. gap: 10,
  376. flex: 1,
  377. },
  378. cardIcon: {
  379. width: 28,
  380. height: 28,
  381. borderRadius: 8,
  382. backgroundColor: '#E7F3EA',
  383. alignItems: 'center',
  384. justifyContent: 'center',
  385. },
  386. countBadge: {
  387. minWidth: 28,
  388. height: 28,
  389. borderRadius: 14,
  390. borderWidth: 1,
  391. borderColor: '#D9D1C2',
  392. backgroundColor: '#F6F1E8',
  393. alignItems: 'center',
  394. justifyContent: 'center',
  395. paddingHorizontal: 8,
  396. },
  397. countText: {
  398. fontSize: 12,
  399. fontWeight: '600',
  400. },
  401. cardHint: {
  402. opacity: 0.7,
  403. marginTop: 6,
  404. },
  405. cardLightShadow: {
  406. shadowColor: '#1E1A12',
  407. shadowOpacity: 0.08,
  408. shadowRadius: 10,
  409. shadowOffset: { width: 0, height: 6 },
  410. elevation: 2,
  411. },
  412. cardIconLight: {
  413. borderWidth: 1,
  414. borderColor: '#E0D7C9',
  415. },
  416. });