Skip to content

Commit

Permalink
[feat] #20 date picker 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
kamja0510 committed Jan 15, 2025
1 parent 0b7e2e9 commit a1297b7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,94 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.android.bbangzip.presentation.model.Date
import org.android.bbangzip.presentation.util.constant.DateConstant
import java.time.YearMonth

@Composable
fun BbangZipDatePicker() {
fun BbangZipDatePicker(
onConfirm: (Date) -> Unit = {},
currentDate: Date = Date(DateConstant.YEAR_OF_TODAY.toString(), "1", "1"),
) {
Row(
modifier = Modifier.fillMaxWidth(),
) {
val years = (2025..2035).map { it.toString() + "" }
val years = DateConstant.YEARS_LIST
val yearPickerState = rememberPickerState()
val startYear = currentDate.year.toInt() - DateConstant.YEAR_OF_TODAY

val months = (1..12).map { it.toString() + "" }
val months = DateConstant.MONTHS_LIST
val monthsPickerState = rememberPickerState()
val startMonth = currentDate.month.toInt() - 1

val days =
remember(yearPickerState.selectedItem, monthsPickerState.selectedItem) {
derivedStateOf {
val year = yearPickerState.selectedItem.filter { it.isDigit() }.toIntOrNull() ?: 2025
val year = yearPickerState.selectedItem.filter { it.isDigit() }.toIntOrNull() ?: DateConstant.YEAR_OF_TODAY
val month = monthsPickerState.selectedItem.filter { it.isDigit() }.toIntOrNull() ?: 1
(1..getDaysInMonth(year, month)).map { it.toString() + "" }
}
}
val daysPickerState = rememberPickerState()
val startDay = currentDate.day.toInt() - 1

LaunchedEffect(yearPickerState.selectedItem, monthsPickerState.selectedItem, daysPickerState.selectedItem) {
val year = yearPickerState.selectedItem.filter { it.isDigit() }
val month = monthsPickerState.selectedItem.filter { it.isDigit() }
val day = daysPickerState.selectedItem.filter { it.isDigit() }
onConfirm(
Date(
year = year,
month = month,
day = day
)
)
}

Picker(
state = yearPickerState,
items = years,
visibleItemsCount = 5,
modifier = Modifier.weight(0.4f),
startIndex = startYear,
visibleItemsCount = 5,
textModifier = Modifier.padding(8.dp),
isCircular = true,
isCircular = false,
)

Picker(
state = monthsPickerState,
items = months,
visibleItemsCount = 5,
modifier = Modifier.weight(0.3f),
startIndex = startMonth,
visibleItemsCount = 5,
textModifier = Modifier.padding(8.dp),
isCircular = true,
isCircular = false,
)

Picker(
state = daysPickerState,
items = days.value,
startIndex = 0, // if(daysPickerState.selectedItem == "") daysPickerState.selectedItem.toInt() else 0,
startIndex = startDay,
visibleItemsCount = 5,
modifier = Modifier.weight(0.3f),
textModifier = Modifier.padding(8.dp),
isCircular = true,
isCircular = false,
)
}
}

// 현재 달이 몇일 있는 지 알아낼 함수, 어디로 빼야할까요?
fun getDaysInMonth(
year: Int,
month: Int,
Expand All @@ -74,12 +103,20 @@ fun getDaysInMonth(
@Preview(showBackground = true)
@Composable
fun BbangZipDatePickerPreview() {
var selectedDate by remember { mutableStateOf<Date?>(null) }

Column(
modifier =
Modifier
.fillMaxSize()
.padding(16.dp),
Modifier
.fillMaxSize()
.padding(16.dp),
) {
BbangZipDatePicker()
BbangZipDatePicker(
onConfirm = {
selectedDate = it
},
currentDate = Date("2026", "3", "16"),
)
Text("Selected Date: ${selectedDate?.year}${selectedDate?.month}${selectedDate?.day}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fun Picker(
if (isCircular) {
listScrollMiddle - listScrollMiddle % pickerItems.size - visibleItemsMiddle + startIndex
} else {
0 // isCircular가 false이면 시작 인덱스를 0으로 설정
0 + startIndex // isCircular가 false이면 시작 인덱스를 0으로 설정
}

fun getItem(index: Int) = pickerItems[index % pickerItems.size]
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/org/android/bbangzip/presentation/model/Date.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.android.bbangzip.presentation.model

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class Date(
val year: String,
val month: String,
val day: String,
) : Parcelable
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.android.bbangzip.presentation.util.constant

import java.time.LocalDate

object DateConstant {
const val YEAR_OF_TODAY = 2025

val YEARS_LIST = (YEAR_OF_TODAY..2099).map { it.toString() + "" }
val MONTHS_LIST = (1..12).map { it.toString() + "" }
}

0 comments on commit a1297b7

Please sign in to comment.