From dba822da31b41b3003e4db1c633bbc20322c0d21 Mon Sep 17 00:00:00 2001 From: HuiChan Seo <78739194+seochan99@users.noreply.github.com> Date: Sun, 18 Feb 2024 17:53:51 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8Feat:=20user,=20word=20viewmodel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/viewModels/user/user_viewmodel.dart | 43 +++++++++++ lib/viewModels/word/word_viewmodel.dart | 94 +++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 lib/viewModels/word/word_viewmodel.dart diff --git a/lib/viewModels/user/user_viewmodel.dart b/lib/viewModels/user/user_viewmodel.dart index f4f4ca5..0dfa5b6 100644 --- a/lib/viewModels/user/user_viewmodel.dart +++ b/lib/viewModels/user/user_viewmodel.dart @@ -9,6 +9,40 @@ class UserViewModel extends GetxController { final FirebaseFirestore _firestore = FirebaseFirestore.instance; final storage = const FlutterSecureStorage(); // Instance for secure storage + // final List wordList = [ + // WordCard( + // id: 1, + // word: "강", + // speaker: "가-앙", + // video: "https://www.youtube.com/watch?v=OzHrIz-wMLA"), + // WordCard( + // id: 2, + // word: "서", + // speaker: "가-앙", + // video: "https://www.youtube.com/watch?v=OzHrIz-wMLA"), + // WordCard( + // id: 3, + // word: "희", + // speaker: "가-앙", + // video: "https://www.youtube.com/watch?v=OzHrIz-wMLA"), + // WordCard( + // id: 4, + // word: "찬", + // speaker: "차-안", + // video: "https://www.youtube.com/watch?v=OzHrIz-wMLA"), + // WordCard( + // id: 5, + // word: "캬", + // speaker: "캬", + // video: "https://www.youtube.com/watch?v=OzHrIz-wMLA"), + // ]; + + // for (final word in wordList) { + // await FirebaseFirestore.instance + // .collection('words') + // .doc(word.id.toString()) + // .set(word.toMap()); + // } // User Profile Data final Rx _firebaseUser = Rx(null); String? get uid => _firebaseUser.value?.uid; @@ -31,6 +65,15 @@ class UserViewModel extends GetxController { Future getUserData() async { if (uid != null) { + // // users 컬렉션의 하위 컬렉션에 단어 완료 정보 삽입 + // for (final word in wordList) { + // await FirebaseFirestore.instance + // .collection('users') + // .doc(uid) + // .collection('words') + // .doc(word.id.toString()) + // .set(UserWord(wordId: word.id, isDone: false).toMap()); + // } final doc = await _firestore.collection('users').doc(uid).get(); userData.value = doc.data() ?? {}; nickname.value = userData.value['nickname'] ?? ''; diff --git a/lib/viewModels/word/word_viewmodel.dart b/lib/viewModels/word/word_viewmodel.dart new file mode 100644 index 0000000..17260c7 --- /dev/null +++ b/lib/viewModels/word/word_viewmodel.dart @@ -0,0 +1,94 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:earlips/models/user_word_model.dart'; +import 'package:earlips/models/word_card_model.dart'; +import 'package:earlips/models/word_data_model.dart'; +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:get/get.dart'; +import 'package:intl/intl.dart'; + +class WordViewModel extends GetxController { + final FirebaseFirestore _firestore = FirebaseFirestore.instance; + final FirebaseAuth _auth = FirebaseAuth.instance; + + RxList wordList = RxList([]); + RxInt currentIndex = 0.obs; + + @override + void onInit() { + super.onInit(); + fetchWords(); + fetchWords(); + wordList.refresh(); + } + + void updateCurrentIndex(int index) { + currentIndex.value = index; + } + + // 단어 데이터 가져오기 + Future fetchWords() async { + final uid = _auth.currentUser?.uid; + if (uid != null) { + // 모든 단어 데이터 가져오기 + final wordsQuery = await _firestore.collection('words').get(); + final allWords = wordsQuery.docs + .map((doc) => WordCard.fromDocument(doc.data())) + .toList(); + + // 현재 사용자의 단어 데이터 가져오기 + final userWordsQuery = await _firestore + .collection('users') + .doc(uid) + .collection('words') + .get(); + final userWords = userWordsQuery.docs + .map((doc) => UserWord.fromDocument(doc.data())) + .toList(); + + // 모든 단어 데이터에 사용자의 단어 데이터를 추가 + wordList.value = allWords.map((word) { + final matchingUserWord = userWords + .firstWhereOrNull((userWord) => userWord.wordId == word.id); + + return WordData( + wordCard: word, + userWord: matchingUserWord, + ); + }).toList(); + update(); + } + } + + // 단어 완료 처리 + Future markWordAsDone(WordCard word) async { + final uid = _auth.currentUser?.uid; + if (uid != null) { + // Add or update data in the user's 'words' subcollection in Firestore + await _firestore + .collection('users') + .doc(uid) + .collection('words') + .doc(word.id.toString()) + .set({ + 'wordId': word.id, + 'isDone': true, + 'doneDate': DateFormat('yyyy/MM/dd').format(DateTime.now()), + }); + + final index = + wordList.indexWhere((element) => element.wordCard.id == word.id); + if (index != -1) { + // Handle the scenario where we don't find the card locally. + wordList[index] = WordData( + wordCard: word, + userWord: UserWord( + wordId: word.id, + isDone: true, + doneDate: DateFormat('yyyy/MM/dd').format(DateTime.now()), + ), + ); + wordList.refresh(); + } + } + } +}