Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ refactor code #333

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.HashMap;
import java.util.WeakHashMap;

import javax.annotation.Nullable;

import lombok.NonNull;
import lombok.val;

Expand All @@ -15,6 +17,7 @@ public class Bean {
private static HashMap<String, BeanProxy> listener = new HashMap<>(20);
private static HashMap<String, Object> proxyBeans = new HashMap<>(20);

@Nullable
public static <T> T XGET(Class<T> clazz) {
WeakReference<T> ref = null;
String key = null;
Expand All @@ -27,7 +30,7 @@ public static <T> T XGET(Class<T> clazz) {
return ref != null ? ref.get() : null;
}

public static <T> void XSET(Class<T> clazz, T bean) {
public static <T> void XSET(Class<T> clazz, @Nullable T bean) {
WeakReference<T> ref = new WeakReference<>(bean);
String key = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Expand All @@ -38,7 +41,7 @@ public static <T> void XSET(Class<T> clazz, T bean) {
beans.put(key, ref);
}

public static <T> void XSET(Class<T>[] clazzs, T bean) {
public static <T> void XSET(Class<T>[] clazzs, @Nullable T bean) {
WeakReference<T> ref = new WeakReference(bean);
for (Class<?> clazz : clazzs) {
String key = null;
Expand Down
85 changes: 51 additions & 34 deletions android/app/src/main/java/top/ourfor/app/iplayx/page/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -40,12 +42,11 @@ public class Router implements Navigator {
private static final Map<Page, Integer> pageId = new HashMap<>();
private static final Map<String, Class<Page>> pageMap = new HashMap<>();

Interpolator interpolator = new AccelerateDecelerateInterpolator();
long duration = 300;

ViewGroup container;
BottomNavigationView bottomNavigation;
Toolbar toolbar;
Animation pushAnimation;
Animation popAnimation;

static {
pageType = new HashMap<>();
Expand All @@ -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());
Expand Down Expand Up @@ -124,21 +127,19 @@ void navigate(int id) {
public void pushPage(int id, Map<String, Object> 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;
newPage.create(container.getContext(), 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);
}

Expand Down Expand Up @@ -178,17 +179,15 @@ public void pushPage(Page newPage, Map<String, Object> 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;
Expand Down Expand Up @@ -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));
}
}
2 changes: 1 addition & 1 deletion android/app/src/main/res/anim/slide_in.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:interpolator="@android:anim/decelerate_interpolator"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="100%"
android:toXDelta="0%" />
2 changes: 1 addition & 1 deletion android/app/src/main/res/anim/slide_out.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:interpolator="@android:anim/decelerate_interpolator"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0%"
android:toXDelta="100%" />