Skip to content

Commit

Permalink
Merge pull request #184 from deepueg/bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
deepueg authored May 13, 2021
2 parents 7114ce9 + 00b7b47 commit c432690
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.ern.api.impl.navigation.NavUtils;
import com.ernnavigationApi.ern.model.ErnNavRoute;
import com.facebook.react.ReactRootView;
import com.walmartlabs.electrode.reactnative.bridge.helpers.Logger;
Expand Down Expand Up @@ -182,9 +183,7 @@ public void refresh(@Nullable Bundle data) {
if (mMiniAppView instanceof ReactRootView) {
Bundle props = getDefaultProps();
addGlobalProps(props);
if (data != null) {
props.putAll(data);
}
NavUtils.mergeBundleWithJsonPayloads(props, data);
((ReactRootView) mMiniAppView).setAppProperties(props);
} else {
Logger.w(TAG, "Refresh called on a null mMiniAppView. Should never reach here");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.ernnavigationApi.ern.model.NavigationBar;
import com.ernnavigationApi.ern.model.NavigationBarButton;
import com.walmartlabs.electrode.reactnative.bridge.helpers.Logger;
import com.walmartlabs.ern.container.ElectrodeReactContainer;

import java.io.IOException;
import java.net.URL;
Expand Down Expand Up @@ -127,7 +126,7 @@ private static MenuItem addButtonAsMenuItem(@NonNull NavigationBarButton button,
}

public static boolean canLoadIconFromURI(String icon) {
return ElectrodeReactContainer.isReactNativeDeveloperSupport() && URLUtil.isValidUrl(icon);
return URLUtil.isValidUrl(icon);
}

public static Drawable getBitmapFromURL(Context context, String iconLocation) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

public final class NavUtils {

private static final String TAG = NavUtils.class.getSimpleName();
public static final String KEY_JSON_PAYLOAD = "jsonPayload";

private NavUtils() {

Expand Down Expand Up @@ -62,8 +65,8 @@ public static String getPath(@NonNull Bundle args) {
*/
@Nullable
public static JSONObject getPayload(@NonNull Bundle args) {
if (args.containsKey("jsonPayload")) {
Object obj = args.get("jsonPayload");
if (args.containsKey(KEY_JSON_PAYLOAD)) {
Object obj = args.get(KEY_JSON_PAYLOAD);
if (obj instanceof String) {
try {
return new JSONObject((String) obj);
Expand All @@ -76,6 +79,44 @@ public static JSONObject getPayload(@NonNull Bundle args) {
return null;
}

/**
* Merges the two bundle and also performs a merge for KEY_JSON_PAYLOAD entry if both bundles have a KEY_JSON_PAYLOAD entry.
*
* @param oldBundle old bundle
* @param newBundle new bundle
* @return Bundle Always returns the oldBundle with combined jsonPayload from newBundle along with other bundle elements.
*/
@NonNull
public static Bundle mergeBundleWithJsonPayloads(@NonNull Bundle oldBundle, @Nullable Bundle newBundle) {
if (newBundle != null) {
JSONObject mergedJsonPayload = null;
if (newBundle.containsKey(KEY_JSON_PAYLOAD)) { //Merge the KEY_JSON_PAYLOAD entry if the new bundle has an entry
JSONObject newJsonPayload = NavUtils.getPayload(newBundle);
mergedJsonPayload = NavUtils.getPayload(oldBundle);
if (newJsonPayload != null) {
if (mergedJsonPayload != null) {
Iterator<String> keys = newJsonPayload.keys();
while (keys.hasNext()) {
String key = keys.next();
try {
mergedJsonPayload.put(key, newJsonPayload.get(key));
} catch (JSONException e) {
Logger.e(TAG, "Error merging jsonPayload: %s", e.getMessage());
}
}
} else {
mergedJsonPayload = newJsonPayload;
}
}
}
oldBundle.putAll(newBundle);
if (mergedJsonPayload != null) {
oldBundle.putString(KEY_JSON_PAYLOAD, mergedJsonPayload.toString());
}
}
return oldBundle;
}

/**
* Extracts the value for the key 'jsonPayload' from the given bundle. Return null if the key is not present or if the value cannot be converted to a {@link JSONObject}.
*
Expand Down

0 comments on commit c432690

Please sign in to comment.