Skip to content

Commit 0f9fd44

Browse files
committed
Add syncImmediately.
1 parent 0808b45 commit 0f9fd44

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ A good example could be `syncInterval: 12 * 60 * 60` (12 hours) and `syncFlexTim
114114

115115
Notice that `syncFlexTime` only works for Android 4.4+, for older versions, that value will be ignored and syncs will be always exact.
116116

117+
### syncImmediately
118+
119+
Invoke the sync task. Use the same values as in the [init](#init) call.
120+
121+
```js
122+
Object: {
123+
syncInterval: number;
124+
syncFlexTime: number;
125+
}
126+
```
127+
128+
Be aware that for this method to work (if you call it from inside your app) you need to allow the task to [work on the foreground](https://github.com/ferrannp/react-native-sync-adapter#running-the-task-while-the-app-is-in-the-foreground).
129+
117130
## Running example
118131

119132
You can try this library running the `example` app:

android/src/main/java/com/fnp/reactnativesyncadapter/HeadlessService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
3030
}
3131

3232
// From https://facebook.github.io/react-native/docs/headless-js-android.html
33-
private boolean isAppOnForeground(Context context) {
33+
public static boolean isAppOnForeground(Context context) {
3434
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
3535
assert activityManager != null;
3636
List<ActivityManager.RunningAppProcessInfo> appProcesses =

android/src/main/java/com/fnp/reactnativesyncadapter/SyncAdapterModule.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.fnp.reactnativesyncadapter;
22

3+
import android.widget.Toast;
4+
35
import com.facebook.react.bridge.ReactApplicationContext;
46
import com.facebook.react.bridge.ReactContextBaseJavaModule;
57
import com.facebook.react.bridge.ReactMethod;
68

7-
@SuppressWarnings("unused")
8-
class SyncAdapterModule extends ReactContextBaseJavaModule {
9+
@SuppressWarnings("unused") class SyncAdapterModule extends ReactContextBaseJavaModule {
910

1011
SyncAdapterModule(ReactApplicationContext reactContext) {
1112
super(reactContext);
@@ -17,9 +18,21 @@ public void init(int syncInterval, int syncFlexTime) {
1718
}
1819

1920
@ReactMethod
20-
public void syncImmediately() {
21-
// TODO implement this
22-
// SyncAdapter.syncImmediately(getReactApplicationContext(), syncInterval, syncFlexTime);
21+
public void syncImmediately(int syncInterval, int syncFlexTime) {
22+
boolean allowForeground = Boolean.valueOf(getReactApplicationContext().getString(R.string.rnsb_allow_foreground));
23+
24+
if (!allowForeground && HeadlessService.isAppOnForeground(getReactApplicationContext())) {
25+
if (getCurrentActivity() != null) {
26+
Toast.makeText(
27+
getCurrentActivity(),
28+
"This sync task has not been configured to run on the foreground!",
29+
Toast.LENGTH_SHORT)
30+
.show();
31+
}
32+
return;
33+
}
34+
35+
SyncAdapter.syncImmediately(getReactApplicationContext(), syncInterval, syncFlexTime);
2336
}
2437

2538
@Override

example/index.android.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
/* @flow */
22

33
import React, { Component } from 'react';
4-
import {
5-
AppRegistry,
6-
// Button,
7-
StyleSheet,
8-
Text,
9-
View,
10-
} from 'react-native';
4+
import { AppRegistry, Button, StyleSheet, Text, View } from 'react-native';
115
import SyncAdapter from 'react-native-sync-adapter';
126

137
import TestTask from './TestTask';
@@ -23,19 +17,19 @@ export default class SyncAdapterExample extends Component<{}> {
2317
});
2418
}
2519

26-
// _onSyncPress = () => {
27-
// SyncAdapter.syncImmediately();
28-
// };
20+
_onSyncPress = () => {
21+
SyncAdapter.syncImmediately({
22+
syncInterval,
23+
syncFlexTime,
24+
});
25+
};
2926

3027
render() {
3128
return (
3229
<View style={styles.container}>
3330
<Text style={styles.title}>React Native Sync Adapter</Text>
3431
<Text style={styles.subTitle}>Example is running!</Text>
35-
{/* <Button */}
36-
{/* onPress={this._onSyncPress} */}
37-
{/* title="Sync now" */}
38-
{/* /> */}
32+
<Button onPress={this._onSyncPress} title="Sync now" />
3933
</View>
4034
);
4135
}

index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@ type Init = {
99
syncFlexTime: number,
1010
};
1111

12+
const _checkIntervals = (syncInterval: number, syncFlexTime: number) => {
13+
if (syncFlexTime > syncInterval) {
14+
throw new Error(
15+
'Specified syncInterval must be greater than the specified syncFlexTime.'
16+
);
17+
}
18+
};
19+
1220
export default {
1321
init: ({ syncInterval, syncFlexTime }: Init) => {
14-
if (syncFlexTime > syncInterval) {
15-
throw new Error(
16-
'Specified syncInterval must be greater than the specified syncFlexTime.'
17-
);
18-
}
22+
_checkIntervals(syncInterval, syncFlexTime);
1923
SyncAdapter.init(syncInterval, syncFlexTime);
2024
},
2125

22-
syncImmediately: () => {
23-
SyncAdapter.syncImmediately();
26+
syncImmediately: ({ syncInterval, syncFlexTime }: Init) => {
27+
_checkIntervals(syncInterval, syncFlexTime);
28+
SyncAdapter.syncImmediately(syncInterval, syncFlexTime);
2429
},
2530
};

0 commit comments

Comments
 (0)