Skip to content

Commit

Permalink
#219 SignRepositoryImpl에서 AccountSessionRepository 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
pknujsp committed Mar 11, 2024
1 parent 163e062 commit 5f11a9a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession

interface AccountSessionRepository {
val session: CognitoUserSession?
val isSignedIn: Boolean
val signedIn: Boolean
suspend fun updateSession(session: CognitoUserSession?)
suspend fun updateAccount(email: String, nickName: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.android.mediproject.core.data.sign

import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession
import com.android.mediproject.core.datastore.AppDataStore

class AccountSessionRepositoryImpl(
private val appDataStore: AppDataStore,
) : AccountSessionRepository {

private var mutableSession: CognitoUserSession? = null
override val session: CognitoUserSession? get() = mutableSession

override val signedIn: Boolean
get() = session != null

override suspend fun updateSession(session: CognitoUserSession?) {
mutableSession = session
if (session == null) {
appDataStore.clearMyAccountInfo()
}
}

override suspend fun updateAccount(email: String, nickName: String) {
appDataStore.saveMyAccountInfo(email, nickName)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.android.mediproject.core.data.sign

import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserSession
import com.amazonaws.services.cognitoidentityprovider.model.UserNotConfirmedException
import com.android.mediproject.core.datastore.AppDataStore
import com.android.mediproject.core.model.sign.LoginParameter
Expand All @@ -9,34 +8,16 @@ import com.android.mediproject.core.network.datasource.sign.SignDataSource

internal class SignRepositoryImpl(
private val signDataSource: SignDataSource,
private val accountSessionRepository: AccountSessionRepository,
private val appDataStore: AppDataStore,
private val userInfoRepository: UserInfoRepository,
) : SignRepository, AccountSessionRepository {
) : SignRepository {

private var _session: CognitoUserSession? = null
override val session: CognitoUserSession? get() = _session

override val isSignedIn: Boolean
get() = session != null

override suspend fun login(loginParameter: LoginParameter) = signDataSource.logIn(loginParameter).fold(
onSuccess = {
_session = it.userSession
appDataStore.run {
saveSkipIntro(true)
userInfoRepository.updateMyAccountInfo(
AccountState.SignedIn(
myId = 0L,
email = loginParameter.email,
myNickName = it.userSession.username,
),
)
saveMyAccountInfo(
userEmail = loginParameter.email,
nickName = it.userSession.username,
myAccountId = 0L,
)
}
accountSessionRepository.updateSession(it.userSession)
accountSessionRepository.updateAccount(loginParameter.email, it.userSession.username)
appDataStore.saveSkipIntro(true)
LoginState.Success
},
onFailure = {
Expand All @@ -57,7 +38,7 @@ internal class SignRepositoryImpl(
}

override suspend fun signOut() {
_session = null
accountSessionRepository.updateSession(null)
signDataSource.signOut()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ interface AppDataStore {
val nickName: Flow<String>
val skipIntro: Flow<Boolean>
val myAccountId: Flow<Long>
suspend fun saveMyAccountInfo(userEmail: String, nickName: String, myAccountId: Long)
suspend fun saveMyAccountInfo(email: String, nickName: String)
suspend fun saveNickName(nickName: String)
suspend fun saveSkipIntro(skipIntro: Boolean)
suspend fun clearMyAccountInfo()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import javax.inject.Inject


class AppDataStoreImpl @Inject constructor(
@ApplicationContext private val context: Context
@ApplicationContext private val context: Context,
) : AppDataStore {
private val Context.dataStore by preferencesDataStore("user_preferences")

Expand All @@ -36,9 +36,9 @@ class AppDataStoreImpl @Inject constructor(
}


override suspend fun saveMyAccountInfo(userEmail: String, nickName: String, myAccountId: Long) {
override suspend fun saveMyAccountInfo(email: String, nickName: String) {
context.dataStore.edit {
it[KEY_USER_EMAIL] = userEmail
it[KEY_USER_EMAIL] = email
it[KEY_NICK_NAME] = nickName
it[KEY_MY_ACCOUNT_ID] = myAccountId
}
Expand All @@ -61,4 +61,4 @@ class AppDataStoreImpl @Inject constructor(
}
}

}
}

0 comments on commit 5f11a9a

Please sign in to comment.