-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for activity scoped navigation request handlers
This gives more flexibility for applications that follows a complex UI design
- Loading branch information
Showing
10 changed files
with
270 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...id/lib/src/main/java/com/ern/api/impl/navigation/ElectrodeNavigationActivityDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.ern.api.impl.navigation; | ||
|
||
import android.os.Bundle; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.fragment.app.Fragment; | ||
import androidx.fragment.app.FragmentActivity; | ||
import androidx.lifecycle.Lifecycle; | ||
import androidx.lifecycle.Observer; | ||
import androidx.lifecycle.OnLifecycleEvent; | ||
import androidx.lifecycle.ViewModelProvider; | ||
|
||
import com.ern.api.impl.core.ElectrodeBaseActivityDelegate; | ||
import com.ern.api.impl.core.LaunchConfig; | ||
import com.walmartlabs.electrode.reactnative.bridge.helpers.Logger; | ||
|
||
public class ElectrodeNavigationActivityDelegate extends ElectrodeBaseActivityDelegate<NavigationLaunchConfig> { | ||
|
||
private static final String TAG = ElectrodeNavigationActivityDelegate.class.getSimpleName(); | ||
|
||
@Nullable | ||
private ReactNavigationViewModel mNavViewModel; | ||
|
||
/** | ||
* @param activity Hosting activity | ||
* @param rootComponentName First react native component to be launched. | ||
* @param defaultLaunchConfig : {@link LaunchConfig} that acts as the the initial configuration to load the rootComponent as well as the default launch config for subsequent navigation flows. | ||
* This configuration will also be used as a default configuration when the root component tries to navigate to a new pages if a proper launch config is passed inside {@link #startMiniAppFragment(String, LaunchConfig)}. | ||
*/ | ||
public ElectrodeNavigationActivityDelegate(@NonNull FragmentActivity activity, @Nullable String rootComponentName, @NonNull NavigationLaunchConfig defaultLaunchConfig) { | ||
super(activity, rootComponentName, defaultLaunchConfig); | ||
} | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
if (mDefaultLaunchConfig.mUseActivityScopedNavigation) { | ||
registerNavRequestHandler(); | ||
} | ||
super.onCreate(savedInstanceState); | ||
} | ||
|
||
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) | ||
@Override | ||
public void onResume() { | ||
super.onResume(); | ||
if (mDefaultLaunchConfig.mUseActivityScopedNavigation && mNavViewModel != null) { | ||
mNavViewModel.registerNavRequestHandler(); | ||
} | ||
} | ||
|
||
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) | ||
@Override | ||
public void onPause() { | ||
super.onPause(); | ||
if (mDefaultLaunchConfig.mUseActivityScopedNavigation && mNavViewModel != null) { | ||
mNavViewModel.unRegisterNavRequestHandler(); | ||
} | ||
} | ||
|
||
private void registerNavRequestHandler() { | ||
mNavViewModel = new ViewModelProvider(mFragmentActivity, new ViewModelProvider.NewInstanceFactory()).get(ReactNavigationViewModel.class); | ||
mNavViewModel.getRouteLiveData().observe(mFragmentActivity, new Observer<Route>() { | ||
@Override | ||
public void onChanged(Route route) { | ||
Logger.d(TAG, "Navigation request handled by Activity(%s)", mFragmentActivity); | ||
NavigationRouteHandler routeHandler = null; | ||
if (mDefaultLaunchConfig.mRouteHandlerProvider != null && mDefaultLaunchConfig.mRouteHandlerProvider.getRouteHandler() != null) { | ||
Logger.v(TAG, "Getting route handler via RouteHandlerProvider"); | ||
routeHandler = mDefaultLaunchConfig.mRouteHandlerProvider.getRouteHandler(); | ||
} else { | ||
Fragment f = mFragmentActivity.getSupportFragmentManager().findFragmentById(mDefaultLaunchConfig.getFragmentContainerId()); | ||
if (f instanceof NavigationRouteHandler) { | ||
Logger.v(TAG, "Getting fragment(route handler) hosted inside getFragmentContainer of activity"); | ||
routeHandler = (NavigationRouteHandler) f; | ||
} else { | ||
Logger.w(TAG, "Fragment: %s is not a NavigationRouteHandler", f); | ||
} | ||
} | ||
|
||
if (routeHandler != null) { | ||
routeHandler.handleRoute(route); | ||
} else { | ||
throw new IllegalStateException("Should never reach here. Missing route handler"); | ||
} | ||
} | ||
}); | ||
mNavViewModel.registerNavRequestHandler(); | ||
} | ||
|
||
protected boolean fragmentScopedNavModel() { | ||
return !mDefaultLaunchConfig.mUseActivityScopedNavigation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.