From 2506e949a9b9dc57dbdcda02bd02056ef1c254c4 Mon Sep 17 00:00:00 2001 From: ourfor Date: Wed, 18 Dec 2024 11:33:02 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../top/ourfor/app/iplayx/module/Bean.java | 7 +- .../top/ourfor/app/iplayx/page/Router.java | 85 +++++++++++-------- android/app/src/main/res/anim/slide_in.xml | 2 +- android/app/src/main/res/anim/slide_out.xml | 2 +- 4 files changed, 58 insertions(+), 38 deletions(-) diff --git a/android/app/src/main/java/top/ourfor/app/iplayx/module/Bean.java b/android/app/src/main/java/top/ourfor/app/iplayx/module/Bean.java index c53565ee..72a25832 100644 --- a/android/app/src/main/java/top/ourfor/app/iplayx/module/Bean.java +++ b/android/app/src/main/java/top/ourfor/app/iplayx/module/Bean.java @@ -7,6 +7,8 @@ import java.util.HashMap; import java.util.WeakHashMap; +import javax.annotation.Nullable; + import lombok.NonNull; import lombok.val; @@ -15,6 +17,7 @@ public class Bean { private static HashMap listener = new HashMap<>(20); private static HashMap proxyBeans = new HashMap<>(20); + @Nullable public static T XGET(Class clazz) { WeakReference ref = null; String key = null; @@ -27,7 +30,7 @@ public static T XGET(Class clazz) { return ref != null ? ref.get() : null; } - public static void XSET(Class clazz, T bean) { + public static void XSET(Class clazz, @Nullable T bean) { WeakReference ref = new WeakReference<>(bean); String key = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { @@ -38,7 +41,7 @@ public static void XSET(Class clazz, T bean) { beans.put(key, ref); } - public static void XSET(Class[] clazzs, T bean) { + public static void XSET(Class[] clazzs, @Nullable T bean) { WeakReference ref = new WeakReference(bean); for (Class clazz : clazzs) { String key = null; diff --git a/android/app/src/main/java/top/ourfor/app/iplayx/page/Router.java b/android/app/src/main/java/top/ourfor/app/iplayx/page/Router.java index 15b6f8bb..ab63ac56 100644 --- a/android/app/src/main/java/top/ourfor/app/iplayx/page/Router.java +++ b/android/app/src/main/java/top/ourfor/app/iplayx/page/Router.java @@ -7,6 +7,8 @@ import android.view.View; import android.view.ViewGroup; import android.view.animation.AccelerateDecelerateInterpolator; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; import android.view.animation.BounceInterpolator; import android.view.animation.Interpolator; @@ -40,12 +42,11 @@ public class Router implements Navigator { private static final Map pageId = new HashMap<>(); private static final Map> pageMap = new HashMap<>(); - Interpolator interpolator = new AccelerateDecelerateInterpolator(); - long duration = 300; - ViewGroup container; BottomNavigationView bottomNavigation; Toolbar toolbar; + Animation pushAnimation; + Animation popAnimation; static { pageType = new HashMap<>(); @@ -71,6 +72,8 @@ public class Router implements Navigator { this.container = container; this.bottomNavigation = bottomNavigation; this.toolbar = toolbar; + pushAnimation = AnimationUtils.loadAnimation(container.getContext(), R.anim.slide_in); + popAnimation = AnimationUtils.loadAnimation(container.getContext(), R.anim.slide_out); navigators = new HashMap<>(); bottomNavigation.setOnItemSelectedListener(item -> { navigate(item.getItemId()); @@ -124,8 +127,7 @@ void navigate(int id) { public void pushPage(int id, Map params) { val pages = navigators.computeIfAbsent(stackId, k -> new Stack<>()); if (pages.isEmpty()) return; - val page = pages.peek(); - page.viewWillDisappear(); + val oldPage = pages.peek(); val newPage = makePage(id); pageId.put(newPage, id); assert newPage != null; @@ -133,12 +135,11 @@ public void pushPage(int id, Map params) { pages.push(newPage); val view = newPage.view(); view.setBackgroundResource(R.drawable.bg); - newPage.viewWillAppear(); container.addView(view, LayoutUtil.fill()); if (view instanceof PageLifecycle lifecycle) { lifecycle.onAttach(); } - pushPageAnimation(page, newPage); + pushPageAnimation(oldPage, newPage); onNavigateChange(id); } @@ -178,17 +179,15 @@ public void pushPage(Page newPage, Map params) { public boolean popPage() { val pages = navigators.computeIfAbsent(stackId, k -> new Stack<>()); if (pages.isEmpty()) return false; - val page = pages.pop(); - pageId.remove(page); - page.viewWillDisappear(); - if (page.view() instanceof PageLifecycle lifecycle) { + val oldPage = pages.pop(); + pageId.remove(oldPage); + if (oldPage.view() instanceof PageLifecycle lifecycle) { lifecycle.onDetach(); } val newPage = pages.peek(); val view = newPage.view(); - newPage.viewWillAppear(); container.addView(view, 0, LayoutUtil.fill()); - popPageAnimation(page, newPage); + popPageAnimation(oldPage, newPage); onNavigateChange(pageId.get(newPage)); newPage.viewDidAppear(); return true; @@ -266,29 +265,47 @@ public int getCurrentPageId() { } void pushPageAnimation(Page oldPage, Page newPage) { - newPage.view().setTranslationX(oldPage.view().getWidth()); - newPage.view().animate() - .translationX(0) - .setDuration(duration) - .setInterpolator(interpolator) - .withEndAction(() -> { - container.removeView(oldPage.view()); - oldPage.viewDidDisappear(); - newPage.viewDidAppear(); - }) - .start(); + val view = newPage.view(); + view.setVisibility(View.INVISIBLE); + pushAnimation.setAnimationListener(new Animation.AnimationListener() { + @Override + public void onAnimationStart(Animation animation) { + view.setVisibility(View.VISIBLE); + oldPage.viewWillDisappear(); + newPage.viewWillAppear(); + } + + @Override + public void onAnimationEnd(Animation animation) { + container.removeView(oldPage.view()); + oldPage.viewDidDisappear(); + newPage.viewDidAppear(); + } + + @Override + public void onAnimationRepeat(Animation animation) { } + }); + container.post(() -> view.startAnimation(pushAnimation)); } void popPageAnimation(Page oldPage, Page newPage) { - oldPage.view().animate() - .translationX(oldPage.view().getWidth()) - .setDuration(duration) - .setInterpolator(interpolator) - .withEndAction(() -> { - container.removeView(oldPage.view()); - oldPage.viewDidDisappear(); - newPage.viewDidAppear(); - }) - .start(); + popAnimation.setAnimationListener(new Animation.AnimationListener() { + @Override + public void onAnimationStart(Animation animation) { + oldPage.viewWillDisappear(); + newPage.viewWillAppear(); + } + + @Override + public void onAnimationEnd(Animation animation) { + container.removeView(oldPage.view()); + oldPage.viewDidDisappear(); + newPage.viewDidAppear(); + } + + @Override + public void onAnimationRepeat(Animation animation) { } + }); + container.post(() -> oldPage.view().startAnimation(popAnimation)); } } diff --git a/android/app/src/main/res/anim/slide_in.xml b/android/app/src/main/res/anim/slide_in.xml index 24bd03cd..9bfc58cf 100644 --- a/android/app/src/main/res/anim/slide_in.xml +++ b/android/app/src/main/res/anim/slide_in.xml @@ -1,6 +1,6 @@ \ No newline at end of file diff --git a/android/app/src/main/res/anim/slide_out.xml b/android/app/src/main/res/anim/slide_out.xml index 60055523..e894b500 100644 --- a/android/app/src/main/res/anim/slide_out.xml +++ b/android/app/src/main/res/anim/slide_out.xml @@ -1,6 +1,6 @@ \ No newline at end of file