Skip to content

Commit 576fbe9

Browse files
committed
FEAT: 멤버 이름 로컬에 저장 처리
1 parent 5119616 commit 576fbe9

File tree

7 files changed

+80
-18
lines changed

7 files changed

+80
-18
lines changed

core/data/src/main/java/com/plottwist/core/data/auth/repository/AuthRepositoryImpl.kt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.plottwist.core.data.auth.repository
22

33
import com.plottwist.core.data.common.DeviceInfoProvider
44
import com.plottwist.core.domain.auth.repository.AuthRepository
5+
import com.plottwist.core.domain.onboarding.OnboardingRepository
56
import com.plottwist.core.domain.push.repository.PushRepository
67
import com.plottwist.core.network.model.auth.DeviceInfo
78
import com.plottwist.core.network.model.auth.GoogleLoginRequest
@@ -13,6 +14,7 @@ import kotlinx.coroutines.flow.Flow
1314
import kotlinx.coroutines.flow.collect
1415
import kotlinx.coroutines.flow.combine
1516
import kotlinx.coroutines.flow.firstOrNull
17+
import kotlinx.coroutines.flow.onEach
1618
import javax.inject.Inject
1719

1820

@@ -21,7 +23,8 @@ class AuthRepositoryImpl @Inject constructor(
2123
private val authDataSource: AuthDataSource,
2224
private val deviceInfoProvider: DeviceInfoProvider,
2325
private val pushRepository: PushRepository,
24-
private val tukApiService: TukApiService
26+
private val tukApiService: TukApiService,
27+
private val onboardingRepository: OnboardingRepository
2528
) : AuthRepository {
2629

2730
override suspend fun googleLogin(accountId: String): Result<Boolean> {
@@ -46,9 +49,17 @@ class AuthRepositoryImpl @Inject constructor(
4649
val result = response.data
4750
authDataSource.setAccessToken(result.accessToken).collect()
4851
authDataSource.setRefreshToken(result.refreshToken).collect()
49-
authDataSource.setOnboardingCompleted(!result.isFirstLogin).collect()
5052

51-
Result.success(!result.isFirstLogin)
53+
if(!result.isFirstLogin) {
54+
val name = onboardingRepository.getMemberInfo().getOrNull()?.name
55+
if(name.isNullOrEmpty()) {
56+
return Result.success(false)
57+
}else {
58+
Result.success(true)
59+
}
60+
} else {
61+
Result.success(false)
62+
}
5263

5364
} else {
5465
Result.failure(Exception("Fail Google Login"))
@@ -62,9 +73,9 @@ class AuthRepositoryImpl @Inject constructor(
6273
override fun checkLoginStatus(): Flow<Boolean> {
6374
return combine(
6475
authDataSource.getAccessToken(),
65-
authDataSource.getOnboardingCompleted()
66-
) { accessToken, onboardingCompleted ->
67-
accessToken?.isNotEmpty() ?: false && onboardingCompleted ?: false
76+
authDataSource.getMemberName()
77+
) { accessToken, name ->
78+
accessToken?.isNotEmpty() ?: false && !name.isNullOrEmpty()
6879
}
6980
}
7081

@@ -98,6 +109,7 @@ class AuthRepositoryImpl @Inject constructor(
98109
)
99110

100111
if (response.success) {
112+
setMemberName(name).collect()
101113
Result.success(Unit)
102114
} else {
103115
Result.failure(Exception("Fail Delete member"))
@@ -106,4 +118,12 @@ class AuthRepositoryImpl @Inject constructor(
106118
Result.failure(e)
107119
}
108120
}
121+
122+
override fun getMemberName(): Flow<String?> {
123+
return authDataSource.getMemberName()
124+
}
125+
126+
override fun setMemberName(name: String): Flow<Unit> {
127+
return authDataSource.setMemberName(name)
128+
}
109129
}

core/data/src/main/java/com/plottwist/core/data/onboarding/OnboardingRepositoryImpl.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class OnboardingRepositoryImpl @Inject constructor(
2020
val result = onboardingService.updateOnboardingInfo(UpdateOnboardingInfoRequest(name))
2121

2222
if (result.success) {
23-
authDataSource.setOnboardingCompleted(true).collect()
23+
authDataSource.setMemberName(name).collect()
2424
return Result.success(Unit)
2525
}
2626

@@ -35,6 +35,7 @@ class OnboardingRepositoryImpl @Inject constructor(
3535
val result = onboardingService.getMemberInfo()
3636

3737
if (result.success) {
38+
authDataSource.setMemberName(result.data.name).collect()
3839
return Result.success(result.data.toDomainModel())
3940
}
4041

core/domain/src/main/java/com/plottwist/core/domain/auth/repository/AuthRepository.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ interface AuthRepository {
99
fun getAccessToken() : Flow<String?>
1010
suspend fun deleteAccount(): Result<Boolean>
1111
suspend fun updateMemberName(name: String): Result<Unit>
12+
fun getMemberName(): Flow<String?>
13+
fun setMemberName(name: String): Flow<Unit>
1214
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.plottwist.core.domain.auth.usecase
2+
3+
import com.plottwist.core.domain.auth.repository.AuthRepository
4+
import kotlinx.coroutines.flow.Flow
5+
import javax.inject.Inject
6+
7+
class GetMemberNameUseCase @Inject constructor(
8+
private val authRepository: AuthRepository
9+
) {
10+
operator fun invoke(): Flow<String?> {
11+
return authRepository.getMemberName()
12+
}
13+
}

core/preference/src/main/java/com/plottwist/core/preference/datasource/AuthDataSource.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ interface AuthDataSource {
66

77
fun getAccessToken(): Flow<String?>
88
fun getRefreshToken(): Flow<String?>
9-
fun getOnboardingCompleted(): Flow<Boolean?>
9+
fun getMemberName(): Flow<String?>
1010

1111
fun setAccessToken(accessToken: String): Flow<Unit>
1212
fun setRefreshToken(refreshToken: String): Flow<Unit>
13-
fun setOnboardingCompleted(completed: Boolean): Flow<Unit>
13+
fun setMemberName(name: String): Flow<Unit>
1414

1515
fun clear(): Flow<Unit>
1616
}

core/preference/src/main/java/com/plottwist/core/preference/datasource/AuthDataSourceImpl.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.plottwist.core.preference.datasource
22

33
import androidx.datastore.core.DataStore
44
import androidx.datastore.preferences.core.Preferences
5-
import androidx.datastore.preferences.core.booleanPreferencesKey
65
import androidx.datastore.preferences.core.edit
76
import androidx.datastore.preferences.core.stringPreferencesKey
87
import kotlinx.coroutines.flow.Flow
@@ -24,9 +23,9 @@ class AuthDataSourceImpl @Inject constructor(
2423
preferences[REFRESH_TOKEN]
2524
}
2625

27-
override fun getOnboardingCompleted(): Flow<Boolean?> =
26+
override fun getMemberName(): Flow<String?> =
2827
dataStore.data.map { preferences ->
29-
preferences[ONBOARDING_COMPLETED]
28+
preferences[MEMBER_NAME]
3029
}
3130

3231

@@ -42,23 +41,24 @@ class AuthDataSourceImpl @Inject constructor(
4241
}
4342
}
4443

45-
override fun setOnboardingCompleted(completed: Boolean): Flow<Unit> = flow {
44+
override fun setMemberName(name: String): Flow<Unit> = flow {
4645
dataStore.edit { preferences ->
47-
preferences[ONBOARDING_COMPLETED] = completed
46+
preferences[MEMBER_NAME] = name
4847
}
4948
}
5049

5150
override fun clear(): Flow<Unit> = flow {
5251
dataStore.edit { preferences ->
5352
preferences[ACCESS_TOKEN] = ""
5453
preferences[REFRESH_TOKEN] = ""
54+
preferences[MEMBER_NAME] = ""
5555
}
5656
}
5757

5858
companion object {
5959
const val TOKEN_PREFERENCES_NAME = "token_preferences"
6060
private val ACCESS_TOKEN = stringPreferencesKey("access_token")
6161
private val REFRESH_TOKEN = stringPreferencesKey("refresh_token")
62-
private val ONBOARDING_COMPLETED = booleanPreferencesKey("onboarding_completed")
62+
private val MEMBER_NAME = stringPreferencesKey("member_name")
6363
}
6464
}

feature/home/src/main/java/com/plottwist/feature/home/HomeViewModel.kt

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.plottwist.feature.home
33
import androidx.compose.foundation.text.input.TextFieldState
44
import androidx.lifecycle.ViewModel
55
import com.plottwist.core.domain.auth.usecase.CheckLoginStatusUseCase
6+
import com.plottwist.core.domain.auth.usecase.GetMemberNameUseCase
67
import com.plottwist.core.domain.gathering.usecase.GetGatheringsUseCase
78
import com.plottwist.core.domain.gathering.usecase.GetPurposesUseCase
89
import com.plottwist.core.domain.model.Purposes
@@ -11,6 +12,7 @@ import com.plottwist.core.domain.onboarding.usecase.UpdateDeviceTokenUseCase
1112
import com.plottwist.core.ui.UiState
1213
import com.plottwist.core.weburl.WebUrlConfig
1314
import dagger.hilt.android.lifecycle.HiltViewModel
15+
import kotlinx.coroutines.flow.catch
1416
import kotlinx.coroutines.flow.collectLatest
1517
import kotlinx.coroutines.flow.distinctUntilChanged
1618
import kotlinx.coroutines.flow.map
@@ -26,12 +28,13 @@ class HomeViewModel @Inject constructor(
2628
private val getPurposesUseCase: GetPurposesUseCase,
2729
private val webViewConfig: WebUrlConfig,
2830
private val updateDeviceTokenUseCase: UpdateDeviceTokenUseCase,
29-
private val getMemberInfoUseCase: GetMemberInfoUseCase
31+
private val getMemberInfoUseCase: GetMemberInfoUseCase,
32+
private val getMemberNameUseCase: GetMemberNameUseCase
3033
) : ContainerHost<HomeState, HomeSideEffect>, ViewModel() {
3134
override val container = container<HomeState, HomeSideEffect>(HomeState()) {
3235
observeLoginState()
36+
observeMemberName()
3337
getPurposes()
34-
getMemberInfo()
3538
}
3639

3740
private fun getPurposes() = intent {
@@ -122,7 +125,6 @@ class HomeViewModel @Inject constructor(
122125
}.distinctUntilChanged().collectLatest { loginState ->
123126
when (loginState) {
124127
LoginState.LoggedIn -> {
125-
getMemberInfo()
126128
fetchGatherings(loginState)
127129
updateDeviceTokenUseCase()
128130
postSideEffect(HomeSideEffect.RequestNotificationPermission)
@@ -140,6 +142,30 @@ class HomeViewModel @Inject constructor(
140142
}
141143
}
142144

145+
private fun observeMemberName() = intent {
146+
getMemberNameUseCase().catch {
147+
reduce {
148+
state.copy(
149+
userName = UiState.Error
150+
)
151+
}
152+
}.collectLatest { name ->
153+
if(name.isNullOrEmpty()){
154+
reduce {
155+
state.copy(
156+
userName = UiState.Error
157+
)
158+
}
159+
} else {
160+
reduce {
161+
state.copy(
162+
userName = UiState.Success(name)
163+
)
164+
}
165+
}
166+
}
167+
}
168+
143169
private fun fetchGatherings(loginState: LoginState) = intent {
144170
val result = getGatheringsUseCase()
145171

0 commit comments

Comments
 (0)