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/#113] 필터링 재설정 서버통신 구현 #132

Merged
merged 13 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ripple.LocalRippleTheme
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.terning.data.datasource

import com.terning.data.dto.BaseResponse
import com.terning.data.dto.NonDataBaseResponse
import com.terning.data.dto.request.ChangeFilterRequestDto
import com.terning.data.dto.response.HomeFilteringInfoResponseDto
import com.terning.data.dto.response.HomeRecommendInternResponseDto
import com.terning.data.dto.response.HomeTodayInternResponseDto

Expand All @@ -12,4 +15,8 @@ interface HomeDataSource {
startYear: Int,
startMonth: Int
): BaseResponse<List<HomeRecommendInternResponseDto>>

suspend fun getFilteringInfo(): BaseResponse<HomeFilteringInfoResponseDto>

suspend fun putFilteringInfo(changeFilterRequestDto: ChangeFilterRequestDto): NonDataBaseResponse
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.terning.data.datasourceimpl

import com.terning.data.datasource.HomeDataSource
import com.terning.data.dto.BaseResponse
import com.terning.data.dto.NonDataBaseResponse
import com.terning.data.dto.request.ChangeFilterRequestDto
import com.terning.data.dto.response.HomeFilteringInfoResponseDto
import com.terning.data.dto.response.HomeRecommendInternResponseDto
import com.terning.data.dto.response.HomeTodayInternResponseDto
import com.terning.data.service.HomeService
Expand All @@ -23,4 +26,10 @@ class HomeDataSourceImpl @Inject constructor(
startYear = startYear,
startMonth = startMonth
)

override suspend fun getFilteringInfo(): BaseResponse<HomeFilteringInfoResponseDto> =
homeService.getFilteringInfo()

override suspend fun putFilteringInfo(request: ChangeFilterRequestDto): NonDataBaseResponse =
homeService.putFilteringInfo(request)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.terning.data.dto.request

import com.terning.domain.entity.request.ChangeFilteringRequestModel
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ChangeFilterRequestDto(
@SerialName("grade")
val grade: Int,
@SerialName("workingPeriod")
val workingPeriod: Int,
@SerialName("startYear")
val startYear: Int,
@SerialName("startMonth")
val startMonth: Int,
)

