Repository navigation
Expand file tree
/
Copy pathExerciseScreen.tsx
More file actions
441 lines (378 loc) · 9.07 KB
/
Copy pathExerciseScreen.tsx
File metadata and controls
441 lines (378 loc) · 9.07 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import React, {useState, useEffect} from 'react';
import {
View,
ScrollView,
TouchableOpacity,
Text,
FlatList,
StyleSheet,
Image,
ActivityIndicator,
} from 'react-native';
import {getDatabase} from './src/database/database';
import {
initializeExerciseDatabaseWithSeed,
} from './src/database/exerciseDatabase';
const ExerciseScreen = () => {
const [selectedCategory, setSelectedCategory] = useState<number | null>(
null,
);
const [categories, setCategories] = useState<any[]>([]);
const [exercises, setExercises] = useState<any[]>([]);
const [selectedExercise, setSelectedExercise] = useState<any | null>(
null,
);
const [loading, setLoading] = useState(true);
/**
* ExerciseScreen 최초 진입 시
*
* 1. 운동 DB 초기화
* 2. 기본 데이터 확인 및 삽입
* 3. 카테고리 조회
* 4. 첫 번째 카테고리 자동 선택
*/
useEffect(() => {
const init = async () => {
try {
setLoading(true);
// SQLite DB 연결
await getDatabase();
// 운동 DB 초기화 + seed
await initializeExerciseDatabaseWithSeed();
// 카테고리 조회
const data = await fetchCategories();
setCategories(data);
// 첫 번째 카테고리 자동 선택
if (data.length > 0) {
setSelectedCategory(data[0].id);
}
} catch (error) {
console.error('운동 화면 초기화 실패:', error);
} finally {
setLoading(false);
}
};
init();
}, []);
/**
* 카테고리가 선택되면 해당 운동 조회
*/
useEffect(() => {
if (selectedCategory === null) {
return;
}
const loadExercises = async () => {
try {
const data = await fetchExercises(selectedCategory);
setExercises(data);
setSelectedExercise(null);
} catch (error) {
console.error('운동 목록 조회 실패:', error);
setExercises([]);
}
};
loadExercises();
}, [selectedCategory]);
/**
* 카테고리 조회
*/
const fetchCategories = async () => {
try {
console.log('카테고리 조회 시작');
const db = await getDatabase();
const [result] = await db.executeSql(`
SELECT *
FROM exercise_categories
ORDER BY id ASC
`);
const categories = [];
for (let i = 0; i < result.rows.length; i++) {
categories.push(result.rows.item(i));
}
console.log('카테고리 조회 성공:', categories);
return categories;
} catch (error) {
console.error('카테고리 조회 실패:', error);
return [];
}
};
/**
* 운동 조회
*/
const fetchExercises = async (categoryId: number) => {
try {
console.log('운동 조회 시작:', categoryId);
const db = await getDatabase();
const [result] = await db.executeSql(
`
SELECT
e.*,
p.picture_url
FROM exercises e
LEFT JOIN exercise_pictures p
ON p.id = (
SELECT id
FROM exercise_pictures
WHERE exercise_id = e.id
ORDER BY id ASC
LIMIT 1
)
WHERE e.exercise_category_id = ?
ORDER BY e.id ASC
`,
[categoryId],
);
const exercises = [];
for (let i = 0; i < result.rows.length; i++) {
exercises.push(result.rows.item(i));
}
console.log('운동 조회 성공:', exercises);
return exercises;
} catch (error) {
console.error('운동 조회 실패:', error);
return [];
}
};
/**
* 카테고리 아이템
*/
const renderCategoryItem = (category: any) => (
<TouchableOpacity
key={category.id}
style={[
styles.categoryButton,
selectedCategory === category.id &&
styles.categoryButtonActive,
]}
onPress={() => setSelectedCategory(category.id)}
>
<Text
style={[
styles.categoryText,
selectedCategory === category.id &&
styles.categoryTextActive,
]}
>
{category.title}
</Text>
</TouchableOpacity>
);
/**
* 운동 아이템
*/
const renderExerciseItem = ({item}: {item: any}) => (
<TouchableOpacity
style={styles.exerciseItem}
onPress={() => setSelectedExercise(item)}
>
<View style={styles.exerciseImageContainer}>
{item.picture_url ? (
<Image
source={{
uri: `https://humake.blob.core.windows.net/humake/exercise/${item.picture_url}`,
}}
style={styles.exerciseImage}
resizeMode="cover"
/>
) : (
<Image
source={require('./assets/photo_none.gif')}
style={styles.exerciseImage}
resizeMode="cover"
/>
)}
</View>
<View style={styles.exerciseInfo}>
<Text>{item.title}</Text>
<Text
style={{
color: '#ff8d1d',
fontWeight: 'bold',
}}
>
{item.content}
</Text>
</View>
</TouchableOpacity>
);
/**
* 운동 상세
*/
const renderExerciseDetail = () => {
if (!selectedExercise) {
return null;
}
return (
<View style={styles.detailContainer}>
<TouchableOpacity
onPress={() => setSelectedExercise(null)}
style={styles.backButton}
>
<Text style={styles.backButtonText}>
← 목록으로
</Text>
</TouchableOpacity>
<View style={styles.exerciseImageContainer}>
{selectedExercise.picture_url ? (
<Image
source={{
uri: `https://humake.blob.core.windows.net/humake/exercise/medium_thumb_${selectedExercise.picture_url}`,
}}
style={styles.exerciseImage}
resizeMode="cover"
/>
) : (
<Image
source={require('./assets/photo_none.gif')}
style={styles.exerciseImage}
resizeMode="cover"
/>
)}
</View>
<Text style={styles.itemTextActive}>
{selectedExercise.content}
</Text>
</View>
);
};
/**
* 로딩
*/
if (loading) {
return (
<View style={styles.loadingContainer}>
<ActivityIndicator size="large" />
<Text style={styles.loadingText}>
운동 정보를 준비하고 있습니다...
</Text>
</View>
);
}
/**
* 화면
*/
return (
<View style={styles.container}>
<ScrollView
horizontal
showsHorizontalScrollIndicator={false}
style={styles.categoryScroll}
contentContainerStyle={styles.categoryContainer}
>
{categories.map(renderCategoryItem)}
</ScrollView>
{selectedExercise ? (
renderExerciseDetail()
) : (
<FlatList
data={exercises}
keyExtractor={item => item.id.toString()}
contentContainerStyle={styles.listContent}
renderItem={renderExerciseItem}
/>
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
loadingContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
},
loadingText: {
marginTop: 12,
color: '#666',
},
categoryScroll: {
paddingVertical: 10,
maxHeight: 70,
height: 70,
},
categoryContainer: {
paddingHorizontal: 10,
},
categoryButton: {
paddingVertical: 8,
paddingHorizontal: 16,
backgroundColor: '#eee',
borderRadius: 20,
marginRight: 8,
justifyContent: 'center',
alignItems: 'center',
},
categoryButtonActive: {
backgroundColor: '#007AFF',
},
categoryText: {
color: '#333',
},
categoryTextActive: {
color: '#fff',
},
listContent: {
padding: 16,
},
exerciseItem: {
flexDirection: 'row',
alignItems: 'center',
padding: 12,
marginBottom: 12,
backgroundColor: '#fafafa',
borderRadius: 10,
borderWidth: 1,
borderColor: '#eee',
},
exerciseImageContainer: {
width: 60,
height: 70,
marginRight: 16,
borderRadius: 8,
overflow: 'hidden',
backgroundColor: '#eee',
justifyContent: 'center',
alignItems: 'center',
},
exerciseImage: {
width: 60,
height: 60,
},
exerciseInfo: {
flex: 1,
justifyContent: 'center',
},
detailContainer: {
flex: 1,
padding: 16,
backgroundColor: '#fff',
justifyContent: 'flex-start',
},
backButton: {
marginBottom: 16,
alignSelf: 'flex-start',
paddingHorizontal: 12,
paddingVertical: 6,
backgroundColor: '#eee',
borderRadius: 8,
},
backButtonText: {
color: '#007AFF',
fontSize: 16,
fontWeight: 'bold',
},
itemTextActive: {
color: '#ff8d1d',
backgroundColor: '#eee',
borderRadius: 8,
fontWeight: 'bold',
fontSize: 20,
marginVertical: 10,
padding: 16,
},
});
export default ExerciseScreen;