Skip to content

Commit

Permalink
[Merge] #55 -> develop
Browse files Browse the repository at this point in the history
[Feat/#55] fix step sensor and onboarding
  • Loading branch information
chattymin authored Sep 21, 2024
2 parents a65ff4c + 77db613 commit ae295e2
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.kkkk.core.state.UiState
import com.kkkk.domain.entity.request.RecordRequestModel
import com.kkkk.domain.repository.RhythmRepository
import com.kkkk.domain.repository.UserRepository
import com.kkkk.presentation.onboarding.onbarding.OnboardingViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -45,7 +44,13 @@ constructor(
private val _stepCount = MutableStateFlow(0)
val stepCount: StateFlow<Int> = _stepCount

private val _firstStepTime = MutableStateFlow(0L)
private val _oddStepCount = MutableStateFlow(0)
private val _oddStepTime = MutableStateFlow(0L)

private val _evenStepCount = MutableStateFlow(0)
private val _evenStepTime = MutableStateFlow(0L)

private val _beforeStepTime = MutableStateFlow(0L)

init {
initRhythmLevelFromDataStore()
Expand All @@ -60,7 +65,15 @@ constructor(
}

fun addStepCount(newStepCount: Int) {
if ((_oddStepCount.value + _evenStepCount.value) % 2 == 0) {
_oddStepCount.value += newStepCount
_oddStepTime.value = System.currentTimeMillis() - _beforeStepTime.value
} else {
_evenStepCount.value += newStepCount
_evenStepTime.value = System.currentTimeMillis() - _beforeStepTime.value
}
_stepCount.value += newStepCount
_beforeStepTime.value = System.currentTimeMillis()
}

fun setTempRhythmLevel(level: Int) {
Expand Down Expand Up @@ -102,7 +115,7 @@ constructor(
rhythmRepository.getRhythmWav(url)
.onSuccess {
_downloadWavState.value = UiState.Success(it)
_firstStepTime.value = System.currentTimeMillis()
_beforeStepTime.value = System.currentTimeMillis()
}
.onFailure {
_downloadWavState.value = UiState.Failure(it.message.toString())
Expand All @@ -111,11 +124,7 @@ constructor(
}

fun posRhythmRecordToSave() {
var accuracy =
(stepCount.value.toDouble() / (bpm / 60 * ((System.currentTimeMillis() - _firstStepTime.value) / 10000)))
if (accuracy > 1) {
accuracy = max(2 - accuracy, 0.0)
}
val accuracy = calculateAccuracy(_oddStepTime.value, _evenStepTime.value)

viewModelScope.launch {
rhythmRepository.postRhythmRecord(
Expand All @@ -133,6 +142,16 @@ constructor(
}
}

private fun calculateAccuracy(time1: Long, time2: Long): Double {
val difference = kotlin.math.abs(time1 - time2)

return when {
difference == 0L -> 1.0
difference >= MAX_ALLOWED_DIFFERENCE -> 0.0
else -> (1 - difference.toDouble() / MAX_ALLOWED_DIFFERENCE)
}
}

fun posRhythmRecordToSaveWatch(
accuracy: Double,
) {
Expand All @@ -153,8 +172,11 @@ constructor(
}

private fun resetStepInfo() {
_stepCount.value = 0
_firstStepTime.value = 0L
_oddStepCount.value = 0
_evenStepCount.value = 0
_oddStepTime.value = 0L
_evenStepTime.value = 0L
_beforeStepTime.value = 0L
}

fun getBpmFromDataStore() = userRepository.getBpm()
Expand All @@ -176,5 +198,6 @@ constructor(

companion object {
const val LEVEL_UNDEFINED = -1
const val MAX_ALLOWED_DIFFERENCE = 360000L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,10 @@ class OnboardingMeasureFragment :
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

initButtonListener()
initializeSensor()
checkAndRequestPermission()
}

private fun initButtonListener() {
with(binding) {
btnOnboardingMeasureLater.setOnClickListener {
viewModel.setState(OnboardingState.DONE)
}
}
}

private fun initializeSensor() {
sensorManager = requireContext().getSystemService(Context.SENSOR_SERVICE) as SensorManager
stepDetectorSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class OnboardingStartFragment :

private fun initButtonListener() {
with(binding) {
btnOnboardingStartLater.setOnClickListener {
viewModel.setState(OnboardingState.DONE)
}
btnOnboardingStartMeasure.setOnClickListener {
viewModel.setState(OnboardingState.MEASURE)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package com.kkkk.presentation.onboarding.splash

import android.Manifest
import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import androidx.activity.viewModels
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.kkkk.core.base.BaseActivity
Expand All @@ -29,8 +41,15 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>(R.layout.activity_spl
setStatusBarColor()
setNavigationBarColor()
observeStates()
}

viewModel.checkTokenState()
override fun onResume() {
super.onResume()
if (isActivityRecognitionPermissionGranted(this)) {
viewModel.checkTokenState()
} else {
showDialog()
}
}

private fun setStatusBarColor() = setStatusBarColorFromResource(R.color.purple_50)
Expand Down Expand Up @@ -65,6 +84,65 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>(R.layout.activity_spl
viewModel.setAndroidId(getDeviceTag())
}

private fun isActivityRecognitionPermissionGranted(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACTIVITY_RECOGNITION
) == PackageManager.PERMISSION_GRANTED
} else {
// Android Q 미만 버전에서는 이 권한이 필요하지 않으므로 항상 true 반환
true
}
}

private fun showDialog() {
val dialog = Dialog(this)
dialog.setContentView(R.layout.dialog_single_button)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.setCanceledOnTouchOutside(false)
dialog.setCancelable(false)
dialog.show()

dialog.findViewById<View>(R.id.btn_onboarding_start_measure).setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACTIVITY_RECOGNITION),
200
)
} else {
navigateToSettings()
}
}
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray,
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)

if (requestCode == 200) {
if (permissions.isNotEmpty() && permissions[0] == Manifest.permission.ACTIVITY_RECOGNITION) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
viewModel.checkTokenState()
} else {
navigateToSettings()
}
}
}
}

private fun navigateToSettings() {
Intent().apply {
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
data = Uri.fromParts("package", packageName, null)
startActivity(this)
}
}

@SuppressLint("HardwareIds")
private fun getDeviceTag(): String =
Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
Expand Down
75 changes: 75 additions & 0 deletions presentation/src/main/res/layout/dialog_single_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_white_fill_16_rect"
app:cardCornerRadius="16dp"
app:cardElevation="4dp">


<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">

<TextView
android:id="@+id/tvTitle"
style="@style/TextAppearance.Stempo.Head3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="12dp"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="앱 권한 설정 필요"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/subTitle"
style="@style/TextAppearance.Stempo.Body1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="12dp"
android:layout_marginTop="12dp"
android:gravity="center"
android:text="스템포 이용을 위해서는 \n접근 권한 허용이 필요해요."
android:textColor="@color/gray_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTitle" />

<TextView
android:id="@+id/tvContent"
style="@style/TextAppearance.Stempo.Body2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="12dp"
android:layout_marginTop="12dp"
android:gravity="start"
android:text=" - 신체활동 권한\n - 걸음 수 측정 및 기록"
android:textColor="@color/gray_700"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/subTitle" />

<TextView
android:id="@+id/btn_onboarding_start_measure"
style="@style/TextAppearance.Stempo.Head4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:background="@drawable/shape_purple50_fill_12_rect"
android:gravity="center"
android:paddingVertical="15dp"
android:text="@string/ok"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvContent" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
android:background="@drawable/shape_purple10_fill_12_rect"
android:gravity="center"
android:paddingVertical="15dp"
android:text="@string/do_later"
android:text="@string/ing"
android:textColor="@color/purple_50"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
19 changes: 1 addition & 18 deletions presentation/src/main/res/layout/fragment_onboarding_start.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
android:src="@drawable/ic_logo_illustration"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_onboarding_start_title"
/>
app:layout_constraintTop_toBottomOf="@id/tv_onboarding_start_title" />


<TextView
Expand All @@ -56,22 +55,6 @@
android:paddingVertical="15dp"
android:text="@string/onboarding_start_measure_button"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@id/btn_onboarding_start_later"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/btn_onboarding_start_later"
style="@style/TextAppearance.Stempo.Head4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginBottom="26dp"
android:background="@drawable/shape_purple10_fill_12_rect"
android:gravity="center"
android:paddingVertical="15dp"
android:text="@string/do_later"
android:textColor="@color/purple_50"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Expand Down
3 changes: 2 additions & 1 deletion presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
<string name="view_type_error_msg">Unknown View Type : %d</string>

<string name="getting_start">시작하기</string>
<string name="do_later">나중에 할래요</string>
<string name="ing">측정 중...</string>
<string name="before_page">이전페이지</string>
<string name="next_page">다음페이지</string>
<string name="ok">확인</string>

<string name="menu_rhythm">리듬</string>
<string name="menu_report">기록</string>
Expand Down

0 comments on commit ae295e2

Please sign in to comment.