Skip to content

Commit

Permalink
✨Feat: user, word viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
seochan99 committed Feb 18, 2024
1 parent 83b35e5 commit dba822d
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/viewModels/user/user_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@ class UserViewModel extends GetxController {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final storage = const FlutterSecureStorage(); // Instance for secure storage

// final List<WordCard> 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<User?> _firebaseUser = Rx<User?>(null);
String? get uid => _firebaseUser.value?.uid;
Expand All @@ -31,6 +65,15 @@ class UserViewModel extends GetxController {

Future<void> 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'] ?? '';
Expand Down
94 changes: 94 additions & 0 deletions lib/viewModels/word/word_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -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<WordData> wordList = RxList<WordData>([]);
RxInt currentIndex = 0.obs;

@override
void onInit() {
super.onInit();
fetchWords();
fetchWords();
wordList.refresh();
}

void updateCurrentIndex(int index) {
currentIndex.value = index;
}

// 단어 데이터 가져오기
Future<void> 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<void> 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();
}
}
}
}

0 comments on commit dba822d

Please sign in to comment.