-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from bunju20/develop
✨Feat: 문단 교정 페이지 생성, DB연동
- Loading branch information
Showing
6 changed files
with
212 additions
and
52 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
lib/viewModels/Paragraph/learning_session_screen_viewmodel.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
import 'package:get/get_rx/src/rx_types/rx_types.dart'; | ||
import 'package:get/get_state_manager/src/simple/get_controllers.dart'; | ||
|
||
|
||
|
||
class LearningSessionScreenViewModel extends GetxController { | ||
final FirebaseFirestore _firestore = FirebaseFirestore.instance; | ||
final FirebaseAuth _auth = FirebaseAuth.instance; | ||
|
||
final RxBool isLoading = false.obs; // 로딩 상태 관리 | ||
final RxList<Paragraph> paragraphs = <Paragraph>[].obs; // Paragraph 객체 리스트 | ||
|
||
// Firestore에서 paragraphs 컬렉션의 데이터를 가져오는 함수 | ||
Future<void> fetchParagraphs() async { | ||
final uid = _auth.currentUser?.uid; // 사용자 UID 가져오기 | ||
try { | ||
isLoading(true); // 로딩 시작 | ||
final QuerySnapshot paragraphSnapshot = await _firestore | ||
.collection('paragraph') // 사용자의 paragraph 컬렉션에 접근 | ||
.limit(5) // 예제로 5개의 문서만 가져오기 | ||
.get(); | ||
|
||
final List<Paragraph> fetchedParagraphs = paragraphSnapshot.docs | ||
.map((doc) { | ||
// doc.data() 호출 결과를 Map<String, dynamic>으로 타입 캐스팅 | ||
final data = doc.data() as Map<String, dynamic>?; | ||
// Null-safety를 고려하여, 필드에 접근하기 전에 null 체크 | ||
final title = data?['title'] as String? ?? ''; // title이 없으면 빈 문자열 할당 | ||
final text = data?['text'] as String? ?? ''; // text가 없으면 빈 문자열 할당 | ||
return Paragraph(title: title, text: text); | ||
}) | ||
.toList(); | ||
|
||
|
||
paragraphs.value = fetchedParagraphs; // 상태 업데이트 | ||
} catch (e) { | ||
print("Error fetching paragraphs: $e"); // 오류 처리 | ||
} finally { | ||
isLoading(false); // 로딩 종료 | ||
} | ||
} | ||
} | ||
|
||
// Firestore 문서로부터 생성되는 Paragraph 모델 | ||
class Paragraph { | ||
final String title; | ||
final String text; | ||
|
||
Paragraph({required this.title, required this.text}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import 'package:earlips/viewModels/Paragraph/learning_session_screen_viewmodel.dart'; | ||
import 'package:earlips/views/word/widget/blue_back_appbar.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:get/get.dart'; | ||
import 'package:earlips/views/paragraph/create_script_screen.dart'; | ||
|
||
class LearningSessionScreen extends StatefulWidget { | ||
LearningSessionScreen({Key? key}) : super(key: key); | ||
|
||
@override | ||
State<LearningSessionScreen> createState() => _LearningSessionScreenState(); | ||
} | ||
|
||
class _LearningSessionScreenState extends State<LearningSessionScreen> { | ||
final viewModel = Get.put( | ||
LearningSessionScreenViewModel()); // ViewModel 인스턴스 생성 | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
viewModel.fetchParagraphs(); // 화면이 로드될 때 Firestore에서 데이터 가져오기 | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: PreferredSize( | ||
preferredSize: const Size.fromHeight(kToolbarHeight), | ||
child: BlueBackAppbar(title: "문단교정"), | ||
), | ||
body: Obx(() { | ||
if (viewModel.isLoading.value) { | ||
return Center(child: CircularProgressIndicator()); | ||
} else { | ||
return ListView.separated( | ||
padding: const EdgeInsets.fromLTRB(25, 20, 25, 20), | ||
itemCount: viewModel.paragraphs.length, | ||
itemBuilder: (context, index) { | ||
var paragraph = viewModel.paragraphs[index]; | ||
return Container( | ||
decoration: BoxDecoration( | ||
color: Colors.white, | ||
borderRadius: BorderRadius.circular(15.0), | ||
boxShadow: [ | ||
BoxShadow( | ||
color: Colors.grey.withOpacity(0.15), | ||
spreadRadius: 0.1, | ||
blurRadius: 10, | ||
offset: const Offset(0, 2), | ||
), | ||
], | ||
), | ||
child: ListTile( | ||
contentPadding: const EdgeInsets.all(20), | ||
title: Text( | ||
paragraph.title, | ||
style: const TextStyle( | ||
fontSize: 20.0, | ||
fontWeight: FontWeight.bold, | ||
), | ||
), | ||
onTap: () { | ||
// title과 text만 다음 페이지로 전달 | ||
Get.to(() => | ||
CreateScriptPage( | ||
title: paragraph.title, text: paragraph.text)); | ||
}, | ||
), | ||
); | ||
}, | ||
separatorBuilder: (context, index) => const SizedBox(height: 20), | ||
); | ||
} | ||
}), | ||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.