Skip to content

Commit

Permalink
Add syncImmediately.
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrannp committed Oct 20, 2017
1 parent 0808b45 commit 0f9fd44
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -114,6 +114,19 @@ A good example could be `syncInterval: 12 * 60 * 60` (12 hours) and `syncFlexTim

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

### syncImmediately

Invoke the sync task. Use the same values as in the [init](#init) call.

```js
Object: {
syncInterval: number;
syncFlexTime: number;
}
```

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).

## Running example

You can try this library running the `example` app:
Expand Down
Expand Up @@ -30,7 +30,7 @@ protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
}

// From https://facebook.github.io/react-native/docs/headless-js-android.html
private boolean isAppOnForeground(Context context) {
public static boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
assert activityManager != null;
List<ActivityManager.RunningAppProcessInfo> appProcesses =
Expand Down
@@ -1,11 +1,12 @@
package com.fnp.reactnativesyncadapter;

import android.widget.Toast;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

@SuppressWarnings("unused")
class SyncAdapterModule extends ReactContextBaseJavaModule {
@SuppressWarnings("unused") class SyncAdapterModule extends ReactContextBaseJavaModule {

SyncAdapterModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -17,9 +18,21 @@ public void init(int syncInterval, int syncFlexTime) {
}

@ReactMethod
public void syncImmediately() {
// TODO implement this
// SyncAdapter.syncImmediately(getReactApplicationContext(), syncInterval, syncFlexTime);
public void syncImmediately(int syncInterval, int syncFlexTime) {
boolean allowForeground = Boolean.valueOf(getReactApplicationContext().getString(R.string.rnsb_allow_foreground));

if (!allowForeground && HeadlessService.isAppOnForeground(getReactApplicationContext())) {
if (getCurrentActivity() != null) {
Toast.makeText(
getCurrentActivity(),
"This sync task has not been configured to run on the foreground!",
Toast.LENGTH_SHORT)
.show();
}
return;
}

SyncAdapter.syncImmediately(getReactApplicationContext(), syncInterval, syncFlexTime);
}

@Override
Expand Down
22 changes: 8 additions & 14 deletions example/index.android.js
@@ -1,13 +1,7 @@
/* @flow */

import React, { Component } from 'react';
import {
AppRegistry,
// Button,
StyleSheet,
Text,
View,
} from 'react-native';
import { AppRegistry, Button, StyleSheet, Text, View } from 'react-native';
import SyncAdapter from 'react-native-sync-adapter';

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

// _onSyncPress = () => {
// SyncAdapter.syncImmediately();
// };
_onSyncPress = () => {
SyncAdapter.syncImmediately({
syncInterval,
syncFlexTime,
});
};

render() {
return (
<View style={styles.container}>
<Text style={styles.title}>React Native Sync Adapter</Text>
<Text style={styles.subTitle}>Example is running!</Text>
{/* <Button */}
{/* onPress={this._onSyncPress} */}
{/* title="Sync now" */}
{/* /> */}
<Button onPress={this._onSyncPress} title="Sync now" />
</View>
);
}
Expand Down
19 changes: 12 additions & 7 deletions index.js
Expand Up @@ -9,17 +9,22 @@ type Init = {
syncFlexTime: number,
};

const _checkIntervals = (syncInterval: number, syncFlexTime: number) => {
if (syncFlexTime > syncInterval) {
throw new Error(
'Specified syncInterval must be greater than the specified syncFlexTime.'
);
}
};

export default {
init: ({ syncInterval, syncFlexTime }: Init) => {
if (syncFlexTime > syncInterval) {
throw new Error(
'Specified syncInterval must be greater than the specified syncFlexTime.'
);
}
_checkIntervals(syncInterval, syncFlexTime);
SyncAdapter.init(syncInterval, syncFlexTime);
},

syncImmediately: () => {
SyncAdapter.syncImmediately();
syncImmediately: ({ syncInterval, syncFlexTime }: Init) => {
_checkIntervals(syncInterval, syncFlexTime);
SyncAdapter.syncImmediately(syncInterval, syncFlexTime);
},
};

0 comments on commit 0f9fd44

Please sign in to comment.