From 666a9ef44d952a47d8c9c736db480a7e2e034762 Mon Sep 17 00:00:00 2001 From: Paul Akhamiogu Date: Wed, 8 Sep 2021 20:00:59 +0100 Subject: [PATCH] Reduce timer icon sizes, add animation for timer removal --- app/build.gradle | 2 +- .../kotlin/com/simplemobiletools/clock/App.kt | 5 +-- .../clock/fragments/TimerFragment.kt | 15 ++++--- .../helpers/DisabledItemChangeAnimator.kt | 39 +++++++++++++++++++ .../clock/models/TimerEvent.kt | 4 +- app/src/main/res/layout/item_timer.xml | 10 ++--- app/src/main/res/values/dimens.xml | 2 + 7 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/clock/helpers/DisabledItemChangeAnimator.kt diff --git a/app/build.gradle b/app/build.gradle index edff5d1c..0d96cbe0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -67,7 +67,7 @@ android { } dependencies { - implementation 'com.github.SimpleMobileTools:Simple-Commons:a9e600f664' + implementation 'com.github.SimpleMobileTools:Simple-Commons:b6e2ffb710' implementation 'com.facebook.stetho:stetho:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'com.shawnlin:number-picker:2.4.6' diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/App.kt b/app/src/main/kotlin/com/simplemobiletools/clock/App.kt index af18cac4..e97244a9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/App.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/App.kt @@ -14,7 +14,6 @@ import com.facebook.stetho.Stetho import com.simplemobiletools.clock.extensions.getOpenTimerTabIntent import com.simplemobiletools.clock.extensions.getTimerNotification import com.simplemobiletools.clock.extensions.timerHelper -import com.simplemobiletools.clock.helpers.INVALID_TIMER_ID import com.simplemobiletools.clock.models.TimerEvent import com.simplemobiletools.clock.models.TimerState import com.simplemobiletools.clock.services.TimerStopService @@ -78,7 +77,7 @@ class App : Application(), LifecycleObserver { fun onMessageEvent(event: TimerEvent.Delete) { countDownTimers[event.timerId]?.cancel() timerHelper.deleteTimer(event.timerId){ - EventBus.getDefault().post(TimerEvent.Refresh(INVALID_TIMER_ID)) + EventBus.getDefault().post(TimerEvent.Refresh) } } @@ -120,7 +119,7 @@ class App : Application(), LifecycleObserver { timerHelper.getTimer(timerId) { timer -> val newTimer = timer.copy(state = state) timerHelper.insertOrUpdateTimer(newTimer) { - EventBus.getDefault().post(TimerEvent.Refresh(timerId)) + EventBus.getDefault().post(TimerEvent.Refresh) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt index d5249287..46942ade 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt @@ -12,6 +12,7 @@ import com.simplemobiletools.clock.dialogs.EditTimerDialog import com.simplemobiletools.clock.extensions.config import com.simplemobiletools.clock.extensions.createNewTimer import com.simplemobiletools.clock.extensions.timerHelper +import com.simplemobiletools.clock.helpers.DisabledItemChangeAnimator import com.simplemobiletools.clock.models.Timer import com.simplemobiletools.clock.models.TimerEvent import com.simplemobiletools.commons.extensions.hideKeyboard @@ -49,7 +50,7 @@ class TimerFragment : Fragment() { storeStateVariables() timers_list.adapter = timerAdapter - timers_list.itemAnimator = null + timers_list.itemAnimator = DisabledItemChangeAnimator() timer_add.setOnClickListener { activity?.run { @@ -81,11 +82,13 @@ class TimerFragment : Fragment() { activity?.timerHelper?.getTimers { timers -> timerAdapter.submitList(timers) { view.timers_list.post { - if (timerPositionToScrollTo != INVALID_POSITION && timerAdapter.itemCount > timerPositionToScrollTo) { - view.timers_list.scrollToPosition(timerPositionToScrollTo) - timerPositionToScrollTo = INVALID_POSITION - } else if (scrollToLatest) { - view.timers_list.scrollToPosition(timers.lastIndex) + if (getView() != null) { + if (timerPositionToScrollTo != INVALID_POSITION && timerAdapter.itemCount > timerPositionToScrollTo) { + view.timers_list.scrollToPosition(timerPositionToScrollTo) + timerPositionToScrollTo = INVALID_POSITION + } else if (scrollToLatest) { + view.timers_list.scrollToPosition(timers.lastIndex) + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/DisabledItemChangeAnimator.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/DisabledItemChangeAnimator.kt new file mode 100644 index 00000000..7a627afe --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/DisabledItemChangeAnimator.kt @@ -0,0 +1,39 @@ +package com.simplemobiletools.clock.helpers + +import androidx.recyclerview.widget.DefaultItemAnimator +import androidx.recyclerview.widget.RecyclerView + +/** + * Simple RecyclerView animator that disable itemChange animations + */ +class DisabledItemChangeAnimator : DefaultItemAnimator() { + override fun animateChange( + oldHolder: RecyclerView.ViewHolder, + newHolder: RecyclerView.ViewHolder, + preInfo: ItemHolderInfo, + postInfo: ItemHolderInfo + ): Boolean { + dispatchChangeFinished(oldHolder, false) + return false + } + + override fun animateChange( + oldHolder: RecyclerView.ViewHolder?, + newHolder: RecyclerView.ViewHolder?, + fromX: Int, + fromY: Int, + toX: Int, + toY: Int + ): Boolean { + dispatchChangeFinished(oldHolder, false) + return false + } + + override fun canReuseUpdatedViewHolder(viewHolder: RecyclerView.ViewHolder): Boolean { + return true + } + + override fun canReuseUpdatedViewHolder(viewHolder: RecyclerView.ViewHolder, payloads: MutableList): Boolean { + return true + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/models/TimerEvent.kt b/app/src/main/kotlin/com/simplemobiletools/clock/models/TimerEvent.kt index b88a4148..a9732eb0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/models/TimerEvent.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/models/TimerEvent.kt @@ -1,10 +1,12 @@ package com.simplemobiletools.clock.models +import com.simplemobiletools.clock.helpers.INVALID_TIMER_ID + sealed class TimerEvent(open val timerId: Int) { data class Delete(override val timerId: Int) : TimerEvent(timerId) data class Reset(override val timerId: Int) : TimerEvent(timerId) data class Start(override val timerId: Int, val duration: Long) : TimerEvent(timerId) data class Pause(override val timerId: Int, val duration: Long) : TimerEvent(timerId) data class Finish(override val timerId: Int, val duration: Long) : TimerEvent(timerId) - data class Refresh(override val timerId: Int) : TimerEvent(timerId) + object Refresh : TimerEvent(INVALID_TIMER_ID) } diff --git a/app/src/main/res/layout/item_timer.xml b/app/src/main/res/layout/item_timer.xml index 8a710a00..7574ef0d 100644 --- a/app/src/main/res/layout/item_timer.xml +++ b/app/src/main/res/layout/item_timer.xml @@ -21,7 +21,7 @@ android:id="@+id/timer_time" android:layout_width="0dp" android:layout_height="wrap_content" - android:textSize="@dimen/stopwatch_text_size" + android:textSize="@dimen/alarm_text_size" app:layout_constraintEnd_toStartOf="@id/timer_reset" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" @@ -43,8 +43,8 @@ 180dp 110dp 68dp + 50dp + 56dp 70sp 60sp