Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +23,8 @@ class MainViewModel @Inject constructor() : ViewModel() {
private val _time: MutableStateFlow<Int> = MutableStateFlow(0)
val time: StateFlow<Int> = _time

private val _formattedTime = MutableStateFlow("0:0")
val formattedTime: StateFlow<String> = _formattedTime.asStateFlow()
fun updateTime(time: Int) {
_time.value = time
}
Expand All @@ -28,26 +33,43 @@ 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

val mTimer = Timer()
// 반복적으로 사용할 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +32,7 @@ class PlayFragment : BindFragment<FragmentPlayBinding>() {
binding.lifecycleOwner = viewLifecycleOwner

observeViewModel()
observeMainViewModel()
viewModel.checkValid()
observeNavigationToEnd()
setupListeners()
Expand All @@ -46,6 +48,26 @@ class PlayFragment : BindFragment<FragmentPlayBinding>() {
}
}

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 ->
Expand All @@ -65,16 +87,5 @@ class PlayFragment : BindFragment<FragmentPlayBinding>() {
viewModel.onPlayButtonClick()
binding.etAnswer.text.clear()
}

mainViewModel.startTimer()

viewLifecycleOwner.lifecycleScope.launch {
mainViewModel.isEnd
.collectLatest { isEnd ->
if (isEnd) {
findNavController().navigate(R.id.endFragment)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ class PlayViewModel @Inject constructor(
private val _solvedProblem: MutableStateFlow<Int> = MutableStateFlow(1)
val solvedProblem: StateFlow<Int> = _solvedProblem.asStateFlow()

private val _timerHour: MutableStateFlow<String> = MutableStateFlow("00")
val timerHour: StateFlow<String> = _timerHour.asStateFlow()

private val _timerMinute: MutableStateFlow<String> = MutableStateFlow("00")
val timerMinute: StateFlow<String> = _timerMinute.asStateFlow()
private val _formattedTime = MutableStateFlow("00:00")
val formattedTime: StateFlow<String> = _formattedTime.asStateFlow()

// 유효성 판단에 따른 버튼 활성화 관련 코드
// 사용자가 입력한 답안
Expand Down
Loading