Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Feat: 데일리 기록 #34

Merged
merged 5 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ SPEC CHECKSUMS:
FirebaseFirestore: 21be9ea244830f6cac15464550c2975c43f9dffc
FirebaseFirestoreInternal: 7ac1e0c5b4e75aeb898dfe4b1d6d77abbac9eca3
FirebaseSharedSwift: 19b3f709993d6fa1d84941d41c01e3c4c11eab93
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_inappwebview: 3d32228f1304635e7c028b0d4252937730bbc6cf
flutter_localization: f43b18844a2b3d2c71fd64f04ffd6b1e64dd54d4
flutter_native_splash: 52501b97d1c0a5f898d687f1646226c1f93c56ef
Expand Down Expand Up @@ -966,4 +966,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 0b2c97823421f8b156b8e4753a469ac167670df8

COCOAPODS: 1.14.3
COCOAPODS: 1.15.2
1 change: 1 addition & 0 deletions lib/main_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MainApp extends StatelessWidget {
Routes.routes[1],
Routes.routes[2],
Routes.routes[3],
Routes.routes[4],
],
);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/utilities/app_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:earlips/views/home/home_screen.dart';
import 'package:earlips/views/profile/profile_account/profile_account_screen.dart';
import 'package:earlips/views/profile/profile_language_setting/profile_language_setting.dart';
import 'package:earlips/views/profile/profile_screen.dart';
import 'package:earlips/views/study/study_main.dart';
import 'package:get/get.dart';

