Skip to content

Commit 822a734

Browse files
committed
Refactor: RoutineLocalDataSource 추가 및 DI 연결
1 parent faea8a4 commit 822a734

3 files changed

Lines changed: 78 additions & 1 deletion

File tree

app/src/main/java/com/threegap/bitnagil/di/data/DataSourceModule.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import com.threegap.bitnagil.data.recommendroutine.datasource.RecommendRoutineDa
2020
import com.threegap.bitnagil.data.recommendroutine.datasourceImpl.RecommendRoutineDataSourceImpl
2121
import com.threegap.bitnagil.data.report.datasource.ReportDataSource
2222
import com.threegap.bitnagil.data.report.datasourceImpl.ReportDataSourceImpl
23+
import com.threegap.bitnagil.data.routine.datasource.RoutineLocalDataSource
2324
import com.threegap.bitnagil.data.routine.datasource.RoutineRemoteDataSource
25+
import com.threegap.bitnagil.data.routine.datasourceImpl.RoutineLocalDataSourceImpl
2426
import com.threegap.bitnagil.data.routine.datasourceImpl.RoutineRemoteDataSourceImpl
2527
import com.threegap.bitnagil.data.user.datasource.UserLocalDataSource
2628
import com.threegap.bitnagil.data.user.datasource.UserRemoteDataSource
@@ -52,7 +54,11 @@ abstract class DataSourceModule {
5254

5355
@Binds
5456
@Singleton
55-
abstract fun bindRoutineDataSource(routineDataSourceImpl: RoutineRemoteDataSourceImpl): RoutineRemoteDataSource
57+
abstract fun bindRoutineRemoteDataSource(routineDataSourceImpl: RoutineRemoteDataSourceImpl): RoutineRemoteDataSource
58+
59+
@Binds
60+
@Singleton
61+
abstract fun bindRoutineLocalDataSource(impl: RoutineLocalDataSourceImpl): RoutineLocalDataSource
5662

5763
@Binds
5864
@Singleton
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.threegap.bitnagil.data.routine.datasource
2+
3+
import com.threegap.bitnagil.domain.routine.model.RoutineCompletionInfo
4+
import com.threegap.bitnagil.domain.routine.model.RoutineSchedule
5+
import kotlinx.coroutines.flow.StateFlow
6+
7+
interface RoutineLocalDataSource {
8+
val routineSchedule: StateFlow<RoutineSchedule?>
9+
val lastFetchRange: Pair<String, String>?
10+
fun saveSchedule(schedule: RoutineSchedule, startDate: String, endDate: String)
11+
fun getCompletionInfo(dateKey: String, routineId: String): RoutineCompletionInfo?
12+
fun applyOptimisticToggle(dateKey: String, routineId: String, completionInfo: RoutineCompletionInfo)
13+
fun clearCache()
14+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.threegap.bitnagil.data.routine.datasourceImpl
2+
3+
import com.threegap.bitnagil.data.routine.datasource.RoutineLocalDataSource
4+
import com.threegap.bitnagil.domain.routine.model.RoutineCompletionInfo
5+
import com.threegap.bitnagil.domain.routine.model.RoutineSchedule
6+
import kotlinx.coroutines.flow.MutableStateFlow
7+
import kotlinx.coroutines.flow.StateFlow
8+
import kotlinx.coroutines.flow.asStateFlow
9+
import kotlinx.coroutines.flow.update
10+
import javax.inject.Inject
11+
12+
class RoutineLocalDataSourceImpl @Inject constructor() : RoutineLocalDataSource {
13+
private val _routineSchedule = MutableStateFlow<RoutineSchedule?>(null)
14+
override val routineSchedule: StateFlow<RoutineSchedule?> = _routineSchedule.asStateFlow()
15+
16+
override var lastFetchRange: Pair<String, String>? = null
17+
private set
18+
19+
override fun saveSchedule(schedule: RoutineSchedule, startDate: String, endDate: String) {
20+
lastFetchRange = startDate to endDate
21+
_routineSchedule.update { schedule }
22+
}
23+
24+
override fun getCompletionInfo(dateKey: String, routineId: String): RoutineCompletionInfo? {
25+
val routine = _routineSchedule.value?.dailyRoutines?.get(dateKey)?.routines?.find { it.id == routineId }
26+
return routine?.let { RoutineCompletionInfo(routineId, it.isCompleted, it.subRoutineCompletionStates) }
27+
}
28+
29+
override fun applyOptimisticToggle(dateKey: String, routineId: String, completionInfo: RoutineCompletionInfo) {
30+
_routineSchedule.update { current ->
31+
current ?: return@update current
32+
val dailyRoutines = current.dailyRoutines[dateKey] ?: return@update current
33+
val updatedRoutines = dailyRoutines.routines.map { routine ->
34+
if (routine.id == routineId) {
35+
routine.copy(
36+
isCompleted = completionInfo.routineCompleteYn,
37+
subRoutineCompletionStates = completionInfo.subRoutineCompleteYn,
38+
)
39+
} else {
40+
routine
41+
}
42+
}
43+
val updatedDailyRoutines = dailyRoutines.copy(
44+
routines = updatedRoutines,
45+
isAllCompleted = updatedRoutines.all { it.isCompleted },
46+
)
47+
48+
current.copy(
49+
dailyRoutines = current.dailyRoutines + (dateKey to updatedDailyRoutines),
50+
)
51+
}
52+
}
53+
54+
override fun clearCache() {
55+
_routineSchedule.update { null }
56+
}
57+
}

0 commit comments

Comments
 (0)