Skip to content

Commit 7aa01fd

Browse files
bngshyonghanJu
andcommitted
✨ RunningRepository 구현
Co-authored-by: yonghanJu <[email protected]>
1 parent f0873d6 commit 7aa01fd

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.whyranoid.presentation.running
2+
3+
data class RunningPosition(
4+
val latitude: Double,
5+
val longitude: Double
6+
)
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.whyranoid.presentation.running
2+
3+
import android.location.Location
4+
import com.whyranoid.domain.model.RunningHistory
5+
import kotlinx.coroutines.flow.MutableStateFlow
6+
import kotlinx.coroutines.flow.asStateFlow
7+
import java.util.UUID
8+
import javax.inject.Inject
9+
import javax.inject.Singleton
10+
11+
@Singleton
12+
class RunningRepository @Inject constructor() {
13+
private var _runningState = MutableStateFlow<RunningState>(
14+
RunningState.NotRunning()
15+
)
16+
val runningState get() = _runningState.asStateFlow()
17+
18+
fun startRunning(startPosition: RunningPosition) {
19+
if (runningState.value is RunningState.NotRunning) {
20+
_runningState.value = RunningState.Running(
21+
RunningData(
22+
startTime = System.currentTimeMillis(),
23+
runningTime = 0,
24+
totalDistance = 0.0,
25+
pace = 0.0,
26+
runningPositionList = listOf(startPosition)
27+
)
28+
)
29+
}
30+
}
31+
32+
fun pauseRunning() = runCatching {
33+
if (runningState.value is RunningState.Running) {
34+
_runningState.value = RunningState.Paused(
35+
runningState.value.runningData
36+
)
37+
} else {
38+
throw Exception("상태가 잘못되었습니다.")
39+
}
40+
}
41+
42+
fun resumeRunning() = runCatching {
43+
if (runningState.value is RunningState.Paused) {
44+
_runningState.value = RunningState.Running(
45+
runningState.value.runningData
46+
)
47+
} else {
48+
throw Exception("상태가 잘못되었습니다.")
49+
}
50+
}
51+
52+
fun finishRunning(): Result<RunningFinishData> = runCatching {
53+
val finishData = when (runningState.value) {
54+
is RunningState.NotRunning -> {
55+
throw Exception("상태가 잘못되었습니다.")
56+
}
57+
is RunningState.Running -> {
58+
runningState.value.runningData.toRunningFinishData()
59+
}
60+
is RunningState.Paused -> {
61+
runningState.value.runningData.toRunningFinishData()
62+
}
63+
}
64+
_runningState.value = RunningState.NotRunning()
65+
finishData
66+
}
67+
68+
fun setRunningState(runningPosition: RunningPosition) {
69+
if (_runningState.value is RunningState.Running) {
70+
val prevRunningData = _runningState.value.runningData
71+
val newRunningTime = prevRunningData.runningTime + 1
72+
val distance = FloatArray(1)
73+
Location.distanceBetween(
74+
prevRunningData.runningPositionList.last().latitude,
75+
prevRunningData.runningPositionList.last().longitude,
76+
runningPosition.latitude,
77+
runningPosition.longitude,
78+
distance
79+
)
80+
val newTotalDistance = prevRunningData.totalDistance + distance.first()
81+
val newPace = newTotalDistance / newRunningTime
82+
val newRunningPositionList = prevRunningData.runningPositionList.plus(runningPosition)
83+
84+
_runningState.value = RunningState.Running(
85+
RunningData(
86+
startTime = prevRunningData.startTime,
87+
runningTime = newRunningTime,
88+
totalDistance = newTotalDistance,
89+
pace = newPace,
90+
runningPositionList = newRunningPositionList
91+
)
92+
)
93+
}
94+
}
95+
}
96+
97+
// TODO : 모델 파일 분리
98+
data class RunningData(
99+
val startTime: Long = 0L,
100+
val runningTime: Int = 0,
101+
val totalDistance: Double = 0.0,
102+
val pace: Double = 0.0,
103+
val runningPositionList: List<RunningPosition> = emptyList()
104+
)
105+
106+
data class RunningFinishData(
107+
val runningHistory: RunningHistory,
108+
val runningPositionList: List<RunningPosition>
109+
)
110+
111+
sealed interface RunningState {
112+
val runningData: RunningData
113+
114+
data class NotRunning(override val runningData: RunningData = RunningData()) : RunningState
115+
116+
data class Running(override val runningData: RunningData) : RunningState
117+
118+
data class Paused(override val runningData: RunningData) : RunningState
119+
}
120+
121+
fun RunningData.toRunningFinishData() =
122+
RunningFinishData(
123+
RunningHistory(
124+
historyId = UUID.randomUUID().toString(),
125+
startedAt = startTime,
126+
finishedAt = System.currentTimeMillis(),
127+
totalRunningTime = runningTime,
128+
pace = pace,
129+
totalDistance = totalDistance
130+
),
131+
runningPositionList
132+
)

0 commit comments

Comments
 (0)