Skip to content

Commit

Permalink
#91 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
pknujsp committed Jun 7, 2023
1 parent 80e96bf commit 0481551
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AesCoder @Inject constructor(@ApplicationContext context: Context) {
}
if (extraLength > 0) {
val extra = "5".toByte()
IntRange(0, extraLength).forEachIndexed { index, i ->
IntRange(0, extraLength - 1).forEachIndexed { index, i ->
this[keyLength + index] = extra
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.android.mediproject.core.common.network.Dispatcher
import com.android.mediproject.core.common.network.MediDispatchers
import com.android.mediproject.core.data.remote.adminaction.AdminActionRepository
import com.android.mediproject.core.data.remote.adminaction.AdminActionRepositoryImpl
import com.android.mediproject.core.data.remote.ai.VertexAiRepository
import com.android.mediproject.core.data.remote.ai.VertexAiRepositoryImpl
import com.android.mediproject.core.data.remote.comments.CommentsRepository
import com.android.mediproject.core.data.remote.comments.CommentsRepositoryImpl
import com.android.mediproject.core.data.remote.dur.DurRepository
Expand All @@ -12,6 +14,8 @@ import com.android.mediproject.core.data.remote.elderlycaution.ElderlyCautionRep
import com.android.mediproject.core.data.remote.elderlycaution.ElderlyCautionRepositoryImpl
import com.android.mediproject.core.data.remote.granule.GranuleIdentificationRepository
import com.android.mediproject.core.data.remote.granule.GranuleIdentificationRepositoryImpl
import com.android.mediproject.core.data.remote.interestedmedicine.InterestedMedicineRepository
import com.android.mediproject.core.data.remote.interestedmedicine.InterestedMedicineRepositoryImpl
import com.android.mediproject.core.data.remote.medicineapproval.MedicineApprovalRepository
import com.android.mediproject.core.data.remote.medicineapproval.MedicineApprovalRepositoryImpl
import com.android.mediproject.core.data.remote.medicineid.MedicineIdRepository
Expand All @@ -20,21 +24,29 @@ import com.android.mediproject.core.data.remote.recallsuspension.RecallSuspensio
import com.android.mediproject.core.data.remote.recallsuspension.RecallSuspensionRepositoryImpl
import com.android.mediproject.core.data.remote.sign.SignRepository
import com.android.mediproject.core.data.remote.sign.SignRepositoryImpl
import com.android.mediproject.core.data.remote.user.UserInfoRepository
import com.android.mediproject.core.data.remote.user.UserInfoRepositoryImpl
import com.android.mediproject.core.data.remote.user.UserRepository
import com.android.mediproject.core.data.remote.user.UserRepositoryImpl
import com.android.mediproject.core.data.search.SearchHistoryRepository
import com.android.mediproject.core.data.search.SearchHistoryRepositoryImpl
import com.android.mediproject.core.database.searchhistory.SearchHistoryDao
import com.android.mediproject.core.datastore.AppDataStore
import com.android.mediproject.core.datastore.TokenDataSourceImpl
import com.android.mediproject.core.network.datasource.ai.VertextAiDataSource
import com.android.mediproject.core.network.datasource.comments.CommentsDataSource
import com.android.mediproject.core.network.datasource.dur.DurDataSource
import com.android.mediproject.core.network.datasource.elderlycaution.ElderlyCautionDataSource
import com.android.mediproject.core.network.datasource.granule.GranuleIdentificationDataSource
import com.android.mediproject.core.network.datasource.interestedmedicine.InterestedMedicineDataSource
import com.android.mediproject.core.network.datasource.medicineapproval.MedicineApprovalDataSource
import com.android.mediproject.core.network.datasource.medicineid.MedicineIdDataSource
import com.android.mediproject.core.network.datasource.penalties.adminaction.AdminActionListDataSourceImpl
import com.android.mediproject.core.network.datasource.penalties.recallsuspension.RecallSuspensionDataSource
import com.android.mediproject.core.network.datasource.penalties.recallsuspension.RecallSuspensionListDataSourceImpl
import com.android.mediproject.core.network.datasource.sign.SignDataSource
import com.android.mediproject.core.network.datasource.user.UserDataSource
import com.android.mediproject.core.network.datasource.user.UserInfoDataSource
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -50,7 +62,7 @@ object RepositoryModule {
@Singleton
fun provideUserRepository(
userDataSource: UserDataSource
) : UserRepository = UserRepositoryImpl(userDataSource)
): UserRepository = UserRepositoryImpl(userDataSource)

@Provides
@Singleton
Expand All @@ -62,26 +74,26 @@ object RepositoryModule {
fun provideMedicineApprovalRepository(
medicineApprovalDataSource: MedicineApprovalDataSource,
searchHistoryRepository: SearchHistoryRepository,
@Dispatcher(MediDispatchers.IO) ioDispatcher: CoroutineDispatcher): MedicineApprovalRepository =
MedicineApprovalRepositoryImpl(medicineApprovalDataSource, searchHistoryRepository, ioDispatcher)
@Dispatcher(MediDispatchers.IO) ioDispatcher: CoroutineDispatcher
): MedicineApprovalRepository = MedicineApprovalRepositoryImpl(medicineApprovalDataSource, searchHistoryRepository, ioDispatcher)

@Provides
@Singleton
fun provideRecallSuspensionRepository(
recallSuspensionDataSource: RecallSuspensionDataSource,
recallSuspensionListDataSource: RecallSuspensionListDataSourceImpl): RecallSuspensionRepository =
RecallSuspensionRepositoryImpl(recallSuspensionDataSource, recallSuspensionListDataSource)
recallSuspensionDataSource: RecallSuspensionDataSource, recallSuspensionListDataSource: RecallSuspensionListDataSourceImpl
): RecallSuspensionRepository = RecallSuspensionRepositoryImpl(recallSuspensionDataSource, recallSuspensionListDataSource)

@Provides
@Singleton
fun providesAdminActionRepository(
adminActionDataSource: AdminActionListDataSourceImpl): AdminActionRepository = AdminActionRepositoryImpl(adminActionDataSource)
adminActionDataSource: AdminActionListDataSourceImpl
): AdminActionRepository = AdminActionRepositoryImpl(adminActionDataSource)

@Provides
@Singleton
fun providesGranuleIdentificationRepository(
granuleIdentificationDataSource: GranuleIdentificationDataSource): GranuleIdentificationRepository =
GranuleIdentificationRepositoryImpl(granuleIdentificationDataSource)
granuleIdentificationDataSource: GranuleIdentificationDataSource
): GranuleIdentificationRepository = GranuleIdentificationRepositoryImpl(granuleIdentificationDataSource)

