Menu

 

React Native

React Native library for integrating WeFitter and Samsung Health into your app

  • This library is written for React Native CLI. Expo Go is unsupported

Installation

yarn add https://github.com/wefitter/react-native-wefitter-samsung.git#v1.5.1

 

Usage

Add the following code:

See the example app for the full source.

Show code example
Hide code example
import WeFitterSamsung, {
  ConfiguredEvent,
  ConnectedEvent,
  ErrorEvent,
} from 'react-native-wefitter-samsung';

// ...

const [connected, setConnected] = useState<boolean>(false);

useEffect(() => {
  if (Platform.OS === 'android') {
    // create native event emitter and event listeners to handle status updates
    const emitter = new NativeEventEmitter();
    const configuredListener = emitter.addListener(
      'onConfiguredWeFitterSamsung',
      (event: ConfiguredEvent) =>
        console.log(`WeFitterSamsung configured: ${event.configured}`)
    );
    const connectedListener = emitter.addListener(
      'onConnectedWeFitterSamsung',
      (event: ConnectedEvent) => {
        console.log(`WeFitterSamsung connected: ${event.connected}`);
        setConnected(event.connected);
      }
    );
    const errorListener = emitter.addListener(
      'onErrorWeFitterSamsung',
      (event: ErrorEvent) => {
        console.log(`WeFitterSamsung error: ${event.error}`);

        // `error` can be checked to override specific messages
        // `forUser` boolean indicates the error requires user interaction
        if (event.forUser) {
          Alert.alert('', event.error, [
            {
              text: 'OK',
              onPress: () => {
                // `tryToResolveError` will guide the user on fixing certain errors
                // for example:
                // - link to the app store to install or update Samsung Health
                // - open Samsung Health to accept privacy policy
                // - open Settings when Samsung Health needs to be enabled
                WeFitterSamsung.tryToResolveError(event.error);
              },
            },
          ]);
        }
      }
    );

    // create config
    const config = {
      token: 'YOUR_TOKEN', // required
      apiUrl: 'YOUR_API_URL', // required, the url should be base without `v1/ingest/` as the library will append this. For example: `https://api.wefitter.com/api/`
      startDate: 'CUSTOM_START_DATE', // optional with format `yyyy-MM-dd`, by default data of the past 20 days will be uploaded
      notificationTitle: 'CUSTOM_TITLE', // optional
      notificationText: 'CUSTOM_TEXT', // optional
      notificationIcon: 'CUSTOM_ICON', // optional, e.g. `ic_notification` placed in either drawable, mipmap or raw
      notificationChannelId: 'CUSTOM_CHANNEL_ID', // optional
      notificationChannelName: 'CUSTOM_CHANNEL_NAME', // optional
    };

    // configure WeFitterSamsung
    WeFitterSamsung.configure(config);

    return () => {
      configuredListener.remove();
      connectedListener.remove();
      errorListener.remove();
    };
  }
  return;
}, []);

const onPressConnectOrDisconnect = () => {
  if (Platform.OS === 'android') {
    connected ? WeFitterSamsung.disconnect() : WeFitterSamsung.connect();
  } else {
    Alert.alert('Not supported', 'WeFitterSamsung is not supported on iOS');
  }
};

You are subscribed!

You will soon receive our newsletter!
WeFitter team.
footer_bg