-
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 #27 from seochan99/feature/profile
✨Feat: 음소 교정 연동 및 구현
- Loading branch information
Showing
12 changed files
with
445 additions
and
171 deletions.
There are no files selected for viewing
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
Empty file.
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,27 @@ | ||
class UserWord { | ||
final int wordId; | ||
final bool isDone; | ||
final String? doneDate; | ||
|
||
UserWord({ | ||
required this.wordId, | ||
required this.isDone, | ||
this.doneDate, | ||
}); | ||
|
||
Map<String, dynamic> toMap() { | ||
return { | ||
'wordId': wordId, | ||
'isDone': isDone, | ||
'doneDate': doneDate, | ||
}; | ||
} | ||
|
||
factory UserWord.fromDocument(Map<String, dynamic> doc) { | ||
return UserWord( | ||
wordId: doc['wordId'], | ||
isDone: doc['isDone'], | ||
doneDate: doc['doneDate'], | ||
); | ||
} | ||
} |
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,47 @@ | ||
class WordCard { | ||
final int id; | ||
final String word; | ||
final String speaker; | ||
final String video; | ||
|
||
WordCard({ | ||
required this.id, | ||
required this.word, | ||
required this.speaker, | ||
required this.video, | ||
}); | ||
|
||
Map<String, dynamic> toMap() { | ||
return { | ||
'id': id, | ||
'word': word, | ||
'speaker': speaker, | ||
'video': video, | ||
}; | ||
} | ||
|
||
factory WordCard.fromDocument(Map<String, dynamic> doc) { | ||
return WordCard( | ||
id: doc['id'], | ||
word: doc['word'], | ||
speaker: doc['speaker'], | ||
video: doc['video'], | ||
); | ||
} | ||
|
||
WordCard copyWith({ | ||
int? id, | ||
String? word, | ||
String? speaker, | ||
bool? isDone, | ||
String? doneDate, | ||
String? video, | ||
}) { | ||
return WordCard( | ||
id: id ?? this.id, | ||
word: word ?? this.word, | ||
speaker: speaker ?? this.speaker, | ||
video: video ?? this.video, | ||
); | ||
} | ||
} |
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,12 @@ | ||
import 'package:earlips/models/user_word_model.dart'; | ||
import 'package:earlips/models/word_card_model.dart'; | ||
|
||
class WordData { | ||
final WordCard wordCard; | ||
final UserWord? userWord; | ||
|
||
WordData({ | ||
required this.wordCard, | ||
this.userWord, | ||
}); | ||
} |
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
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,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(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.