@Provides
@Singleton
Expand All @@ -95,7 +107,8 @@ object RepositoryModule {

@Provides
@Singleton
fun providesCommentsRepository(commentsDataSource: CommentsDataSource): CommentsRepository = CommentsRepositoryImpl(commentsDataSource)
fun providesCommentsRepository(commentsDataSource: CommentsDataSource, signRepository: SignRepository): CommentsRepository =
CommentsRepositoryImpl(commentsDataSource, signRepository)


@Provides
Expand Down Expand Up @@ -128,7 +141,7 @@ object RepositoryModule {
@Provides
@Singleton
fun providesUserInfoRepository(
userInfoDataSource: UserInfoDataSource
): UserInfoRepository = UserInfoRepositoryImpl(userInfoDataSource)
userInfoDataSource: UserInfoDataSource, appDataStore: AppDataStore
): UserInfoRepository = UserInfoRepositoryImpl(userInfoDataSource, appDataStore)

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.android.mediproject.core.network.datasource.sign.SignDataSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.zip
import javax.inject.Inject

class SignRepositoryImpl @Inject constructor(
Expand All @@ -31,32 +30,22 @@ class SignRepositoryImpl @Inject constructor(
* @return 응답받은 토큰
*/
override fun signIn(signInParameter: SignInParameter): Flow<Result<Unit>> = channelFlow {
signDataSource.signIn(signInParameter).zip(userInfoRepository.getMyAccountInfo()) { r1, r2 ->
r1 to r2
}.collect { resultPair ->
val signInResult = resultPair.first
val myAccountInfoResult = resultPair.second

// 두개 모두 성공하지 않은 경우에는 로그인 실패 처리
if (signInResult.isFailure || myAccountInfoResult.isFailure) {
signDataSource.signIn(signInParameter).collect { signInResult ->
// 로그인 실패 처리
if (signInResult.isFailure) {
// 로그인 실패
trySend(Result.failure(signInResult.exceptionOrNull() ?: myAccountInfoResult.exceptionOrNull() ?: Exception("로그인 실패")))
trySend(Result.failure(signInResult.exceptionOrNull() ?: Exception("로그인 실패")))
return@collect
}

// 로그인 성공
// 이메일 저장, 인트로 스킵, ID 저장
appDataStore.apply {
saveSkipIntro(true)
myAccountInfoResult.onSuccess {
signInResult.onSuccess {
// 내 계정 정보 메모리에 저장
userInfoRepository.updateMyAccountInfo(AccountState.SignedIn(it.userId, it.nickname, it.email))

if (signInParameter.isSavedEmail) {
saveMyAccountInfo(it.email, it.nickname, it.userId)
} else {
saveMyAccountInfo("", it.nickname, it.userId)
}
userInfoRepository.updateMyAccountInfo(AccountState.SignedIn(it._userId!!.toLong(), it._nickName!!, it._email!!))
saveMyAccountInfo(if (signInParameter.isSavedEmail) it._email!! else "", it._nickName!!, it._userId!!.toLong())
}
}

Expand All @@ -73,28 +62,23 @@ class SignRepositoryImpl @Inject constructor(
*/
override fun signUp(signUpParameter: SignUpParameter): Flow<Result<Unit>> = channelFlow {
// 회원가입 요청
signDataSource.signUp(signUpParameter).zip(userInfoRepository.getMyAccountInfo()) { r1, r2 ->
r1 to r2
}.collect { resultPair ->
val signUpResult = resultPair.first
val myAccountInfoResult = resultPair.second
signDataSource.signUp(signUpParameter).collect { signUpResult ->

// 두개 모두 성공하지 않은 경우에는 가입 실패 처리
if (signUpResult.isFailure || myAccountInfoResult.isFailure) {
if (signUpResult.isFailure) {
// 가입 실패
trySend(Result.failure(signUpResult.exceptionOrNull() ?: myAccountInfoResult.exceptionOrNull() ?: Exception("로그인 실패")))
trySend(Result.failure(signUpResult.exceptionOrNull() ?: Exception("로그인 실패")))
return@collect
}

// 가입 성공
// 인트로 스킵
appDataStore.apply {
saveSkipIntro(true)
myAccountInfoResult.onSuccess {
signUpResult.onSuccess {
// 내 계정 정보 메모리에 저장

userInfoRepository.updateMyAccountInfo(AccountState.SignedIn(it.userId, it.nickname, it.email))
saveMyAccountInfo("", it.nickname, it.userId)
userInfoRepository.updateMyAccountInfo(AccountState.SignedIn(it._userId!!.toLong(), it._nickName!!, it._email!!))
saveMyAccountInfo("", it._nickName!!, it._userId!!.toLong())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.android.mediproject.core.data.remote.user

import com.android.mediproject.core.data.remote.sign.SignRepository
import com.android.mediproject.core.model.remote.token.TokenState
import com.android.mediproject.core.datastore.AppDataStore
import com.android.mediproject.core.model.user.AccountState
import com.android.mediproject.core.network.datasource.user.UserInfoDataSource
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -10,12 +9,13 @@ import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.zip
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class UserInfoRepositoryImpl @Inject constructor(
private val userInfoDataSource: UserInfoDataSource
private val userInfoDataSource: UserInfoDataSource, private val appDataStore: AppDataStore
) : UserInfoRepository {

@Inject lateinit var signRepository: SignRepository

private val _myAccountInfo = MutableStateFlow<AccountState>(AccountState.Unknown)

Expand All @@ -29,20 +29,22 @@ class UserInfoRepositoryImpl @Inject constructor(
* 로그인 상태 확인, 동시에 토큰 처리
*/
override suspend fun loadAccountState() {
getMyAccountInfo().zip(signRepository.getCurrentTokens()) { r1, r2 -> r1 to r2 }.collect { results ->
val accountResult = results.first
val tokenResult = results.second
appDataStore.apply {
myAccountId.zip(userEmail) { id, email ->
id to email
}.zip(nickName) { (id, email), nickName ->
Triple(id, email, nickName)
}.collectLatest {
val id = it.first
val email = it.second
val nickName = it.third

accountResult.onSuccess {
if (tokenResult is TokenState.Valid) {
accountResult.onSuccess {
_myAccountInfo.value = AccountState.SignedIn(it.userId, it.nickname, it.email)
}
if (id != 0L && email.isNotEmpty() && nickName.isNotEmpty()) {
_myAccountInfo.value = AccountState.SignedIn(id, nickName, email)
} else {
_myAccountInfo.value = AccountState.Unknown
}
}.onFailure {
_myAccountInfo.value = AccountState.Unknown
}

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.android.mediproject.core.domain.user

import android.util.Log
import com.android.mediproject.core.data.remote.user.UserInfoRepository
import com.android.mediproject.core.data.remote.user.UserRepository
import com.android.mediproject.core.datastore.AppDataStore
import com.android.mediproject.core.model.requestparameters.ChangeNicknameParameter
import com.android.mediproject.core.model.requestparameters.ChangePasswordParamter
import com.android.mediproject.core.model.user.AccountState
import com.android.mediproject.core.model.user.UserDto
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
Expand All @@ -15,7 +17,8 @@ import javax.inject.Inject

class UserUseCase @Inject constructor(
private val appDataStore: AppDataStore,
private val userRepository: UserRepository
private val userRepository: UserRepository,
private val getUserInfoRepository: UserInfoRepository
) {
suspend operator fun invoke(): Flow<UserDto> = channelFlow {
appDataStore.nickName.collect { nickName ->
Expand All @@ -31,15 +34,18 @@ class UserUseCase @Inject constructor(
}

suspend fun changePassword(changePasswordParamter: ChangePasswordParamter) = channelFlow {
userRepository.changePassword(changePasswordParamter).map {
val email = (getUserInfoRepository.myAccountInfo.value as AccountState.SignedIn).email.toCharArray()
userRepository.changePassword(changePasswordParamter.apply {
this.email = email
}).map {
it.fold(onSuccess = { Result.success(it) }, onFailure = { Result.failure(it) })
}.collectLatest { trySend(it) }
}

suspend fun withdrawal() = channelFlow {
Log.d("wap", "UserUseCase : withdrawal()")
userRepository.withdrawal().map {
Log.d("wap", "UserUseCase : withdrawal()"+it.toString())
Log.d("wap", "UserUseCase : withdrawal()" + it.toString())
it.fold(onSuccess = { Result.success(it) }, onFailure = { Result.failure(it) })
}.collectLatest { trySend(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package com.android.mediproject.core.model.remote.sign


import com.android.mediproject.core.model.awscommon.BaseAwsSignResponse
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
class SignInResponse : BaseAwsSignResponse()
data class SignInResponse(
@SerialName("userId") val _userId: Long?,
@SerialName("nickname") val _nickName: String?,
@SerialName("email") val _email: String?,
) : BaseAwsSignResponse()
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package com.android.mediproject.core.model.remote.sign


import com.android.mediproject.core.model.awscommon.BaseAwsSignResponse
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
class SignUpResponse : BaseAwsSignResponse()
data class SignUpResponse(
@SerialName("userId") val _userId: Long?,
@SerialName("nickname") val _nickName: String?,
@SerialName("email") val _email: String?,
) : BaseAwsSignResponse()
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ import kotlinx.serialization.Serializable
data class ChangePasswordParamter(
val newPassword: CharArray
) {

var email: CharArray = CharArray(0)
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as ChangePasswordParamter

if (!newPassword.contentEquals(other.newPassword)) return false
if (!email.contentEquals(other.email)) return false

return true
}

override fun hashCode(): Int {
return newPassword.contentHashCode()
var result = newPassword.contentHashCode()
result = 31 * result + email.contentHashCode()
return result
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import java.lang.ref.WeakReference
import javax.inject.Inject

class SignDataSourceImpl @Inject constructor(
private val awsNetworkApi: AwsNetworkApi, private val tokenDataSource: TokenDataSource, private val aesCoder: AesCoder) :
private val awsNetworkApi: AwsNetworkApi, private val tokenDataSource: TokenDataSource, private val aesCoder: AesCoder
) :
SignDataSource {

/**
Expand All @@ -47,7 +48,7 @@ class SignDataSourceImpl @Inject constructor(
*/
override fun signUp(signUpParameter: SignUpParameter): Flow<Result<SignUpResponse>> = channelFlow {
val email = WeakReference(signUpParameter.email.joinToString("")).get()!!
val password = WeakReference(signUpParameter.password.hashCode().toString()).get()!!
val password = WeakReference(aesCoder.encodePassword(signUpParameter.email, signUpParameter.password)).get()!!

awsNetworkApi.signUp(SignUpRequestParameter(email, password, signUpParameter.nickName))
.onResponseWithTokens(RequestBehavior.NewTokens).fold(onSuccess = {
Expand Down
Loading

0 comments on commit 0481551

Please sign in to comment.