|
1 | | -import React, { useState } from 'react'; |
| 1 | +import React, { useState, useEffect } from 'react'; |
2 | 2 | import { TextInput, View, Text, TouchableOpacity, StyleSheet, PermissionsAndroid, Platform, Modal } from 'react-native'; |
3 | 3 | import RNFS from 'react-native-fs'; |
4 | 4 | import { launchCamera, CameraOptions } from 'react-native-image-picker'; |
5 | 5 | import { request, PERMISSIONS, RESULTS } from 'react-native-permissions'; |
6 | 6 | import { Calendar, DateData } from 'react-native-calendars'; |
| 7 | +import SQLite, { SQLiteDatabase, Transaction, ResultSet } from 'react-native-sqlite-storage'; |
7 | 8 |
|
8 | 9 | const App = () => { |
9 | 10 | const [inputText, setInputText] = useState(''); |
10 | 11 | const [isCalendarVisible, setIsCalendarVisible] = useState(false); |
11 | 12 | const [selectedDate, setSelectedDate] = useState(''); |
| 13 | + const [dbData, setDbData] = useState<any[]>([]); |
| 14 | + |
| 15 | + const insertFakeData = (db: SQLiteDatabase) => { |
| 16 | + db.transaction((tx: Transaction) => { |
| 17 | + const unixTime = Math.floor(Date.now() / 1000); |
| 18 | + tx.executeSql( |
| 19 | + `INSERT INTO content (user_id, content_type, content_data, post_date, published) VALUES (?, ?, ?, ?, ?)`, |
| 20 | + [1, 'post', 'testphone', unixTime, 0], |
| 21 | + () => { |
| 22 | + console.log('Fake data inserted successfully'); |
| 23 | + }, |
| 24 | + (error) => { |
| 25 | + console.log('Error inserting fake data:', error); |
| 26 | + } |
| 27 | + ); |
| 28 | + }); |
| 29 | + }; |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + // In the useEffect: |
| 34 | + useEffect(() => { |
| 35 | + const dbPath = `${RNFS.DocumentDirectoryPath}/database_default.sqlite3`; |
| 36 | + console.log('Database path:', dbPath); |
| 37 | + |
| 38 | + const listDirectoryContents = async (path: string) => { |
| 39 | + try { |
| 40 | + const files = await RNFS.readDir(path); |
| 41 | + console.log('Directory contents:', files); |
| 42 | + } catch (error) { |
| 43 | + console.error('Error reading directory:', error); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + listDirectoryContents(RNFS.DocumentDirectoryPath); |
| 48 | + |
| 49 | + RNFS.exists(dbPath) |
| 50 | + .then((exists) => { |
| 51 | + if (exists) { |
| 52 | + const db = SQLite.openDatabase( |
| 53 | + { name: 'database_default.sqlite3', location: 'default' }, |
| 54 | + () => { |
| 55 | + console.log('Database opened'); |
| 56 | + // insertFakeData(db); |
| 57 | + fetchDbData(db); |
| 58 | + }, |
| 59 | + (error) => { |
| 60 | + console.log('Error opening database:', error); |
| 61 | + } |
| 62 | + ); |
| 63 | + } else { |
| 64 | + const filePath = `${RNFS.DocumentDirectoryPath}/database_default.sqlite3`; |
| 65 | + RNFS.writeFile(filePath, '', 'utf8') |
| 66 | + .then(() => { |
| 67 | + console.log('SQLite database file created:', filePath); |
| 68 | + const db = SQLite.openDatabase( |
| 69 | + { name: 'database_default.sqlite3', location: 'default' }, |
| 70 | + () => { |
| 71 | + console.log('New database created and opened'); |
| 72 | + db.transaction((tx: Transaction) => { |
| 73 | + createTables(tx); |
| 74 | + }, (error: any) => { |
| 75 | + console.log('Transaction error:', error); |
| 76 | + }, () => { |
| 77 | + console.log('Tables created successfully'); |
| 78 | + // insertFakeData(db); |
| 79 | + listDirectoryContents(RNFS.DocumentDirectoryPath); |
| 80 | + }); |
| 81 | + }, |
| 82 | + (error) => { |
| 83 | + console.log('Error creating new database:', error); |
| 84 | + } |
| 85 | + ); |
| 86 | + }) |
| 87 | + .catch((error) => { |
| 88 | + console.error('Error creating SQLite database file:', error); |
| 89 | + }); |
| 90 | + } |
| 91 | + }) |
| 92 | + .catch((error) => { |
| 93 | + console.log('Error checking if database exists:', error); |
| 94 | + }); |
| 95 | + }, []); |
| 96 | + |
| 97 | + |
| 98 | + const createTables = (tx: Transaction) => { |
| 99 | + tx.executeSql(` |
| 100 | + CREATE TABLE IF NOT EXISTS users ( |
| 101 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 102 | + name TEXT |
| 103 | + ); |
| 104 | + `); |
| 105 | + tx.executeSql(` |
| 106 | + CREATE TABLE IF NOT EXISTS social_media_accounts ( |
| 107 | + account_id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 108 | + user_id INTEGER, |
| 109 | + platform_name TEXT, |
| 110 | + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE |
| 111 | + ); |
| 112 | + `); |
| 113 | + tx.executeSql(` |
| 114 | + CREATE TABLE IF NOT EXISTS scheduler ( |
| 115 | + scheduler_id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 116 | + content_id INTEGER, |
| 117 | + social_media_account_id INTEGER, |
| 118 | + scheduled_time DATETIME, |
| 119 | + FOREIGN KEY (content_id) REFERENCES content(content_id) ON DELETE CASCADE, |
| 120 | + FOREIGN KEY (social_media_account_id) REFERENCES social_media_accounts(account_id) ON DELETE CASCADE |
| 121 | + ); |
| 122 | + `); |
| 123 | + tx.executeSql(` |
| 124 | + CREATE TABLE IF NOT EXISTS content ( |
| 125 | + content_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, |
| 126 | + user_id INTEGER NOT NULL, |
| 127 | + content_type TEXT NOT NULL CHECK (content_type IN ('image', 'video', 'post')), |
| 128 | + content_data TEXT NOT NULL, |
| 129 | + post_date DATE NOT NULL, |
| 130 | + description TEXT, |
| 131 | + tags TEXT, |
| 132 | + published INTEGER NOT NULL DEFAULT (0), |
| 133 | + FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE |
| 134 | + ); |
| 135 | + `); |
| 136 | + tx.executeSql(` |
| 137 | + CREATE TABLE IF NOT EXISTS meta_accounts ( |
| 138 | + ID INTEGER PRIMARY KEY AUTOINCREMENT, |
| 139 | + account_id INTEGER, |
| 140 | + meta_id TEXT, |
| 141 | + meta_token TEXT, |
| 142 | + account_name TEXT |
| 143 | + ); |
| 144 | + `); |
| 145 | + tx.executeSql(` |
| 146 | + CREATE TABLE IF NOT EXISTS twitter_accounts ( |
| 147 | + ID INTEGER PRIMARY KEY AUTOINCREMENT, |
| 148 | + account_id INTEGER, |
| 149 | + twitter_consumer_key TEXT, |
| 150 | + twitter_access_token TEXT, |
| 151 | + twitter_access_token_secret TEXT, |
| 152 | + account_name TEXT, |
| 153 | + twitter_consumer_secret TEXT |
| 154 | + ); |
| 155 | + `); |
| 156 | + tx.executeSql(` |
| 157 | + CREATE TABLE IF NOT EXISTS linkedin_accounts ( |
| 158 | + app_id TEXT, |
| 159 | + account_id INTEGER PRIMARY KEY REFERENCES social_media_accounts (account_id), |
| 160 | + app_secret TEXT, |
| 161 | + app_token TEXT, |
| 162 | + app_refresh_token TEXT, |
| 163 | + app_token_expires_in INTEGER, |
| 164 | + app_token_refresh_expires_in INTEGER, |
| 165 | + account_name TEXT, |
| 166 | + timestamp DATETIME |
| 167 | + ); |
| 168 | + `); |
| 169 | + }; |
| 170 | + |
| 171 | + |
| 172 | + const fetchDbData = (db: SQLiteDatabase) => { |
| 173 | + db.transaction((tx: Transaction) => { |
| 174 | + tx.executeSql('SELECT * FROM content', [], (tx: Transaction, results: ResultSet) => { |
| 175 | + const rows = results.rows; |
| 176 | + let data: any[] = []; |
| 177 | + for (let i = 0; i < rows.length; i++) { |
| 178 | + data.push(rows.item(i)); |
| 179 | + } |
| 180 | + console.log('Fetched data:', data); |
| 181 | + setDbData(data); |
| 182 | + }); |
| 183 | + }); |
| 184 | + }; |
| 185 | + |
12 | 186 |
|
13 | 187 | const requestPermissions = async () => { |
14 | 188 | if (Platform.OS === 'android') { |
@@ -77,8 +251,6 @@ const App = () => { |
77 | 251 | setIsCalendarVisible(true); |
78 | 252 | }; |
79 | 253 |
|
80 | | - |
81 | | - |
82 | 254 | const onDayPress = (day: DateData) => { |
83 | 255 | setSelectedDate(day.dateString); |
84 | 256 | setIsCalendarVisible(false); |
@@ -113,37 +285,41 @@ const App = () => { |
113 | 285 | onChangeText={setInputText} |
114 | 286 | /> |
115 | 287 |
|
116 | | -{/* Full screen Modal */} |
117 | | -<Modal presentationStyle='fullScreen' |
118 | | -visible={isCalendarVisible} |
119 | | -animationType='slide' |
120 | | -onRequestClose={() => setIsCalendarVisible(false)} |
121 | | -> |
122 | | - |
123 | | -<Calendar |
124 | | - onDayPress={onDayPress} |
125 | | - markedDates={{ |
126 | | - [selectedDate]: { selected: true, marked: true, selectedColor: 'blue' }, |
127 | | - }} |
128 | | - theme={{ |
129 | | - 'stylesheet.calendar.main': { |
130 | | - base: { |
131 | | - width: '100%', |
132 | | - height: '100%', |
133 | | - justifyContent: 'center', |
134 | | - alignItems: 'center', |
135 | | - }, |
136 | | - }, |
137 | | - }} |
138 | | -/> |
139 | | - |
140 | | - |
141 | | -</Modal> |
| 288 | + <Modal |
| 289 | + presentationStyle="fullScreen" |
| 290 | + visible={isCalendarVisible} |
| 291 | + animationType="slide" |
| 292 | + onRequestClose={() => setIsCalendarVisible(false)} |
| 293 | + > |
| 294 | + <Calendar |
| 295 | + onDayPress={onDayPress} |
| 296 | + markedDates={{ |
| 297 | + [selectedDate]: { selected: true, marked: true, selectedColor: 'blue' }, |
| 298 | + }} |
| 299 | + theme={{ |
| 300 | + 'stylesheet.calendar.main': { |
| 301 | + base: { |
| 302 | + width: '100%', |
| 303 | + height: '100%', |
| 304 | + justifyContent: 'center', |
| 305 | + alignItems: 'center', |
| 306 | + }, |
| 307 | + }, |
| 308 | + }} |
| 309 | + /> |
| 310 | + </Modal> |
142 | 311 |
|
| 312 | + <View> |
| 313 | + <Text style={styles.title}>Database Data:</Text> |
| 314 | + {dbData.map((item, index) => ( |
| 315 | + <Text key={index} style={styles.dbText}> |
| 316 | + {JSON.stringify(item)} |
| 317 | + </Text> |
| 318 | + ))} |
| 319 | + </View> |
143 | 320 | </View> |
144 | 321 | ); |
145 | 322 | }; |
146 | | - |
147 | 323 | const styles = StyleSheet.create({ |
148 | 324 | fullScreenModal: { |
149 | 325 | flex: 1, |
@@ -196,6 +372,10 @@ const styles = StyleSheet.create({ |
196 | 372 | alignItems: 'center', |
197 | 373 | backgroundColor: 'rgba(0,0,0,0.5)', |
198 | 374 | }, |
| 375 | + dbText: { |
| 376 | + color: 'white', |
| 377 | + }, |
199 | 378 | }); |
200 | 379 |
|
| 380 | + |
201 | 381 | export default App; |
0 commit comments