Skip to content

Commit

Permalink
Refactor(quick settings): better lifecycle interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias-Boulay committed Nov 21, 2024
1 parent 1aface5 commit 6888de8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
30 changes: 20 additions & 10 deletions app_pojavlauncher/src/main/java/com/kdt/SideDialogView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -190,7 +189,8 @@ protected final boolean isAtRight() {
public final void disappear(boolean destroy) {
if (!mDisplaying) {
if(destroy) {
onDisappear(true);
onDisappear();
onDestroy();
deflateLayout();
}
return;
Expand All @@ -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) {
Expand All @@ -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() {}


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

0 comments on commit 6888de8

Please sign in to comment.