Skip to content

Commit

Permalink
[FEAT/#49] change logic to get accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
chattymin committed Aug 29, 2024
1 parent 3ef9bd4 commit eb9eeb8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,6 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(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()
}
}
}

Expand All @@ -323,18 +319,6 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,10 +45,7 @@ constructor(
private val _stepCount = MutableStateFlow(0)
val stepCount: StateFlow<Int> = _stepCount

private val _speed = MutableStateFlow(0.0)

private val _lastStepTime = MutableStateFlow(0L)
val lastStepTime: StateFlow<Long> = _lastStepTime
private val _firstStepTime = MutableStateFlow(0L)

init {
initRhythmLevelFromDataStore()
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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
)
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ fun HomeScreen(
KEY_RECORD,
sideEffect.accuracy
)

viewModel.resetStepInfo()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down

0 comments on commit eb9eeb8

Please sign in to comment.