diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.kt index 86e4212def..30b1272155 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.kt @@ -273,12 +273,13 @@ class ActionsFragment : DaggerFragment() { private fun checkPumpCustomActions() { val activePump = activePlugin.activePump val customActions = activePump.customActions ?: return + val currentContext = context ?: return removePumpCustomActions() for (customAction in customActions) { if (!customAction.isEnabled) continue - val btn = SingleClickButton(context, null, android.R.attr.buttonStyle) + val btn = SingleClickButton(currentContext, null, android.R.attr.buttonStyle) btn.text = resourceHelper.gs(customAction.name) val layoutParams = LinearLayout.LayoutParams( diff --git a/core/src/main/java/info/nightscout/androidaps/utils/ui/SingleClickButton.java b/core/src/main/java/info/nightscout/androidaps/utils/ui/SingleClickButton.java deleted file mode 100644 index 28638629a2..0000000000 --- a/core/src/main/java/info/nightscout/androidaps/utils/ui/SingleClickButton.java +++ /dev/null @@ -1,68 +0,0 @@ -package info.nightscout.androidaps.utils.ui; - -import android.app.Activity; -import android.content.Context; -import android.os.SystemClock; -import androidx.annotation.Nullable; -import android.util.AttributeSet; -import android.view.View; - -import org.slf4j.Logger; - -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; - -/** - * Created by mike on 22.12.2017. - */ - -public class SingleClickButton extends androidx.appcompat.widget.AppCompatButton implements View.OnClickListener { - private static Logger log = StacktraceLoggerWrapper.getLogger(SingleClickButton.class); - - Context context; - OnClickListener listener = null; - - public SingleClickButton(Context context) { - super(context); - this.context = context; - super.setOnClickListener(this); - } - - public SingleClickButton(Context context, AttributeSet attrs) { - super(context, attrs); - this.context = context; - super.setOnClickListener(this); - } - - public SingleClickButton(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - this.context = context; - super.setOnClickListener(this); - } - - @Override - public void setOnClickListener(@Nullable OnClickListener l) { - listener = l; - } - - @Override - public void onClick(final View v) { - setEnabled(false); - new Thread(new Runnable() { - @Override - public void run() { - SystemClock.sleep(3000); - Activity activity = (Activity) context; - if (activity != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - setEnabled(true); - log.debug("Button enabled"); - } - }); - } - }).start(); - if (listener != null) - listener.onClick(v); - } -} diff --git a/core/src/main/java/info/nightscout/androidaps/utils/ui/SingleClickButton.kt b/core/src/main/java/info/nightscout/androidaps/utils/ui/SingleClickButton.kt new file mode 100644 index 0000000000..74269091d0 --- /dev/null +++ b/core/src/main/java/info/nightscout/androidaps/utils/ui/SingleClickButton.kt @@ -0,0 +1,25 @@ +package info.nightscout.androidaps.utils.ui + +import android.content.Context +import android.util.AttributeSet +import androidx.appcompat.R +import androidx.appcompat.widget.AppCompatButton +import info.nightscout.androidaps.logging.StacktraceLoggerWrapper.Companion.getLogger +import org.slf4j.Logger + +class SingleClickButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = R.attr.buttonStyle) : AppCompatButton(context, attrs, defStyleAttr) { + + override fun performClick(): Boolean = guardClick { super.performClick() } + override fun callOnClick(): Boolean = guardClick { super.callOnClick() } + + private fun guardClick(block: () -> Boolean): Boolean { + isEnabled = false + postDelayed({ isEnabled = true; log.debug("Button enabled") }, BUTTON_REFRACTION_PERIOD) + return block() + } + + companion object { + const val BUTTON_REFRACTION_PERIOD = 3000L + private val log: Logger = getLogger(SingleClickButton::class.java) + } +} \ No newline at end of file