Skip to content

Commit

Permalink
add sentry to sync app (opt out available)
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCoderSJ committed Oct 8, 2024
1 parent 5bb6af7 commit 9c5c995
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 60 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,20 @@ You can self host the server and database. To do this, follow the steps below:
- run `pip install -r requirements.txt`
- rename `.env.example` to `.env` and fill in the values
- run `python3 main.py` to start the server

To run the mobile application:
- in another window/tab, `cd` into the app/ folder
- run `npm install`
- If you wish to remove sentry:
```
yarn remove @sentry/react-native
npx @sentry/wizard -i reactNative -p android --uninstall
```
- If you wish to change sentry to your own instance:
- Change the `dsn` in `App.js` to your own DSN
- Change the server, org name, and project name in app.json
- Change these details again in android/sentry.properties
- Change the DSN in the AndroidManifest.xml
- run `npx patch-package` to apply a patch to the foreground service library
- run `npm run android` to start the application, or follow the instructions at https://medium.com/geekculture/react-native-generate-apk-debug-and-release-apk-4e9981a2ea51 to build an APK file.
- It is also possible to now use eas build to build the APK file. You can find more at https://docs.expo.dev/build/eas-build/ **NOTE: This must be a local build, since you need to run patch-package before building the APK file.**
4 changes: 4 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ yarn-error.*

# typescript
*.tsbuildinfo


*.log
.env.local
94 changes: 91 additions & 3 deletions app/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StyleSheet, Text, View, TextInput, Button } from 'react-native';
import { StyleSheet, Text, View, TextInput, Button, Switch } from 'react-native';
import React from 'react';
import {
initialize,
Expand All @@ -11,14 +11,43 @@ import Toast from 'react-native-toast-message';
import axios from 'axios';
import ReactNativeForegroundService from '@supersami/rn-foreground-service';
import {requestNotifications} from 'react-native-permissions';
ReactNativeForegroundService.register();
import * as Sentry from '@sentry/react-native';

const setObj = async (key, value) => { try { const jsonValue = JSON.stringify(value); await AsyncStorage.setItem(key, jsonValue) } catch (e) { console.log(e) } }
const setPlain = async (key, value) => { try { await AsyncStorage.setItem(key, value) } catch (e) { console.log(e) } }
const get = async (key) => { try { const value = await AsyncStorage.getItem(key); if (value !== null) { try { return JSON.parse(value) } catch { return value } } } catch (e) { console.log(e) } }
const delkey = async (key, value) => { try { await AsyncStorage.removeItem(key) } catch (e) { console.log(e) } }
const getAll = async () => { try { const keys = await AsyncStorage.getAllKeys(); return keys } catch (error) { console.error(error) } }

let isSentryEnabled = true;
get('sentryEnabled')
.then(res => {
if (res != "false") {
Sentry.init({
dsn: 'https://[email protected]/6',
// enableSpotlight: __DEV__,
});
Toast.show({
type: 'success',
text1: "Sentry enabled from settings",
});
} else {
isSentryEnabled = false;
Toast.show({
type: 'info',
text1: "Sentry is disabled",
});
}
})
.catch(err => {
console.log(err);
Toast.show({
type: 'error',
text1: "Failed to check Sentry settings",
});
});
ReactNativeForegroundService.register();

let login;
let apiBase = 'https://api.hcgateway.shuchir.dev';
let lastSync = null;
Expand Down Expand Up @@ -326,6 +355,35 @@ export default function App() {
}}
/>

<View style={{ flexDirection: 'row', alignItems: 'center', marginVertical: 10 }}>
<Text style={{ fontSize: 15 }}>Enable Sentry:</Text>
<Switch
value={isSentryEnabled}
onValueChange={async (value) => {
if (value) {
Sentry.init({
dsn: 'https://[email protected]/6',
});
Toast.show({
type: 'success',
text1: "Sentry enabled",
});
isSentryEnabled = true;
forceUpdate();
} else {
Sentry.close();
Toast.show({
type: 'success',
text1: "Sentry disabled",
});
isSentryEnabled = false;
forceUpdate();
}
await setPlain('sentryEnabled', value.toString());
}}
/>
</View>

<View style={{ marginTop: 20 }}>
<Button
title="Sync Now"
Expand Down Expand Up @@ -419,6 +477,36 @@ export default function App() {
}}
/>

