Skip to content

Commit

Permalink
✨Feat: dailyWords 연동
Browse files Browse the repository at this point in the history
  • Loading branch information
seochan99 committed Feb 19, 2024
1 parent 12416c9 commit eda6bcc
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
51 changes: 49 additions & 2 deletions lib/viewModels/word/word_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,62 @@ 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<Map<String, dynamic>> existingWordsList =
List<Map<String, dynamic>>.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
.collection('users')
.doc(uid)
.collection('records')
.doc(DateFormat('yyyyMMdd').format(DateTime.now()));

try {
await _firestore.runTransaction((transaction) async {
// Get the current record
Expand All @@ -107,7 +155,6 @@ class WordViewModel extends GetxController {
});
}
} else {
print('different date!!!!!!!!!!!!!');
transaction.set(recordRef, {
'cnt': 1,
'date': currentDate,
Expand Down
28 changes: 28 additions & 0 deletions lib/views/study/widget/study_row_card_widget.dart
Original file line number Diff line number Diff line change
@@ -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,
),
),
);
}
}

0 comments on commit eda6bcc

Please sign in to comment.