Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve wifi state management #220

Closed
wants to merge 1 commit into from
Closed

Conversation

EvanHahn
Copy link
Contributor

@EvanHahn EvanHahn commented Apr 4, 2024

We need the device's wifi state at a few points in the app. The way we previously did this had a few disadvantages:

  1. If you were connected to wifi and another network (like cellular), we might get the wrong network's IP address. You might also be unable to fetch the SSID.

  2. It used two different dependencies, because neither does exactly what we want.

  3. These dependencies rely on deprecated APIs.

This change fixes those by implementing a native module, WifiModule, to address these problems. It also adds an React hook for using it.

Here's what it looks like in the app:

Screenshot

I think this is a useful change on its own, but will also help us as we try to improve local peer discovery in upcoming work.

We need the device's wifi state at a few points in the app. The way we
previously did this had a few disadvantages:

1. If you were connected to wifi and another network (like cellular),
   we might get the wrong network's IP address. You might also be unable
   to fetch the SSID.

2. It used two different dependencies, because neither does exactly what
   we want.

3. These dependencies rely on deprecated APIs.

This change fixes those by implementing a native module, `WifiModule`,
to address these problems. It also adds an React hook for using it.

I think this is a useful change on its own, but will also help us as we
try to [improve local peer discovery][0] in upcoming work.

[0]: digidem/comapeo-core#474
@EvanHahn EvanHahn force-pushed the wifi-improvements branch from 1fdcb39 to 8a2b459 Compare April 4, 2024 19:24
// here,
// for example:
// add(MyReactNativePackage())
add(WifiPackage())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the relevant React Native docs.

@@ -0,0 +1,16 @@
package com.comapeo
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +80 to +94
@ReactMethod
fun addListener(eventName: String) {
if (numberOfListeners == 0) {
startListening()
}
numberOfListeners++
}

@ReactMethod
fun removeListeners(count: Int) {
numberOfListeners -= count
if (numberOfListeners == 0) {
stopListening()
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the relevant React Native docs for sending events from a native module.

@EvanHahn EvanHahn marked this pull request as ready for review April 4, 2024 19:27
@EvanHahn EvanHahn requested a review from gmaclennan April 4, 2024 19:27
Copy link
Member

@gmaclennan gmaclennan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, ok, however I'm concerned about the increased maintenance overhead here. We also will want to know whether wifi is turned on or off, and in the future will likely want to add signal strength and whether the internet is connected, all of which is returned by @react-native-community/netinfo. Also we will need iOS support this year.

I know the netinfo module above emits events about the current connected interface, rather than the wifi interface, which is why with the current usage we always request the wifi state when the network connectivity changes, and we also poll wifi state. This isn't ideal, but is it a huge issue? It would obviously be nicer to correctly register the change listener to the wifi interface like you do here, so we don't need to poll, but we also take on a lot of other maintenance and bug fixing for edge-case devices. Is there a bug in the way the netinfo module returns the SSID or IP address when calling NetInfo.fetch('wifi')? Maybe better to submit a PR to the netinfo module, or even just maintain a patch, rather than write and maintain our own wifi module, given that netinfo is fairly mature and actively maintained.

throw new Error('Invalid wifi state from native module');
};

export const useWifiState = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a request for the current state when the hook is first initialized - if there is no change event then the state will remain null.

@EvanHahn
Copy link
Contributor Author

You're right. I wrongly assumed that NetInfo.fetch() wouldn't return wifi info if you were also connected to cellular, but that's what the "wifi" argument is for.

Closing this PR.

@EvanHahn EvanHahn closed this Apr 10, 2024
@EvanHahn EvanHahn deleted the wifi-improvements branch April 10, 2024 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants