The Getting Started tutorial walks you through creating a simple movie application using Electrode Native.
We'll use Android for this tutorial. If you wish to use iOS instead, select the iOS tab.
The movie application includes two React Native MiniApps and two APIs:
MovieListMiniApp | This MiniApp displays a list of movies.
MovieDetailsMiniApp | This MiniApp displays the details of a selected movie.
MoviesApi | An API used to retrieve a list of movies.
NavigationApi | An API used to navigate from one MiniApp to another.
The tutorial shows how easy it is to integrate multiple React Native applications into a native application, and how to easily communicate between the JavaScript and the native side using APIs.
Download Android 7.0 (Nougat) and accept it's license: 1. In Android Studio go to Tools → Android → SDK Manager 2. Select the “Android 7.0 (Nougat)” checkbox 3. Click “Apply” and follow the prompts:
The manager will download all the dependencies:
Set up an emulator if you want to run the application in an emulator. For more information on how to setup an emulator, you can check the Android documentation
Create a working directory named ElectrodeNativeTutorial to hold all tutorial project files
Creating the MovieList MiniApp
1) Move to the working directory and create a MiniApp named MovieListMiniApp using the ern create-miniapp command.
2) When asked to enter a package name for this MiniApp, hit enter to use the default name. You may check the package name requirements
3) Move to the MovieListMiniApp directory and run the MiniApp to view it, using the ern run command.
$cdMovieListMiniApp$ernrun-android
4) First time users will need to grant the SYSTEM_ALERT_WINDOW permission for ErnRunner app . (Learn More). Once done exit from the ErnRunner app and launch it again from applications.
5) Select an emulator (or device) from the list when prompted.
Once the command completes, you will see your first MiniApp running. If you used React Native previously, you'll notice that this MiniApp is the same as the React Native default starter app--after all, a MiniApp is nothing more than a React Native application!
Now let's update the UI of this MiniApp to display a list of movies.
Updating the MovieList MiniApp UI
1) Open the App.js file in your favorite JavaScript editor.
2) Replace the content of this source file with the following code.
3) Save your changes to App.js and reload the application to see the updated UI. Press the R key twice or select Reload from the Developer Menu (⌘M).
Congratulations! You've successfully run and modified the initial UI of the MovieList MiniApp.
Now let's add an API to the MiniApp so that we can retrieve movies from the native application instead of manually hard coding them in the source code of our MiniApp.
Adding the MoviesApi to the MovieList MiniApp
We have already created and published the MoviesApi and a native implementation of the api for the needs of this tutorial. You may view the created API code in react-native-ernmovie-api repository and the implementation code in ReactNativeErnmovieApiImpl repository. We also created a NavigationApi that will be of use later in this tutorial, you can view its code in the react-native-ernnavigation-api repository.
1) Add the MoviesApi, the MoviesApiImpl, the NavigationApi and react-native-electrode-bridge as dependencies of MovieListMiniApp, using the ern add command.
6) Because we added an API, that contains some native code, we'll need to regenerate the container used by the native application, in order for it to include the native code of the API. This can be done using the run command which recreates a new local container and launches the application. Enter the following run command:
$ernrun-android
The UI displays the movie names that are returned by the native implementation of the movie api.
Using the Navigation API
We will use the NavigationApi that we already added to our MiniApp earlier on. This very simple API will be used for navigating from the MovieListMiniApp to the MovieDetailsMiniApp.
1) Mofify the App.js file as follows so that when selecting a movie in the list, the MovieListMiniApp will call the navigation API to navigate to the MovieDetailsMiniApp to display the details of the selected Movie.
6) Implement the NavigationApi in the native application.
Replace the content of MainActivity.java with the following code:
packagecom.walmartlabs.ern;importandroid.content.Intent;importandroid.support.annotation.NonNull;importandroid.support.annotation.Nullable;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.util.Log;importandroid.widget.Toast;importcom.ernnavigation.ern.api.NavigateData;importcom.ernnavigation.ern.api.NavigationApi;importcom.walmartlabs.electrode.reactnative.bridge.BridgeFailureMessage;importcom.walmartlabs.electrode.reactnative.bridge.ElectrodeBridgeRequestHandler;importcom.walmartlabs.electrode.reactnative.bridge.ElectrodeBridgeResponseListener;importcom.walmartlabs.ern.container.ElectrodeMiniAppActivity;importcom.walmartlabs.ern.container.miniapps.MiniAppsConfig;importcom.walmartlabs.ern.container.miniapps.MovieListMiniAppActivity;// This is the main activity that gets launched upon app start // It just launches the activity containing the miniapp // Feel free to modify it at your convenience. publicclassMainActivityextendsAppCompatActivity { @OverrideprotectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getIntent().getBundleExtra("data");Intent i =newIntent(this,MovieListMiniAppActivity.class);this.startActivity(i);NavigationApi.requests().registerNavigateRequestHandler(newElectrodeBridgeRequestHandler<NavigateData,Boolean>() { @OverridepublicvoidonRequest(@NullableNavigateData navigateData, @NonNullElectrodeBridgeResponseListener<Boolean> responseListener) {if (!MainActivity.this.isFinishing()) {if (navigateData !=null) {Class activityClass =MiniAppsConfig.MINIAPP_ACTIVITIES.get(navigateData.getminiAppName());if (activityClass !=null) {Bundle bundle =newBundle();bundle.putString("payload",navigateData.getinitialPayload());Intent intent =newIntent(MainActivity.this, activityClass);ElectrodeMiniAppActivity.addInitialProps(intent, bundle);MainActivity.this.startActivity(intent);responseListener.onSuccess(true); } else {Toast.makeText(MainActivity.this,"No activity found to navigate for: "+navigateData.getminiAppName(),Toast.LENGTH_LONG).show();responseListener.onFailure(BridgeFailureMessage.create("ERROR_NAVIGATION_FAILED","Something went wrong.",newException("Data received is not enough to navigate. Unable to find activity for MiniApp: "+navigateData.getminiAppName()))); } } else {Log.e("NAVIGATION","Not enough data provided to navigate");responseListener.onFailure(BridgeFailureMessage.create("ERROR_NAVIGATION_FAILED","Something went wrong.",newException("Data received is null. No MiniApp name provided to navigate."))); } } else {Log.w("NAVIGATION","Activity is finishing or null, cannot get a valid activity context to navigate");responseListener.onFailure(BridgeFailureMessage.create("ERROR_NAVIGATION_FAILED","Something went wrong.",newException("No valid activity context found. Current activity is either null or finishing."))); } } }); }}
Adding the MovieDetailsMiniApp
To complete the tutorial, add the MovieDetailsMiniApp to the application.
We've developed and published this MiniApp to reuse it in this tutorial. You may view the code of this MiniApp in the MovieDetailsMiniApp repository.
To add this MiniApp to the local container used by the native application, use a variation of the ern run command that allows you to include extra MiniApps to the local Container. Let's do that magic now.
Once the app is launched click on any movie and you will be taken to the details page of MovieDetailsMiniApp.
This is how easy it is to combine multiple MiniApps in a local container!
You've successfully used Electrode Native to build your first native application, composed of multiple MiniApps.
In this tutorial, we've covered only a small part of what Electrode Native offers. Be sure to check the rest of the Electrode Native documentation to learn about all the features that Electrode Native offers.
The Getting Started tutorial walks you through creating a simple movie application using Electrode Native.
We'll use iOS for this tutorial. If you wish to use Android instead, select the Android tab.
The movie application includes two React Native MiniApps and two APIs:
MovieListMiniApp | This MiniApp displays a list of movies.
MovieDetailsMiniApp | This MiniApp displays the details of a selected movie.
MoviesApi | An API used to retrieve a list of movies.
NavigationApi | An API used to navigate from one MiniApp to another.
The tutorial shows how easy it is to integrate multiple React Native applications into a native application, and how to easily communicate between the JavaScript and the native side using APIs.
2) When asked to enter a package name for this MiniApp, hit enter to use the default name. You may check the package name requirements
3) Move to the MovieListMiniApp directory and run the MiniApp to view it, using the ern run command.
$cdMovieListMiniApp$ernrun-ios
4) Select a simulator from the list when prompted.
Once the command completes, you will see your first MiniApp running. If you used React Native previously, you'll notice that this MiniApp is the same as the React Native default starter app--after all, a MiniApp is nothing more than a React Native application!
Now let's update the UI of this MiniApp to display a list of movies.
Updating the MovieList MiniApp UI
1) Open the App.js file in your favorite JavaScript editor.
2) Replace the content of this source file with the following code.
3) Save your changes to App.js and reload the application to see the updated UI. Hit ⌘ + R in your iOS Simulator to reload the app and see your changes.
Congratulations! You've successfully run and modified the initial UI of the MovieList MiniApp.
Now let's add an API to the MiniApp so that we can retrieve movies from the native application instead of manually hard coding them in the source code of our MiniApp.
Adding the MoviesApi to the MovieList MiniApp
We have already created and published the MoviesApi and a native implementation of the api for the needs of this tutorial. You may view the created API code in react-native-ernmovie-api repository and the implementation code in ReactNativeErnmovieApiImpl repository. We also created a NavigationApi that will be of use later in this tutorial, you can view its code in the react-native-ernnavigation-api repository.
1) Add the MoviesApi, the MoviesApiImpl, the NavigationApi and react-native-electrode-bridge as dependencies of MovieListMiniApp, using the ern add command.
6) Because we added an API, that contains some native code, we'll need to regenerate the container used by the native application, in order for it to include the native code of the API. This can be done using the run command which recreates a new local container and launches the application. Enter the following run command:
$ernrun-ios
The UI displays the movie names that are returned by the native implementation of the movie api.
Using the Navigation API
We will use the NavigationApi that we already added to our MiniApp earlier on. This very simple API will be used for navigating from the MovieListMiniApp to the MovieDetailsMiniApp.
1) Mofify the App.js file as follows so that when selecting a movie in the list, the MovieListMiniApp will call the navigation API to navigate to the MovieDetailsMiniApp to display the details of the selected Movie.
Make sure that you add the appDelegate import statement to ViewController.m file as well.
#import "AppDelegate.h"
Finally, let's convert our ViewController to a UINavigationController as we're going to navigate betwen different ViewControllers. In the ViewController.h file, replace
To complete the tutorial, add the MovieDetailsMiniApp to the application.
We've developed and published this MiniApp to reuse it in this tutorial. You may view the code of this MiniApp in the MovieDetailsMiniApp repository.
To add this MiniApp to the local container used by the native application, use a variation of the ern run command that allows you to include extra MiniApps to the local Container. Let's do that magic now.
Once the app is launched click on any movie and you will be taken to the details page of MovieDetailsMiniApp.
This is how easy it is to combine multiple MiniApps in a local container!
You've successfully used Electrode Native to build your first native application, composed of multiple MiniApps.
In this tutorial, we've covered only a small part of what Electrode Native offers. Be sure to check the rest of the Electrode Native documentation to learn about all the features that Electrode Native offers.