From 8412d17b087bbb90381a307debb49cb5e89fcadc Mon Sep 17 00:00:00 2001 From: HuiChan Seo <78739194+seochan99@users.noreply.github.com> Date: Wed, 21 Feb 2024 11:32:43 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8Feat:=20=EB=85=B9=EC=9D=8C=20-=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/viewModels/record/record_viewmodel.dart | 4 +- .../word/widget/word_sentence_widget.dart | 327 +++++++++--------- 2 files changed, 164 insertions(+), 167 deletions(-) diff --git a/lib/viewModels/record/record_viewmodel.dart b/lib/viewModels/record/record_viewmodel.dart index 729fb45..d12cf9b 100644 --- a/lib/viewModels/record/record_viewmodel.dart +++ b/lib/viewModels/record/record_viewmodel.dart @@ -29,7 +29,6 @@ class RecordViewModel extends GetxController { } Future _startRecording() async { - if (!_isRecorderInitialized || isRecording.value) return; if (!_audioRecorder.isStopped) { await _audioRecorder.closeRecorder(); } @@ -38,8 +37,10 @@ class RecordViewModel extends GetxController { await _audioRecorder.openRecorder(); _isRecorderInitialized = true; final directory = await getApplicationDocumentsDirectory(); + final filePath = '${directory.path}/${DateTime.now().millisecondsSinceEpoch}.aac'; + print(filePath); try { await _audioRecorder.startRecorder( toFile: filePath, @@ -47,6 +48,7 @@ class RecordViewModel extends GetxController { ); isRecording.value = true; + print('Recording started'); } catch (_) {} } diff --git a/lib/views/word/widget/word_sentence_widget.dart b/lib/views/word/widget/word_sentence_widget.dart index 881a97e..3e47ee3 100644 --- a/lib/views/word/widget/word_sentence_widget.dart +++ b/lib/views/word/widget/word_sentence_widget.dart @@ -7,7 +7,7 @@ import 'package:get/get.dart'; // Import GetX library class WordSentenceWidget extends StatelessWidget { final List wordDataList; final int type; - // pageController + PageController pageController = PageController(); WordSentenceWidget( @@ -18,184 +18,179 @@ class WordSentenceWidget extends StatelessWidget { @override Widget build(BuildContext context) { - Get.put(RecordViewModel()); // Ensure RecordViewmodel is initialized - final wordViewModel = Get.find(); // Access your ViewModel! + Get.put(RecordViewModel()); + final wordViewModel = Get.find(); return GetBuilder( builder: (RecordViewModel model) { return Center( child: Column( children: [ - Positioned( - bottom: 20, - left: 0, - right: 0, - child: Align( - alignment: Alignment.bottomCenter, - child: Ink( - decoration: BoxDecoration( - color: model.isRecording.value ? Colors.red : Colors.blue, - borderRadius: BorderRadius.circular(40), - ), - child: InkWell( - borderRadius: BorderRadius.circular(40), - onTap: () async { - if (model.isRecording.value) { - // 녹음 토글 버튼 - model.toggleRecording(); + Align( + alignment: Alignment.bottomCenter, + child: Ink( + decoration: BoxDecoration( + color: model.isRecording.value ? Colors.red : Colors.blue, + borderRadius: BorderRadius.circular(40), + ), + child: InkWell( + borderRadius: BorderRadius.circular(40), + onTap: () async { + if (model.isRecording.value) { + // 녹음 토글 버튼 + print('녹음 종료'); + model.toggleRecording(); - await model.sendTextAndAudio( - wordDataList[wordViewModel.currentIndex.value] - .wordCard - .word, - type); - // Handle the response here, e.g., show it in a dialog - Get.dialog( - // height - AlertDialog( - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(20), - ), - title: const Text('결과'), - content: GetBuilder( - builder: (model) => Column( - children: [ - Text( - '발음: ${model.response['pronunciation']}', - ), - Text( - '정확도: ${model.response['similarity']}', - ), - ], - ), + await model.sendTextAndAudio( + wordDataList[wordViewModel.currentIndex.value] + .wordCard + .word, + type); + // Handle the response here, e.g., show it in a dialog + Get.dialog( + // height + AlertDialog( + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(20), + ), + title: const Text('결과'), + content: GetBuilder( + builder: (model) => Column( + children: [ + Text( + '발음: ${model.response['pronunciation']}', + ), + Text( + '정확도: ${model.response['similarity']}', + ), + ], ), - actions: [ - TextButton( - onPressed: () { - Get.back(); - if (model.response['similarity'] == null) { - return; - } - if (model.response['similarity'] >= 80) { - Get.dialog( - AlertDialog( - title: const Text('학습 완료'), - content: const Text( - '다음으로 넘어가려면 아래 버튼을 눌러주세요.'), - actions: [ - ElevatedButton( - onPressed: () async { - await wordViewModel - .markWordAsDone(wordViewModel - .wordList[wordViewModel - .currentIndex.value] - .wordCard); - wordViewModel.currentIndex - .value < - wordViewModel.wordList - .length - - 1 - ? Get.back() - : Get.offAllNamed('/'); - - // 다음 단어로 넘어가기 - if (wordViewModel - .currentIndex.value < - wordViewModel - .wordList.length - - 1) { - pageController.animateToPage( - wordViewModel.currentIndex - .value + - 1, - duration: const Duration( - milliseconds: 300), - curve: Curves.ease, - ); + ), + actions: [ + TextButton( + onPressed: () { + Get.back(); + if (model.response['similarity'] == null) { + return; + } + if (model.response['similarity'] >= 80) { + Get.dialog( + AlertDialog( + title: const Text('학습 완료'), + content: const Text( + '다음으로 넘어가려면 아래 버튼을 눌러주세요.'), + actions: [ + ElevatedButton( + onPressed: () async { + await wordViewModel + .markWordAsDone(wordViewModel + .wordList[wordViewModel + .currentIndex.value] + .wordCard); + wordViewModel.currentIndex.value < + wordViewModel + .wordList.length - + 1 + ? Get.back() + : Get.offAllNamed('/'); - // currentIndex 증가 - wordViewModel.currentIndex - .value = wordViewModel + // 다음 단어로 넘어가기 + if (wordViewModel + .currentIndex.value < + wordViewModel + .wordList.length - + 1) { + pageController.animateToPage( + wordViewModel .currentIndex.value + - 1; - } - }, - child: const Text('다음 단어'), - ), - ], - ), - ); - } else { - // 80 미만일 경우 다른 AlertDialog를 표시 다시하기 혹은 다음으로 - Get.dialog( - AlertDialog( - title: const Text('학습 실패'), - content: const Text('다시 한번 녹음해주세요.'), - actions: [ - ElevatedButton( - onPressed: () async { - Get.back(); - }, - child: const Text('다시하기'), - ), - ElevatedButton( - onPressed: () async { - Get.back(); - // 마지막 단어가 아닐 경우 뒤로가기, 마지막 단어일 경우 홈으로 이동 - wordViewModel.currentIndex - .value < - wordViewModel.wordList - .length - - 1 - ? Get.back() - : Get.offAllNamed('/'); + 1, + duration: const Duration( + milliseconds: 300), + curve: Curves.ease, + ); - // 다음 단어로 넘어가기 - if (wordViewModel - .currentIndex.value < - wordViewModel - .wordList.length - - 1) { - pageController.animateToPage( - wordViewModel.currentIndex - .value + - 1, - duration: const Duration( - milliseconds: 300), - curve: Curves.ease, - ); + // currentIndex 증가 + wordViewModel.currentIndex + .value = wordViewModel + .currentIndex.value + + 1; + } + }, + child: const Text('다음 단어'), + ), + ], + ), + ); + } else { + // 80 미만일 경우 다른 AlertDialog를 표시 다시하기 혹은 다음으로 + Get.dialog( + AlertDialog( + title: const Text('학습 실패'), + content: const Text('다시 한번 녹음해주세요.'), + actions: [ + ElevatedButton( + onPressed: () async { + Get.back(); + }, + child: const Text('다시하기'), + ), + ElevatedButton( + onPressed: () async { + Get.back(); + // 마지막 단어가 아닐 경우 뒤로가기, 마지막 단어일 경우 홈으로 이동 + wordViewModel.currentIndex.value < + wordViewModel + .wordList.length - + 1 + ? Get.back() + : Get.offAllNamed('/'); - // currentIndex 증가 - wordViewModel.currentIndex - .value = wordViewModel + // 다음 단어로 넘어가기 + if (wordViewModel + .currentIndex.value < + wordViewModel + .wordList.length - + 1) { + pageController.animateToPage( + wordViewModel .currentIndex.value + - 1; - } - }, - child: const Text('다음 단어'), - ), - ], - ), - ); - } - }, - child: const Text('OK'), - ), - ], - ), - ); - } else { - // Start recording - model.toggleRecording(); - } - }, - child: Padding( - padding: const EdgeInsets.all(20), - child: Icon( - model.isRecording.value ? Icons.stop : Icons.mic, - size: 30, - color: Colors.white, - ), + 1, + duration: const Duration( + milliseconds: 300), + curve: Curves.ease, + ); + + // currentIndex 증가 + wordViewModel.currentIndex + .value = wordViewModel + .currentIndex.value + + 1; + } + }, + child: const Text('다음 단어'), + ), + ], + ), + ); + } + }, + child: const Text('OK'), + ), + ], + ), + ); + } else { + // Start recording + print('녹음 시작'); + model.toggleRecording(); + } + }, + child: Padding( + padding: const EdgeInsets.all(20), + child: Icon( + model.isRecording.value ? Icons.stop : Icons.mic, + size: 30, + color: Colors.white, ), ), ),