Skip to content

Commit f05c940

Browse files
committed
✨ 데이터스토어 저장된 닉네임 가져와서 마이런 페이지에 띄우기
1 parent f899c96 commit f05c940

File tree

17 files changed

+271
-134
lines changed

17 files changed

+271
-134
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
android:name=".MogakrunApplication"
1515
tools:targetApi="31">
1616
<activity
17-
android:name="com.whyranoid.presentation.MainActivity"
18-
android:theme="@style/Theme.MoGakRun"
17+
android:name="com.whyranoid.signin.SignInActivity"
18+
android:theme="@style/Theme.MoGakRun.Splash"
1919
android:exported="true">
2020
<intent-filter>
2121
<action android:name="android.intent.action.MAIN" />

data/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ dependencies {
4848
// Hilt
4949
implementation "com.google.dagger:hilt-android:$hiltVersion"
5050
kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
51+
52+
// datastore
53+
implementation "androidx.datastore:datastore-preferences:$dataStoreVersion"
54+
implementation "androidx.datastore:datastore-preferences-core:$dataStoreVersion"
5155
}
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 kotlinx.coroutines.flow.Flow
4+
5+
interface AccountDataSource {
6+
fun getUserNickName(): Flow<String>
7+
fun getUserProfileImgUri(): Flow<String>
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.whyranoid.data.account
2+
3+
import androidx.datastore.core.DataStore
4+
import androidx.datastore.preferences.core.Preferences
5+
import androidx.datastore.preferences.core.stringPreferencesKey
6+
import com.whyranoid.data.account.AccountDataSourceImpl.PreferenceKeys.nickName
7+
import com.whyranoid.data.account.AccountDataSourceImpl.PreferenceKeys.profileImgUri
8+
import kotlinx.coroutines.flow.map
9+
import javax.inject.Inject
10+
11+
class AccountDataSourceImpl @Inject constructor(
12+
private val dataStoreDb: DataStore<Preferences>
13+
) : AccountDataSource {
14+
private object PreferenceKeys {
15+
val uid = stringPreferencesKey(UID_KEY)
16+
val email = stringPreferencesKey(EMAIL_KEY)
17+
val nickName = stringPreferencesKey(NICK_NAME_KEY)
18+
val profileImgUri = stringPreferencesKey(PROFILE_IMG_URI)
19+
}
20+
21+
override fun getUserNickName() = dataStoreDb.data
22+
.map { preferences ->
23+
preferences[nickName] ?: EMPTY_STRING
24+
}
25+
26+
override fun getUserProfileImgUri() = dataStoreDb.data
27+
.map { preferences ->
28+
preferences[profileImgUri] ?: EMPTY_STRING
29+
}
30+
31+
companion object {
32+
private const val UID_KEY = "uid"
33+
private const val EMAIL_KEY = "email"
34+
private const val NICK_NAME_KEY = "nick_name"
35+
private const val PROFILE_IMG_URI = "profile_img_uri"
36+
private const val EMPTY_STRING = ""
37+
}
38+
}

data/src/main/java/com/whyranoid/data/account/AccountRepositoryImpl.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package com.whyranoid.data.account
22

33
import com.whyranoid.domain.model.User
44
import com.whyranoid.domain.repository.AccountRepository
5+
import kotlinx.coroutines.flow.Flow
56
import javax.inject.Inject
67

78
// TODO AccountRepositoryImpl 재구현 필요!! 현재는 Fake 상태
8-
class AccountRepositoryImpl @Inject constructor() : AccountRepository {
9+
class AccountRepositoryImpl @Inject constructor(
10+
private val accountDataSource: AccountDataSource
11+
) : AccountRepository {
912
override suspend fun getUser(): Result<User> {
1013
return Result.success(User("byeonghee-uid", "병희", "github.com/bngsh"))
1114
}
@@ -18,16 +21,16 @@ class AccountRepositoryImpl @Inject constructor() : AccountRepository {
1821
return Result.success("byeonghee-uid")
1922
}
2023

21-
override suspend fun getNickname(): Result<String> {
22-
return Result.success("병희")
24+
override suspend fun getNickname(): Flow<String> {
25+
return accountDataSource.getUserNickName()
2326
}
2427

2528
override suspend fun updateNickname(newNickname: String): Boolean {
2629
return true
2730
}
2831

29-
override suspend fun getProfileUrl(): Result<String> {
30-
return Result.success("github.com/bngsh")
32+
override suspend fun getProfileUri(): Flow<String> {
33+
return accountDataSource.getUserProfileImgUri()
3134
}
3235

3336
override suspend fun updateProfileUrl(newProfileUrl: String): Boolean {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
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
8+
import com.whyranoid.data.account.AccountDataSource
9+
import com.whyranoid.data.account.AccountDataSourceImpl
310
import com.whyranoid.data.account.AccountRepositoryImpl
411
import com.whyranoid.domain.repository.AccountRepository
512
import dagger.Binds
613
import dagger.Module
14+
import dagger.Provides
715
import dagger.hilt.InstallIn
16+
import dagger.hilt.android.qualifiers.ApplicationContext
817
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+
}
935

1036
@Module
1137
@InstallIn(SingletonComponent::class)
1238
abstract class AccountModule {
1339

1440
@Binds
1541
abstract fun bindAccountRepository(accountRepositoryImpl: AccountRepositoryImpl): AccountRepository
42+
43+
@Binds
44+
abstract fun bindAccountDataSource(accountDataSourceImpl: AccountDataSourceImpl): AccountDataSource
1645
}

domain/src/main/java/com/whyranoid/domain/repository/AccountRepository.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.whyranoid.domain.repository
22

33
import com.whyranoid.domain.model.User
4+
import kotlinx.coroutines.flow.Flow
45

56
interface AccountRepository {
67

@@ -14,14 +15,14 @@ interface AccountRepository {
1415
suspend fun getUid(): Result<String>
1516

1617
// 데이터스토어에서 닉네임 가져오기
17-
suspend fun getNickname(): Result<String>
18+
suspend fun getNickname(): Flow<String>
1819

1920
// 닉네임 수정, 서버에 먼저 보내고 성공하면 로컬에 반영
2021
// 실패하면 실패 사용자에게 알리기
2122
suspend fun updateNickname(newNickname: String): Boolean
2223

2324
// 데이터스토어에서 프로필 이미지 가져오기
24-
suspend fun getProfileUrl(): Result<String>
25+
suspend fun getProfileUri(): Flow<String>
2526

2627
// 프로필 사진 서버에 업데이트
2728
suspend fun updateProfileUrl(newProfileUrl: String): Boolean
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.whyranoid.domain.usecase
22

33
import com.whyranoid.domain.repository.AccountRepository
4+
import kotlinx.coroutines.flow.Flow
45
import javax.inject.Inject
56

67
class GetNicknameUseCase @Inject constructor(private val accountRepository: AccountRepository) {
7-
suspend operator fun invoke(): Result<String> {
8+
suspend operator fun invoke(): Flow<String> {
89
return accountRepository.getNickname()
910
}
1011
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.whyranoid.domain.usecase
2+
3+
import com.whyranoid.domain.repository.AccountRepository
4+
import kotlinx.coroutines.flow.Flow
5+
import javax.inject.Inject
6+
7+
class GetProfileUriUseCase @Inject constructor(private val accountRepository: AccountRepository) {
8+
suspend operator fun invoke(): Flow<String> {
9+
return accountRepository.getProfileUri()
10+
}
11+
}

domain/src/main/java/com/whyranoid/domain/usecase/GetProfileUrlUseCase.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)