diff --git a/app/src/main/java/com/yourssu/unscramble/presentation/MainViewModel.kt b/app/src/main/java/com/yourssu/unscramble/presentation/MainViewModel.kt index a138375..9e25fcb 100644 --- a/app/src/main/java/com/yourssu/unscramble/presentation/MainViewModel.kt +++ b/app/src/main/java/com/yourssu/unscramble/presentation/MainViewModel.kt @@ -4,9 +4,12 @@ import android.os.Handler import android.os.Looper import android.util.Log import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import dagger.hilt.android.scopes.ViewModelScoped import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.launch import java.util.Timer import java.util.TimerTask import javax.inject.Inject @@ -20,6 +23,8 @@ class MainViewModel @Inject constructor() : ViewModel() { private val _time: MutableStateFlow = MutableStateFlow(0) val time: StateFlow = _time + private val _formattedTime = MutableStateFlow("0:0") + val formattedTime: StateFlow = _formattedTime.asStateFlow() fun updateTime(time: Int) { _time.value = time } @@ -28,6 +33,15 @@ class MainViewModel @Inject constructor() : ViewModel() { _isEnd.value = false } + fun formattedTimer(cnt: Int) { + val minutes = cnt / 60 + val remainingSeconds = cnt % 60 + viewModelScope.launch { + _formattedTime.emit(String.format("%02d:%02d", minutes, remainingSeconds)) + } + + } + fun startTimer() { var cnt = _time.value @@ -35,19 +49,27 @@ class MainViewModel @Inject constructor() : ViewModel() { // 반복적으로 사용할 TimerTask val mTimerTask = object : TimerTask() { override fun run() { - val mHandler = Handler(Looper.getMainLooper()) - mHandler.postDelayed({ - // 반복실행할 구문 - cnt-- - Log.d("MainViewModel", "$cnt") - if (cnt <= 0) { - mTimer.cancel() - Log.d("MainViewModel", "타이머 종료") - _isEnd.value = true - Log.d("MainViewModel", isEnd.value.toString()) - - } - }, 0) + val mHandler = Handler(Looper.getMainLooper()) + + mHandler.postDelayed({ + + // 반복실행할 구문 + formattedTimer(cnt) + cnt-- + + Log.d("MainViewModel", "Formatted Time: ${_formattedTime.value}") + + if (cnt <= 0) { + mTimer.cancel() + Log.d("MainViewModel", "타이머 종료") + _isEnd.value = true + Log.d("MainViewModel", isEnd.value.toString()) + + + } + }, 0) + + } } mTimer.schedule(mTimerTask, 0, 1000) diff --git a/app/src/main/java/com/yourssu/unscramble/presentation/play/PlayFragment.kt b/app/src/main/java/com/yourssu/unscramble/presentation/play/PlayFragment.kt index 9d42990..e48ff8c 100644 --- a/app/src/main/java/com/yourssu/unscramble/presentation/play/PlayFragment.kt +++ b/app/src/main/java/com/yourssu/unscramble/presentation/play/PlayFragment.kt @@ -1,6 +1,7 @@ package com.yourssu.unscramble.presentation.play import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import androidx.fragment.app.activityViewModels @@ -31,6 +32,7 @@ class PlayFragment : BindFragment() { binding.lifecycleOwner = viewLifecycleOwner observeViewModel() + observeMainViewModel() viewModel.checkValid() observeNavigationToEnd() setupListeners() @@ -46,6 +48,26 @@ class PlayFragment : BindFragment() { } } + private fun observeMainViewModel() { + lifecycleScope.launch { + mainViewModel.startTimer() + + mainViewModel.isEnd + .collectLatest { isEnd -> + if (isEnd) { + findNavController().navigate(R.id.endFragment) + } + } + + mainViewModel.formattedTime + .collect { formattedTime -> + Log.d("play", formattedTime) //여기 로그 안나옴 + // UI 업데이트 + binding.tvPlayTime.text = formattedTime.toString() + } + } + } + private fun observeNavigationToEnd() { lifecycleScope.launch { viewModel.navigateToEnd.collect { navigateToEnd -> @@ -65,16 +87,5 @@ class PlayFragment : BindFragment() { viewModel.onPlayButtonClick() binding.etAnswer.text.clear() } - - mainViewModel.startTimer() - - viewLifecycleOwner.lifecycleScope.launch { - mainViewModel.isEnd - .collectLatest { isEnd -> - if (isEnd) { - findNavController().navigate(R.id.endFragment) - } - } - } } } diff --git a/app/src/main/java/com/yourssu/unscramble/presentation/play/PlayViewModel.kt b/app/src/main/java/com/yourssu/unscramble/presentation/play/PlayViewModel.kt index 461b094..817c4b7 100644 --- a/app/src/main/java/com/yourssu/unscramble/presentation/play/PlayViewModel.kt +++ b/app/src/main/java/com/yourssu/unscramble/presentation/play/PlayViewModel.kt @@ -28,11 +28,8 @@ class PlayViewModel @Inject constructor( private val _solvedProblem: MutableStateFlow = MutableStateFlow(1) val solvedProblem: StateFlow = _solvedProblem.asStateFlow() - private val _timerHour: MutableStateFlow = MutableStateFlow("00") - val timerHour: StateFlow = _timerHour.asStateFlow() - - private val _timerMinute: MutableStateFlow = MutableStateFlow("00") - val timerMinute: StateFlow = _timerMinute.asStateFlow() + private val _formattedTime = MutableStateFlow("00:00") + val formattedTime: StateFlow = _formattedTime.asStateFlow() // 유효성 판단에 따른 버튼 활성화 관련 코드 // 사용자가 입력한 답안 diff --git a/app/src/main/res/layout/fragment_play.xml b/app/src/main/res/layout/fragment_play.xml index 04adddc..13837df 100644 --- a/app/src/main/res/layout/fragment_play.xml +++ b/app/src/main/res/layout/fragment_play.xml @@ -13,39 +13,38 @@ + + - - - - + - - - - - - - - - - - - - - + - + + android:layout_height="wrap_content"> - + app:layout_constraintTop_toTopOf="parent"> + + + + + + + + - + + + app:layout_constraintTop_toBottomOf="@+id/et_answer"> + + + + + + + + + + - - -