Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -3,8 +3,10 @@ package com.project200.feature.timer
import android.graphics.Color
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import androidx.core.graphics.drawable.toDrawable
import com.project200.presentation.base.BaseDialogFragment
import com.project200.presentation.utils.TimeEditTextLimiter.addRangeLimit
import com.project200.undabang.feature.timer.R
import com.project200.undabang.feature.timer.databinding.DialogTimePickerBinding
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -14,28 +16,29 @@ import java.util.Locale
class TimePickerDialog(
private val initialTime: Int? = null,
private val onTimeSelected: (Int) -> Unit,
) :
BaseDialogFragment<DialogTimePickerBinding>(R.layout.dialog_time_picker) {
) : BaseDialogFragment<DialogTimePickerBinding>(R.layout.dialog_time_picker) {
override fun getViewBinding(view: View): DialogTimePickerBinding {
return DialogTimePickerBinding.bind(view)
}

override fun setupViews() {
super.setupViews()

// 다이얼로그 가로 너비 및 배경 투명화 설정
dialog?.window?.let { window ->
val screenWidth = resources.displayMetrics.widthPixels
val desiredWidth = (screenWidth * 0.85).toInt()
window.setLayout(desiredWidth, WindowManager.LayoutParams.WRAP_CONTENT)
window.setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
}

setupTimePickers()
setupTimeInputs()
setupFocusAndRangeListeners()

binding.confirmBtn.setOnClickListener {
// NumberPicker에서 선택된 분과 초를 가져옵니다.
val minutes = binding.minutePicker.value
val seconds = binding.secondPicker.value
// 빈 칸일 경우 0으로 처리, 범위 재확인
val minutes = (binding.minuteEt.text.toString().toIntOrNull() ?: 0).coerceIn(0, 59)
val seconds = (binding.secondEt.text.toString().toIntOrNull() ?: 0).coerceIn(0, 59)
val totalTimeInSeconds = (minutes * 60) + seconds

onTimeSelected(totalTimeInSeconds)
Expand All @@ -47,22 +50,43 @@ class TimePickerDialog(
}
}

private fun setupTimePickers() {
binding.minutePicker.apply {
minValue = 0
maxValue = 59 // 최대 59분
value = initialTime?.div(60) ?: 0 // 초기값이 null인 경우 0으로 설정
setFormatter { String.format(Locale.KOREA, "%02d", it) }
}
private fun setupTimeInputs() {
// 초 단위를 분과 초로 계산하여 초기값 설정
val minutes = initialTime?.div(60) ?: 0
val seconds = initialTime?.rem(60) ?: 0

binding.secondPicker.apply {
minValue = 0
maxValue = 59 // 최대 59초
value = initialTime?.rem(60) ?: 0 // 초기값이 null인 경우 0으로 설정
setFormatter { String.format(Locale.KOREA, "%02d", it) }
}
binding.minuteEt.setText(String.format(Locale.KOREA, "%02d", minutes))
binding.secondEt.setText(String.format(Locale.KOREA, "%02d", seconds))
}

companion object {
private fun setupFocusAndRangeListeners() {
// 1. 포커스 상태에 따른 배경 및 포맷팅 처리
val focusChangeListener =
View.OnFocusChangeListener { view, hasFocus ->
if (view is EditText) {
if (hasFocus) {
// 포커스 얻었을 때: 배경 적용 및 텍스트 전체 선택
view.setBackgroundResource(com.project200.undabang.presentation.R.drawable.bg_solid_corner)
view.selectAll()
} else {
// 포커스 잃었을 때: 배경 제거 및 00~59 보정 후 포맷팅
view.setBackgroundResource(0)
val input = view.text.toString().toIntOrNull() ?: 0
val clamped = input.coerceIn(0, 59)
view.setText(String.format(Locale.KOREA, "%02d", clamped))
}
}
}

binding.minuteEt.onFocusChangeListener = focusChangeListener
binding.secondEt.onFocusChangeListener = focusChangeListener

// 실시간 입력 범위 제한 (0~59)
binding.minuteEt.addRangeLimit(59)
binding.secondEt.addRangeLimit(59)

// 초기 상태 배경 제거
binding.minuteEt.setBackgroundResource(0)
binding.secondEt.setBackgroundResource(0)
}
}
72 changes: 56 additions & 16 deletions feature/timer/src/main/res/layout/dialog_time_picker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,72 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@drawable/dialog_radius"
android:padding="@dimen/base_horizontal_margin">

<LinearLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_margin="24dp">

<NumberPicker
android:id="@+id/minute_picker"
android:layout_width="0dp"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:descendantFocusability="blocksDescendants"/>
style="@style/subtext_14"
android:text="@string/minute"
android:layout_marginBottom="6dp"
app:layout_constraintStart_toStartOf="@id/minute_et"
app:layout_constraintEnd_toEndOf="@id/minute_et"
app:layout_constraintBottom_toTopOf="@id/minute_et"/>
<EditText
android:id="@+id/minute_et"
android:background="@null"
android:layout_width="100dp"
android:layout_height="70dp"
style="@style/content_regular"
android:textSize="40sp"
android:maxLength="2"
android:gravity="center"
android:maxLines="1"
android:inputType="numberSigned"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

<NumberPicker
android:id="@+id/second_picker"
android:layout_width="0dp"
<TextView
style="@style/content_regular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_weight="1"
android:descendantFocusability="blocksDescendants"/>
</LinearLayout>
android:layout_gravity="center"
android:text="@string/time_divider"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/second_et"
app:layout_constraintStart_toEndOf="@id/minute_et"
app:layout_constraintTop_toTopOf="@+id/minute_et" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/subtext_14"
android:text="@string/second"
android:layout_marginBottom="6dp"
app:layout_constraintStart_toStartOf="@id/second_et"
app:layout_constraintEnd_toEndOf="@id/second_et"
app:layout_constraintBottom_toTopOf="@id/second_et"/>
<EditText
android:id="@+id/second_et"
android:background="@null"
android:layout_width="100dp"
android:layout_height="70dp"
style="@style/content_regular"
android:textSize="40sp"
android:inputType="numberSigned"
android:maxLength="2"
android:maxLines="1"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

<LinearLayout
android:layout_width="match_parent"
Expand Down
3 changes: 3 additions & 0 deletions feature/timer/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<string name="timer_start">시작</string>
<string name="timer_stop">일시 정지</string>
<string name="timer_end">종료</string>
<string name="minute">분</string>
<string name="second">초</string>
<string name="time_divider">:</string>

<string name="custom_timer_notify_title_format">%1$d / %2$d %3$s</string>
<string name="timer_notify_time_format">%02d:%02d</string>
Expand Down