-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
138 lines (126 loc) · 3.81 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import React, { useState, useEffect } from "react";
import { View, TextInput, Button, Text, FlatList, Image } from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
const App = () => {
const [todos, setTodos] = useState([]);
const [newTodo, setNewTodo] = useState("");
const [editingTodo, setEditingTodo] = useState(null);
useEffect(() => {
loadTodos();
}, []);
useEffect(() => {
saveTodos();
}, [todos]);
const loadTodos = async () => {
try {
const storedTodos = await AsyncStorage.getItem("todos");
if (storedTodos !== null) {
setTodos(JSON.parse(storedTodos));
}
} catch (error) {
console.error("Error loading todos from AsyncStorage:", error);
}
};
const saveTodos = async () => {
try {
await AsyncStorage.setItem("todos", JSON.stringify(todos));
} catch (error) {
console.error("Error saving todos to AsyncStorage:", error);
}
};
const addTodo = () => {
if (newTodo.trim() !== "") {
const newTodoItem = { id: Date.now(), content: newTodo };
setTodos([...todos, newTodoItem]);
setNewTodo("");
}
};
const deleteTodo = async (id) => {
const updatedTodos = todos.filter((todo) => todo.id !== id);
setTodos(updatedTodos);
};
const editTodo = (id) => {
setEditingTodo(id);
const todoToEdit = todos.find((todo) => todo.id === id);
setNewTodo(todoToEdit.content);
};
const updateTodo = () => {
if (newTodo.trim() !== "") {
const updatedTodos = todos.map((todo) =>
todo.id === editingTodo ? { ...todo, content: newTodo } : todo
);
setTodos(updatedTodos);
setNewTodo("");
setEditingTodo(null);
}
};
return (
<View style={{ margin: 20, marginTop: 50 }}>
{/* Image at the top */}
<Image
source={{
uri: "https://t4.ftcdn.net/jpg/02/18/12/59/360_F_218125902_9k6teJpNNQCcg4YgMI3HypyTUNMCRkdR.jpg",
}}
style={{ height: 100, width: "100%", marginBottom: 30 }}
resizeMode="cover"
/>
{/* Main Content */}
<TextInput
style={{
height: 40,
borderColor: "gray",
borderWidth: 1,
marginBottom: 10,
paddingLeft: 10,
backgroundColor: "#fff", // White background for TextInput
}}
onChangeText={(text) => setNewTodo(text)}
value={newTodo}
placeholder="Enter a new todo"
/>
<Button title="Add" onPress={addTodo} />
<FlatList
data={todos}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => (
<View
style={{
flexDirection: "row",
justifyContent: "space-between",
marginVertical: 5,
backgroundColor: "#d3d3d3", // Gray background for FlatList item
padding: 10,
borderRadius: 5,
}}
>
<Text>{item.content}</Text>
<View style={{ flexDirection: "row" }}>
{/* Edit Icon */}
<Icon
name="pencil-outline"
size={24}
color="blue"
onPress={() => editTodo(item.id)}
/>
{/* Delete Icon */}
<Icon
name="delete-outline"
size={24}
color="red"
onPress={() => deleteTodo(item.id)}
/>
</View>
</View>
)}
/>
{editingTodo && (
<View style={{ flexDirection: "row", marginTop: 10 }}>
<Button title="Update" onPress={updateTodo} />
<Button title="Cancel" onPress={() => setEditingTodo(null)} />
</View>
)}
</View>
);
};
export default App;