Skip to content

Commit

Permalink
Fix crash on LoginRequestFragment caused by navigation animator
Browse files Browse the repository at this point in the history
  • Loading branch information
patzly committed Nov 26, 2023
1 parent 79cd62a commit 42c4396
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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() {
Expand Down
53 changes: 53 additions & 0 deletions app/src/main/java/xyz/zedler/patrick/grocy/util/UiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions app/src/main/res/raw/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 3.4.1

- Fixed: Crash when navigating to settings (#805)
- Fixed: Crash on login page

## 3.4.0

Expand Down

0 comments on commit 42c4396

Please sign in to comment.