Skip to content

Commit

Permalink
test fabric (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabOnReact authored Feb 11, 2024
1 parent 5c1201a commit 2ce405b
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.wearconnectivity;

import com.wearconnectivity.WearConnectivitySpec;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.JSONArguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactMethod;
Expand All @@ -35,13 +33,13 @@ public class WearConnectivityModule extends WearConnectivitySpec
private final MessageClient client;
private String CLIENT_ADDED =
TAG + "onMessageReceived listener added when activity is created. Client receives messages.";
private String SEND_MESSAGE_FAILED = TAG + "sendMessage failed with exception: ";
private String NO_NODES_FOUND = TAG + "sendMessage failed. No connected nodes found.";
private String REMOVE_CLIENT =
TAG
+ "onMessageReceived listener removed when activity is destroyed. Client does not receive messages.";
private String ADD_CLIENT =
TAG + "onMessageReceived listener added when activity is resumed. Client receives messages.";
private String RETRIEVE_NODES_FAILED = TAG + "failed to retrieve nodes with error: ";

WearConnectivityModule(ReactApplicationContext context) {
super(context);
Expand All @@ -57,21 +55,14 @@ public String getName() {
return NAME;
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
@ReactMethod
public void multiply(double a, double b, Promise promise) {
promise.resolve(a * b);
}

private List<Node> retrieveNodes(Callback errorCb) {
try {
NodeClient nodeClient = Wearable.getNodeClient(getReactApplicationContext());
// TODO: implement Runnable to run task in the background thread
// https://stackoverflow.com/a/64969640/7295772
return Tasks.await(nodeClient.getConnectedNodes());
} catch (Exception e) {
errorCb.invoke(SEND_MESSAGE_FAILED + e);
errorCb.invoke(RETRIEVE_NODES_FAILED + e);
return null;
}
}
Expand All @@ -86,7 +77,7 @@ public void sendMessage(ReadableMap messageData, Callback replyCb, Callback erro
}
}
} else {
errorCb.invoke(NO_NODES_FOUND + " client: " + client + " connectedNodes: " + connectedNodes);
FLog.w(TAG, NO_NODES_FOUND + " client: " + client + " connectedNodes: " + connectedNodes);
}
}

Expand Down
3 changes: 0 additions & 3 deletions android/src/oldarch/WearConnectivitySpec.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.wearconnectivity;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReadableMap;
Expand All @@ -11,7 +10,5 @@ abstract class WearConnectivitySpec extends ReactContextBaseJavaModule {
super(context);
}

public abstract void multiply(double a, double b, Promise promise);

public abstract void sendMessage(ReadableMap messageData, Callback replyCb, Callback errCb);
}
2 changes: 1 addition & 1 deletion example/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
# your application. You should enable this flag either if you want
# to write custom TurboModules/Fabric components OR use libraries that
# are providing them.
newArchEnabled=false
newArchEnabled=true

# Use this property to enable or disable the Hermes JS engine.
# If set to false, you will be using JSC instead.
Expand Down
12 changes: 0 additions & 12 deletions ios/WearConnectivity.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
@implementation WearConnectivity
RCT_EXPORT_MODULE()

// Example method
// See // https://reactnative.dev/docs/native-modules-ios
RCT_EXPORT_METHOD(multiply:(double)a
b:(double)b
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
NSNumber *result = @(a * b);

resolve(result);
}

// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
Expand Down
7 changes: 3 additions & 4 deletions src/NativeWearConnectivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

// Messages
export type Payload = Record<string, unknown>;
export type Payload = Object;
export type ReplyCallback = (reply: Payload) => void;
export type ErrorCallback = (err: Error) => void;
export type ErrorCallback = (err: string) => void;

const UNHANDLED_CALLBACK =
'The sendMessage function was called without a callback function. ';
Expand All @@ -16,7 +16,7 @@ const UNHANDLED_CALLBACK_ERROR =
export const defaultReplyCb = (reply: Payload) => {
console.log(UNHANDLED_CALLBACK + UNHANDLED_CALLBACK_REPLY, reply);
};
export const defaultErrCb = (err: Error) => {
export const defaultErrCb = (err: string) => {
console.warn(UNHANDLED_CALLBACK + UNHANDLED_CALLBACK_ERROR, err);
};

Expand All @@ -27,7 +27,6 @@ export type SendMessage = (
) => void;

export interface Spec extends TurboModule {
multiply(a: number, b: number): Promise<number>;
sendMessage: SendMessage;
}

Expand Down
4 changes: 0 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ const WearConnectivity = WearConnectivityModule
}
);

export function multiply(a: number, b: number): Promise<number> {
return WearConnectivity.multiply(a, b);
}

let sendMessageExport;
let watchEventsExport;
// let WearConnectivity;
Expand Down
1 change: 0 additions & 1 deletion src/messages.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { SendMessage } from './NativeWearConnectivity';
import { defaultReplyCb, defaultErrCb } from './NativeWearConnectivity';
import { WearConnectivity } from './index';
import { platformCheck } from './utilities';

const sendMessage: SendMessage = (message, cb, errCb) => {
const callbackWithDefault = cb ?? defaultReplyCb;
Expand Down
2 changes: 1 addition & 1 deletion watch-example/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
# your application. You should enable this flag either if you want
# to write custom TurboModules/Fabric components OR use libraries that
# are providing them.
newArchEnabled=false
newArchEnabled=true

# Use this property to enable or disable the Hermes JS engine.
# If set to false, you will be using JSC instead.
Expand Down

0 comments on commit 2ce405b

Please sign in to comment.