Skip to content

Commit

Permalink
[FEAT/#309] 데이트피커 명세서대로 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeyubin committed Dec 20, 2024
1 parent 39f647b commit 23f2f6a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand All @@ -34,6 +35,10 @@ import com.terning.core.designsystem.theme.Grey200
import com.terning.core.designsystem.theme.TerningTheme
import com.terning.core.designsystem.type.Grade
import com.terning.core.designsystem.type.WorkingPeriod
import com.terning.core.designsystem.util.CalendarDefaults.END_MONTH
import com.terning.core.designsystem.util.CalendarDefaults.END_YEAR
import com.terning.core.designsystem.util.CalendarDefaults.START_MONTH
import com.terning.core.designsystem.util.CalendarDefaults.START_YEAR
import java.util.Calendar

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -65,8 +70,11 @@ fun HomeFilteringBottomSheet(
// todo : 만약 year와 month의 서버통신 값이 null이면 false로 넣기
var isYearNull by remember { mutableStateOf(false) }
var isMonthNull by remember { mutableStateOf(false) }
// todo: year와 month의 서버통신 값이 null이면 true로 넣기
var isCheck by remember { mutableStateOf(false) }

var isFirstNullAndFirstChange by remember { mutableStateOf(false) }

TerningBasicBottomSheet(
content = {
Column(
Expand Down Expand Up @@ -132,9 +140,32 @@ fun HomeFilteringBottomSheet(
.padding(horizontal = 24.dp),
)

ChangeFilteringTitleText(
text = stringResource(id = R.string.change_filter_start_work_title),
modifier = Modifier
.padding(top = 32.dp, bottom = 49.dp)
.padding(horizontal = 24.dp)
)

val years = (START_YEAR..END_YEAR).map { "${it}" }
val months = (START_MONTH..END_MONTH).map { "${it}" }

val yearsWithNull by remember(isYearNull) {
mutableStateOf(
if (isYearNull || isFirstNullAndFirstChange) years + "-"
else years
)
}
val monthsWithNull by remember(isMonthNull) {
mutableStateOf(
if (isMonthNull || isFirstNullAndFirstChange) months + "-"
else months
)
}

//todo: 추후 삭제 부탁합니다!
Checkbox(
checked = isCheck && isYearNull && isMonthNull,
checked = isCheck,
onCheckedChange = { isChecked ->
if (isChecked) {
isYearNull = true
Expand All @@ -147,33 +178,31 @@ fun HomeFilteringBottomSheet(
modifier = Modifier.padding(start = 20.dp)
)

ChangeFilteringTitleText(
text = stringResource(id = R.string.change_filter_start_work_title),
modifier = Modifier
.padding(top = 32.dp, bottom = 49.dp)
.padding(horizontal = 24.dp)
)

// todo: null 처리 제대로
// todo: null 처리 제대로
HomeYearMonthPicker(
chosenYear = defaultStartYear ?: Calendar.getInstance().currentYear,
chosenMonth = defaultStartMonth ?: Calendar.getInstance().currentMonth,
onYearChosen = { year, isNull ->
isYearNull = isNull
onYearChosen = { year, isNull, isFirst ->
if (year != null) {
currentStartYear = year
isCheck = false
isYearNull = false
isFirstNullAndFirstChange = isFirst
}
Log.d("LYB", "Year chosen: $year, isYearNull: $isYearNull")
},
onMonthChosen = { month, isNull ->
isMonthNull = isNull
onMonthChosen = { month, isNull, isFirst ->
if (month != null) {
currentStartMonth = month
isCheck = false
isMonthNull = false
isFirstNullAndFirstChange = isFirst
}
Log.d("LYB", "Month chosen: $month, isMonthNull: $isMonthNull")
},
isYearNull = isYearNull,
isMonthNull = isMonthNull,
years = yearsWithNull,
months = monthsWithNull,
isFirstNullAndFirstChange = isFirstNullAndFirstChange
)

RoundButton(
Expand All @@ -199,7 +228,6 @@ fun HomeFilteringBottomSheet(
isEnabled = currentGrade != null && currentPeriod != null
)
}

},
onDismissRequest = onDismiss,
sheetState = sheetState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,31 @@ fun HomeYearMonthPicker(
modifier: Modifier = Modifier,
chosenYear: Int?,
chosenMonth: Int?,
onYearChosen: (Int?, Boolean) -> Unit,
onMonthChosen: (Int?,Boolean) -> Unit,
onYearChosen: (Int?, Boolean, Boolean) -> Unit,
onMonthChosen: (Int?, Boolean, Boolean) -> Unit,
isYearNull: Boolean,
isMonthNull : Boolean,
isMonthNull: Boolean,
years: List<String>,
months: List<String>,
isFirstNullAndFirstChange: Boolean
) {
// todo: 변수명 바꾸기
val yearsWithNull = if (isYearNull) years + "-" else years
val monthsWithNull = if (isMonthNull) months + "-" else months
// val yearsWithNull = if (isYearNull) years + "-" else years
// val monthsWithNull = if (isMonthNull) months + "-" else months

val yearPickerState = rememberPickerState()
val monthPickerState = rememberPickerState()

Log.d("LYB", yearsWithNull.toString())
Log.d("LYB", monthsWithNull.toString())
var isFirst = isFirstNullAndFirstChange

val startYearIndex =
if (isYearNull) yearsWithNull.lastIndex else yearsWithNull.indexOf("${chosenYear ?: "-"}")
.takeIf { it >= 0 } ?: 0
val startMonthIndex =
if (isMonthNull) monthsWithNull.lastIndex else monthsWithNull.indexOf("${chosenMonth ?: "-"}")
.takeIf { it >= 0 } ?: 0

LaunchedEffect(isYearNull, chosenYear) {
yearPickerState.selectedItem = if (isYearNull) "-" else "${chosenYear ?: "-"}"
}
val startYearIndex = remember(isYearNull) {
if (isYearNull) years.lastIndex else years.indexOf("${chosenYear ?: "-"}")
.takeIf { it >= 0 } ?: 0
}

LaunchedEffect(isMonthNull, chosenMonth) {
monthPickerState.selectedItem = if (isMonthNull) "-" else "${chosenMonth ?: "-"}"
val startMonthIndex = remember(isMonthNull) {
if (isMonthNull) months.lastIndex else months.indexOf("${chosenMonth ?: "-"}")
.takeIf { it >= 0 } ?: 0
}

Row(
Expand All @@ -100,20 +97,30 @@ fun HomeYearMonthPicker(
DatePicker(
modifier = Modifier.weight(1f),
pickerState = yearPickerState,
items = yearsWithNull,
items = years,
startIndex = startYearIndex,
onItemSelected = { year ->
onYearChosen(if (year == "-") null else year.dropLast(1).toInt(), year == "-")
if (year == "-" && !isFirst) isFirst = true
onYearChosen(
if (year == "-") null else year.dropLast(1).toInt(),
year == "-",
isFirst
)
}
)
Spacer(modifier = Modifier.width(18.dp))
DatePicker(
modifier = Modifier.weight(1f),
pickerState = monthPickerState,
items = monthsWithNull,
items = months,
startIndex = startMonthIndex,
onItemSelected = { month ->
onMonthChosen(if (month == "-") null else month.dropLast(1).toInt(), month == "-")
if (month == "-" && !isFirst) isFirst = true
onMonthChosen(
if (month == "-") null else month.dropLast(1).toInt(),
month == "-",
isFirst
)
}
)
}
Expand All @@ -136,17 +143,28 @@ fun DatePicker(
val flingBehavior = rememberSnapFlingBehavior(lazyListState = scrollState)

LaunchedEffect(itemHeightPixel, startIndex) {
if (itemHeightPixel > 0 && startIndex >= 0) scrollState.scrollToItem(startIndex)
if (itemHeightPixel > 0 && startIndex >= 0) {
scrollState.scrollToItem(startIndex)
}
}

// todo: type safety 하게 수정 필요 && 변수명도 수정
val isNullSize = items.size == 12 || items.size == 21

LaunchedEffect(scrollState) {
snapshotFlow { scrollState.firstVisibleItemIndex }
.map { index -> items.getOrNull(index) }
.map { index ->
var newItem = items
if (isNullSize) newItem = items + "-"
Log.d("PickerDebug", "Index: $index, Item: ${newItem.getOrNull(index)}")
newItem.getOrNull(index)
}
.distinctUntilChanged()
.collect { item ->
item?.let {
pickerState.selectedItem = it
onItemSelected(it)
Log.d("PickerDebug", "Selected Item: $it")
}
}
}
Expand All @@ -166,11 +184,12 @@ fun DatePicker(
Spacer(modifier = Modifier.height(itemHeightDp))
}
items(items.size) { index ->
val isSelected = pickerState.selectedItem == items[index]
DatePickerContent(
modifier = Modifier
.onSizeChanged { intSize: IntSize -> itemHeightPixel = intSize.height },
text = items[index],
color = if (pickerState.selectedItem == items[index]) Grey500 else Grey300
color = if (isSelected) Grey500 else Grey300
)
}
items(visibleItemsMiddle) {
Expand Down

0 comments on commit 23f2f6a

Please sign in to comment.