fun ChangeFilteringRequestModel.toChangeFilterRequestDto(): ChangeFilterRequestDto =
ChangeFilterRequestDto(
grade = grade,
workingPeriod = workingPeriod,
startYear = startYear,
startMonth = startMonth,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.terning.data.dto.response

import com.terning.domain.entity.response.HomeFilteringInfoModel
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class HomeFilteringInfoResponseDto(
@SerialName("grade")
val grade: Int?,
@SerialName("workingPeriod")
val workingPeriod: Int?,
@SerialName("startYear")
val startYear: Int?,
@SerialName("startMonth")
val startMonth: Int?,
) {
fun toHomeFilteringInfoModel(): HomeFilteringInfoModel =
HomeFilteringInfoModel(
grade = this.grade,
workingPeriod = this.workingPeriod,
startYear = startYear,
startMonth = startMonth,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startYear랑 startMonth는 this를 빼준 의도가 있나용?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헉스 통일시키겠습니다!!

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.terning.data.repositoryimpl

import com.terning.data.datasource.HomeDataSource
import com.terning.data.dto.request.toChangeFilterRequestDto
import com.terning.domain.entity.request.ChangeFilteringRequestModel
import com.terning.domain.entity.response.HomeFilteringInfoModel
import com.terning.domain.entity.response.HomeRecommendInternModel
import com.terning.domain.entity.response.HomeTodayInternModel
import com.terning.domain.repository.HomeRepository
Expand Down Expand Up @@ -30,4 +33,16 @@ class HomeRepositoryImpl @Inject constructor(
it.toRecommendInternEntity()
}
}

override suspend fun getFilteringInfo(): Result<HomeFilteringInfoModel> =
runCatching {
homeDataSource.getFilteringInfo().result.toHomeFilteringInfoModel()
}

override suspend fun putFilteringInfo(putFilteringRequest: ChangeFilteringRequestModel): Result<Unit> =
runCatching {
homeDataSource.putFilteringInfo(
putFilteringRequest.toChangeFilterRequestDto()
)
}
}
13 changes: 13 additions & 0 deletions data/src/main/java/com/terning/data/service/HomeService.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.terning.data.service

import com.terning.data.dto.BaseResponse
import com.terning.data.dto.NonDataBaseResponse
import com.terning.data.dto.request.ChangeFilterRequestDto
import com.terning.data.dto.response.HomeFilteringInfoResponseDto
import com.terning.data.dto.response.HomeRecommendInternResponseDto
import com.terning.data.dto.response.HomeTodayInternResponseDto
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.PUT
import retrofit2.http.Query

interface HomeService {
Expand All @@ -16,4 +21,12 @@ interface HomeService {
@Query("startYear") startYear: Int,
@Query("startMonth") startMonth: Int,
): BaseResponse<List<HomeRecommendInternResponseDto>>

@GET("api/v1/filters")
suspend fun getFilteringInfo(): BaseResponse<HomeFilteringInfoResponseDto>

@PUT("api/v1/filters")
suspend fun putFilteringInfo(
@Body body: ChangeFilterRequestDto,
): NonDataBaseResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.terning.domain.entity.request

data class ChangeFilteringRequestModel(
val grade: Int,
val workingPeriod: Int,
val startYear: Int,
val startMonth: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.terning.domain.entity.response

data class HomeFilteringInfoModel(
val grade: Int?,
val workingPeriod: Int?,
val startYear: Int?,
val startMonth: Int?,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.terning.domain.repository

import com.terning.domain.entity.request.ChangeFilteringRequestModel
import com.terning.domain.entity.response.HomeFilteringInfoModel
import com.terning.domain.entity.response.HomeRecommendInternModel
import com.terning.domain.entity.response.HomeTodayInternModel

Expand All @@ -11,4 +13,10 @@ interface HomeRepository {
startYear: Int,
startMonth: Int
): Result<List<HomeRecommendInternModel>>

suspend fun getFilteringInfo(): Result<HomeFilteringInfoModel>

suspend fun putFilteringInfo(
putFilteringRequest: ChangeFilteringRequestModel,
): Result<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,90 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.navigation.NavController
import com.terning.core.designsystem.component.button.RectangleButton
import com.terning.core.designsystem.component.datepicker.DatePickerUI
import com.terning.core.designsystem.component.topappbar.BackButtonTopAppBar
import com.terning.core.designsystem.theme.TerningTheme
import com.terning.core.extension.toast
import com.terning.core.state.UiState
import com.terning.domain.entity.request.ChangeFilteringRequestModel
import com.terning.domain.entity.response.HomeFilteringInfoModel
import com.terning.feature.R
import com.terning.feature.home.changefilter.component.ChangeFilteringRadioGroup
import com.terning.feature.home.changefilter.component.FilteringMainTitleText
import com.terning.feature.home.changefilter.component.FilteringSubTitleText
import com.terning.feature.home.changefilter.navigation.navigateChangeFilter
import com.terning.feature.home.home.HomeSideEffect
import com.terning.feature.home.home.HomeViewModel
import com.terning.feature.home.home.model.InternFilterData
import com.terning.feature.home.home.model.UserNameState
import com.terning.feature.home.home.navigation.navigateHome
import java.util.Calendar

val currentYear = Calendar.getInstance().get(Calendar.YEAR)
val currentMonth = Calendar.getInstance().get(Calendar.MONTH)

@Composable
fun ChangeFilterRoute(
navController: NavController,
viewModel: HomeViewModel = hiltViewModel(),
) {
ChangeFilterScreen(navController)
val lifecycleOwner = LocalLifecycleOwner.current
val context = LocalContext.current

val filteringState by viewModel.homeFilteringState.collectAsStateWithLifecycle()

when (filteringState) {
is UiState.Success -> ChangeFilterScreen(
(filteringState as UiState.Success<HomeFilteringInfoModel>).data,
navController,
viewModel,
)

else -> {}
}

LaunchedEffect(viewModel.homeSideEffect, lifecycleOwner) {
viewModel.homeSideEffect.flowWithLifecycle(lifecycle = lifecycleOwner.lifecycle)
.collect { sideEffect ->
when (sideEffect) {
is HomeSideEffect.ShowToast -> context.toast(sideEffect.message)
is HomeSideEffect.NavigateToChangeFilter -> navController.navigateChangeFilter()
is HomeSideEffect.NavigateToHome -> navController.navigateHome()
}
}
}
}

@Composable
fun ChangeFilterScreen(
filterData: HomeFilteringInfoModel,
navController: NavController,
viewModel: HomeViewModel = hiltViewModel(),
viewModel: HomeViewModel,
) {
val isGradeButtonValid = remember {
mutableStateOf(viewModel.userName.value.internFilter?.grade != null)
var currentGrade by remember { mutableIntStateOf(filterData.grade ?: -1) }
var currentWorkingPeriod by remember { mutableIntStateOf(filterData.workingPeriod ?: -1) }
var currentStartYear by remember {
mutableIntStateOf(
filterData.startYear ?: viewModel.currentYear
)
}

val isWorkingPeriodButtonValid = remember {
mutableStateOf(viewModel.userName.value.internFilter?.workingPeriod != null)
var currentStartMonth by remember {
mutableIntStateOf(
filterData.startMonth ?: viewModel.currentMonth
)
}


Scaffold(
topBar = {
BackButtonTopAppBar(
Expand All @@ -76,23 +115,18 @@ fun ChangeFilterScreen(
)
)
ChangeFilteringRadioGroup(
filterType = 0,
internFilterData = viewModel.userName.value.internFilter,
onButtonClick = {
isGradeButtonValid.value = true
initOption = filterData.grade ?: -1,
optionList = listOf(
R.string.filtering_status1_button1,
R.string.filtering_status1_button2,
R.string.filtering_status1_button3,
R.string.filtering_status1_button4,
),
onButtonClick = { index ->
currentGrade = index
}
)

UserNameState(
userName = "남지우자랑스러운티엘이되",
internFilter = InternFilterData(
grade = 4,
workingPeriod = 1,
startYear = 2024,
startMonth = 7,
)
)

ShowTitle(
mainTitle = stringResource(id = R.string.filtering_status2_title),
subTitle = stringResource(id = R.string.filtering_status2_sub),
Expand All @@ -103,10 +137,14 @@ fun ChangeFilterScreen(
)
)
ChangeFilteringRadioGroup(
filterType = 1,
internFilterData = viewModel.userName.value.internFilter,
onButtonClick = {
isWorkingPeriodButtonValid.value = true
initOption = filterData.workingPeriod ?: -1,
optionList = listOf(
R.string.filtering_status2_button1,
R.string.filtering_status2_button2,
R.string.filtering_status2_button3,
),
onButtonClick = { index ->
currentWorkingPeriod = index
}
)

Expand All @@ -122,8 +160,10 @@ fun ChangeFilterScreen(

Spacer(modifier = Modifier.weight(1f))
DatePickerUI(
chosenYear = currentYear,
chosenMonth = currentMonth,
chosenYear = filterData.startYear ?: currentStartYear,
chosenMonth = filterData.startMonth ?: currentStartMonth,
onYearChosen = { currentStartYear = it },
onMonthChosen = { currentStartMonth = it }
)
Spacer(modifier = Modifier.weight(1f))

Expand All @@ -132,9 +172,16 @@ fun ChangeFilterScreen(
paddingVertical = 19.dp,
text = R.string.change_filter_save,
onButtonClick = {
navController.navigateHome()
viewModel.putFilteringInfo(
ChangeFilteringRequestModel(
grade = currentGrade,
workingPeriod = currentWorkingPeriod,
startYear = currentStartYear,
startMonth = currentStartMonth,
)
)
},
isEnabled = isGradeButtonValid.value && isWorkingPeriodButtonValid.value
isEnabled = currentGrade in 0..3 && currentWorkingPeriod in 0..2
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요거 상수화로 빼주면 더 좋을 것 같아요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 좋아요!!

}
}
Expand Down
Loading
Loading