Skip to content

Commit

Permalink
✨ timer feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ii2001 committed Sep 19, 2024
1 parent 5095fd1 commit 6444ae8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,12 +11,14 @@ class RecipeStepPagerRecyclerAdapter(
private var pageList: List<RecipeStep> = emptyList(),
private val viewPager2: ViewPager2,
) : RecyclerView.Adapter<RecipeStepViewHolder>() {

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)
}

Expand All @@ -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<RecipeStep>) {
pageList = list
notifyItemInserted(pageList.size)
notifyDataSetChanged()
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
}
}
1 change: 1 addition & 0 deletions android/app/src/main/res/layout/item_step_recipe.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
18 changes: 3 additions & 15 deletions android/app/src/main/res/layout/item_timer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,17 @@
android:paddingHorizontal="30dp"
android:paddingVertical="5dp">

<Chronometer
android:id="@+id/chronometer"
<TextView
android:id="@+id/tv_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:countDown="true"
android:textSize="32sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:base="1000" />

<ImageView
android:id="@+id/iv_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:src="@drawable/ic_menu_dots"
app:layout_constraintBottom_toBottomOf="@+id/chronometer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/chronometer"
app:layout_constraintTop_toTopOf="@+id/chronometer" />
tools:text="00:00" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

0 comments on commit 6444ae8

Please sign in to comment.