Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.
Open
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 @@ -23,9 +23,9 @@ import androidx.core.app.NotificationManagerCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import com.android.samples.donuttracker.databinding.DonutListBinding
import com.android.samples.donuttracker.extension.clickWithDebounce
import com.android.samples.donuttracker.storage.DonutDatabase
import kotlinx.android.synthetic.main.donut_list.*

Expand Down Expand Up @@ -60,8 +60,8 @@ class DonutList : Fragment() {

recyclerView.adapter = adapter

binding.fab.setOnClickListener { fabView ->
fabView.findNavController().navigate(
binding.fab.clickWithDebounce {
findNavController().navigate(
DonutListDirections.actionDonutListToDonutEntryDialogFragment()
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.android.samples.donuttracker.extension

import android.view.View

fun View.clickWithDebounce(debounceTime: Long = 1000L, action: () -> Unit) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be easier to understand the variable's purpose if it had a more meaningful name, such as "minClickInterval" or "clickThrottleInterval". This indicates that the variable represents the minimum amount of time between clicks to execute an action.

this.setOnClickListener(object : View.OnClickListener {
private var lastClickTime: Long = 0

override fun onClick(v: View?) {
if (System.currentTimeMillis() - lastClickTime < debounceTime) {
return
}
action()
lastClickTime = System.currentTimeMillis()
}

})
}