Skip to content

Commit

Permalink
✨Feat: 카드 type 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
seochan99 committed Feb 18, 2024
1 parent 8e68655 commit 904fc4a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 23 deletions.
8 changes: 6 additions & 2 deletions lib/models/word_card_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ class WordCard {
final String word;
final String speaker;
final String video;
final int type;

WordCard({
required this.id,
required this.word,
required this.speaker,
required this.video,
required this.type,
});

Map<String, dynamic> toMap() {
Expand All @@ -17,6 +19,7 @@ class WordCard {
'word': word,
'speaker': speaker,
'video': video,
'type': type,
};
}

Expand All @@ -26,22 +29,23 @@ class WordCard {
word: doc['word'],
speaker: doc['speaker'],
video: doc['video'],
type: doc['type'],
);
}

WordCard copyWith({
int? id,
String? word,
String? speaker,
bool? isDone,
String? doneDate,
String? video,
int? type,
}) {
return WordCard(
id: id ?? this.id,
word: word ?? this.word,
speaker: speaker ?? this.speaker,
video: video ?? this.video,
type: type ?? this.type,
);
}
}
14 changes: 10 additions & 4 deletions lib/viewModels/word/word_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import 'package:intl/intl.dart';
class WordViewModel extends GetxController {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;
final int type;

RxList<WordData> wordList = RxList<WordData>([]);
RxInt currentIndex = 0.obs;

WordViewModel({required this.type});

@override
void onInit() {
super.onInit();
fetchWords();
fetchWords();
fetchWords(type);
wordList.refresh();
}

Expand All @@ -26,11 +28,15 @@ class WordViewModel extends GetxController {
}

// 단어 데이터 가져오기
Future<void> fetchWords() async {
Future<void> fetchWords(int type) async {
final uid = _auth.currentUser?.uid;
if (uid != null) {
// 모든 단어 데이터 가져오기
final wordsQuery = await _firestore.collection('words').get();
final wordsQuery = await _firestore
.collection('words')
.where('type', isEqualTo: type)
.get();

final allWords = wordsQuery.docs
.map((doc) => WordCard.fromDocument(doc.data()))
.toList();
Expand Down
16 changes: 9 additions & 7 deletions lib/views/word/widget/word_list_widget.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:earlips/models/word_card_model.dart';
import 'package:earlips/models/word_data_model.dart';
import 'package:earlips/utilities/style/color_styles.dart';
import 'package:earlips/viewModels/word/word_viewmodel.dart';
Expand All @@ -7,11 +6,15 @@ import 'package:get/get.dart';

class WordList extends StatefulWidget {
final List<WordData> wordDataList;
final int type;

PageController pageController;

WordList(
{super.key, required this.wordDataList, required this.pageController});
{super.key,
required this.wordDataList,
required this.pageController,
required this.type});

@override
_WordListState createState() => _WordListState();
Expand All @@ -38,7 +41,6 @@ class _WordListState extends State<WordList> {
setState(() {
wordViewModel.currentIndex.value = index;
});
print('currentIndex: ${wordViewModel.currentIndex.value}');
},
itemBuilder: (context, index) {
final wordData = widget.wordDataList[index];
Expand Down Expand Up @@ -102,17 +104,17 @@ class _WordListState extends State<WordList> {
tileColor: Colors.white,
title: Text(
wordData.wordCard.word,
style: const TextStyle(
fontSize: 24,
style: TextStyle(
fontSize: widget.type == 2 ? 19 : 24,
fontWeight: FontWeight.w600,
color: ColorSystem.black,
),
textAlign: TextAlign.center,
),
subtitle: Text(
wordData.wordCard.speaker,
style: const TextStyle(
fontSize: 16,
style: TextStyle(
fontSize: widget.type == 2 ? 16 : 20,
fontWeight: FontWeight.w600,
color: ColorSystem.gray4,
),
Expand Down
67 changes: 57 additions & 10 deletions lib/views/word/word_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class WordScreen extends StatelessWidget {

@override
Widget build(BuildContext context) {
final wordViewModel = Get.put(WordViewModel());
final wordViewModel = Get.put(WordViewModel(
type: type,
));

final PageController pageController =
PageController(initialPage: wordViewModel.currentIndex.value);
Expand Down Expand Up @@ -43,6 +45,7 @@ class WordScreen extends StatelessWidget {
builder: (controller) => WordList(
// viewmodel
wordDataList: controller.wordList,
type: type,
pageController: pageController,
),
),
Expand All @@ -67,20 +70,40 @@ class WordScreen extends StatelessWidget {
],
),
),

const Spacer(),
// wordViewModel final String video로 영상 유튜브 링크를 바로 볼 수 있게 하기
// const Padding(
// padding: EdgeInsets.all(20.0),
// child: YoutubeWordPlayer(),
// ),
const Spacer(),
// wordViewModel final String video로 영상 유튜브 링크를 바로 볼 수 있게 하기
ElevatedButton(
onPressed: () {
Get.toNamed(
'/video',
arguments: wordViewModel
.wordList[wordViewModel.currentIndex.value]
.wordCard
.video,
onPressed: () async {
await wordViewModel.markWordAsDone(wordViewModel
.wordList[wordViewModel.currentIndex.value].wordCard);

// YouTubePlayer 위젯 추가
Get.dialog(
AlertDialog(
title: const Text('동영상 재생'),
content: Column(
children: [
YoutubePlayer(
controller: YoutubePlayerController(
initialVideoId: wordViewModel
.wordList[wordViewModel.currentIndex.value]
.wordCard
.video
.split('v=')[1],
),
),
],
),
),
);
},
child: const Text("영상 보기"),
child: const Text(""),
),
ElevatedButton(
onPressed: () async {
Expand Down Expand Up @@ -121,3 +144,27 @@ class WordScreen extends StatelessWidget {
);
}
}

class YoutubeWordPlayer extends StatefulWidget {
const YoutubeWordPlayer({super.key});

@override
State<YoutubeWordPlayer> createState() => _YoutubeWordPlayerState();
}

class _YoutubeWordPlayerState extends State<YoutubeWordPlayer> {
final wordViewModel = Get.find<WordViewModel>();

@override
Widget build(BuildContext context) {
print(
'video: ${wordViewModel.wordList[wordViewModel.currentIndex.value].wordCard.video}');
return YoutubePlayer(
controller: YoutubePlayerController(
initialVideoId: wordViewModel
.wordList[wordViewModel.currentIndex.value].wordCard.video
.split('v=')[1],
),
);
}
}

0 comments on commit 904fc4a

Please sign in to comment.