Skip to content

Commit

Permalink
Merge pull request #107 from deepueg/cleanup-minor
Browse files Browse the repository at this point in the history
Up navigation refactoring for Android
  • Loading branch information
deepueg authored Apr 23, 2020
2 parents 1571f31 + ee26a1a commit 3f372fd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ElectrodeBaseActivityDelegate(@NonNull FragmentActivity activity, @Nullab
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null && mDefaultLaunchConfig.startRootInOnCreate) {
if (savedInstanceState == null && mDefaultLaunchConfig.mStartRootInOnCreate) {
launchRootComponent();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public class LaunchConfig {
/**
* Decides of the root component needs to be launched when the activity delegates onCreate is called.
*/
boolean startRootInOnCreate = true;
boolean mStartRootInOnCreate = true;

/***
/**
* Set this to to true if the root page up click is handled by React native.
*/
boolean rootBackPressHandledByRN = false;
boolean mRootBackPressHandledByRN;

public LaunchConfig() {
}
Expand Down Expand Up @@ -178,7 +178,7 @@ public boolean isShowAsOverlay() {
* @param startRootInOnCreate true | false Default value: true
*/
public void setStartRootInOnCreate(boolean startRootInOnCreate) {
this.startRootInOnCreate = startRootInOnCreate;
mStartRootInOnCreate = startRootInOnCreate;
}

/**
Expand All @@ -187,7 +187,7 @@ public void setStartRootInOnCreate(boolean startRootInOnCreate) {
*/

public boolean isStartRootInOnCreate() {
return startRootInOnCreate;
return mStartRootInOnCreate;
}

/**
Expand All @@ -196,7 +196,7 @@ public boolean isStartRootInOnCreate() {
* @return true | false
*/
public void setRootBackPressHandledByRN(boolean rootBackPressHandledByRN) {
this.rootBackPressHandledByRN = rootBackPressHandledByRN;
mRootBackPressHandledByRN = rootBackPressHandledByRN;
}

/**
Expand All @@ -205,6 +205,6 @@ public void setRootBackPressHandledByRN(boolean rootBackPressHandledByRN) {
* @return true | false
*/
public boolean isRootBackPressHandledByRN() {
return rootBackPressHandledByRN;
return mRootBackPressHandledByRN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ public class ElectrodeNavigationFragmentDelegate<T extends ElectrodeBaseFragment
@Nullable
private Menu mMenu;

private OnBackPressedCallback mBackPressedCallback;

/**
* Click listener that gets instantiated when React Native sends a left button configuration inside the {@link NavigationBar} object as part of the navigate() or update() api call.
*/
@Nullable
private OnHomeAsUpClickedCallback mOnHomeAsUpClickedCallback;
private BackPressedCallback mBackPressedCallback;

private final Observer<Route> routeObserver = new Observer<Route>() {
@Override
Expand Down Expand Up @@ -192,7 +186,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Fragment fragment = getTopOfTheStackFragment();
if (mFragment == fragment) {
if ((mOnHomeAsUpClickedCallback != null && mOnHomeAsUpClickedCallback.onHomAsUpClicked())) {
if (mBackPressedCallback.isEnabled()) {
mBackPressedCallback.handleOnBackPressed();
return true;
} else {
mFragment.requireActivity().onBackPressed();
Expand Down Expand Up @@ -376,24 +371,17 @@ private void updateNavBar(@NonNull NavigationBar navigationBar) {
private void updateHomeAsUpIndicator(@Nullable NavigationBarLeftButton leftButton) {
ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
mBackPressedCallback.setEnabled(leftButton);
if (leftButton != null) {
if (leftButton.getId() != null) {
Logger.v(TAG, "Enabling backPressedCallback for component:%s. LeftButton has id. Back press event will now be sent to react native for handling", getReactComponentName());
mOnHomeAsUpClickedCallback = new HomeAsUpClickedCallback(leftButton);
mBackPressedCallback.setEnabled(true);
} else {
disableBackPressCallback();
}
if (leftButton.getDisabled() != null && leftButton.getDisabled()) {
Logger.d(TAG, "Disabling DisplayHomeAsUp for component: %s", getReactComponentName());
supportActionBar.setDisplayHomeAsUpEnabled(false);
return;
} else if (setHomeAsUpIndicatorIcon(supportActionBar, leftButton)) {
return;
}
} else {
disableBackPressCallback();
}

//Default action
if (mFragment.getArguments() != null && mFragment.getArguments().getBoolean(ActivityDelegateConstants.KEY_MINI_APP_FRAGMENT_SHOW_UP_ENABLED)) {
Logger.d(TAG, "Defaulting up indicator for component: %s", getReactComponentName());
Expand All @@ -405,11 +393,6 @@ private void updateHomeAsUpIndicator(@Nullable NavigationBarLeftButton leftButto
}
}

private void disableBackPressCallback() {
mOnHomeAsUpClickedCallback = null;
mBackPressedCallback.setEnabled(false);
}

private boolean setHomeAsUpIndicatorIcon(@NonNull ActionBar supportActionBar, @NonNull NavigationBarLeftButton leftButton) {
if (mFragment.getActivity() != null) {
final String iconName = leftButton.getIcon();
Expand Down Expand Up @@ -473,34 +456,10 @@ private void updateTitle(@NonNull NavigationBar navigationBar) {
}
}

private interface OnHomeAsUpClickedCallback {
boolean onHomAsUpClicked();
}

private class HomeAsUpClickedCallback implements OnHomeAsUpClickedCallback {
NavigationBarLeftButton mLeftButton;

HomeAsUpClickedCallback(@NonNull NavigationBarLeftButton button) {
mLeftButton = button;
}

@Override
public boolean onHomAsUpClicked() {
if (mLeftButton.getId() != null) {
Logger.v(TAG, "HomeAsUpClickedCallback: firing event to React Native, button id: %s", mLeftButton.getId());
//TODO: This line should be removed with next major version update of ern-navigation. Keeping here for backward compatibility
EnNavigationApi.events().emitOnNavButtonClick(mLeftButton.getId());
EnNavigationApi.events().emitNavEvent(new NavEventData.Builder(NavEventType.BUTTON_CLICK.toString()).viewId(getMiniAppViewIdentifier()).jsonPayload("{\"id\": \"" + mLeftButton.getId() + "\"}").build());
return true;
} else if (mLeftButton.getDisabled() != null && mLeftButton.getDisabled()) {
//Back press disabled.
return true;
}
return false;
}
}

private class BackPressedCallback extends OnBackPressedCallback {
@Nullable
private NavigationBarLeftButton mLeftButton;

/**
* Create a {@link OnBackPressedCallback}.
*
Expand All @@ -511,15 +470,31 @@ private BackPressedCallback(boolean enabled) {
super(enabled);
}

private void setEnabled(@Nullable NavigationBarLeftButton leftButton) {
mLeftButton = leftButton;
if (leftButton != null && (leftButton.getId() != null || (leftButton.getDisabled() != null && leftButton.getDisabled()))) {
setEnabled(true);
} else {
setEnabled(false);
}
}

@Override
public void handleOnBackPressed() {
if (mOnHomeAsUpClickedCallback == null) {
throw new IllegalStateException("Should never reach here. OnBackPressedCallback should only be enabled if the mOnHomeAsUpClickedCallback is set by React Native component via NavigationBarLeftButton");
if (mLeftButton == null) {
throw new IllegalStateException("handleOnBackPressed: Should never reach here. NavigationBarLeftButton is null.");
}
if (mOnHomeAsUpClickedCallback.onHomAsUpClicked()) {
Logger.v(TAG, "BackPressedCallback, handled by %s", getReactComponentName());

if (mLeftButton.getId() != null) {
Logger.v(TAG, "handleOnBackPressed: firing event to React Native, button id: %s", mLeftButton.getId());
//TODO: This line should be removed with next major version update of ern-navigation. Keeping here for backward compatibility
EnNavigationApi.events().emitOnNavButtonClick(mLeftButton.getId());

EnNavigationApi.events().emitNavEvent(new NavEventData.Builder(NavEventType.BUTTON_CLICK.toString()).viewId(getMiniAppViewIdentifier()).jsonPayload("{\"id\": \"" + mLeftButton.getId() + "\"}").build());
} else if (mLeftButton.getDisabled() != null && mLeftButton.getDisabled()) {
Logger.v(TAG, "handleOnBackPressed: Back press disabled, component:%s", getReactComponentName());
} else {
Logger.w(TAG, "BackPressedCallback, not handled by %s.", getReactComponentName());
Logger.w(TAG, "handleOnBackPressed: not handled by %s. [SHOULD NEVER REACH HERE]", getReactComponentName());
}
}
}
Expand Down

0 comments on commit 3f372fd

Please sign in to comment.