Skip to content

Commit

Permalink
Use ReactActivity in AirMapView (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
PainStaker0331 committed Jun 14, 2016
1 parent cb1aa33 commit 6c2d4d4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.airbnb.android.react.maps;

import android.view.View;
import android.app.Activity;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
Expand Down Expand Up @@ -40,16 +41,19 @@ public class AirMapManager extends ViewGroupManager<AirMapView> {

private ReactContext reactContext;

private Activity reactActivity;
private AirMapMarkerManager markerManager;
private AirMapPolylineManager polylineManager;
private AirMapPolygonManager polygonManager;
private AirMapCircleManager circleManager;

public AirMapManager(
Activity activity,
AirMapMarkerManager markerManager,
AirMapPolylineManager polylineManager,
AirMapPolygonManager polygonManager,
AirMapCircleManager circleManager) {
this.reactActivity = activity;
this.markerManager = markerManager;
this.polylineManager = polylineManager;
this.polygonManager = polygonManager;
Expand All @@ -64,14 +68,14 @@ public String getName() {
@Override
protected AirMapView createViewInstance(ThemedReactContext context) {
reactContext = context;
AirMapView view = new AirMapView(context, this);

try {
MapsInitializer.initialize(context.getApplicationContext());
MapsInitializer.initialize(reactActivity);
} catch (Exception e) {
e.printStackTrace();
emitMapError("Map initialize error", "map_init_error");
}
AirMapView view = new AirMapView(context, reactActivity, this);

return view;
}
Expand Down Expand Up @@ -258,7 +262,6 @@ public void updateExtraData(AirMapView view, Object extraData) {
}

public void pushEvent(View view, String name, WritableMap data) {
ReactContext reactContext = (ReactContext) view.getContext();
reactContext.getJSModule(RCTEventEmitter.class)
.receiveEvent(view.getId(), name, data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.airbnb.android.react.maps;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.os.Handler;
Expand Down Expand Up @@ -62,12 +63,14 @@ public class AirMapView extends MapView implements GoogleMap.InfoWindowAdapter,
private final AirMapManager manager;
private LifecycleEventListener lifecycleListener;
private boolean paused = false;
private ThemedReactContext context;

final EventDispatcher eventDispatcher;

public AirMapView(ThemedReactContext context, AirMapManager manager) {
super(context);
public AirMapView(ThemedReactContext context, Activity activity, AirMapManager manager) {
super(activity);
this.manager = manager;
this.context = context;

super.onCreate(null);
super.onResume();
Expand Down Expand Up @@ -226,7 +229,7 @@ public void onHostDestroy() {
}
};

((ThemedReactContext) getContext()).addLifecycleEventListener(lifecycleListener);
context.addLifecycleEventListener(lifecycleListener);
}

private boolean hasPermissions() {
Expand All @@ -239,7 +242,7 @@ private boolean hasPermissions() {
*/
public synchronized void doDestroy() {
if (lifecycleListener != null) {
((ThemedReactContext) getContext()).removeLifecycleEventListener(lifecycleListener);
context.removeLifecycleEventListener(lifecycleListener);
lifecycleListener = null;
}
if (!paused) {
Expand Down Expand Up @@ -326,7 +329,7 @@ public View getFeatureAt(int index) {

public void removeFeatureAt(int index) {
AirMapFeature feature = features.remove(index);


if (feature instanceof AirMapMarker) {
markerMap.remove(feature.getFeature());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.airbnb.android.react.maps;

import android.app.Activity;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
Expand All @@ -11,6 +13,12 @@
import java.util.List;

public class MapsPackage implements ReactPackage {
private Activity reactActivity = null;

public MapsPackage(Activity activity) {
reactActivity = activity;
}

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Collections.emptyList();
Expand All @@ -29,6 +37,7 @@ public List<ViewManager> createViewManagers(ReactApplicationContext reactContext
AirMapPolygonManager polygonManager = new AirMapPolygonManager(reactContext);
AirMapCircleManager circleManager = new AirMapCircleManager(reactContext);
AirMapManager mapManager = new AirMapManager(
reactActivity,
annotationManager,
polylineManager,
polygonManager,
Expand Down
6 changes: 3 additions & 3 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ project(':react-native-maps').projectDir = new File(rootProject.projectDir, '../
...
import com.airbnb.android.react.maps.MapsPackage; // <--- Add this!
...
public class MainActivity extends ReactActivity {
...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new MapsPackage() // <-- Add this!
new MapsPackage(this) // <-- Add this!
);
}
}
Expand All @@ -88,7 +88,7 @@ project(':react-native-maps').projectDir = new File(rootProject.projectDir, '../
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new MapsPackage()) // <---- and This!
.addPackage(new MapsPackage(this)) // <---- and This!
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ protected boolean getUseDeveloperSupport() {
protected List<ReactPackage> getPackages() {
return Arrays.asList(
new MainReactPackage(),
new MapsPackage());
new MapsPackage(this));
}
}

0 comments on commit 6c2d4d4

Please sign in to comment.