From dc2894f51ff0ee794f4293e3405fac92a4edf552 Mon Sep 17 00:00:00 2001 From: Jakub Kalinowski Date: Sun, 12 Sep 2021 18:22:55 +0200 Subject: [PATCH] Added delay after toggling a habit and before sort --- .../activities/habits/list/ListHabitsMenu.kt | 21 ++++++++++++++----- .../habits/list/views/CheckmarkButtonView.kt | 19 +++++++++++++---- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsMenu.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsMenu.kt index c92d0dd3f..97d9afd2f 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsMenu.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsMenu.kt @@ -20,6 +20,8 @@ package org.isoron.uhabits.activities.habits.list import android.content.Context +import android.os.Handler +import android.os.Looper import android.view.Menu import android.view.MenuInflater import android.view.MenuItem @@ -122,31 +124,40 @@ class ListHabitsMenu @Inject constructor( } R.id.actionSortColor -> { - behavior.onSortByColor() + { behavior.onSortByColor() }.delay(SORT_DELAY_MILLIS) return true } R.id.actionSortManual -> { - behavior.onSortByManually() + { behavior.onSortByManually() }.delay(SORT_DELAY_MILLIS) return true } R.id.actionSortName -> { - behavior.onSortByName() + { behavior.onSortByName() }.delay(SORT_DELAY_MILLIS) return true } R.id.actionSortScore -> { - behavior.onSortByScore() + { behavior.onSortByScore() }.delay(SORT_DELAY_MILLIS) return true } R.id.actionSortStatus -> { - behavior.onSortByStatus() + { behavior.onSortByStatus() }.delay(SORT_DELAY_MILLIS) return true } else -> return false } } + + companion object { + const val SORT_DELAY_MILLIS = 1000L + const val TOGGLE_DELAY_MILLIS = 2000L + + fun (() -> Unit).delay(delayInMillis: Long) { + Handler(Looper.getMainLooper()).postDelayed(this, delayInMillis) + } + } } diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/views/CheckmarkButtonView.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/views/CheckmarkButtonView.kt index 339cc46b4..2911a7e8c 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/views/CheckmarkButtonView.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/views/CheckmarkButtonView.kt @@ -29,6 +29,8 @@ import android.view.HapticFeedbackConstants import android.view.View import android.view.View.MeasureSpec.EXACTLY import org.isoron.uhabits.R +import org.isoron.uhabits.activities.habits.list.ListHabitsMenu.Companion.delay +import org.isoron.uhabits.activities.habits.list.ListHabitsMenu.Companion.TOGGLE_DELAY_MILLIS import org.isoron.uhabits.core.models.Entry import org.isoron.uhabits.core.models.Entry.Companion.NO import org.isoron.uhabits.core.models.Entry.Companion.SKIP @@ -80,15 +82,24 @@ class CheckmarkButtonView( setOnLongClickListener(this) } - fun performToggle() { + private fun performToggle() { value = Entry.nextToggleValue( value = value, isSkipEnabled = preferences.isSkipEnabled, areQuestionMarksEnabled = preferences.areQuestionMarksEnabled ) - onToggle(value) - performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) - invalidate() + + if (preferences.isSkipEnabled) { + { + onToggle(value) + performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) + invalidate() + }.delay(TOGGLE_DELAY_MILLIS) + } else { + onToggle(value) + performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) + invalidate() + } } override fun onClick(v: View) {