From eda6bcc08326432c96c81ffd3e92b2cf338da001 Mon Sep 17 00:00:00 2001 From: HuiChan Seo <78739194+seochan99@users.noreply.github.com> Date: Mon, 19 Feb 2024 20:39:05 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8Feat:=20dailyWords=20=EC=97=B0?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/viewModels/word/word_viewmodel.dart | 51 ++++++++++++++++++- .../study/widget/study_row_card_widget.dart | 28 ++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 lib/views/study/widget/study_row_card_widget.dart diff --git a/lib/viewModels/word/word_viewmodel.dart b/lib/viewModels/word/word_viewmodel.dart index 65d8553..cc26767 100644 --- a/lib/viewModels/word/word_viewmodel.dart +++ b/lib/viewModels/word/word_viewmodel.dart @@ -86,6 +86,55 @@ class WordViewModel extends GetxController { 'isDone': true, 'doneDate': currentDate, }); + // daily record 업데이트 + DocumentReference dailyRecordRef = _firestore + .collection('users') + .doc(uid) + .collection('daily_records') + .doc(DateFormat('yyyyMMdd').format(DateTime.now())); + + try { + await _firestore.runTransaction((transaction) async { + // 현재 daily record 가져오기 + DocumentSnapshot recordSnapshot = + await transaction.get(dailyRecordRef); + + // 만약 단어가 이미 있는지 확인 + if (recordSnapshot.exists) { + // 기존 단어 리스트 가져오기 + List> existingWordsList = + List>.from( + recordSnapshot.get('wordsList') ?? []); + + // 이미 있는 단어인지 확인 + if (existingWordsList + .any((element) => element['word'] == word.word)) { + // 만약 이미 있는 단어라면 return + return; + } else { + // 만약 없는 단어라면 추가 + existingWordsList.add({ + 'word': word.word, + 'type': word.type, + }); + + // 업데이트 + transaction + .update(dailyRecordRef, {'wordsList': existingWordsList}); + } + } else { + // If the document doesn't exist, create a new one with the initial word + transaction.set(dailyRecordRef, { + 'wordsList': [ + { + 'word': word.word, + 'type': word.type, + }, + ], + }); + } + }); + } catch (_) {} // 유저 record 업데이트 DocumentReference recordRef = _firestore @@ -93,7 +142,6 @@ class WordViewModel extends GetxController { .doc(uid) .collection('records') .doc(DateFormat('yyyyMMdd').format(DateTime.now())); - try { await _firestore.runTransaction((transaction) async { // Get the current record @@ -107,7 +155,6 @@ class WordViewModel extends GetxController { }); } } else { - print('different date!!!!!!!!!!!!!'); transaction.set(recordRef, { 'cnt': 1, 'date': currentDate, diff --git a/lib/views/study/widget/study_row_card_widget.dart b/lib/views/study/widget/study_row_card_widget.dart new file mode 100644 index 0000000..c21cfc9 --- /dev/null +++ b/lib/views/study/widget/study_row_card_widget.dart @@ -0,0 +1,28 @@ +import 'package:flutter/material.dart'; + +class SmallCard extends StatelessWidget { + final String name; + + const SmallCard({super.key, required this.name}); + + @override + Widget build(BuildContext context) { + return Container( + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(4.0), + color: const Color(0xFF1FA9DC), + ), + alignment: Alignment.center, + width: 50, + height: 24, + child: Text( + name, + style: const TextStyle( + color: Colors.white, + fontSize: 14, + fontWeight: FontWeight.w600, + ), + ), + ); + } +}