Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#160 정책 api 마이그레이션 #164

Merged
merged 12 commits into from
Jul 20, 2024
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {
implementation(project(":feature:postdetail"))
implementation(project(":feature:profileeditor"))
implementation(project(":feature:policydetail"))
implementation(project(":feature:policybookmarks"))
implementation(project(":core:ui"))
implementation(project(":core:interceptor"))
implementation(project(":core:data"))
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/withpeace/withpeace/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class MainActivity : ComponentActivity() {
"네트워크 상태가 원활하지 않습니다.",
Toast.LENGTH_SHORT,
).show()
finish()
finish() // 강제 업데이트
}
MainUiState.Loading -> {}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/withpeace/withpeace/WithpeaceApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fun WithpeaceApp(
val snackBarHostState = remember { SnackbarHostState() }
var snackBarState: SnackbarState =
remember { SnackbarState("", SnackbarType.Normal) }

val coroutineScope = rememberCoroutineScope()
fun showSnackBar(snackbarState: SnackbarState) = coroutineScope.launch {
snackBarState = snackbarState
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/withpeace/withpeace/navigation/NavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import com.withpeace.withpeace.feature.signup.navigation.navigateSignUp
import com.withpeace.withpeace.feature.signup.navigation.signUpNavGraph
import com.withpeace.withpeace.feature.termsofservice.navigation.navigateToTermsOfService
import com.withpeace.withpeace.feature.termsofservice.navigation.termsOfServiceGraph
import navigatePolicyBookmarks
import policyBookmarksNavGraph

@Composable
fun WithpeaceNavHost(
Expand Down Expand Up @@ -166,7 +168,7 @@ fun WithpeaceNavHost(
homeNavGraph(
onShowSnackBar = { onShowSnackBar(SnackbarState(it)) },
onPolicyClick = {
navController.navigateToPolicyDetail(policy = it)
navController.navigateToPolicyDetail(policyId = it)
},
)
policyDetailNavGraph(
Expand Down Expand Up @@ -203,6 +205,9 @@ fun WithpeaceNavHost(
onAuthExpired = {
onAuthExpired(onShowSnackBar, navController)
},
onDibsOfPolicyClick = {
navController.navigatePolicyBookmarks()
},
)
profileEditorNavGraph(
onShowSnackBar = { onShowSnackBar(SnackbarState(it)) },
Expand All @@ -224,6 +229,12 @@ fun WithpeaceNavHost(

},
)
policyBookmarksNavGraph(
onShowSnackBar = { onShowSnackBar(SnackbarState(it)) },
onClickBackButton = {
navController.popBackStack()
}
)
}
postDetailGraph(
onShowSnackBar = { onShowSnackBar(SnackbarState(it)) },
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.withpeace.withpeace.core.data.mapper.youthpolicy

import com.withpeace.withpeace.core.domain.model.policy.PolicyClassification
import com.withpeace.withpeace.core.domain.model.policy.YouthPolicyDetail
import com.withpeace.withpeace.core.network.di.response.policy.PolicyDetailResponse

internal fun PolicyDetailResponse.toDomain(): YouthPolicyDetail {
return YouthPolicyDetail(
id = id,
title = title,
introduce = introduce,
ageInfo = ageInfo,
classification = classification.codeToPolicyClassification(),

applicationDetails = applicationDetails,
residenceAndIncome = residenceAndIncome,
education = education,
specialization = specialization,
additionalNotes = additionalNotes,
participationRestrictions = participationRestrictions,
applicationProcess = applicationProcess,
screeningAndAnnouncement = screeningAndAnnouncement,
applicationSite = applicationSite,
submissionDocuments = submissionDocuments,
additionalUsefulInformation = additionalUsefulInformation,
supervisingAuthority = supervisingAuthority,
operatingOrganization = operatingOrganization,
businessRelatedReferenceSite1 = businessRelatedReferenceSite1,
businessRelatedReferenceSite2 = businessRelatedReferenceSite2,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.withpeace.withpeace.core.data.mapper.youthpolicy

import com.withpeace.withpeace.core.domain.model.policy.PolicyClassification
import com.withpeace.withpeace.core.domain.model.policy.PolicyRegion
import com.withpeace.withpeace.core.domain.model.policy.YouthPolicy
import com.withpeace.withpeace.core.network.di.response.policy.PolicyResponse

internal fun PolicyResponse.toDomain(): YouthPolicy {
return YouthPolicy(
id = id,
title = title,
introduce = introduce,
region = region.codeToRegion(),
policyClassification = classification.codeToPolicyClassification(),
ageInfo = ageInfo,
)
}

internal fun String?.codeToRegion(): PolicyRegion {
return PolicyRegion.entries.find { it.toString() == this } ?: PolicyRegion.기타
}

internal fun PolicyRegion.toCode(): String {
return when (this) {
PolicyRegion.중앙부처 -> "003001"
PolicyRegion.서울 -> "003002001"
PolicyRegion.부산 -> "003002002"
PolicyRegion.대구 -> "003002003"
PolicyRegion.인천 -> "003002004"
PolicyRegion.광주 -> "003002005"
PolicyRegion.대전 -> "003002006"
PolicyRegion.울산 -> "003002007"
PolicyRegion.경기 -> "003002008"
PolicyRegion.강원 -> "003002009"
PolicyRegion.충북 -> "003002010"
PolicyRegion.충남 -> "003002011"
PolicyRegion.전북 -> "003002012"
PolicyRegion.전남 -> "003002013"
PolicyRegion.경북 -> "003002014"
PolicyRegion.경남 -> "003002015"
PolicyRegion.제주 -> "003002016"
PolicyRegion.세종 -> "003002017"
PolicyRegion.기타 -> throw IllegalStateException("찾을 수 없는 지역입니다.")
}
}

internal fun String?.codeToPolicyClassification(): PolicyClassification {
return PolicyClassification.entries.find { it.toString() == this } ?: PolicyClassification.ETC
}

internal fun PolicyClassification.toCode(): String {
return when (this) {
PolicyClassification.JOB -> "023010"
PolicyClassification.RESIDENT -> "023020"
PolicyClassification.EDUCATION -> "023030"
PolicyClassification.WELFARE_AND_CULTURE -> "023040"
PolicyClassification.PARTICIPATION_AND_RIGHT -> "023050"
PolicyClassification.ETC -> throw IllegalStateException("정책 분류를 찾을 수 없습니다.")
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.withpeace.withpeace.core.data.paging

import android.util.Log
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.skydoves.sandwich.ApiResponse
import com.withpeace.withpeace.core.data.BuildConfig
import com.withpeace.withpeace.core.data.mapper.toCode
import com.withpeace.withpeace.core.data.mapper.toDomain
import com.withpeace.withpeace.core.data.mapper.youthpolicy.toCode
import com.withpeace.withpeace.core.data.mapper.youthpolicy.toDomain
import com.withpeace.withpeace.core.domain.model.error.CheonghaError
import com.withpeace.withpeace.core.domain.model.error.ResponseError
import com.withpeace.withpeace.core.domain.model.policy.PolicyFilters
Expand All @@ -23,15 +21,12 @@ class YouthPolicyPagingSource(
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, YouthPolicy> {
val pageIndex = params.key ?: 1
val response = youthPolicyService.getPolicies(
apiKey = BuildConfig.YOUTH_POLICY_API_KEY,
pageSize = params.loadSize,
display = params.loadSize,
pageIndex = pageIndex,
classification = filterInfo.classifications.joinToString(",") { it.toCode() },
region = filterInfo.regions.joinToString(",") { it.toCode() },
classification = filterInfo.classifications.joinToString(",") { it.toCode() },
)



if (response is ApiResponse.Failure) {
val error: CheonghaError =
if (response is ApiResponse.Failure.Exception) ResponseError.HTTP_EXCEPTION_ERROR
Expand All @@ -40,11 +35,11 @@ class YouthPolicyPagingSource(
return LoadResult.Error(IllegalStateException("API response error that $error"))
}

val data = (response as ApiResponse.Success).data
val successResponse = (response as ApiResponse.Success).data
return LoadResult.Page(
data = data.youthPolicyEntity?.map { it.toDomain() } ?: emptyList(),
data = successResponse.data.map { it.toDomain() },
prevKey = if (pageIndex == STARTING_PAGE_INDEX) null else pageIndex - 1,
nextKey = if (response.data.youthPolicyEntity.isNullOrEmpty()) null else pageIndex + (params.loadSize / pageSize),
nextKey = if (successResponse.data.isEmpty()) null else pageIndex + (params.loadSize / pageSize),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ package com.withpeace.withpeace.core.data.repository
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.skydoves.sandwich.suspendMapSuccess
import com.skydoves.sandwich.suspendOnError
import com.withpeace.withpeace.core.data.mapper.youthpolicy.toDomain
import com.withpeace.withpeace.core.data.paging.YouthPolicyPagingSource
import com.withpeace.withpeace.core.data.util.handleApiFailure
import com.withpeace.withpeace.core.domain.model.error.CheonghaError
import com.withpeace.withpeace.core.domain.model.error.ClientError
import com.withpeace.withpeace.core.domain.model.error.ResponseError
import com.withpeace.withpeace.core.domain.model.policy.PolicyFilters
import com.withpeace.withpeace.core.domain.model.policy.YouthPolicy
import com.withpeace.withpeace.core.domain.model.policy.YouthPolicyDetail
import com.withpeace.withpeace.core.domain.repository.UserRepository
import com.withpeace.withpeace.core.domain.repository.YouthPolicyRepository
import com.withpeace.withpeace.core.network.di.service.YouthPolicyService
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class DefaultYouthPolicyRepository @Inject constructor(
private val youthPolicyService: YouthPolicyService,
private val userRepository: UserRepository,
) : YouthPolicyRepository {
override fun getPolicies(
filterInfo: PolicyFilters,
Expand All @@ -30,6 +40,30 @@ class DefaultYouthPolicyRepository @Inject constructor(
},
).flow

override fun getPolicy(
policyId: String,
onError: suspend (CheonghaError) -> Unit,
): Flow<YouthPolicyDetail> = flow {
youthPolicyService.getPolicyDetail(policyId).suspendMapSuccess {
emit(data.toDomain())
}.handleApiFailure {
onErrorWithAuthExpired(it, onError)
}
}

private suspend fun onErrorWithAuthExpired(
it: ResponseError,
onError: suspend (CheonghaError) -> Unit,
) {
if (it == ResponseError.INVALID_TOKEN_ERROR) {
userRepository.logout(onError).collect {
onError(ClientError.AuthExpired)
}
} else {
onError(it)
}
}

companion object {
private const val PAGE_SIZE = 10
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ fun WithPeaceBackButtonTopAppBar(
navigationIcon = {
Icon(
modifier =
Modifier
.padding(start = 20.dp, bottom = 16.dp, top = 16.dp, end = 28.dp)
.clickable {
onClickBackButton()
}
.padding(4.dp),
Modifier
.padding(start = 20.dp, bottom = 16.dp, top = 16.dp, end = 28.dp)
.clickable {
onClickBackButton()
}
.padding(4.dp),
painter = painterResource(id = R.drawable.ic_backarrow_left),
contentDescription = "BackArrowLeft",
)
Expand Down
Loading
Loading