Skip to content

Commit

Permalink
Merge pull request #67 from step-Mate/home
Browse files Browse the repository at this point in the history
fix : 걸음수 로직 수정
  • Loading branch information
jowunnal authored Jul 1, 2024
2 parents 137b1e6 + 53156ca commit fa102c3
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface CacheSettingsDataSource {
suspend fun setMissedTodayStepAfterReboot(step: Long)
fun getLatestEndEpochSecond(): Flow<Long>

suspend fun setLatestEndTime(epochSecond: Long)
suspend fun setLatestEndEpochSecond(epochSecond: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class CacheSettingsDataSourceImpl @Inject constructor(
override fun getLatestEndEpochSecond(): Flow<Long> =
data.map { prefs -> prefs.latestEndEpochSecond }

override suspend fun setLatestEndTime(epochSecond: Long) {
override suspend fun setLatestEndEpochSecond(epochSecond: Long) {
dataStore.updateData { pref ->
pref
.toBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ class StepRepositoryImpl @Inject constructor(

override fun getLatestEndEpochSecond(): Flow<Long> = cacheSettingsDataSource.getLatestEndEpochSecond()

override suspend fun setLatestEndTime(epochSecond: Long) =
cacheSettingsDataSource.setLatestEndTime(epochSecond)
override suspend fun setLatestEndEpochSecond(epochSecond: Long) =
cacheSettingsDataSource.setLatestEndEpochSecond(epochSecond)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface StepRepository {
suspend fun setMissedTodayStepAfterReboot(step: Long)
fun getLatestEndEpochSecond(): Flow<Long>

suspend fun setLatestEndTime(epochSecond: Long)
suspend fun setLatestEndEpochSecond(epochSecond: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ManageStepUseCaseImpl @Inject constructor(

override fun getLatestEndEpochSecond(): Flow<Long> = stepRepository.getLatestEndEpochSecond()

override suspend fun setLatestEndTime(epochSecond: Long) = stepRepository.setLatestEndTime(epochSecond)
override suspend fun setLatestEndEpochSecond(epochSecond: Long) = stepRepository.setLatestEndEpochSecond(epochSecond)
}

interface ManageStepUseCase {
Expand All @@ -46,7 +46,7 @@ interface ManageStepUseCase {

fun getLatestEndEpochSecond(): Flow<Long>

suspend fun setLatestEndTime(epochSecond: Long)
suspend fun setLatestEndEpochSecond(epochSecond: Long)
}

@Module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,22 @@ internal class StepSensorViewModel @Inject constructor(
}

suspend fun initStepData(stepBySensor: Long) {
val latestEndEpochSecond = manageStepUseCase.getLatestEndEpochSecond().first()
val latestEndZonedDateTime = Instant.ofEpochSecond(latestEndEpochSecond).atZone(ZoneId.systemDefault())

val todayStep = manageStepUseCase.getTodayStep().first()

val missedTodayStep = if (todayStep > 0L)
todayStep
else
0L
val missedTodayStep =
if (latestEndZonedDateTime.dayOfYear < ZonedDateTime.now().dayOfYear)
0L
else
todayStep

//헬스커넥트에 저장이 되지 않은 상태에서 서비스를 껏다가 다시 켰을 경우
val latestEndTime = manageStepUseCase.getLatestEndEpochSecond().first()
//헬스커넥트에 저장이 되지 않은 상태에서 서비스를 껏다가 다시 켰을 경우, 차이만큼을 헬스커넥트에 저장해줌
val insertTime =
ZonedDateTime.ofInstant(Instant.ofEpochSecond(latestEndTime), ZoneId.of("+9"))
ZonedDateTime.ofInstant(Instant.ofEpochSecond(latestEndEpochSecond), ZoneId.systemDefault())

val healthConnectTodayStep = healthConnector.getSpecificDayTotalStep(latestEndTime)
val healthConnectTodayStep = healthConnector.getSpecificDayTotalStep(latestEndEpochSecond)

if (todayStep > 0 && healthConnectTodayStep < todayStep) {

Expand Down Expand Up @@ -181,6 +184,8 @@ internal class StepSensorViewModel @Inject constructor(
if (!sensorTimeScheduler.isRunning)
startTime = ZonedDateTime.now()

manageStepUseCase.setLatestEndEpochSecond(startTime.toEpochSecond())

sensorTimeScheduler.setTime(second * 1000L)

endTime = ZonedDateTime.now()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ import com.stepmate.domain.usecase.step.SetUserDayStepUseCase
import com.stepmate.home.fake.ManageStepUseCaseFake
import com.stepmate.home.service.StepSensorViewModel
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.longs.shouldBeGreaterThan
import io.kotest.matchers.longs.shouldBeLessThan
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.setMain
import java.time.Instant
import java.time.ZoneId
import java.time.ZonedDateTime

@OptIn(ExperimentalCoroutinesApi::class)
internal abstract class StepSensorViewModelTest : BehaviorSpec({
Expand Down Expand Up @@ -112,13 +118,21 @@ internal class ScenarioOnFirstInstall : StepSensorViewModelTest() {
then("앱 최초 실행시, 헬스커넥트에 저장된 걸음수가 없기 때문에 last 는 0 이다.") {
viewModel.step.value.last shouldBe 0L
}

then("저장된 이전의 endTime 은 0이 아니며, 과거 이어야 한다.") {
delay(1000L)
val latestEndTime = manageStepUseCase.getLatestEndEpochSecond().first()

latestEndTime shouldNotBe 0L
latestEndTime shouldBeLessThan ZonedDateTime.now().toEpochSecond()
}
}
}
}
}
}

internal class ScenarioReExecuteServiceAfterInstall : StepSensorViewModelTest() {
internal class ScenarioReExecuteServiceOnTodayAfterInstallOnYesterday : StepSensorViewModelTest() {

init {
given("앱 설치 이후") {
Expand All @@ -134,8 +148,61 @@ internal class ScenarioReExecuteServiceAfterInstall : StepSensorViewModelTest()
coEvery { healthConnector.getSpecificDayTotalStep(any()) } returns 0L
manageStepUseCase.setTodayStep(notAddedStep)

and("서비스를 어제 껏다가, 오늘 재실행 하는 상황일 때") {
manageStepUseCase.setLatestEndEpochSecond(ZonedDateTime.now().minusDays(1L).toEpochSecond())

sensorCallBack(
isCreated = true,
isReboot = false,
isDestroyedBySystem = false,
)

and("100 걸음을 걸었다면") {
val walked = 100

testWalking(walked)

then("어제 값에 센서값이 저장 된다.") {
viewModel.step.value.yesterday shouldBe step
}

then("서비스 실행 이전에 저장되지 않은 걸음수는 0 이다.") {
viewModel.step.value.missedTodayStepAfterReboot shouldBe 0L
}

then("오늘 걸음수는 100 이다.") {
viewModel.step.value.current shouldBe walked
}

then("헬스커넥트에 저장된 오늘의 걸음수가 0 이므로, last 는 0 이다.") {
viewModel.step.value.last shouldBe 0L
}
}
}
}
}
}
}
}

internal class ScenarioReExecuteServiceOnTodayAfterInstallOnToday : StepSensorViewModelTest() {

init {
given("앱 설치 이후") {
coEvery { hasTokenUseCase() } returns flow { emit(false) }

`when`("걸음수 센서의 값이 100일 때") {
val step = 100L
stepBySensor = step

and("서비스 재실행 이전에 헬스커넥트에 저장되지 않은 걸음수가 50 일 때 (타이머 완료 이전에 서비스 종료)") {
val notAddedStep = 50L
coEvery { healthConnector.getTodayTotalStep() } returns notAddedStep
coEvery { healthConnector.getSpecificDayTotalStep(any()) } returns 0L
manageStepUseCase.setTodayStep(notAddedStep)

and("서비스를 당일에 껏다가 재실행 하는 상황일 때") {
manageStepUseCase.setLatestEndTime(Instant.now().epochSecond)
manageStepUseCase.setLatestEndEpochSecond(Instant.now().epochSecond)

viewModel = StepSensorViewModel(
setUserDayStepUseCase = setUserDayStepUseCase,
Expand All @@ -153,6 +220,8 @@ internal class ScenarioReExecuteServiceAfterInstall : StepSensorViewModelTest()

viewModel.initLastValueByHealthConnect()

viewModel.onSensorChanged(stepBySensor)

and("100 걸음을 걸었다면") {
val walked = 100

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class ManageStepUseCaseFake : ManageStepUseCase {

override fun getLatestEndEpochSecond(): Flow<Long> = flow { emit(latestEpochSecond) }

override suspend fun setLatestEndTime(epochSecond: Long) {
override suspend fun setLatestEndEpochSecond(epochSecond: Long) {
latestEpochSecond = epochSecond
}

Expand Down

0 comments on commit fa102c3

Please sign in to comment.