-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
239 lines (224 loc) · 6.86 KB
/
App.tsx
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import React, { useCallback, useEffect, useState } from "react"
import {
AppState,
Platform,
SafeAreaView,
StyleSheet,
ToastAndroid,
View,
} from "react-native"
import { LanguageProvider } from "./app/providers/language/LanguageProvider"
import { ContactList } from "./app/screens/ContactList"
import {
addMessage,
connectToDatabase,
createTables,
getContacts,
getSingleUserPreference,
updateSingleUserPreference,
} from "./app/db/dbService"
import { Contact } from "./app/components/ContactSummary/ContactSummary.typing"
import { AddContact } from "./app/screens/AddContact"
import { ColorProvider } from "./app/providers/color/ColorProvider"
import {
formatDateString,
formatDateStringTimestamp,
} from "./app/utils/format/date"
import { colors } from "./app/utils/theme/colors"
import { MessageContact } from "./app/screens/MessageContact"
import { PERMISSIONS, request } from "react-native-permissions"
import SmsListener from "react-native-android-sms-listener"
import SmsAndroid from "react-native-get-sms-android"
import { SQLiteDatabase } from "react-native-sqlite-storage"
import { PermissionError } from "./app/screens/PermissionError"
type CurrentScreen =
| "ContactList"
| "AddContact"
| "MessageContact"
| "PermissionError"
export interface ScreenProps {
currentScreen: CurrentScreen
data?: Contact | undefined
}
function App(): JSX.Element {
const [screenData, setScreenData] = useState<ScreenProps>({
currentScreen: "ContactList",
})
const [contacts, setContacts] = useState<Contact[]>([])
const getLastUsageDate = useCallback(async () => {
const db = await connectToDatabase()
const databaseLastUsageDate = await getSingleUserPreference(
db,
"lastUsageDate"
)
if (databaseLastUsageDate) {
ToastAndroid.show(databaseLastUsageDate, 3)
}
}, [])
const getBackgroundMessages = async (db: SQLiteDatabase) => {
const databaseLastUsageDate = await getSingleUserPreference(
db,
"lastUsageDate"
)
let filter = {
box: "inbox",
minDate: formatDateStringTimestamp(databaseLastUsageDate),
maxDate: Date.now(),
}
SmsAndroid.list(
JSON.stringify(filter),
(fail: any) => {
console.error("Failed with error: " + fail)
},
(count: any, smsList: any) => {
let incomingMessageList = JSON.parse(smsList)
incomingMessageList
?.slice()
?.reverse()
?.forEach(async (item: any) => {
await addMessage(db, item.address, {
isReceived: true,
content: item.body,
timestamp: item.date,
})
})
}
)
}
const handleAppStateChange = useCallback(async () => {
const subscription = AppState.addEventListener(
"change",
async (nextAppState) => {
const db = await connectToDatabase()
if (nextAppState === "background") {
const stringDate = formatDateString(new Date())
await updateSingleUserPreference(db, "lastUsageDate", stringDate)
} else if (nextAppState === "active") {
if (Platform.OS === "android") {
await getBackgroundMessages(db)
const lastUsageDate = await getSingleUserPreference(
db,
"lastUsageDate"
)
if (lastUsageDate) {
ToastAndroid.show(lastUsageDate.toString(), 3)
}
}
}
}
)
return subscription
}, [])
const loadDataCallback = useCallback(async () => {
try {
const db = await connectToDatabase()
await createTables(db)
const databaseContacts = await getContacts(db)
if (databaseContacts.length) {
if (Platform.OS === "android") {
await getBackgroundMessages(db)
}
setContacts(databaseContacts)
}
} catch (error) {
console.error(error)
}
}, [])
const askForPermissions = async () => {
const readSMSPermission = await request(PERMISSIONS.ANDROID.READ_SMS)
const receiveSMSPermission = await request(PERMISSIONS.ANDROID.RECEIVE_SMS)
const sendSMSPermission = await request(PERMISSIONS.ANDROID.SEND_SMS)
return (
readSMSPermission === "granted" &&
receiveSMSPermission === "granted" &&
sendSMSPermission === "granted"
)
}
const setSmsListener = useCallback(() => {
const subscription = SmsListener.addListener(
async (incomingMessage: any) => {
if (incomingMessage.originatingAddress) {
const newMessage = {
content: incomingMessage.body,
isReceived: true,
timestamp: incomingMessage.timestamp,
}
const db = await connectToDatabase()
await addMessage(db, incomingMessage.originatingAddress, newMessage)
}
}
)
return subscription
}, [])
useEffect(() => {
loadDataCallback()
let smsUnsubscribe: any = null
if (Platform.OS === "android") {
askForPermissions().then((isGranted) => {
if (isGranted) {
smsUnsubscribe = setSmsListener()
getLastUsageDate()
handleAppStateChange().then((appStateSubscription) => {
return () => {
appStateSubscription?.remove()
}
})
} else {
setScreenData({ currentScreen: "PermissionError" })
}
})
}
return () => {
smsUnsubscribe?.remove()
}
}, [loadDataCallback, setSmsListener, getLastUsageDate, handleAppStateChange])
return (
<SafeAreaView style={styles.appBackground}>
<LanguageProvider>
<ColorProvider>
<View style={styles.screenContainer}>
{screenData.currentScreen === "PermissionError" && (
<PermissionError />
)}
{screenData.currentScreen === "ContactList" && (
<ContactList
contacts={contacts}
setContacts={setContacts}
setScreenData={setScreenData}
/>
)}
{screenData.currentScreen === "AddContact" && (
<AddContact
contacts={contacts}
setContacts={setContacts}
setScreenData={setScreenData}
/>
)}
{screenData.currentScreen === "MessageContact" &&
screenData.data && (
<MessageContact
contact={screenData.data}
onBackPress={() =>
setScreenData({
currentScreen: "ContactList",
data: undefined,
})
}
/>
)}
</View>
</ColorProvider>
</LanguageProvider>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
screenContainer: {
height: "100%",
width: "100%",
},
appBackground: {
backgroundColor: colors.palette.white,
},
})
export default App