Skip to content

Commit

Permalink
✨ feat : records
Browse files Browse the repository at this point in the history
  • Loading branch information
seochan99 committed Feb 18, 2024
1 parent 5f7a95b commit 8b4a9a1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
42 changes: 39 additions & 3 deletions lib/viewModels/word/word_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ class WordViewModel extends GetxController {
// 단어 완료 처리
Future<void> markWordAsDone(WordCard word) async {
final uid = _auth.currentUser?.uid;
String currentDate = DateFormat('yyyy/MM/dd').format(DateTime.now());

if (uid != null) {
// Add or update data in the user's 'words' subcollection in Firestore
// 유저 단어 데이터 업데이트
await _firestore
.collection('users')
.doc(uid)
Expand All @@ -78,13 +80,47 @@ class WordViewModel extends GetxController {
.set({
'wordId': word.id,
'isDone': true,
'doneDate': DateFormat('yyyy/MM/dd').format(DateTime.now()),
'doneDate': currentDate,
});

// 유저 record 업데이트
DocumentReference recordRef = _firestore
.collection('users')
.doc(uid)
.collection('records')
.doc(DateFormat('yyyyMMdd').format(DateTime.now()));

try {
await _firestore.runTransaction((transaction) async {
// Get the current record
DocumentSnapshot recordSnapshot = await transaction.get(recordRef);

if (recordSnapshot.exists) {
String existingDate = recordSnapshot.get('date');
if (existingDate == currentDate) {
transaction.update(recordRef, {
'cnt': FieldValue.increment(1),
});
}
} else {
print('different date!!!!!!!!!!!!!');
transaction.set(recordRef, {
'cnt': 1,
'date': currentDate,
'dateFormat': DateTime.now(),
});
}
});
} catch (e) {
print("Transaction failed: $e");
}
// 로컬에서 단어 데이터 업데이트
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(
Expand Down
8 changes: 4 additions & 4 deletions lib/views/word/word_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ class WordScreen extends StatelessWidget {
),
const Spacer(),
// wordViewModel final String video로 영상 유튜브 링크를 바로 볼 수 있게 하기
// const Padding(
// padding: EdgeInsets.all(20.0),
// child: YoutubeWordPlayer(),
// ),
const Padding(
padding: EdgeInsets.all(20.0),
// child: YoutubeWordPlayer(),
),
const Spacer(),
// wordViewModel final String video로 영상 유튜브 링크를 바로 볼 수 있게 하기
ElevatedButton(
Expand Down

0 comments on commit 8b4a9a1

Please sign in to comment.