abstract class Routes {
Expand All @@ -20,6 +21,10 @@ abstract class Routes {
name: HOME,
page: () => HomeScreen(),
),
GetPage(
name: STUDY,
page: () => const StudyMain(),
),
GetPage(
name: PROFILE,
page: () => const ProfileScreen(),
Expand Down
61 changes: 49 additions & 12 deletions lib/viewModels/study/date_study_screen_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,61 @@
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:intl/intl.dart';

class LearningSession {
final String type; // 세션 유형 (음소, 단어, 문장)
final DateTime createdDate; // 세션 생성 날짜
final int type; // 세션 유형 (음소, 단어, 문장)
final String createdDate; // 세션 생성 날짜
final String text; // 세션과 관련된 텍스트
final int index; // 세션 인덱스

LearningSession({required this.type, required this.createdDate, required this.text});
LearningSession(
{required this.type,
required this.createdDate,
required this.text,
required this.index});
}

class DateStudyViewModel {
final DateTime date;

final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;
DateStudyViewModel({required this.date});

List<LearningSession> getSessions() {
// 실제 애플리케이션에서는 여기서 날짜에 따라 데이터를 조회하거나 필터링하는 로직을 구현할 수 있습니다.
return [
LearningSession(type: '음소', createdDate: DateTime(2024, 2, 12), text: "가"),
LearningSession(type: '단어', createdDate: DateTime(2024, 2, 13), text: "가다"),
LearningSession(type: '문장', createdDate: DateTime(2024, 2, 14), text: "가다 보면 길이 있다."),
];
Future<List<LearningSession>> getSessions() async {
// 파이어스토어에 저장된 날짜 형식에 맞게 날짜를 변환
final uid = _auth.currentUser?.uid;

String formattedDate = DateFormat('yyyyMMdd').format(date);
try {
// 해당 날짜의 daily record 문서를 가져옴
DocumentSnapshot dailyRecordSnapshot = await _firestore
.collection('users')
.doc(uid)
.collection('daily_records')
.doc(formattedDate)
.get();

if (dailyRecordSnapshot.exists) {
// 해당 날짜의 세션 데이터를 가져옴
List<Map<String, dynamic>> sessionsData =
List<Map<String, dynamic>>.from(
dailyRecordSnapshot.get('wordsList') ?? []);
// 세션 데이터를 LearningSession 객체로 변환
List<LearningSession> sessions = sessionsData.map((session) {
return LearningSession(
type: session['type'],
createdDate: formattedDate,
text: session['word'],
index: session['index'],
);
}).toList();

return sessions;
} else {
return [];
}
} catch (error) {
return [];
}
}
}
53 changes: 51 additions & 2 deletions lib/viewModels/word/word_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,64 @@ 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,
'index': currentIndex.value
});

// 업데이트
transaction
.update(dailyRecordRef, {'wordsList': existingWordsList});
}
} else {
// 만약 daily record가 없다면 새로 생성
transaction.set(dailyRecordRef, {
'wordsList': [
{
'word': word.word,
'type': word.type,
'index': currentIndex.value
},
],
});
}
});
} 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 +157,6 @@ class WordViewModel extends GetxController {
});
}
} else {
print('different date!!!!!!!!!!!!!');
transaction.set(recordRef, {
'cnt': 1,
'date': currentDate,
Expand Down
25 changes: 11 additions & 14 deletions lib/views/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import 'package:firebase_auth/firebase_auth.dart';
import 'package:earlips/views/script/learning_session_screen.dart';
import '../realtime/real_create_script_screen.dart';

import 'package:earlips/viewModels/user/user_viewmodel.dart';


class HomeScreen extends BaseScreen<HomeViewModel> {
final User? user =
FirebaseAuth.instance.currentUser; // FirebaseFirestore 인스턴스 생성
Expand All @@ -40,7 +37,10 @@ class HomeScreen extends BaseScreen<HomeViewModel> {
child: Column(
children: [
HomeHeaderWidget(isLoggedIn: isLoggedIn, vm: viewModel),
_Top(isLoggedIn: isLoggedIn, vm: viewModel,),
_Top(
isLoggedIn: isLoggedIn,
vm: viewModel,
),
const _Middle(),
// 로그인 상태에 따라 _Bottom 클래스의 컨테이너 색상을 변경
_Bottom(isLoggedIn: isLoggedIn),
Expand All @@ -59,8 +59,6 @@ class _Top extends StatelessWidget {
final UserViewModel vm;
const _Top({required this.isLoggedIn, required this.vm});



@override
Widget build(BuildContext context) {
return Center(
Expand All @@ -75,8 +73,8 @@ class _Top extends StatelessWidget {
height: Get.height * 0.19,
child: Stack(
children: [
Row(children: [
_Circle(vm : vm),
Row(children: [
_Circle(vm: vm),
_SpeakingAbility(vm: vm),
]),
if (!isLoggedIn) // 로그인 안 됐을 때만 블러 효과와 자물쇠 아이콘 표시
Expand Down Expand Up @@ -228,10 +226,10 @@ class _Bottom extends StatelessWidget {
),
),
),
Container(
alignment: Alignment.centerLeft,
Container(
alignment: Alignment.centerLeft,
margin: const EdgeInsets.only(left: 15.0),
child: LineChartSample2()),
child: LineChartSample2()),
],
),
),
Expand Down Expand Up @@ -261,7 +259,7 @@ class _Bottom extends StatelessWidget {

class _Circle extends StatelessWidget {
final UserViewModel vm;
const _Circle({super.key, required this.vm});
const _Circle({super.key, required this.vm});

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -296,7 +294,7 @@ class _Circle extends StatelessWidget {

class _SpeakingAbility extends StatelessWidget {
final UserViewModel vm;
const _SpeakingAbility( {super.key, required this.vm});
const _SpeakingAbility({super.key, required this.vm});

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -351,4 +349,3 @@ class _SpeakingAbility extends StatelessWidget {
);
}
}

2 changes: 1 addition & 1 deletion lib/views/profile/profile_header_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ProfileHeader extends StatelessWidget {
child: Container(
color: ColorSystem.background,
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 48, 20, 24),
padding: const EdgeInsets.fromLTRB(20, 30, 20, 24),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expand Down
5 changes: 2 additions & 3 deletions lib/views/root/root_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ class RootScreen extends BaseScreen<RootViewModel> {
@override
Color? get screenBackgroundColor => const Color(0xFFAAA4F8);


@override
Widget buildBody(BuildContext context) {
return Obx(
() => IndexedStack(
index: viewModel.selectedIndex,
children: [
HomeScreen(),
StudyMain(),
ProfileScreen(),
const StudyMain(),
const ProfileScreen(),
],
),
);
Expand Down
Loading
Loading