diff --git a/android/app/src/main/java/net/pengcook/android/presentation/step/RecipeStepPagerRecyclerAdapter.kt b/android/app/src/main/java/net/pengcook/android/presentation/step/RecipeStepPagerRecyclerAdapter.kt index 882777e2..25622a1f 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/step/RecipeStepPagerRecyclerAdapter.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/step/RecipeStepPagerRecyclerAdapter.kt @@ -1,7 +1,6 @@ package net.pengcook.android.presentation.step import android.view.LayoutInflater -import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import androidx.viewpager2.widget.ViewPager2 @@ -12,12 +11,14 @@ class RecipeStepPagerRecyclerAdapter( private var pageList: List = emptyList(), private val viewPager2: ViewPager2, ) : RecyclerView.Adapter() { + override fun onCreateViewHolder( parent: ViewGroup, viewType: Int, ): RecipeStepViewHolder { - val binding = - ItemStepRecipeBinding.inflate(LayoutInflater.from(parent.context), parent, false) + val binding = ItemStepRecipeBinding.inflate( + LayoutInflater.from(parent.context), parent, false + ) return RecipeStepViewHolder(binding) } @@ -27,20 +28,17 @@ class RecipeStepPagerRecyclerAdapter( holder: RecipeStepViewHolder, position: Int, ) { - holder.bind(pageList[position], createOnClickListener()) - } - - private fun createOnClickListener(): View.OnClickListener { - return View.OnClickListener { + val moveToNextStep = { val currentItem = viewPager2.currentItem if (currentItem < itemCount - 1) { viewPager2.currentItem = currentItem + 1 } } + holder.bind(pageList[position], moveToNextStep) } fun updateList(list: List) { pageList = list - notifyItemInserted(pageList.size) + notifyDataSetChanged() } } diff --git a/android/app/src/main/java/net/pengcook/android/presentation/step/RecipeStepViewHolder.kt b/android/app/src/main/java/net/pengcook/android/presentation/step/RecipeStepViewHolder.kt index 0a6da792..78e2b26c 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/step/RecipeStepViewHolder.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/step/RecipeStepViewHolder.kt @@ -1,6 +1,6 @@ package net.pengcook.android.presentation.step -import android.os.SystemClock +import android.os.CountDownTimer import android.view.View import androidx.recyclerview.widget.RecyclerView import net.pengcook.android.databinding.ItemStepRecipeBinding @@ -9,32 +9,70 @@ import net.pengcook.android.presentation.core.model.RecipeStep class RecipeStepViewHolder( private val binding: ItemStepRecipeBinding, ) : RecyclerView.ViewHolder(binding.root) { - fun bind(recipeStep: RecipeStep, clickListner: View.OnClickListener) { + + private var isTimerRunning = false + private var countDownTimer: CountDownTimer? = null + + fun bind(recipeStep: RecipeStep, moveToNextStep: () -> Unit) { binding.recipeStep = recipeStep - binding.clickListener = clickListner - // cookingTime을 초로 변환 + val cookingTimeInSeconds = convertToSeconds(recipeStep.cookingTime) - // 타이머 설정 - binding.timer.chronometer.format = "%M:%S" - binding.timer.chronometer.base = SystemClock.elapsedRealtime() + cookingTimeInSeconds * 1000 + binding.timer.tvTimer.text = formatTime(cookingTimeInSeconds * 1000) + + val clickListener = View.OnClickListener { + when { + cookingTimeInSeconds <= 1 -> { + moveToNextStep() + } + + !isTimerRunning -> { + startTimer(cookingTimeInSeconds * 1000, moveToNextStep) + isTimerRunning = true + } - binding.timer.setClickListener { - startTimer() + else -> { + stopTimer() + moveToNextStep() + } + } } + binding.clickListener = clickListener binding.executePendingBindings() } - private fun startTimer() { - binding.timer.chronometer.start() + private fun startTimer(durationInMillis: Long, moveToNextStep: () -> Unit) { + countDownTimer = object : CountDownTimer(durationInMillis, 1000) { + override fun onTick(millisUntilFinished: Long) { + binding.timer.tvTimer.text = formatTime(millisUntilFinished) + } + + override fun onFinish() { + binding.timer.tvTimer.text = "00:00" + isTimerRunning = false + moveToNextStep() + } + }.start() + } + + private fun stopTimer() { + countDownTimer?.cancel() + isTimerRunning = false + } + + private fun formatTime(millis: Long): String { + val totalSeconds = millis / 1000 + val minutes = totalSeconds / 60 + val seconds = totalSeconds % 60 + return String.format("%02d:%02d", minutes, seconds) } private fun convertToSeconds(time: String): Long { val timeParts = time.split(":") - val hours = timeParts[0].toIntOrNull() ?: 0 - val minutes = timeParts[1].toIntOrNull() ?: 0 - val seconds = timeParts[2].toIntOrNull() ?: 0 + val hours = timeParts.getOrNull(0)?.toIntOrNull() ?: 0 + val minutes = timeParts.getOrNull(1)?.toIntOrNull() ?: 0 + val seconds = timeParts.getOrNull(2)?.toIntOrNull() ?: 0 return (hours * 3600 + minutes * 60 + seconds).toLong() } } diff --git a/android/app/src/main/res/layout/item_step_recipe.xml b/android/app/src/main/res/layout/item_step_recipe.xml index 3b726aa2..adb2b5cf 100644 --- a/android/app/src/main/res/layout/item_step_recipe.xml +++ b/android/app/src/main/res/layout/item_step_recipe.xml @@ -49,6 +49,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" + app:clickListener="@{clickListener}" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/cl_image_container" /> diff --git a/android/app/src/main/res/layout/item_timer.xml b/android/app/src/main/res/layout/item_timer.xml index a7abbb90..4e44ee15 100644 --- a/android/app/src/main/res/layout/item_timer.xml +++ b/android/app/src/main/res/layout/item_timer.xml @@ -17,29 +17,17 @@ android:paddingHorizontal="30dp" android:paddingVertical="5dp"> - - - + tools:text="00:00" />