# Implementing sync only on Wi-Fi option on mobile (Issue #720)

**URL:** https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812
**Category:** Development
**Created:** [16 March 2021 19:45 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812 "2021-03-16T19:45:05Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mablin7](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/mablin7/32/9958_2.png) [@mablin7](https://discourse.joplinapp.org/u/mablin7)
#### Post date: [16 March 2021 19:45 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/1 "2021-03-16T19:45:05Z")

</div>

I'm thinking how to best implement [this](https://github.com/laurent22/joplin/issues/720) and I'd like to ask laurent22's advice. I can see two main ways of doing it:

- Checking network type each time in `app-mobile/root` before calling `setupRecurrentSync` and aslo before starting initial sync on app launch.

- Adding an optional callback function to `lib/registry` (eg `canSync`) which is called before each scheduled sync. Then this function is defined in `app-mobile/root` to check the network type. This would require less new code and it's resilient to all future changes in how `setupRecurrentSync` is called. On the other hand extra logic is necessary to allow for manual syncs. (which, I assume, should still always be usable)

Thanks!

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [16 March 2021 20:07 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/2 "2021-03-16T20:07:51Z")

</div>

How do you check if the wifi is on or off? I guess you shouldn't poll for this as it's a native call, which would be slow. Instead there should be some event you can listen to, is that correct?

For this you should keep things as low level as possible so that any place that use the sync feature will go through your wifi check, so your second approach I think is best. In registry.ts somewhere there's a call to `sync.start()` and I think that's just before this function that you should add your checks.

Keep in mind that registry.ts is used on all platforms (including desktop and cli) so make sure your check is active only for mobile.

Also be careful when you make the native call to check the wifi state, make sure you handle errors. If there's an error, it should default to have the wifi enabled as we can't risk having sync disabled due to some error. But the error of course should be logged.

The spec on that issue is not well detailed, so if you could describe what you're going to do before implementing it that would be great. We need to know:

- How the UI will look (any new options, UI element, etc.). No need for a mockup, just a description in text is enough.
- An overall idea of how you're going to implement it.

---

<div class="post-metadata">

### Author: ![roman\_r\_m](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/roman_r_m/32/3297_2.png) [@roman\_r\_m](https://discourse.joplinapp.org/u/roman_r_m)
#### Post date: [16 March 2021 20:17 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/3 "2021-03-16T20:17:51Z")

</div>

> [@laurent](#):
>
> How do you check if the wifi is on or off?

There was a lib for this, think it was a part of react-native earlier but got split out into a separate package.

---

<div class="post-metadata">

### Author: ![mablin7](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/mablin7/32/9958_2.png) [@mablin7](https://discourse.joplinapp.org/u/mablin7)
#### Post date: [16 March 2021 20:40 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/4 "2021-03-16T20:40:47Z")

</div>

Thanks for the quick reply!

> [@laurent](#):
>
> How do you check if the wifi is on or off?

I'm planning to use this library: [react-native-netinfo](https://github.com/react-native-netinfo/react-native-netinfo).

It supports both polling and subscribing to changes in network state. I think initially I'll just poll it each time from registry.ts as you suggested and then see if it impacts performance. If it does I'll add an event listener and keep track of the network state in a member field.

As for UI I'd like to keep it minimal, I'd just add a mobile-only toggle option (like "_Synchronize only over WiFi connection_") to Settings.ts, and also some indication above the sync button if automatic syncing is disabled because of mobile data. "_Using mobile data, automatic synchronization is disabled. You can still press Synchronize to start a manual sync._" for example.

As I mentioned, manual sync should still be possible so I think for that I'd pass an argument to `scheduleSync` to ignore network check

---

<div class="post-metadata">

### Author: ![tessus](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/tessus/32/13_2.png) [@tessus](https://discourse.joplinapp.org/u/tessus)
#### Post date: [16 March 2021 20:45 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/5 "2021-03-16T20:45:29Z")

</div>

> [@mablin7](#):
>
> _Synchronize_

Please use British spelling for the source translation in the code. No worries, I'll add the US spelling right after. 😉

(I'm using the US spelling myself, even though I'm in Canada.)

---

<div class="post-metadata">

### Author: ![mablin7](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/mablin7/32/9958_2.png) [@mablin7](https://discourse.joplinapp.org/u/mablin7)
#### Post date: [16 March 2021 20:50 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/6 "2021-03-16T20:50:33Z")

</div>

Alright, sure thing! Sorry, didn't notice the app is using synchronise.

---

<div class="post-metadata">

### Author: ![tessus](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/tessus/32/13_2.png) [@tessus](https://discourse.joplinapp.org/u/tessus)
#### Post date: [16 March 2021 21:03 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/7 "2021-03-16T21:03:05Z")

</div>

No worries, all good. Just wanted to make sure it is consistent.

---

<div class="post-metadata">

### Author: ![Amarpsp10](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/amarpsp10/32/7473_2.png) [@Amarpsp10](https://discourse.joplinapp.org/u/Amarpsp10)
#### Post date: [17 March 2021 03:48 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/8 "2021-03-17T03:48:20Z")

</div>

> [@mablin7](#):
>
> I'm planning to use this library: [react-native-netinfo](https://github.com/react-native-netinfo/react-native-netinfo)

hi ,  
implemented similar with -react-native/netInfo

 ![Screenshot_1615951867](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/1/1bfbda90b0f93e1f306cd5e69f5257c4da9c8926.png)  
I am testing it Right now to make compatible without disturbing other apps, and it create many errors while you import it.  
but i didn't found `netInfo` good library , finding other option to do that!

you can quickly learn working of this library from here - [see](https://github.com/Amarpsp10/Learn-react-native-netinfo/blob/main/App.js)

---

<div class="post-metadata">

### Author: ![mablin7](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/mablin7/32/9958_2.png) [@mablin7](https://discourse.joplinapp.org/u/mablin7)
#### Post date: [17 March 2021 07:20 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/9 "2021-03-17T07:20:54Z")

</div>

> [@Amarpsp10](#):
>
> and it create many errors while you import it.

I haven't seen any errors while integrating netinfo. What kind of errors did you get?

---

<div class="post-metadata">

### Author: ![Amarpsp10](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/amarpsp10/32/7473_2.png) [@Amarpsp10](https://discourse.joplinapp.org/u/Amarpsp10)
#### Post date: [17 March 2021 07:50 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/10 "2021-03-17T07:50:40Z")

</div>

> [@mablin7](#):
>
> I haven't seen any errors while integrating netinfo.

that's great!!

> [@mablin7](#):
>
> What kind of errors did you get?

it got this-  
`Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)`

---

<div class="post-metadata">

### Author: ![mablin7](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/mablin7/32/9958_2.png) [@mablin7](https://discourse.joplinapp.org/u/mablin7)
#### Post date: [17 March 2021 08:02 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/11 "2021-03-17T08:02:24Z")

</div>

> [@Amarpsp10](#):
>
> Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)

You have to recompile the android app when adding a new native module.

---

<div class="post-metadata">

### Author: ![laurent](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@laurent](https://discourse.joplinapp.org/u/laurent)
#### Post date: [17 March 2021 08:15 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/12 "2021-03-17T08:15:56Z")

</div>

> [@mablin7](#):
>
> It supports both polling and subscribing to changes in network state. I think initially I'll just poll it each time from registry.ts as you suggested and then see if it impacts performance. If it does I'll add an event listener and keep track of the network state in a member field.

Ok that makes sense. I guess you could simply check how long the call takes to confirm this.

> [@mablin7](#):
>
> As for UI I'd like to keep it minimal, I'd just add a mobile-only toggle option (like " _Synchronize only over WiFi connection_ ") to Settings.ts, and also some indication above the sync button if automatic syncing is disabled because of mobile data. " _Using mobile data, automatic synchronization is disabled. You can still press Synchronize to start a manual sync._ " for example.

Let's not add text over the sync button because that won't leave much space anymore for the notebook list. It would be good if there was some indications but I'm not sure what. What do other apps do?

Once everything is clear about the feature, please add a post to the original issue with your spec. For some examples of spec, see the [spec label](https://github.com/laurent22/joplin/issues?q=is:open+is:issue+label:spec+)

---

<div class="post-metadata">

### Author: ![Amarpsp10](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/amarpsp10/32/7473_2.png) [@Amarpsp10](https://discourse.joplinapp.org/u/Amarpsp10)
#### Post date: [17 March 2021 10:01 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/13 "2021-03-17T10:01:45Z")

</div>

> [@mablin7](#):
>
> You have to recompile the android app

yes already resolve it!

---

<div class="post-metadata">

### Author: ![Amarpsp10](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/amarpsp10/32/7473_2.png) [@Amarpsp10](https://discourse.joplinapp.org/u/Amarpsp10)
#### Post date: [17 March 2021 10:17 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/14 "2021-03-17T10:17:22Z")

</div>

what I did to make `sync over wifi`

1. created a metadata in `Settings.js` as a boolean value
2. Added a listener in root that detects the connection type and automatically save _the settings_ wifi is enabled or not.
3. added a check before sync target, get the value of connection type from settings and if type is wifi then it proceeds other wise it returns null

---

<div class="post-metadata">

### Author: ![mablin7](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/mablin7/32/9958_2.png) [@mablin7](https://discourse.joplinapp.org/u/mablin7)
#### Post date: [18 March 2021 00:51 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/15 "2021-03-18T00:51:32Z")

</div>

> [@laurent](#):
>
> What do other apps do?

From the ones I use, that support this feature:

- Flym RSS reader - no indication, just does nothing
- Podcast Addict - throws an error when attempting to sync
- Google Drive - files pending upload are grey and have a short message under their filename "Waiting for WiFi"

This is what I could come up with for Joplin:

- A short text, as in my original proposal
- An icon + an extra short text

As you can, see both of them take about as much height as the "Completed: ..." message shown after a successful sync.

Simple text:

 ![Screenshot_1616025445](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/b/b89fd43d93b88f1f4855e1260476a4cbe1b52fb6.png)

Icon + explaining message:

 ![Screenshot_1616028168](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/7/7869d088201761a46761fddf7de0a0cfa721d7cf.png)

Icon + completed sync report:

 ![Screenshot_1616028051](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/2/288c207cf72cc06fdf4e6613184b25bf42ad63d5.png)

Current behavior:

 ![photo5949444142608266482](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/9/9b6603ae652dfbb2afa51d9327ca24ffe74da424.jpeg)

Also I've noticed while testing that on the dev branch this report under the sync button doesn't show up after sync, and the animation doesn't play while syncing either. I've traced it back to lib/Synchronizer.ts, where it seems, the dispatch method is not correctly set, but I can't figure out why. I've tested it on [`abe0013`](https://github.com/laurent22/joplin/commit/abe001391400ba5456a2d12cc3576f80e21477e4) so it's not caused by my changes. (the last screenshot comes from my phone running 1.7.5)

---

<div class="post-metadata">

### Author: ![Amarpsp10](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/amarpsp10/32/7473_2.png) [@Amarpsp10](https://discourse.joplinapp.org/u/Amarpsp10)
#### Post date: [18 March 2021 02:41 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/16 "2021-03-18T02:41:04Z")

</div>

> [@mablin7](#):
>
> Also I've noticed while testing that on the dev branch this report under the sync button doesn't show up after sync, and the animation doesn't play

same here , this is why I use log book to see sync status!

> [@mablin7](#):
>
> As you can, see both of them take about as much height as the

Which branch is this?

---

<div class="post-metadata">

### Author: ![mablin7](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/mablin7/32/9958_2.png) [@mablin7](https://discourse.joplinapp.org/u/mablin7)
#### Post date: [18 March 2021 09:03 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/17 "2021-03-18T09:03:49Z")

</div>

> [@Amarpsp10](#):
>
> Which branch is this?

It's the dev branch. On the 3rd screenshot I just typed in "Completed:..." by hand for testing.

---

<div class="post-metadata">

### Author: ![roman\_r\_m](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/roman_r_m/32/3297_2.png) [@roman\_r\_m](https://discourse.joplinapp.org/u/roman_r_m)
#### Post date: [18 March 2021 09:24 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/18 "2021-03-18T09:24:06Z")

</div>

> [@mablin7](#):
>
> As you can, see both of them take about as much height as the "Completed: ..." message shown after a successful syn

You need to consider that the UI can be in a different language for which this is not necessarily true.

I wonder if it possible to maybe use the sync icon itself, modify it slightly if only manual sync is allowed? I'm not a designer so not sure how that could look like.

---

<div class="post-metadata">

### Author: ![Amarpsp10](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/amarpsp10/32/7473_2.png) [@Amarpsp10](https://discourse.joplinapp.org/u/Amarpsp10)
#### Post date: [18 March 2021 10:35 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/19 "2021-03-18T10:35:13Z")

</div>

> [@mablin7](#):
>
> I just typed

😨 okay!!

---

<div class="post-metadata">

### Author: ![mablin7](https://yyz2.discourse-cdn.com/flex028/user_avatar/discourse.joplinapp.org/mablin7/32/9958_2.png) [@mablin7](https://discourse.joplinapp.org/u/mablin7)
#### Post date: [18 March 2021 10:46 UTC](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812/20 "2021-03-18T10:46:48Z")

</div>

> [@roman\_r\_m](#):
>
> I wonder if it possible to maybe use the sync icon itself

Idea #1  
 ![2021-03-18T11:24:21](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/e/e33f47f716045eac2922753df7d45ae65eb6cf9e.jpeg) ![2021-03-18T11:36:09](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/1/1a43f4d1df34fe98fd9d6be153ba1acd56696c42.jpeg)

Idea #2  
 ![2021-03-18T11:37:49](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/6/6abfac1f220d5f1b6d8c9b0643a43cef036d9728.jpeg) ![2021-03-18T11:36:44](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/8/86c5074971e9d6f23179015222245f3fe67ec6ab.jpeg)

Idea #3  
 ![2021-03-18T11:33:52](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/d/d601d4754641ff2ea32cfd4b5399fd2c17004e47.jpeg) ![2021-03-18T11:34:13](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/c/c342149970f5eba4fbb670cb20b4f87e61e0ebeb.jpeg)

Idea #4  
 ![2021-03-18T11:39:34](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/f/f979300762b0e00a26ff794f5098c0b2bb14575c.jpeg) ![2021-03-18T11:40:28](https://canada1.discourse-cdn.com/flex028/uploads/cozic/original/2X/3/320333212dcd94ecdd32a50ec07c0cf645e4a980.jpeg)

[Next page](https://discourse.joplinapp.org/t/implementing-sync-only-on-wi-fi-option-on-mobile-issue-720/15812.md?page=2)
