From 6888de85fc87e76adaa0837ea2ebf21f2e05485e Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Thu, 21 Nov 2024 22:33:15 +0100 Subject: [PATCH] Refactor(quick settings): better lifecycle interface --- .../src/main/java/com/kdt/SideDialogView.java | 30 ++++++++++++------- .../handleview/EditControlSideDialog.java | 19 +++++------- .../prefs/QuickSettingSideDialog.java | 12 ++++---- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/app_pojavlauncher/src/main/java/com/kdt/SideDialogView.java b/app_pojavlauncher/src/main/java/com/kdt/SideDialogView.java index 80b9cc86ff..6b7a2ce06d 100644 --- a/app_pojavlauncher/src/main/java/com/kdt/SideDialogView.java +++ b/app_pojavlauncher/src/main/java/com/kdt/SideDialogView.java @@ -149,17 +149,16 @@ private void deflateLayout() { /** * Slide the layout into the visible screen area - * @return Whether the layout position has changed */ @CallSuper public final void appear(boolean fromRight) { - boolean willBuild = !mIsInstantiated; if (!mIsInstantiated) { inflateLayout(); + onInflate(); } // To avoid UI sizing issue when the dialog is not fully inflated - onAppear(willBuild); + onAppear(); Tools.runOnUiThread(() -> { if (fromRight) { if (!mDisplaying || !isAtRight()) { @@ -190,7 +189,8 @@ protected final boolean isAtRight() { public final void disappear(boolean destroy) { if (!mDisplaying) { if(destroy) { - onDisappear(true); + onDisappear(); + onDestroy(); deflateLayout(); } return; @@ -203,7 +203,8 @@ public final void disappear(boolean destroy) { mSideDialogAnimator.setFloatValues(mMargin, -mDialogLayout.getWidth()); if(destroy) { - onDisappear(true); + onDisappear(); + onDestroy(); mSideDialogAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { @@ -222,16 +223,25 @@ protected final boolean isDisplaying(){ } /** - * Called after the dialog has appeared with an inflated layout - * @param hasBuilt Whether the layout has JUST been built. If false, the layout has been built before + * Called when the dialog is inflated, ideal for setting up UI elements bindings */ - protected abstract void onAppear(boolean hasBuilt); + protected void onInflate() {} + + /** + * Called after the dialog has appeared + */ + protected void onAppear() {} /** * Called after the dialog has disappeared - * @param willDestroy Whether the dialog will be destroyed after disappearing */ - protected abstract void onDisappear(boolean willDestroy); + protected void onDisappear() {} + + /** + * Called before the dialog gets destroyed (removing views from parent) + * Ideal for cleaning up resources + */ + protected void onDestroy() {} } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java index 4e00c17c21..f2b9c684ff 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/handleview/EditControlSideDialog.java @@ -91,21 +91,16 @@ public EditControlSideDialog(Context context, ViewGroup parent) { } @Override - protected void onAppear(boolean hasBuilt) { - if(hasBuilt) { - bindLayout(); - buildColorSelector(); - - loadAdapter(); - setupRealTimeListeners(); - } + protected void onInflate() { + bindLayout(); + buildColorSelector(); + loadAdapter(); + setupRealTimeListeners(); } @Override - protected void onDisappear(boolean willDestroy) { - if (willDestroy) { - mParent.removeView(mColorSelector.getRootView()); - } + protected void onDestroy() { + mParent.removeView(mColorSelector.getRootView()); } /* While the selector could be retrofitted to side dialog, it's not worth the effort */ diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/QuickSettingSideDialog.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/QuickSettingSideDialog.java index a09017b6b5..c2f9cf6c99 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/QuickSettingSideDialog.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/QuickSettingSideDialog.java @@ -48,16 +48,14 @@ public QuickSettingSideDialog(Context context, ViewGroup parent) { } @Override - protected void onAppear(boolean hasBuilt) { - if (hasBuilt) { - bindLayout(); - Tools.runOnUiThread(this::setupListeners); - } + protected void onInflate() { + bindLayout(); + Tools.runOnUiThread(this::setupListeners); } @Override - protected void onDisappear(boolean willDestroy) { - if (willDestroy) removeListeners(); + protected void onDestroy() { + removeListeners(); } private void bindLayout() {