Skip to content

Commit

Permalink
[Catalog][Predictive Back][Navigation Drawer] Update navigation drawe…
Browse files Browse the repository at this point in the history
…r state handling to be more accurate in custom drawer demo

Partially addresses #3593
Resolves #3596

GIT_ORIGIN_REV_ID=9ca19b41473caa9bb303c7ac1ed2c5191cb4b081
PiperOrigin-RevId: 569180296
  • Loading branch information
manabu-nakamura authored and dsn5ft committed Oct 2, 2023
1 parent d5d604d commit 1252d4f
Showing 1 changed file with 44 additions and 30 deletions.
Expand Up @@ -86,50 +86,64 @@ public View onCreateDemoView(
new SimpleDrawerListener() {
@Override
public void onDrawerOpened(@NonNull View drawerView) {
currentDrawerView = drawerView;
sideContainerBackHelper = new MaterialSideContainerBackHelper(drawerView);

if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (drawerOnBackAnimationCallback == null) {
drawerOnBackAnimationCallback = createOnBackAnimationCallback();
}
drawerLayout.post(
() ->
getOnBackInvokedDispatcher()
.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_OVERLAY,
drawerOnBackAnimationCallback));
} else {
getOnBackPressedDispatcher()
.addCallback(
CustomNavigationDrawerDemoActivity.this, drawerOnBackPressedCallback);
}
registerBackCallback(drawerView);
}

@Override
public void onDrawerClosed(@NonNull View drawerView) {
currentDrawerView = null;
sideContainerBackHelper = null;

if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (drawerOnBackAnimationCallback != null) {
getOnBackInvokedDispatcher()
.unregisterOnBackInvokedCallback(drawerOnBackAnimationCallback);
drawerOnBackAnimationCallback = null;
}
} else {
drawerOnBackPressedCallback.remove();
}
unregisterBackCallback();
}
});

View endDrawer = view.findViewById(R.id.custom_drawer_end);
view.findViewById(R.id.show_end_drawer_gravity)
.setOnClickListener(v -> drawerLayout.openDrawer(endDrawer));

drawerLayout.post(
() -> {
View startDrawer = view.findViewById(R.id.custom_drawer_start);
if (drawerLayout.isDrawerOpen(startDrawer)) {
registerBackCallback(startDrawer);
} else if (drawerLayout.isDrawerOpen(endDrawer)) {
registerBackCallback(endDrawer);
}
});

return view;
}

private void registerBackCallback(@NonNull View drawerView) {
currentDrawerView = drawerView;
sideContainerBackHelper = new MaterialSideContainerBackHelper(drawerView);

if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (drawerOnBackAnimationCallback == null) {
drawerOnBackAnimationCallback = createOnBackAnimationCallback();
}
drawerLayout.post(
() ->
getOnBackInvokedDispatcher()
.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_OVERLAY, drawerOnBackAnimationCallback));
} else {
getOnBackPressedDispatcher().addCallback(this, drawerOnBackPressedCallback);
}
}

private void unregisterBackCallback() {
currentDrawerView = null;
sideContainerBackHelper = null;

if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (drawerOnBackAnimationCallback != null) {
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(drawerOnBackAnimationCallback);
drawerOnBackAnimationCallback = null;
}
} else {
drawerOnBackPressedCallback.remove();
}
}

@Override
protected boolean shouldShowDefaultDemoActionBar() {
return false;
Expand Down

0 comments on commit 1252d4f

Please sign in to comment.