diff --git a/presentation/src/main/java/com/kkkk/presentation/main/rhythm/RhythmFragment.kt b/presentation/src/main/java/com/kkkk/presentation/main/rhythm/RhythmFragment.kt index 91676ab..8a48cc1 100644 --- a/presentation/src/main/java/com/kkkk/presentation/main/rhythm/RhythmFragment.kt +++ b/presentation/src/main/java/com/kkkk/presentation/main/rhythm/RhythmFragment.kt @@ -293,10 +293,6 @@ class RhythmFragment : BaseFragment(R.layout.fragment_rhy override fun onSensorChanged(event: SensorEvent?) { if (event?.sensor?.type == Sensor.TYPE_STEP_DETECTOR) { viewModel.addStepCount(1) - - if (viewModel.stepCount.value % SPEED_CALC_INTERVAL == 0) { - calculateSpeed() - } } } @@ -323,18 +319,6 @@ class RhythmFragment : BaseFragment(R.layout.fragment_rhy } } - private fun calculateSpeed() { - val currentTime = System.currentTimeMillis() - val lastStepTime = viewModel.lastStepTime.value - if (lastStepTime != 0L) { - val timeDiff = currentTime - lastStepTime - val speed = (SPEED_CALC_INTERVAL / (timeDiff / 1000.0)) * 60 // 분당 걸음 수 - - viewModel.setSpeed(speed) - } - viewModel.setLastStepTime(currentTime) - } - override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {} companion object { diff --git a/presentation/src/main/java/com/kkkk/presentation/main/rhythm/RhythmViewModel.kt b/presentation/src/main/java/com/kkkk/presentation/main/rhythm/RhythmViewModel.kt index 3db12b0..0cb937b 100644 --- a/presentation/src/main/java/com/kkkk/presentation/main/rhythm/RhythmViewModel.kt +++ b/presentation/src/main/java/com/kkkk/presentation/main/rhythm/RhythmViewModel.kt @@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch import javax.inject.Inject +import kotlin.math.max @HiltViewModel class RhythmViewModel @@ -44,10 +45,7 @@ constructor( private val _stepCount = MutableStateFlow(0) val stepCount: StateFlow = _stepCount - private val _speed = MutableStateFlow(0.0) - - private val _lastStepTime = MutableStateFlow(0L) - val lastStepTime: StateFlow = _lastStepTime + private val _firstStepTime = MutableStateFlow(0L) init { initRhythmLevelFromDataStore() @@ -65,14 +63,6 @@ constructor( _stepCount.value += newStepCount } - fun setSpeed(newSpeed: Double) { - _speed.value = newSpeed - } - - fun setLastStepTime(newLastStepTime: Long) { - _lastStepTime.value = newLastStepTime - } - fun setTempRhythmLevel(level: Int) { isSubmitted = false tempRhythmLevel.value = level @@ -112,6 +102,7 @@ constructor( rhythmRepository.getRhythmWav(url) .onSuccess { _downloadWavState.value = UiState.Success(it) + _firstStepTime.value = System.currentTimeMillis() } .onFailure { _downloadWavState.value = UiState.Failure(it.message.toString()) @@ -120,10 +111,16 @@ constructor( } fun posRhythmRecordToSave() { + var accuracy = + (stepCount.value.toDouble() / (bpm / 60 * ((System.currentTimeMillis() - _firstStepTime.value) / 10000))) + if (accuracy > 1) { + accuracy = max(2 - accuracy, 0.0) + } + viewModelScope.launch { rhythmRepository.postRhythmRecord( RecordRequestModel( - _speed.value / (_stepCount.value / OnboardingViewModel.SPEED_CALC_INTERVAL + 1), + accuracy, 0, stepCount.value ) @@ -157,8 +154,7 @@ constructor( private fun resetStepInfo() { _stepCount.value = 0 - _speed.value = 0.0 - _lastStepTime.value = 0L + _firstStepTime.value = 0L } fun getBpmFromDataStore() = userRepository.getBpm() diff --git a/stempo/src/main/java/com/kkkk/stempo/presentation/home/HomeScreen.kt b/stempo/src/main/java/com/kkkk/stempo/presentation/home/HomeScreen.kt index 41b2fa6..4191b60 100644 --- a/stempo/src/main/java/com/kkkk/stempo/presentation/home/HomeScreen.kt +++ b/stempo/src/main/java/com/kkkk/stempo/presentation/home/HomeScreen.kt @@ -124,8 +124,6 @@ fun HomeScreen( KEY_RECORD, sideEffect.accuracy ) - - viewModel.resetStepInfo() } } } diff --git a/stempo/src/main/java/com/kkkk/stempo/presentation/home/HomeViewModel.kt b/stempo/src/main/java/com/kkkk/stempo/presentation/home/HomeViewModel.kt index ef61379..a39a3f8 100644 --- a/stempo/src/main/java/com/kkkk/stempo/presentation/home/HomeViewModel.kt +++ b/stempo/src/main/java/com/kkkk/stempo/presentation/home/HomeViewModel.kt @@ -31,8 +31,6 @@ class HomeViewModel @Inject constructor( private var vibrationJob: Job? = null - private val _speed = MutableStateFlow(0.0) - private val _firstStepTime = MutableStateFlow(0L) fun controlMusic() { @@ -58,10 +56,6 @@ class HomeViewModel @Inject constructor( _firstStepTime.value = System.currentTimeMillis() } - fun resetStepInfo() { - _speed.value = 0.0 - } - private fun stopVibration() { vibrationJob?.cancel() vibrationJob = null