From 43aa945fc907b1f575002e55f15ff30e1b6d4263 Mon Sep 17 00:00:00 2001 From: SindreKjelsrud Date: Sat, 5 Jul 2025 17:56:37 +0200 Subject: [PATCH] fix: Persistent storage for todo + font fix Signed-off-by: SindreKjelsrud --- app/styles.ts | 1 + app/todo.tsx | 226 +++++++++++++++++++++++++++++++------------------- 2 files changed, 143 insertions(+), 84 deletions(-) diff --git a/app/styles.ts b/app/styles.ts index 721fdb1..7f7357d 100644 --- a/app/styles.ts +++ b/app/styles.ts @@ -221,6 +221,7 @@ export const styles = StyleSheet.create({ flex: 1, fontSize: 18, color: "#333", + fontFamily: "Minecraft", }, removeTodoButton: { backgroundColor: "#FF6347", diff --git a/app/todo.tsx b/app/todo.tsx index e0a8eae..09e11b3 100644 --- a/app/todo.tsx +++ b/app/todo.tsx @@ -1,14 +1,16 @@ +import AsyncStorage from "@react-native-async-storage/async-storage"; import { useRouter } from "expo-router"; -import { useState } from "react"; +import React, { useEffect, useState } from "react"; import { - ImageBackground, - KeyboardAvoidingView, - Platform, - ScrollView, - Text, - TextInput, - TouchableOpacity, - View + Alert, + ImageBackground, + KeyboardAvoidingView, + Platform, + ScrollView, + Text, + TextInput, + TouchableOpacity, + View } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; import { styles } from "./styles"; @@ -16,86 +18,142 @@ import { styles } from "./styles"; const converterBgImage = require("../assets/images/todo.png"); export default function CoordinateConverter() { - const router = useRouter(); - const [todos, setTodos] = useState([]); - const [newTodo, setNewTodo] = useState(""); + const router = useRouter(); + const [todos, setTodos] = useState([]); + const [newTodo, setNewTodo] = useState(""); - const addTodo = () => { - if (newTodo.trim().length > 0) { - setTodos([...todos, newTodo.trim()]); - setNewTodo(""); - } - }; + // Define a unique key for AsyncStorage + const STORAGE_KEY = "@my_todo_list"; - const removeTodo = (index: number) => { - const updatedTodos = todos.filter((_, i) => i !== index); - setTodos(updatedTodos); - }; + // --- Persistence Logic --- + useEffect(() => { + loadTodos(); + }, []); + useEffect(() => { + saveTodos(); + }, [todos]); - return ( - - - - + // Async function to load todos from AsyncStorage + const loadTodos = async () => { + try { + const jsonValue = await AsyncStorage.getItem(STORAGE_KEY); + if (jsonValue != null) { + const parsedTodos = JSON.parse(jsonValue); + if (Array.isArray(parsedTodos)) { + setTodos(parsedTodos); + } else { + console.warn("Loaded data is not an array, initializing empty todo list."); + setTodos([]); + } + } + } catch (e) { + console.error("Failed to load todos:", e); + Alert.alert("Error", "Failed to load your todo list."); + } + }; - - - - {/* Custom Back Button */} - router.back()} - style={styles.backButton} + // Async function to save todos to AsyncStorage + const saveTodos = async () => { + try { + const jsonValue = JSON.stringify(todos); + await AsyncStorage.setItem(STORAGE_KEY, jsonValue); + } catch (e) { + console.error("Failed to save todos:", e); + Alert.alert("Error", "Failed to save your todo list."); + } + }; + + const addTodo = () => { + if (newTodo.trim().length > 0) { + setTodos((prevTodos) => [...prevTodos, newTodo.trim()]); + setNewTodo(""); + } + }; + + const removeTodo = (index: number) => { + const updatedTodos = todos.filter((_, i) => i !== index); + setTodos(updatedTodos); + }; + + return ( + + - ← Go Back - + + - Todo List - - {/* Todo Input Section */} - - - - Add - - - - {/* Todo List Display */} - - {todos.length === 0 ? ( - - Your todo list is empty. Add some tasks! - - ) : ( - todos.map((todo, index) => ( - - {todo} - removeTodo(index)} - style={styles.removeTodoButton} + + + - X - - - )) - )} - - - - - - ); + {/* Custom Back Button */} + router.back()} + style={styles.backButton} + > + + ← Go Back + + + + Todo List + + {/* Todo Input Section */} + + + + + Add + + + + + {/* Todo List Display */} + + {todos.length === 0 ? ( + + Your todo list is empty. Add some tasks! + + ) : ( + todos.map((todo, index) => ( + + + {todo} + + removeTodo(index)} + style={styles.removeTodoButton} + > + + X + + + + )) + )} + + + + + + ); } \ No newline at end of file