<View style={{ flexDirection: 'row', alignItems: 'center', marginVertical: 10 }}>
<Text style={{ fontSize: 15 }}>Enable Sentry:</Text>
<Switch
value={isSentryEnabled}
defaultValue={isSentryEnabled}
onValueChange={async (value) => {
if (value) {
Sentry.init({
dsn: 'https://[email protected]/6',
});
Toast.show({
type: 'success',
text1: "Sentry enabled",
});
isSentryEnabled = true;
forceUpdate();
} else {
Sentry.close();
Toast.show({
type: 'success',
text1: "Sentry disabled",
});
isSentryEnabled = false;
forceUpdate();
}
await setPlain('sentryEnabled', value.toString());
}}
/>
</View>

<Button
title="Login"
onPress={() => {
Expand All @@ -431,7 +519,7 @@ export default function App() {
<Toast />
</View>
);
}
};

const styles = StyleSheet.create({
container: {
Expand Down
3 changes: 2 additions & 1 deletion app/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def enableProguardInReleaseBuilds = (findProperty('android.enableProguardInRelea
*/
def jscFlavor = 'org.webkit:android-jsc:+'

apply from: new File(["node", "--print", "require.resolve('@sentry/react-native/package.json')"].execute().text.trim(), "../sentry.gradle")
android {
ndkVersion rootProject.ext.ndkVersion

Expand All @@ -89,7 +90,7 @@ android {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "0.1.0"
versionName "1.2.0"
}
signingConfigs {
debug {
Expand Down
61 changes: 20 additions & 41 deletions app/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>

<uses-permission android:name="android.permission.health.READ_ACTIVE_CALORIES_BURNED"/>
<uses-permission android:name="android.permission.health.READ_BASAL_BODY_TEMPERATURE"/>
<uses-permission android:name="android.permission.health.READ_BASAL_METABOLIC_RATE"/>
Expand All @@ -16,8 +16,8 @@
<uses-permission android:name="android.permission.health.READ_BONE_MASS"/>
<uses-permission android:name="android.permission.health.READ_CERVICAL_MUCUS"/>
<uses-permission android:name="android.permission.health.READ_DISTANCE"/>
<uses-permission android:name="android.permission.health.READ_EXERCISE"/>
<uses-permission android:name="android.permission.health.READ_ELEVATION_GAINED"/>
<uses-permission android:name="android.permission.health.READ_EXERCISE"/>
<uses-permission android:name="android.permission.health.READ_FLOORS_CLIMBED"/>
<uses-permission android:name="android.permission.health.READ_HEART_RATE"/>
<uses-permission android:name="android.permission.health.READ_HEART_RATE_VARIABILITY"/>
Expand All @@ -39,9 +39,6 @@
<uses-permission android:name="android.permission.health.READ_VO2_MAX"/>
<uses-permission android:name="android.permission.health.READ_WEIGHT"/>
<uses-permission android:name="android.permission.health.READ_WHEELCHAIR_PUSHES"/>

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<queries>
<intent>
<action android:name="android.intent.action.VIEW"/>
Expand All @@ -50,26 +47,14 @@
</intent>
</queries>
<application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme">
<meta-data
android:name="com.supersami.foregroundservice.notification_channel_name"
android:value="Sticky Title"
/>
<meta-data
android:name="com.supersami.foregroundservice.notification_channel_description"
android:value="Sticky Description."
/>
<meta-data
android:name="com.supersami.foregroundservice.notification_color"
android:resource="@color/blue"
/>
<service android:name="com.supersami.foregroundservice.ForegroundService" android:foregroundServiceType="dataSync"></service>
<service android:name="com.supersami.foregroundservice.ForegroundServiceTask" android:foregroundServiceType="dataSync"></service>


<meta-data android:name="com.supersami.foregroundservice.notification_channel_description" android:value="Sticky Description."/>
<meta-data android:name="com.supersami.foregroundservice.notification_channel_name" android:value="Sticky Title"/>
<meta-data android:name="com.supersami.foregroundservice.notification_color" android:resource="@color/blue"/>
<meta-data android:name="expo.modules.updates.ENABLED" android:value="false"/>
<meta-data android:name="expo.modules.updates.EXPO_UPDATES_CHECK_ON_LAUNCH" android:value="ALWAYS"/>
<meta-data android:name="expo.modules.updates.EXPO_UPDATES_LAUNCH_WAIT_MS" android:value="0"/>

<service android:name="com.supersami.foregroundservice.ForegroundService" android:foregroundServiceType="dataSync"/>
<service android:name="com.supersami.foregroundservice.ForegroundServiceTask" android:foregroundServiceType="dataSync"/>
<activity android:name=".MainActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|uiMode" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize" android:theme="@style/Theme.App.SplashScreen" android:exported="true" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand All @@ -80,42 +65,36 @@
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="dev.shuchir.hcgateway"/>
<data android:scheme="exp+hcgateway"/>
</intent-filter>
<intent-filter>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE"/>
</intent-filter>
<intent-filter>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE"/>
</intent-filter>
</activity>

<activity
android:name=".PermissionsRationaleActivity"
android:exported="true">
<activity android:name=".PermissionsRationaleActivity" android:exported="true">
<intent-filter>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE"/>
</intent-filter>
</activity>

<!-- For versions starting Android 14, create an activity alias to show the rationale
of Health Connect permissions once users click the privacy policy link. -->
<activity-alias
android:name="ViewPermissionUsageActivity"
android:exported="true"
android:targetActivity=".MainActivity"
android:permission="android.permission.START_VIEW_PERMISSION_USAGE">
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" android:exported="false"/>
<activity-alias android:name="ViewPermissionUsageActivity" android:exported="true" android:targetActivity=".MainActivity" android:permission="android.permission.START_VIEW_PERMISSION_USAGE">
<intent-filter>
<action android:name="android.intent.action.VIEW_PERMISSION_USAGE" />
<category android:name="android.intent.category.HEALTH_PERMISSIONS" />
<action android:name="android.intent.action.VIEW_PERMISSION_USAGE"/>
<category android:name="android.intent.category.HEALTH_PERMISSIONS"/>
</intent-filter>
</activity-alias>

<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" android:exported="false"/>
<activity-alias android:name="ViewPermissionUsageActivity" android:exported="true" android:targetActivity=".MainActivity" android:permission="android.permission.START_VIEW_PERMISSION_USAGE">
<intent-filter>
<action android:name="android.intent.action.VIEW_PERMISSION_USAGE"/>
<category android:name="android.intent.category.HEALTH_PERMISSIONS"/>
</intent-filter>
</activity-alias>

<meta-data android:name="io.sentry.dsn" android:value="https://[email protected]/6" />
<meta-data android:name="io.sentry.traces.sample-rate" android:value="1.0" />
<meta-data android:name="io.sentry.traces.profiling.sample-rate" android:value="1.0" />
</application>
</manifest>
19 changes: 9 additions & 10 deletions app/android/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

<resources>
<item name="blue" type="color">#00C4D1
<resources>
<item name="blue" type="color">#00C4D1&#xD;
</item>
<integer-array name="androidcolors">
<integer-array name="androidcolors">
<item>@color/blue</item>
</integer-array>

<color name="colorPrimary">#00C4D1</color>
<color name="iconBackground">#00C4D1</color>
<color name="splashscreen_background">#FFFFFF</color>
</resources>
</integer-array>
<color name="colorPrimary">#023c69</color>
<color name="iconBackground">#ffffff</color>
<color name="splashscreen_background">#ffffff</color>
<color name="colorPrimaryDark">#ffffff</color>
</resources>
4 changes: 4 additions & 0 deletions app/android/sentry.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defaults.url=https://sentry.shuchir.dev/
defaults.org=sentry
defaults.project=hcgateway-app
# Using SENTRY_AUTH_TOKEN environment variable
10 changes: 9 additions & 1 deletion app/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"expo": {
"name": "HCGateway",
"slug": "hcgateway",
"version": "1.1.1",
"version": "1.2.0",
"plugins": [
"expo-health-connect",
[
Expand All @@ -14,6 +14,14 @@
"minSdkVersion": 26
}
}
],
[
"@sentry/react-native/expo",
{
"url": "https://sentry.shuchir.dev/",
"project": "hcgateway-app",
"organization": "sentry"
}
]
],
"orientation": "portrait",
Expand Down
5 changes: 5 additions & 0 deletions app/metro.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { getSentryExpoConfig } = require("@sentry/react-native/metro");

const config = getSentryExpoConfig(__dirname);

module.exports = config;
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@react-native-async-storage/async-storage": "^1.23.1",
"@sentry/react-native": "^5.33.2",
"@supersami/rn-foreground-service": "^2.1.1",
"axios": "^1.7.2",
"expo": "~51.0.18",
Expand Down
Loading

0 comments on commit 9c5c995

Please sign in to comment.