Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store data in database (TypeORM) #12

Merged
merged 23 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = function(api) {
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
presets: ["babel-preset-expo"],
plugins: [
"babel-plugin-transform-typescript-metadata", // https://github.com/typeorm/expo-example/blob/master/babel.config.js
],
};
};
32 changes: 21 additions & 11 deletions config/survey.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
"randomMinuteAddition": { "min": 0, "max": 119 }
},
"streamsOrder": {
"0": ["streamName", "streamName", "streamName", "streamName"],
"1": ["streamName", "streamName", "streamName", "streamName"],
"2": ["streamName", "streamName", "streamName", "streamName"],
"3": ["streamName", "streamName", "streamName", "streamName"],
"4": ["streamName", "streamName", "streamName", "streamName"],
"5": ["streamName", "streamName", "streamName", "streamName"],
"6": ["streamName", "streamName", "streamName", "streamName"]
"0": ["myStream", "myStream", "myStream", "myStream"],
"1": ["myStream", "myStream", "myStream", "myStream"],
"2": ["myStream", "myStream", "myStream", "myStream"],
"3": ["myStream", "myStream", "myStream", "myStream"],
"4": ["myStream", "myStream", "myStream", "myStream"],
"5": ["myStream", "myStream", "myStream", "myStream"],
"6": ["myStream", "myStream", "myStream", "myStream"]
},
"streamInCaseOfError": "streamName",
"streamsNotReplacedByFollowupStream": ["streamName"],
"streamInCaseOfError": "errorStream",
"streamsNotReplacedByFollowupStream": ["myStream"],
"notificationContent": {
"default": {
"title": "You have a new survey!",
Expand All @@ -36,11 +36,12 @@
},
"meta": {
"startingQuestionIds": {
"streamName": "Feel_Current"
"myStream": "Feel_Current",
"errorStream": "firstQuestion"
}
},
"streams": {
"streamName": {
"myStream": {
"Feel_Current": {
"id": "Feel_Current",
"type": "slider",
Expand Down Expand Up @@ -137,6 +138,15 @@
"indexName": "INDEX",
"next": null
}
},
"errorStream": {
"firstQuestion": {
"id": "firstQuestion",
"type": "slider",
"question": "Why are we here?",
"slider": ["extremely negative", "extremely positive"],
"next": null
}
}
}
}
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@
"expo-constants": "~9.0.0",
"expo-crypto": "~8.1.0",
"expo-device": "~2.1.0",
"expo-file-system": "~8.1.0",
"expo-sqlite": "~8.1.0",
"expo-web-browser": "~8.2.1",
"lodash": "^4.17.15",
"react": "16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
"react-native-webview": "8.1.1"
"react-native-sqlite-storage": "^5.0.0",
"react-native-webview": "8.1.1",
"reflect-metadata": "^0.1.13",
"typeorm": "^0.2.25",
"url": "^0.11.0"
},
"devDependencies": {
"@types/node": "^14.0.13",
"@types/react": "^16.9.11",
"@types/react-native": "^0.60.22",
"@typescript-eslint/eslint-plugin": "^3.2.0",
"@typescript-eslint/parser": "^3.2.0",
"babel-plugin-transform-typescript-metadata": "^0.3.0",
"babel-preset-expo": "^8.1.0",
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
Expand Down
74 changes: 51 additions & 23 deletions src/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
import { WebView } from "react-native-webview";

import SurveyScreen, { SurveyScreenState } from "./SurveyScreen";
import { AnswerEntity } from "./entities/AnswerEntity";
import { PingEntity } from "./entities/PingEntity";
import {
uploadDataAsync,
getAllDataAsync,
Expand All @@ -25,22 +27,23 @@ import {
import {
getNotificationTimesAsync,
clearNotificationTimesAsync,
getPingsAsync,
initPingAsync,
insertPingAsync,
clearPingsAsync,
getLatestStartedPingAsync,
getLatestPingAsync,
getPingStateAsync,
PingInfo,
getTodayPingsAsync,
dequeueFuturePingIfAny,
initFuturePingQueueAsync,
getFuturePingsQueue,
clearPingStateAsync,
getTypesOfPingsAnsweredAsync,
getNumbersOfPingsForAllStreamNames,
} from "./helpers/asyncStorage";
import { getAllStreamNames } from "./helpers/configFiles";
import {
shareDatabaseFileAsync,
deleteDatabaseFileAsync,
} from "./helpers/database";
import { getNonCriticalProblemTextForUser } from "./helpers/debug";
import { QuestionType } from "./helpers/helpers";
import {
setNotificationsAsync,
setupNotificationsPermissionAsync,
Expand Down Expand Up @@ -77,7 +80,7 @@ interface HomeScreenState {
time: Date;
allowsNotifications: boolean;
currentNotificationTime: Date | null;
currentPing: PingInfo | null;
currentPing: PingEntity | null;
isLoading: boolean;
storedPingStateAsync: SurveyScreenState | null;

Expand Down Expand Up @@ -160,22 +163,20 @@ export default class HomeScreen extends React.Component<
await doEveryHalfMinutes();
});

const latestStartedPing = await getLatestStartedPingAsync();
const latestPing = await getLatestPingAsync();
//console.warn(latestStartedPing);
const currentNotificationTime = await getCurrentNotificationTimeAsync();
if (
latestStartedPing &&
latestPing &&
currentNotificationTime &&
latestStartedPing.notificationTime.getTime() ===
latestPing.notificationTime.getTime() ===
currentNotificationTime.getTime()
) {
const storedPingStateAsync = await getPingStateAsync(
latestStartedPing.id,
);
const storedPingStateAsync = await getPingStateAsync(latestPing.id);
//console.warn(storedPingStateAsync);
this.setState({
storedPingStateAsync,
currentPing: latestStartedPing,
currentPing: latestPing,
});
}

Expand All @@ -193,7 +194,7 @@ export default class HomeScreen extends React.Component<
const todayPings = await getTodayPingsAsync();
let newPingName: StreamName;

if (todayPings.length > studyInfo.frequency.hoursEveryday.length) {
if (todayPings.length >= studyInfo.frequency.hoursEveryday.length) {
alert(
getNonCriticalProblemTextForUser(
`todayPings.length (${todayPings.length}) > ${studyInfo.frequency.hoursEveryday.length}`,
Expand Down Expand Up @@ -347,6 +348,33 @@ export default class HomeScreen extends React.Component<
this.setState({ displayDebugView: false });
}}
/>
<Button
color="orange"
title="shareDatabaseFileAsync"
onPress={async () => {
/*const ping = new PingEntity();
ping.id = "another";
ping.notificationTime = new Date();
ping.startTime = new Date();
ping.streamName = "one";
ping.tzOffset = 700;
await ping.save();

const answer = new AnswerEntity();
answer.ping = ping;
answer.questionId = "qu";
answer.questionType = QuestionType.YesNo;
answer.preferNotToAnswer = false;
answer.nextWithoutOption = false;
answer.data = {
value: "haha",
};
answer.lastUpdateDate = new Date();
await answer.save();*/

await shareDatabaseFileAsync(survey.studyInfo.id);
}}
/>
<Button
color="orange"
title="getIncomingNotificationTimeAsync()"
Expand All @@ -361,9 +389,9 @@ export default class HomeScreen extends React.Component<
/>
<Button
color="orange"
title="getLatestStartedPingAsync()"
title="getLatestPingAsync()"
onPress={async () => {
const latestStartedPing = await getLatestStartedPingAsync();
const latestStartedPing = await getLatestPingAsync();
alert(JSON.stringify(latestStartedPing));
}}
/>
Expand All @@ -379,7 +407,7 @@ export default class HomeScreen extends React.Component<
color="red"
title="clear current ping state"
onPress={async () => {
const latestStartedPing = await getLatestStartedPingAsync();
const latestStartedPing = await getLatestPingAsync();
if (latestStartedPing) {
await clearPingStateAsync(latestStartedPing.id);
alert("Cleared. Please restart app");
Expand All @@ -402,9 +430,9 @@ export default class HomeScreen extends React.Component<
/>
<Button
color="orange"
title="getTypesOfPingsAnsweredAsync()"
title="getNumbersOfPingsForAllStreamNames()"
onPress={async () => {
const typesOfPingsAnswered = await getTypesOfPingsAnsweredAsync();
const typesOfPingsAnswered = await getNumbersOfPingsForAllStreamNames();
alert(JSON.stringify(typesOfPingsAnswered));
}}
/>
Expand Down Expand Up @@ -528,8 +556,8 @@ export default class HomeScreen extends React.Component<
text: "Confirm",
style: "destructive",
onPress: async () => {
await clearPingsAsync();
await initPingAsync();
await deleteDatabaseFileAsync(survey.studyInfo.id);
alert("Done! Please restart the app.");
},
},
],
Expand Down Expand Up @@ -613,7 +641,7 @@ export default class HomeScreen extends React.Component<
surveyStartingQuestionId={
survey.meta.startingQuestionIds[currentPing.streamName]
}
pingId={currentPing.id}
ping={currentPing}
previousState={this.state.storedPingStateAsync}
onFinish={async (finishedPing) => {
this.setState({ currentPing: finishedPing });
Expand Down
4 changes: 4 additions & 0 deletions src/RootScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Button, TextInput, Text, View, ScrollView, Alert } from "react-native";
import HomeScreen from "./HomeScreen";
import { registerUserAsync } from "./helpers/apiManager";
import { getSurveyFileAsync } from "./helpers/configFiles";
import { connectDatabaseAsync } from "./helpers/database";
import { getCriticalProblemTextForUser } from "./helpers/debug";
import { SurveyFile } from "./helpers/types";
import { getUserAsync, User, clearUserAsync } from "./helpers/user";
Expand Down Expand Up @@ -39,6 +40,9 @@ export default class RootScreen extends React.Component<
const user = await getUserAsync();
if (user) {
const survey = await getSurveyFileAsync();

await connectDatabaseAsync(survey.studyInfo.id);

this.setState({ survey });
}
this.setState({ userInfo: user, isLoading: false });
Expand Down