From 42c439629121c46562585ae7c8cbd32bdcf607a8 Mon Sep 17 00:00:00 2001 From: Patrick Zedler Date: Sun, 26 Nov 2023 16:42:03 +0100 Subject: [PATCH] Fix crash on LoginRequestFragment caused by navigation animator --- .../grocy/fragment/LoginRequestFragment.java | 31 +++++------ .../grocy/fragment/SettingsFragment.java | 39 ++------------ .../xyz/zedler/patrick/grocy/util/UiUtil.java | 53 +++++++++++++++++++ app/src/main/res/raw/changelog.txt | 1 + 4 files changed, 70 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/xyz/zedler/patrick/grocy/fragment/LoginRequestFragment.java b/app/src/main/java/xyz/zedler/patrick/grocy/fragment/LoginRequestFragment.java index 0b15d0012..ad63090e8 100644 --- a/app/src/main/java/xyz/zedler/patrick/grocy/fragment/LoginRequestFragment.java +++ b/app/src/main/java/xyz/zedler/patrick/grocy/fragment/LoginRequestFragment.java @@ -19,6 +19,7 @@ package xyz.zedler.patrick.grocy.fragment; +import android.animation.Animator; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; @@ -38,6 +39,7 @@ import xyz.zedler.patrick.grocy.model.Event; import xyz.zedler.patrick.grocy.model.SnackbarMessage; import xyz.zedler.patrick.grocy.util.ClickUtil; +import xyz.zedler.patrick.grocy.util.UiUtil; import xyz.zedler.patrick.grocy.util.ViewUtil; import xyz.zedler.patrick.grocy.viewmodel.LoginRequestViewModel; @@ -121,27 +123,20 @@ private void navigateToStartDestination() { public void login(boolean checkVersion) { viewModel.clearHassData(); new Handler().postDelayed(() -> viewModel.login(checkVersion), 500); - ; } @Override public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { - if (nextAnim == 0) { - return null; - } - Animation anim = AnimationUtils.loadAnimation(getActivity(), nextAnim); - anim.setAnimationListener(new Animation.AnimationListener() { - @Override - public void onAnimationStart(Animation animation) { - } - @Override - public void onAnimationRepeat(Animation animation) { - } - @Override - public void onAnimationEnd(Animation animation) { - if (enter) login(true); - } - }); - return anim; + return UiUtil.runOnAnimationEnd( + requireContext(), enter, nextAnim, () -> login(true) + ); + } + + @Nullable + @Override + public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { + return UiUtil.runOnAnimatorEnd( + requireContext(), enter, nextAnim, () -> login(true) + ); } } diff --git a/app/src/main/java/xyz/zedler/patrick/grocy/fragment/SettingsFragment.java b/app/src/main/java/xyz/zedler/patrick/grocy/fragment/SettingsFragment.java index 4344122f7..b0c802bf5 100644 --- a/app/src/main/java/xyz/zedler/patrick/grocy/fragment/SettingsFragment.java +++ b/app/src/main/java/xyz/zedler/patrick/grocy/fragment/SettingsFragment.java @@ -41,6 +41,7 @@ import xyz.zedler.patrick.grocy.databinding.FragmentSettingsBinding; import xyz.zedler.patrick.grocy.util.ClickUtil; import xyz.zedler.patrick.grocy.util.PrefsUtil; +import xyz.zedler.patrick.grocy.util.UiUtil; public class SettingsFragment extends BaseFragment { @@ -116,47 +117,13 @@ public boolean shouldNavigateToServer() { @Override public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { - Animation animation = null; - if (nextAnim != 0) { - try { - animation = AnimationUtils.loadAnimation(getActivity(), nextAnim); - animation.setAnimationListener(new Animation.AnimationListener() { - @Override - public void onAnimationStart(Animation animation) {} - - @Override - public void onAnimationRepeat(Animation animation) {} - - @Override - public void onAnimationEnd(Animation animation) { - if (enter) { - navigateToSubpage(); - } - } - }); - } catch (Exception ignored) {} - } - return animation; + return UiUtil.runOnAnimationEnd(requireContext(), enter, nextAnim, this::navigateToSubpage); } @Nullable @Override public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { - Animator animator = null; - if (nextAnim != 0) { - try { - animator = AnimatorInflater.loadAnimator(getActivity(), nextAnim); - animator.addListener(new AnimatorListenerAdapter() { - @Override - public void onAnimationEnd(Animator animation) { - if (enter) { - navigateToSubpage(); - } - } - }); - } catch (Exception ignored) {} - } - return animator; + return UiUtil.runOnAnimatorEnd(requireContext(), enter, nextAnim, this::navigateToSubpage); } private void navigateToSubpage() { diff --git a/app/src/main/java/xyz/zedler/patrick/grocy/util/UiUtil.java b/app/src/main/java/xyz/zedler/patrick/grocy/util/UiUtil.java index a9035a899..f43eb9b30 100644 --- a/app/src/main/java/xyz/zedler/patrick/grocy/util/UiUtil.java +++ b/app/src/main/java/xyz/zedler/patrick/grocy/util/UiUtil.java @@ -19,6 +19,9 @@ package xyz.zedler.patrick.grocy.util; +import android.animation.Animator; +import android.animation.AnimatorInflater; +import android.animation.AnimatorListenerAdapter; import android.animation.ValueAnimator; import android.annotation.SuppressLint; import android.app.Activity; @@ -46,6 +49,8 @@ import android.view.WindowInsetsController; import android.view.WindowManager; import android.view.WindowMetrics; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; import androidx.annotation.Dimension; import androidx.annotation.MenuRes; import androidx.annotation.NonNull; @@ -578,4 +583,52 @@ public static boolean areAnimationsEnabled(Context context) { ) != 0; return duration && transition && window; } + + // Animation and animator + + public static Animation runOnAnimationEnd( + @NonNull Context context, boolean enter, int nextAnim, @NonNull Runnable action + ) { + Animation animation = null; + if (nextAnim != 0) { + try { + animation = AnimationUtils.loadAnimation(context, nextAnim); + animation.setAnimationListener(new Animation.AnimationListener() { + @Override + public void onAnimationStart(Animation animation) {} + + @Override + public void onAnimationRepeat(Animation animation) {} + + @Override + public void onAnimationEnd(Animation animation) { + if (enter) { + action.run(); + } + } + }); + } catch (Exception ignored) {} + } + return animation; + } + + public static Animator runOnAnimatorEnd( + @NonNull Context context, boolean enter, int nextAnim, @NonNull Runnable action + ) { + Animator animator = null; + if (nextAnim != 0) { + try { + animator = AnimatorInflater.loadAnimator(context, nextAnim); + animator.addListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + if (enter) { + action.run(); + } + } + }); + } catch (Exception ignored) {} + } + return animator; + } } diff --git a/app/src/main/res/raw/changelog.txt b/app/src/main/res/raw/changelog.txt index 04f21e904..3af6c61d9 100644 --- a/app/src/main/res/raw/changelog.txt +++ b/app/src/main/res/raw/changelog.txt @@ -1,6 +1,7 @@ ## 3.4.1 - Fixed: Crash when navigating to settings (#805) +- Fixed: Crash on login page ## 3.4.0