Skip to content

Commit de2cd13

Browse files
authored
πŸ”€ #37 from boostcampwm-2022/feat/my_run_room
Room DB κ΅¬ν˜„ 및 λ‚˜μ˜ μš΄λ™κΈ°λ‘ κ°€μ Έμ™€μ„œ 화면에 λ„μš°λŠ” 둜직 κ΅¬ν˜„, λ°”ν…€ λ„€λΉ„κ²Œμ΄μ…˜ κ΅¬ν˜„
2 parents dd3c9de + 9d4d397 commit de2cd13

36 files changed

+597
-177
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
}

β€Ždata/src/main/java/com/whyranoid/data/account/AccountDataSource.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ import kotlinx.coroutines.flow.Flow
55
interface AccountDataSource {
66
fun getUserNickName(): Flow<String>
77
fun getUserProfileImgUri(): Flow<String>
8-
suspend fun updateUserNickName(newNickName: String): Result<String>
8+
fun getUserUid(): Flow<String>
9+
suspend fun updateUserNickName(uid: String, newNickName: String): Result<String>
910
}

β€Ždata/src/main/java/com/whyranoid/data/account/AccountDataSourceImpl.kt

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,45 @@ import com.google.firebase.firestore.FirebaseFirestore
88
import com.whyranoid.data.account.AccountDataSourceImpl.PreferenceKeys.nickName
99
import com.whyranoid.data.account.AccountDataSourceImpl.PreferenceKeys.profileImgUri
1010
import com.whyranoid.data.account.AccountDataSourceImpl.PreferenceKeys.uid
11-
import kotlinx.coroutines.CoroutineScope
12-
import kotlinx.coroutines.Dispatchers
13-
import kotlinx.coroutines.flow.launchIn
1411
import kotlinx.coroutines.flow.map
15-
import kotlinx.coroutines.flow.onEach
1612
import javax.inject.Inject
1713

1814
class AccountDataSourceImpl @Inject constructor(
1915
private val dataStoreDb: DataStore<Preferences>,
2016
private val fireBaseDb: FirebaseFirestore
2117
) : AccountDataSource {
2218

23-
init {
24-
getUid()
25-
}
26-
27-
private var userUid = EMPTY_STRING
28-
2919
private object PreferenceKeys {
3020
val uid = stringPreferencesKey(UID_KEY)
3121
val email = stringPreferencesKey(EMAIL_KEY)
3222
val nickName = stringPreferencesKey(NICK_NAME_KEY)
3323
val profileImgUri = stringPreferencesKey(PROFILE_IMG_URI)
3424
}
3525

36-
// TODO : flow -> suspend둜 λ³€κ²½ν•΄μ•Ό 함.
3726
override fun getUserNickName() = dataStoreDb.data
3827
.map { preferences ->
3928
preferences[nickName] ?: EMPTY_STRING
4029
}
4130

42-
// TODO : flow -> suspend둜 λ³€κ²½ν•΄μ•Ό 함.
4331
override fun getUserProfileImgUri() = dataStoreDb.data
4432
.map { preferences ->
4533
preferences[profileImgUri] ?: EMPTY_STRING
4634
}
4735

48-
// TODO : flow -> suspend둜 λ³€κ²½ν•΄μ•Ό 함.
49-
private fun getUid() = dataStoreDb.data
36+
override fun getUserUid() = dataStoreDb.data
5037
.map { preferences ->
51-
preferences[uid]
52-
}.onEach {
53-
userUid = it ?: EMPTY_STRING
54-
}.launchIn(CoroutineScope(Dispatchers.IO))
38+
preferences[uid] ?: EMPTY_STRING
39+
}
5540

56-
override suspend fun updateUserNickName(newNickName: String) = runCatching {
41+
override suspend fun updateUserNickName(uid: String, newNickName: String) = runCatching {
5742
// λ‘œμ»¬μ— μ—…λ°μ΄νŠΈ
5843
dataStoreDb.edit { preferences ->
5944
preferences[nickName] = newNickName
6045
}
6146

6247
// 리λͺ¨νŠΈμ— μ—…λ°μ΄νŠΈ
6348
fireBaseDb.collection(USER_COLLECTION_PATH)
64-
.document(userUid).update(USER_FIELD_NICK_NAME, newNickName)
49+
.document(uid).update(USER_FIELD_NICK_NAME, newNickName)
6550

6651
newNickName
6752
}

β€Ždata/src/main/java/com/whyranoid/data/account/AccountRepositoryImpl.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ class AccountRepositoryImpl @Inject constructor(
1717
return true
1818
}
1919

20-
override suspend fun getUid(): Result<String> {
21-
return Result.success("byeonghee-uid")
20+
override fun getUid(): Flow<String> {
21+
return accountDataSource.getUserUid()
2222
}
2323

24-
override suspend fun getNickname(): Flow<String> {
24+
override fun getNickname(): Flow<String> {
2525
return accountDataSource.getUserNickName()
2626
}
2727

28-
override suspend fun updateNickname(newNickName: String): Result<String> {
29-
return accountDataSource.updateUserNickName(newNickName)
28+
override suspend fun updateNickname(uid: String, newNickName: String): Result<String> {
29+
return accountDataSource.updateUserNickName(uid, newNickName)
3030
}
3131

32-
override suspend fun getProfileUri(): Flow<String> {
32+
override fun getProfileUri(): Flow<String> {
3333
return accountDataSource.getUserProfileImgUri()
3434
}
3535

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+
}

0 commit comments

Comments
Β (0)