Skip to content

Commit

Permalink
Added event sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
Petter Hesselberg committed Oct 10, 2017
1 parent 17f346b commit e63706e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,32 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Expose Java to JavaScript.
*/
class ActivityStarterModule extends ReactContextBaseJavaModule {

private static DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter = null;

ActivityStarterModule(ReactApplicationContext reactContext) {
super(reactContext);
}

@Override
public void initialize() {
super.initialize();
eventEmitter = getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
}

/**
* @return the name of this module. This will be the name used to {@code require()} this module
* from JavaScript.
Expand All @@ -35,6 +49,14 @@ public String getName() {
return "ActivityStarter";
}

@Nullable
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("MyEventName", "MyEventValue");
return constants;
}

@ReactMethod
void navigateToExample() {
Activity activity = getCurrentActivity();
Expand Down Expand Up @@ -86,4 +108,11 @@ void callJavaScript() {
}
}
}

/**
* To pass an object instead of a simple string, create a {@link WritableNativeMap} and populate it.
*/
static void triggerAlert(@Nonnull String message) {
eventEmitter.emit("MyEventValue", message);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.demo.activity;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
Expand Down
15 changes: 11 additions & 4 deletions android/app/src/main/java/com/demo/activity/ExampleActivity.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.demo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.facebook.react.ReactActivity;

public class ExampleActivity extends AppCompatActivity {
public class ExampleActivity extends ReactActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);

View goBack = findViewById(R.id.go_back_btn);
goBack.setOnClickListener(new View.OnClickListener() {
findViewById(R.id.call_callback_btn).setEnabled(true);
findViewById(R.id.go_back_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});

findViewById(R.id.call_callback_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityStarterModule.triggerAlert("Hello from " + getClass().getSimpleName());
}
});
}
}
8 changes: 8 additions & 0 deletions android/app/src/main/res/layout/activity_example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="@string/go_back"
/>
<Button
android:id="@+id/call_callback_btn"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/trigger_event"
/>

</LinearLayout>
1 change: 1 addition & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<string name="example_activity_title">Example activity</string>
<string name="go_back">Go back!</string>
<string name="call_javascript_method">Call JavaScript method</string>
<string name="trigger_event">Trigger event</string>
</resources>
16 changes: 11 additions & 5 deletions index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React, { Component } from 'react';
import {
AppRegistry,
Button,
NativeEventEmitter,
NativeModules,
StyleSheet,
Text,
Expand All @@ -25,6 +26,8 @@ export class ExposedToJava {
const exposedToJava = new ExposedToJava();
BatchedBridge.registerCallableModule("JavaScriptVisibleToJava", exposedToJava);

const activityStarter = NativeModules.ActivityStarter;

export default class ActivityDemoComponent extends Component {
render() {
return (
Expand All @@ -41,21 +44,21 @@ export default class ActivityDemoComponent extends Component {
</Text>
<View style={styles.buttonContainer}>
<Button
onPress={() => NativeModules.ActivityStarter.navigateToExample()}
onPress={() => activityStarter.navigateToExample()}
title='Start example activity'
/>
<Button
onPress={() => NativeModules.ActivityStarter.dialNumber('+1 (234) 567-8910')}
onPress={() => activityStarter.dialNumber('+1 (234) 567-8910')}
title='Dial +1 (234) 567-8910'
/>
<Button
onPress={() => NativeModules.ActivityStarter.getActivityName((name) => { alert(name); })}
onPress={() => activityStarter.getActivityName((name) => { alert(name); })}
title='Get activity name'
/>
<Button
onPress={async () => {
try {
var name = await NativeModules.ActivityStarter.getActivityNameAsPromise();
var name = await activityStarter.getActivityNameAsPromise();
alert(name);
} catch (e) {
alert(e.message);
Expand All @@ -68,7 +71,7 @@ export default class ActivityDemoComponent extends Component {
title='Copy to clipboard'
/>
<Button
onPress={() => NativeModules.ActivityStarter.callJavaScript()}
onPress={() => activityStarter.callJavaScript()}
title='Call JavaScript from Java'
/>
</View>
Expand Down Expand Up @@ -103,3 +106,6 @@ const styles = StyleSheet.create({
});

AppRegistry.registerComponent('ActivityDemoComponent', () => ActivityDemoComponent);

const eventEmitter = new NativeEventEmitter(activityStarter);
eventEmitter.addListener(activityStarter.MyEventName, (params) => alert(params));

0 comments on commit e63706e

Please sign in to comment.