Skip to content

Commit

Permalink
adding logging and promise (#3)
Browse files Browse the repository at this point in the history
* resolve promise with relevant messages

* adding logging
  • Loading branch information
fabOnReact authored Feb 9, 2024
1 parent 6d615ad commit e27b204
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST;

import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -14,6 +15,7 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.util.RNLog;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
Expand All @@ -28,13 +30,14 @@
public class WearConnectivityModule extends WearConnectivitySpec
implements MessageClient.OnMessageReceivedListener, LifecycleEventListener {
public static final String NAME = "WearConnectivity";
private static final String TAG = "WearConnectivityModule";
private static final String TAG = "WearConnectivityModule ";
private final MessageClient client;

WearConnectivityModule(ReactApplicationContext context) {
super(context);
context.addLifecycleEventListener(this);
client = Wearable.getMessageClient(context);
Log.d(TAG, TAG + "onMessageReceived listener added when activity is created. Client receives messages.");
client.addListener(this);
}

Expand All @@ -51,27 +54,28 @@ public void multiply(double a, double b, Promise promise) {
promise.resolve(a * b);
}

// catch the ExecutionException
@ReactMethod
public void sendMessage(String path, Promise promise) {
try {
NodeClient nodeClient = Wearable.getNodeClient(getReactApplicationContext());
List<Node> nodes = Tasks.await(nodeClient.getConnectedNodes());
if (nodes.size() > 0) {
for (Node node : nodes) {
sendMessageToClient(path, node);
// TODO: Add check that node is listening (companion app activity is used)
// https://developers.google.com/android/reference/com/google/android/gms/wearable/Node
if (node.isNearby()) {
sendMessageToClient(path, node, promise);
}
}
promise.resolve(true);
} else {
Toast.makeText(getReactApplicationContext(), "No connected nodes found", Toast.LENGTH_LONG)
.show();
promise.reject(TAG, TAG + "sendMessage failed. No connected nodes found.");
}
} catch (Exception e) {
FLog.w(TAG, " getConnectedNodes raised Exception: " + e);
promise.reject(TAG, TAG + "sendMessage failed with exception: " + e);
}
}

private void sendMessageToClient(String path, Node node) {
private void sendMessageToClient(String path, Node node, Promise promise) {
try {
Task<Integer> sendTask =
Wearable.getMessageClient(getReactApplicationContext())
Expand All @@ -80,25 +84,25 @@ private void sendMessageToClient(String path, Node node) {
new OnSuccessListener<Object>() {
@Override
public void onSuccess(Object object) {
FLog.d(TAG, " sendMessage called onSuccess for path: " + path);
promise.resolve(TAG + "message sent to client with nodeID: " + object.toString());
}
};
sendTask.addOnSuccessListener(onSuccessListener);
OnFailureListener onFailureListener =
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
FLog.d(TAG, " sendMessage called onFailure with error: " + e);
promise.reject(TAG, TAG + "sendMessage failed: " + e);
}
};
sendTask.addOnFailureListener(onFailureListener);
} catch (Exception e) {
FLog.w(TAG, " sendMessage raised Exception: " + e);
promise.reject(TAG, TAG + "sendMessage failed: " + e);
}
}

public void onMessageReceived(MessageEvent messageEvent) {
FLog.d(TAG, " onMessageReceived called for path: " + messageEvent.getPath());
Log.d(TAG, TAG + "onMessageReceived received message with path: " + messageEvent.getPath());
sendEvent(getReactApplicationContext(), messageEvent.getPath(), null);
}

Expand All @@ -112,17 +116,20 @@ private void sendEvent(
@Override
public void onHostResume() {
if (client != null) {
Log.d(TAG, TAG + "onMessageReceived listener added when activity is resumed. Client receives messages.");
client.addListener(this);
}
}

@Override
public void onHostPause() {
Log.d(TAG, TAG + "onMessageReceived listener removed when the activity paused. Client does not receive messages.");
client.removeListener(this);
}

@Override
public void onHostDestroy() {
Log.d(TAG, TAG + "onMessageReceived listener removed when activity is destroyed. Client does not receive messages.");
client.removeListener(this);
}
}
3 changes: 0 additions & 3 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ const App = () => {
<>
<LoginScreen />
<WearScreen />
<View style={styles.container}>
<Text>Result: {result}</Text>
</View>
</>
);
};
Expand Down
4 changes: 2 additions & 2 deletions example/src/LoginScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
NativeModules,

Check failure on line 7 in example/src/LoginScreen.tsx

View workflow job for this annotation

GitHub Actions / lint

'NativeModules' is defined but never used
Button,
} from 'react-native';
import {GoogleSignin} from '@react-native-google-signin/google-signin';
import { GoogleSignin } from '@react-native-google-signin/google-signin';
import Config from 'react-native-config';

export default function LoginScreen() {
Expand All @@ -19,7 +19,7 @@ export default function LoginScreen() {
const userInfo = await GoogleSignin.signIn();
console.log(JSON.stringify(userInfo));
} catch (error) {
console.log('ERROR IS: ' + JSON.stringify(error));
console.log('GoogleSignin faile with error: ' + JSON.stringify(error));
}
};

Expand Down
5 changes: 3 additions & 2 deletions example/src/WearScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ export default function WearCounter() {
};
}, []);

const onPressHandler = () => {
sendMessage(INCREASE_WEAR_COUNTER_EVENT);
const onPressHandler = async () => {
const result = await sendMessage(INCREASE_WEAR_COUNTER_EVENT);
console.log(result);
};

return (
Expand Down

0 comments on commit e27b204

Please sign in to comment.