a b/mobile/ElderCare/HomeScreen.js
1
import React, { useEffect, useState, useRef } from 'react';
2
import {
3
    StyleSheet,
4
    Text,
5
    View,
6
    Image,
7
    ScrollView,
8
    TouchableOpacity,
9
    Alert,
10
} from 'react-native';
11
12
export default function HomeScreen() {
13
    const [eldersData, setEldersData] = useState([]);
14
    const previousActivityRef = useRef({}); // Track previous activity states
15
16
    useEffect(() => {
17
        // Function to fetch elder data
18
        const fetchEldersData = () => {
19
            fetch('http://192.168.10.7:5000/get_elders') // Replace with your local IP
20
                .then((response) => response.json())
21
                .then((data) => {
22
                    // Check for activity change to 'Shuffling'
23
                    data.forEach((elder) => {
24
                        const prevActivity =
25
                            previousActivityRef.current[elder.id];
26
                        if (
27
                            prevActivity &&
28
                            prevActivity !== 'Shuffling' &&
29
                            elder.activity === 'Shuffling'
30
                        ) {
31
                            Alert.alert(
32
                                'Alert',
33
                                `${elder.name} has started shuffling.`
34
                            );
35
                        }
36
                        // Update previous activity state
37
                        previousActivityRef.current[elder.id] = elder.activity;
38
                    });
39
                    setEldersData(data);
40
                })
41
                .catch((error) =>
42
                    console.error('Error fetching elders data:', error)
43
                );
44
        };
45
46
        // Fetch data immediately
47
        fetchEldersData();
48
49
        // Set up interval to fetch data every second
50
        const intervalId = setInterval(fetchEldersData, 1000); // 1000ms = 1s
51
52
        // Clear interval on component unmount
53
        return () => clearInterval(intervalId);
54
    }, []);
55
56
    const handleRemove = (id) => {
57
        const apiUrl = `http://192.168.10.7:5000/remove_elder/${id}`;
58
        fetch(apiUrl, { method: 'DELETE' })
59
            .then((response) => {
60
                if (!response.ok) {
61
                    throw new Error('Failed to remove elder.');
62
                }
63
                return response.json();
64
            })
65
            .then((data) => {
66
                if (data.message) {
67
                    setEldersData(
68
                        eldersData.filter((elder) => elder.id !== id)
69
                    );
70
                    Alert.alert('Success', data.message);
71
                } else {
72
                    Alert.alert('Error', 'Failed to remove elder.');
73
                }
74
            })
75
            .catch((error) => {
76
                Alert.alert('Server Error', error.message);
77
            });
78
    };
79
80
    const renderActivityIcon = (activity) => {
81
        switch (activity) {
82
            case 'Walking':
83
                return (
84
                    <Image
85
                        source={require('./assets/walking.png')}
86
                        style={styles.icon}
87
                    />
88
                );
89
            case 'Shuffling':
90
                return (
91
                    <Image
92
                        source={require('./assets/shuffling.png')}
93
                        style={styles.icon}
94
                    />
95
                );
96
            case 'Stairs (asce)':
97
                return (
98
                    <Image
99
                        source={require('./assets/stairs_ascending.png')}
100
                        style={styles.icon}
101
                    />
102
                );
103
            case 'Stairs (desc)':
104
                return (
105
                    <Image
106
                        source={require('./assets/stairs_descending.png')}
107
                        style={styles.icon}
108
                    />
109
                );
110
            case 'Standing':
111
                return (
112
                    <Image
113
                        source={require('./assets/standing.png')}
114
                        style={styles.icon}
115
                    />
116
                );
117
            case 'Sitting':
118
                return (
119
                    <Image
120
                        source={require('./assets/sitting.png')}
121
                        style={styles.icon}
122
                    />
123
                );
124
            case 'Lying':
125
                return (
126
                    <Image
127
                        source={require('./assets/lying.png')}
128
                        style={styles.icon}
129
                    />
130
                );
131
            default:
132
                return null;
133
        }
134
    };
135
136
    return (
137
        <View style={styles.container}>
138
            <View style={styles.header}>
139
                <Image
140
                    source={require('./assets/logo.png')} // Replace with your logo path
141
                    style={styles.logo}
142
                />
143
                <Text style={styles.appName}>ElderCare+</Text>
144
                <Text style={styles.slogan}>Caring for Every Step</Text>
145
            </View>
146
            <View style={styles.content}>
147
                <ScrollView contentContainerStyle={styles.scrollContainer}>
148
                    {eldersData.map((elder) => (
149
                        <View
150
                            key={elder.id}
151
                            style={[
152
                                styles.tile,
153
                                elder.activity === 'Shuffling' &&
154
                                    styles.shufflingTile,
155
                            ]}>
156
                            <Text style={styles.elderName}>{elder.name}</Text>
157
                            {renderActivityIcon(elder.activity)}
158
                            <View style={styles.activityContainer}>
159
                                <Text style={styles.activityText}>
160
                                    {elder.activity}
161
                                </Text>
162
                            </View>
163
                            <TouchableOpacity
164
                                style={styles.removeButton}
165
                                onPress={() => handleRemove(elder.id)} // Handle removal
166
                            >
167
                                <Image
168
                                    source={require('./assets/remove.png')}
169
                                    style={styles.icon}
170
                                />
171
                            </TouchableOpacity>
172
                        </View>
173
                    ))}
174
                </ScrollView>
175
            </View>
176
        </View>
177
    );
178
}
179
180
const styles = StyleSheet.create({
181
    container: {
182
        flex: 1,
183
        backgroundColor: '#F2F4F9',
184
    },
185
    header: {
186
        flex: 0.3, // Occupies 30% of the screen height
187
        justifyContent: 'center',
188
        alignItems: 'center',
189
        backgroundColor: '#2D328C',
190
        padding: 20,
191
        borderBottomRightRadius: 30,
192
        borderBottomLeftRadius: 30,
193
    },
194
    logo: {
195
        width: 100,
196
        height: 100,
197
        resizeMode: 'contain',
198
    },
199
    appName: {
200
        fontSize: 24,
201
        color: '#fff',
202
        fontWeight: 'bold',
203
        marginTop: 10,
204
    },
205
    slogan: {
206
        fontSize: 16,
207
        color: '#fff',
208
        marginTop: 5,
209
    },
210
    content: {
211
        flex: 0.7,
212
        padding: 20,
213
    },
214
    scrollContainer: {
215
        flexGrow: 1,
216
        justifyContent: 'flex-start',
217
    },
218
    tile: {
219
        backgroundColor: '#fff',
220
        borderRadius: 15,
221
        padding: 20,
222
        marginBottom: 20,
223
        flexDirection: 'row',
224
        alignItems: 'center',
225
        shadowColor: '#000',
226
        shadowOffset: { width: 0, height: 4 },
227
        shadowOpacity: 0.1,
228
        shadowRadius: 5,
229
        elevation: 5,
230
        borderWidth: 1,
231
        borderColor: '#ddd',
232
    },
233
    shufflingTile: {
234
        borderColor: '#FF1B08', // Red color for shuffling
235
    },
236
    elderName: {
237
        flex: 1,
238
        fontSize: 16,
239
        color: '#000',
240
        marginRight: 10, // Add margin to the right of the elder name
241
    },
242
    activityContainer: {
243
        flexDirection: 'row',
244
        alignItems: 'center',
245
        width: '40%',
246
        marginLeft: 5,
247
    },
248
    activityText: {
249
        fontSize: 16,
250
        color: '#555',
251
        marginLeft: 5,
252
    },
253
    removeButton: {
254
        padding: 5,
255
    },
256
    icon: {
257
        width: 24,
258
        height: 24,
259
        resizeMode: 'contain',
260
    },
261
});