Skip to content

Commit ebd0852

Browse files
committed
✨ Room 데이터베이스 만들고 유저 운동 기록 불러오기 및 리사이클러뷰 코드 작성
1 parent 7d78c72 commit ebd0852

20 files changed

+441
-29
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ buildscript {
2727
desugarVersion = "2.0.0"
2828
calendarVersion = "1.0.4"
2929
glideVersion = "4.14.2"
30+
roomVersion = "2.4.3"
3031
}
3132
dependencies {
3233
classpath "com.google.gms:google-services:$googleServiceVersion"

data/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ dependencies {
5252
// datastore
5353
implementation "androidx.datastore:datastore-preferences:$dataStoreVersion"
5454
implementation "androidx.datastore:datastore-preferences-core:$dataStoreVersion"
55+
56+
// Room
57+
implementation "androidx.room:room-ktx:$roomVersion"
58+
implementation "androidx.room:room-runtime:$roomVersion"
59+
annotationProcessor "androidx.room:room-compiler:$roomVersion"
60+
kapt "androidx.room:room-compiler:$roomVersion"
5561
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.whyranoid.data.account
2+
3+
import androidx.room.Dao
4+
import androidx.room.Insert
5+
import androidx.room.OnConflictStrategy
6+
import androidx.room.Query
7+
import kotlinx.coroutines.flow.Flow
8+
9+
@Dao
10+
interface RunningHistoryDao {
11+
// TODO 일단 쓰기, 읽기만 해놨습니다~
12+
@Insert(onConflict = OnConflictStrategy.ABORT)
13+
suspend fun addRunningHistory(runningHistory: RunningHistoryEntity)
14+
15+
@Query("SELECT * FROM running_history ORDER BY startedAt ASC")
16+
fun getRunningHistory(): Flow<List<RunningHistoryEntity>>
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.whyranoid.data.account
2+
3+
import androidx.room.Entity
4+
import androidx.room.PrimaryKey
5+
import com.whyranoid.domain.model.RunningHistory
6+
7+
@Entity(tableName = "running_history")
8+
data class RunningHistoryEntity(
9+
@PrimaryKey
10+
val historyId: String,
11+
val startedAt: Long,
12+
val finishedAt: Long,
13+
val totalRunningTime: Int,
14+
val pace: Double,
15+
val totalDistance: Double
16+
)
17+
18+
fun RunningHistoryEntity.toRunningHistory(): RunningHistory {
19+
return RunningHistory(
20+
historyId = historyId,
21+
startedAt = startedAt,
22+
finishedAt = finishedAt,
23+
totalRunningTime = totalRunningTime,
24+
pace = pace,
25+
totalDistance = totalDistance
26+
)
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.whyranoid.data.account
2+
3+
import androidx.room.Database
4+
import androidx.room.RoomDatabase
5+
6+
@Database(
7+
entities = [RunningHistoryEntity::class],
8+
version = 1
9+
)
10+
abstract class RunningHistoryLocalDataBase : RoomDatabase() {
11+
abstract fun runningHistoryDao(): RunningHistoryDao
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.whyranoid.data.account
2+
3+
import com.whyranoid.domain.model.RunningHistory
4+
import kotlinx.coroutines.flow.Flow
5+
6+
interface RunningHistoryLocalDataSource {
7+
fun getRunningHistory(): Flow<List<RunningHistory>>
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.whyranoid.data.account
2+
3+
import com.whyranoid.domain.model.RunningHistory
4+
import kotlinx.coroutines.flow.Flow
5+
import kotlinx.coroutines.flow.map
6+
import javax.inject.Inject
7+
8+
class RunningHistoryLocalDataSourceImpl @Inject constructor(
9+
private val roomDb: RunningHistoryLocalDataBase
10+
) : RunningHistoryLocalDataSource {
11+
12+
override fun getRunningHistory(): Flow<List<RunningHistory>> {
13+
return roomDb.runningHistoryDao().getRunningHistory().map { runningHistoryList ->
14+
runningHistoryList.map { runningHistoryEntity ->
15+
runningHistoryEntity.toRunningHistory()
16+
}
17+
}
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.whyranoid.data.account
2+
3+
import com.whyranoid.domain.model.RunningHistory
4+
import com.whyranoid.domain.repository.RunningHistoryRepository
5+
import kotlinx.coroutines.flow.Flow
6+
import javax.inject.Inject
7+
8+
class RunningHistoryRepositoryImpl @Inject constructor(
9+
private val runningHistoryLocalDataSource: RunningHistoryLocalDataSource
10+
) : RunningHistoryRepository {
11+
override fun getRunningHistory(): Flow<List<RunningHistory>> {
12+
return runningHistoryLocalDataSource.getRunningHistory()
13+
}
14+
15+
override suspend fun getUnpostedRunningHistory(): List<RunningHistory> {
16+
TODO("Not yet implemented")
17+
}
18+
19+
override suspend fun saveRunningHistory(
20+
startedAt: Long,
21+
finishedAt: Long,
22+
totalRunningTime: Int,
23+
pace: Double,
24+
totalDistance: Double
25+
): Result<RunningHistory> {
26+
TODO("Not yet implemented")
27+
}
28+
}
Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
11
package com.whyranoid.data.di
22

3-
import android.content.Context
4-
import androidx.datastore.core.DataStore
5-
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
6-
import androidx.datastore.preferences.core.Preferences
7-
import androidx.datastore.preferences.preferencesDataStoreFile
83
import com.whyranoid.data.account.AccountDataSource
94
import com.whyranoid.data.account.AccountDataSourceImpl
105
import com.whyranoid.data.account.AccountRepositoryImpl
6+
import com.whyranoid.data.account.RunningHistoryLocalDataSource
7+
import com.whyranoid.data.account.RunningHistoryLocalDataSourceImpl
8+
import com.whyranoid.data.account.RunningHistoryRepositoryImpl
119
import com.whyranoid.domain.repository.AccountRepository
10+
import com.whyranoid.domain.repository.RunningHistoryRepository
1211
import dagger.Binds
1312
import dagger.Module
14-
import dagger.Provides
1513
import dagger.hilt.InstallIn
16-
import dagger.hilt.android.qualifiers.ApplicationContext
1714
import dagger.hilt.components.SingletonComponent
18-
import javax.inject.Singleton
19-
20-
private const val USER_PREFERENCES = "user_preferences"
21-
22-
@Module
23-
@InstallIn(SingletonComponent::class)
24-
object DataStoreModule {
25-
26-
@Singleton
27-
@Provides
28-
fun providePreferencesDataStore(@ApplicationContext appContext: Context): DataStore<Preferences> =
29-
PreferenceDataStoreFactory.create(
30-
produceFile = {
31-
appContext.preferencesDataStoreFile(USER_PREFERENCES)
32-
}
33-
)
34-
}
3515

3616
@Module
3717
@InstallIn(SingletonComponent::class)
@@ -42,4 +22,10 @@ abstract class AccountModule {
4222

4323
@Binds
4424
abstract fun bindAccountDataSource(accountDataSourceImpl: AccountDataSourceImpl): AccountDataSource
25+
26+
@Binds
27+
abstract fun provideRunningHistoryRepository(runningHistoryRepositoryImpl: RunningHistoryRepositoryImpl): RunningHistoryRepository
28+
29+
@Binds
30+
abstract fun provideRunningHistoryDataSource(runningHistoryLocalDataSourceImpl: RunningHistoryLocalDataSourceImpl): RunningHistoryLocalDataSource
4531
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.whyranoid.data.di
2+
3+
import android.content.Context
4+
import androidx.datastore.core.DataStore
5+
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
6+
import androidx.datastore.preferences.core.Preferences
7+
import androidx.datastore.preferences.preferencesDataStoreFile
8+
import androidx.room.Room
9+
import com.whyranoid.data.account.RunningHistoryLocalDataBase
10+
import dagger.Module
11+
import dagger.Provides
12+
import dagger.hilt.InstallIn
13+
import dagger.hilt.android.qualifiers.ApplicationContext
14+
import dagger.hilt.components.SingletonComponent
15+
import javax.inject.Singleton
16+
17+
@Module
18+
@InstallIn(SingletonComponent::class)
19+
object DataBaseModule {
20+
@Singleton
21+
@Provides
22+
fun provideRoomDataBase(
23+
@ApplicationContext appContext: Context
24+
): RunningHistoryLocalDataBase = Room.databaseBuilder(
25+
appContext,
26+
RunningHistoryLocalDataBase::class.java,
27+
"mogakrun_running_history.db"
28+
)
29+
.build()
30+
}
31+
32+
@Module
33+
@InstallIn(SingletonComponent::class)
34+
object DataStoreModule {
35+
36+
private const val USER_PREFERENCES = "user_preferences"
37+
38+
@Singleton
39+
@Provides
40+
fun providePreferencesDataStore(@ApplicationContext appContext: Context): DataStore<Preferences> =
41+
PreferenceDataStoreFactory.create(
42+
produceFile = {
43+
appContext.preferencesDataStoreFile(USER_PREFERENCES)
44+
}
45+
)
46+
}

0 commit comments

Comments
 (0)