Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat/#25] onboarding / bpm level 저장 #26

Merged
merged 4 commits into from
Jul 30, 2024
Merged
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
1 change: 1 addition & 0 deletions data/src/main/java/com/kkkk/data/local/UserSharedPref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface UserSharedPref {
var accessToken: String
var refreshToken: String
var deviceToken: String
var bpmLevel: Int

fun clearInfo()
}
48 changes: 26 additions & 22 deletions data/src/main/java/com/kkkk/data/local/UserSharedPrefImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,34 @@ import androidx.core.content.edit
import javax.inject.Inject

class UserSharedPrefImpl
@Inject
constructor(
private val dataStore: SharedPreferences,
) : UserSharedPref {
override var accessToken: String
get() = dataStore.getString(ACCESS_TOKEN, "").orEmpty()
set(value) = dataStore.edit { putString(ACCESS_TOKEN, value) }
@Inject
constructor(
private val dataStore: SharedPreferences,
) : UserSharedPref {
override var accessToken: String
get() = dataStore.getString(ACCESS_TOKEN, "").orEmpty()
set(value) = dataStore.edit { putString(ACCESS_TOKEN, value) }

override var refreshToken: String
get() = dataStore.getString(REFRESH_TOKEN, "").orEmpty()
set(value) = dataStore.edit { putString(REFRESH_TOKEN, value) }
override var refreshToken: String
get() = dataStore.getString(REFRESH_TOKEN, "").orEmpty()
set(value) = dataStore.edit { putString(REFRESH_TOKEN, value) }

override var deviceToken: String
get() = dataStore.getString(DEVICE_TOKEN, "").orEmpty()
set(value) = dataStore.edit { putString(DEVICE_TOKEN, value) }
override var deviceToken: String
get() = dataStore.getString(DEVICE_TOKEN, "").orEmpty()
set(value) = dataStore.edit { putString(DEVICE_TOKEN, value) }

override fun clearInfo() {
dataStore.edit().clear().apply()
}
override var bpmLevel: Int
get() = dataStore.getInt(BPM_LEVEL, 1)
set(value) = dataStore.edit { putInt(BPM_LEVEL, value) }

companion object {
private const val ACCESS_TOKEN = "ACCESS_TOKEN"
private const val REFRESH_TOKEN = "REFRESH_TOKEN"
private const val DEVICE_TOKEN = "DEVICE_TOKEN"
private const val USER_ID = "USER_ID"
}
override fun clearInfo() {
dataStore.edit().clear().apply()
}

companion object {
private const val ACCESS_TOKEN = "ACCESS_TOKEN"
private const val REFRESH_TOKEN = "REFRESH_TOKEN"
private const val DEVICE_TOKEN = "DEVICE_TOKEN"
private const val BPM_LEVEL = "BPM_LEVEL"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ constructor(

override fun getRefreshToken(): String = userSharedPref.refreshToken

override fun getBpmLevel(): Int = userSharedPref.bpmLevel

override fun getDeviceToken(): String = userSharedPref.deviceToken

override fun setTokens(
accessToken: String,
refreshToken: String,
Expand All @@ -21,7 +25,9 @@ constructor(
userSharedPref.refreshToken = refreshToken
}

override fun getDeviceToken(): String = userSharedPref.deviceToken
override fun setBpmLevel(bpmLevel: Int) {
userSharedPref.bpmLevel = bpmLevel
}

override fun setDeviceToken(deviceToken: String) {
userSharedPref.deviceToken = deviceToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ interface UserRepository {

fun getRefreshToken(): String

fun getBpmLevel(): Int

fun setTokens(
accessToken: String,
refreshToken: String,
)

fun setBpmLevel(bpmLevel: Int)

fun getDeviceToken(): String

fun setDeviceToken(deviceToken: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import androidx.lifecycle.lifecycleScope
import com.kkkk.core.base.BaseActivity
import com.kkkk.core.extension.navigateToScreenClear
import com.kkkk.presentation.main.MainActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kr.genti.presentation.R
import kr.genti.presentation.databinding.ActivityOnboardingBinding

@AndroidEntryPoint
class OnboardingActivity : BaseActivity<ActivityOnboardingBinding>(R.layout.activity_onboarding) {
private lateinit var timer: CountDownTimer
private val viewModel by viewModels<OnboardingViewModel>()
Expand Down Expand Up @@ -49,11 +51,14 @@ class OnboardingActivity : BaseActivity<ActivityOnboardingBinding>(R.layout.acti
}

private fun startTimer() {
timer = object : CountDownTimer(60000, 1000) {
timer = object : CountDownTimer(TIME, INTERVAL) {
override fun onTick(millisUntilFinished: Long) {}

override fun onFinish() {
viewModel.setState(OnboardingState.END)
with(viewModel) {
setBpmLevel()
setState(OnboardingState.END)
}
}
}.start()
}
Expand All @@ -70,4 +75,9 @@ class OnboardingActivity : BaseActivity<ActivityOnboardingBinding>(R.layout.acti
replace<T>(R.id.fcv_onboarding, T::class.java.canonicalName)
}
}

companion object {
private const val TIME = 60000L
private const val INTERVAL = 1000L
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.kkkk.presentation.onboarding.onbarding

import androidx.lifecycle.ViewModel
import com.kkkk.domain.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject

@HiltViewModel
class OnboardingViewModel @Inject constructor(
private val userRepository: UserRepository
) : ViewModel() {
private val _state = MutableStateFlow(OnboardingState.START)
val state: StateFlow<OnboardingState> = _state
Expand All @@ -16,7 +18,6 @@ class OnboardingViewModel @Inject constructor(
val stepCount: StateFlow<Int> = _stepCount

private val _speed = MutableStateFlow(0f)
val speed: StateFlow<Float> = _speed

private val _lastStepTime = MutableStateFlow(0L)
val lastStepTime: StateFlow<Long> = _lastStepTime
Expand All @@ -37,6 +38,22 @@ class OnboardingViewModel @Inject constructor(
_state.value = newState
}

fun setBpmLevel() {
val level = when (_speed.value / (_stepCount.value / SPEED_CALC_INTERVAL)) {
in 55f..65f -> 2
in 65f..75f -> 3
in 75f..85f -> 4
in 85f..95f -> 5
in 95f..105f -> 6
in 15f..115f -> 7
in 115f..125f -> 8
in 125f..Float.MAX_VALUE -> 9
else -> 1
}

userRepository.setBpmLevel(level)
}
Comment on lines +41 to +55
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else로 1을 넣은 이유는 분모가 0이 될 수 있는 가능성 때문입니다.
1분간 사용자가 만약 10회 이하의 걸음을 걸었다면 분모가 0이 되어 무한에 가까운 숫자가 나오게 되어 level이 9가 되게 됩니다.
하지만, 해당 상황의 경우 level 1에 더 가깝다고 판단되어 else를 1로 설정했습니다.


companion object {
const val SPEED_CALC_INTERVAL = 10
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.kkkk.core.extension.setNavigationBarColorFromResource
import com.kkkk.core.extension.setStatusBarColorFromResource
import com.kkkk.core.extension.toast
import com.kkkk.presentation.main.MainActivity
import com.kkkk.presentation.main.onboarding.onbarding.OnboardingActivity
import com.kkkk.presentation.onboarding.onbarding.OnboardingActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
Expand Down
Loading