-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
191 lines (171 loc) · 5.69 KB
/
Copy pathApp.js
File metadata and controls
191 lines (171 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
uMetric.app
Copyright (C) 2021 Daniel Moreno Medina
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'react-native-gesture-handler'
import React, { useEffect } from 'react'
import { LogBox, Linking } from 'react-native'
import * as Localization from 'expo-localization'
import i18n from 'i18n-js'
import urlParse from 'url-parse'
import Toast from 'react-native-toast-message'
import 'react-native-get-random-values'
import { v4 as uuidv4 } from 'uuid'
import CompleteFlow from './navigation/CompleteFlow'
import * as RootNavigation from './navigation/RootNavigation'
import { en, es, pt, jp, zh, ru, ph, de } from './i18n/supportedLanguages'
import { getCurrentUser, createAndSetCurrentUser } from './utils/userUtils'
import { startAutomaticSync, stopAutomaticSync } from './utils/syncService'
import { Database } from '@nozbe/watermelondb'
import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite'
import { DatabaseProvider } from '@nozbe/watermelondb/DatabaseProvider'
import { umetricSchema } from './model/schema'
import Category from './model/Category'
import Event from './model/Event'
import EventLog from './model/EventLog'
import Goal from './model/Goal'
import LikertScale from './model/LikertScale'
import LikertScaleTranslation from './model/LikertScaleTranslation'
import Question from './model/Question'
import QuestionLikert from './model/QuestionLikert'
import QuestionTranslation from './model/QuestionTranslation'
import Questionnaire from './model/Questionnaire'
import QuestionnaireResponse from './model/QuestionnaireResponse'
import QuestionnaireTranslation from './model/QuestionnaireTranslation'
import Response from './model/Response'
import User from './model/User'
import { setGenerator } from '@nozbe/watermelondb/utils/common/randomId'
import { schemaMigrations, addColumns } from '@nozbe/watermelondb/Schema/migrations'
i18n.fallbacks = true
i18n.translations = { en, es, pt, jp, zh, ru, ph, de }
i18n.locale = Localization.getLocales()[0].languageCode
LogBox.ignoreLogs(['Setting a timer'])
setGenerator(() => uuidv4())
const migrations = schemaMigrations({
migrations: [
{
toVersion: 2,
steps: [
addColumns({
table: 'users',
columns: [
{ name: 'server_url', type: 'string', isOptional: true },
{ name: 'encryption_key', type: 'string', isOptional: true },
{ name: 'sync_frequency', type: 'string', isOptional: true },
],
}),
],
},
],
})
const adapter = new SQLiteAdapter({ schema: umetricSchema, migrations })
const database = new Database({
adapter,
modelClasses: [
Category,
Event,
EventLog,
Goal,
LikertScale,
LikertScaleTranslation,
Question,
QuestionLikert,
QuestionTranslation,
Questionnaire,
QuestionnaireResponse,
QuestionnaireTranslation,
Response,
User,
],
actionsEnabled: true,
})
const App = () => {
useEffect(() => {
const handleUrl = async (url) => {
console.log('Deep link received:', url)
try {
const parsedUrl = urlParse(url, true)
let category_id = null
// umetric://category/UUID
if (parsedUrl.pathname) {
const parts = parsedUrl.pathname.split('/').filter(part => part.length > 0)
if (parts.length >= 1) {
category_id = parts[0]
}
}
if (category_id) {
try {
const category = await database.collections
.get('categories')
.find(category_id.toString())
RootNavigation.navigate('Input', {
screen: 'ListEvents',
params: {
category_id: category_id,
category_name: category.name,
fromDeepLink: true
}
})
} catch (error) {
RootNavigation.navigate('Input', { screen: 'ListCategories' })
}
} else {
console.log('No valid category_id found in URL')
}
} catch (error) {
console.error('Error handling deep link:', error)
}
}
const subscription = Linking.addEventListener('url', ({ url }) => {
console.log('URL event received:', url)
handleUrl(url)
})
Linking.getInitialURL().then((url) => {
console.log('Initial URL:', url)
if (url) {
handleUrl(url)
}
})
return () => {
subscription?.remove()
}
}, [])
// Ensure a user exists at app start and initialize sync
useEffect(() => {
const ensureUser = async () => {
const user = await getCurrentUser(database)
if (!user) {
await createAndSetCurrentUser(database)
} else {
// Start automatic sync if enabled
try {
await startAutomaticSync(database)
} catch (error) {
console.error('Failed to start automatic sync:', error)
}
}
}
ensureUser()
// Cleanup on unmount
return () => {
stopAutomaticSync()
}
}, [])
return (
<DatabaseProvider database={database}>
<CompleteFlow />
<Toast />
</DatabaseProvider>
)
}
export default App