From 4867999dbb5472bc586f5966916c3e514427130c Mon Sep 17 00:00:00 2001 From: GabrielTrettel Date: Thu, 10 Dec 2020 19:52:53 -0300 Subject: [PATCH] Finishing an MVP from the SharingFloodZone screen with integration on MapFeedScreen. --- src/app/database/databaseLoader.js | 36 +++ src/app/database/db.js | 296 +++++++++++---------- src/app/screens/MapFeedScreen.js | 121 +++++---- src/app/screens/SharingFloodZonesScreen.js | 18 +- 4 files changed, 263 insertions(+), 208 deletions(-) create mode 100644 src/app/database/databaseLoader.js diff --git a/src/app/database/databaseLoader.js b/src/app/database/databaseLoader.js new file mode 100644 index 0000000..f2c1ab6 --- /dev/null +++ b/src/app/database/databaseLoader.js @@ -0,0 +1,36 @@ +import "../config/globals"; + +function transaction(db, query, values) { + db.transaction((tx) => { + tx.executeSql( + query, + values, + (_, results) => { + console.debug("Values inserted successfully" + results.rowsAffected); + }, + (_, err) => { + console.debug("Error while inserting values: " + JSON.stringify(err)); + } + ); + }); +} + +function insertFloodZone({ images, description, passable, location }) { + const query = `INSERT INTO FloodZones(Description, Images, Latitude, Longitude, Passable) VALUES(?, ?, ?, ?, ?);`; + if (location === undefined) { + console.debug("undefined location"); + return; + } + + const values = [ + description, + JSON.stringify(images), + parseFloat(location["latitude"]), + parseFloat(location["longitude"]), + parseInt(passable), + ]; + + transaction(global.userDataBase, query, values); +} + +export default insertFloodZone; diff --git a/src/app/database/db.js b/src/app/database/db.js index 651b68d..56420a9 100644 --- a/src/app/database/db.js +++ b/src/app/database/db.js @@ -1,149 +1,161 @@ -const init_queries = [ - `CREATE TABLE IF NOT EXISTS Users ( - Id integer PRIMARY KEY autoincrement, - Email text NOT NULL, - FirstName text NOT NULL, - SurName text NOT NULL, - Avatar text NOT NULL, - Active integer NOT NULL - );`, - - `CREATE TABLE IF NOT EXISTS Profiles ( - Id integer PRIMARY KEY autoincrement, - Name text NOT NULL, - Active integer NOT NULL - );`, - - `CREATE TABLE IF NOT EXISTS UsersProfiles ( - Id integer PRIMARY KEY autoincrement, - IdUsers integer NOT NULL, - IdProfiles integer NOT NULL, - Active integer NOT NULL, - FOREIGN KEY (IdUsers) REFERENCES Users (Id), - FOREIGN KEY (IdProfiles) REFERENCES Profiles (Id) - );`, +// FIXME: Refactor to our database ERM already combined - `CREATE TABLE IF NOT EXISTS Permissions ( - Id integer PRIMARY KEY autoincrement, - Name text NOT NULL, - Active integer NOT NULL - );`, - - `CREATE TABLE IF NOT EXISTS ProfilesPermissions ( - Id integer PRIMARY KEY autoincrement, - IdProfiles integer NOT NULL, - IdPermissions integer NOT NULL, - Active integer NOT NULL, - FOREIGN KEY (IdProfiles) REFERENCES Profiles (Id), - FOREIGN KEY (IdPermissions) REFERENCES Permissions (Id) - );`, - - `CREATE TABLE IF NOT EXISTS FormsOrigins ( - Id integer PRIMARY KEY autoincrement, - Name text NOT NULL, - Active integer NOT NULL - );`, - - `CREATE TABLE IF NOT EXISTS Forms ( - Id integer PRIMARY KEY autoincrement, - IdFormsOrigins integer NOT NULL, - Name text NOT NULL, - Description text NOT NULL, - DtCreation TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - Active integer NOT NULL, - FOREIGN KEY (IdFormsOrigins) REFERENCES FormsOrigins (Id) - );`, - - `CREATE TABLE IF NOT EXISTS FieldsDataTypes ( - Id integer PRIMARY KEY autoincrement, - Name text NOT NULL, - Description text NOT NULL, - Active integer NOT NULL - );`, - - `CREATE TABLE IF NOT EXISTS Fields ( - Id integer PRIMARY KEY autoincrement, - IdFieldsDataTypes integer NOT NULL, - Name text NOT NULL, - Description text NOT NULL, - FillingClue text NOT NULL, - Active integer NOT NULL, - FOREIGN KEY (IdFieldsDataTypes) REFERENCES FieldsDataTypes (Id) - );`, - - `CREATE TABLE IF NOT EXISTS FormsFields ( - Id integer PRIMARY KEY autoincrement, - IdForms integer NOT NULL, - IdFields integer NOT NULL, - Active integer NOT NULL, - FOREIGN KEY (IdForms) REFERENCES Forms (Id), - FOREIGN KEY (IdFields) REFERENCES Fields (Id) - );`, - - `CREATE TABLE IF NOT EXISTS Alternatives ( +const init_queries = [ + `CREATE TABLE IF NOT EXISTS FloodZones ( Id integer PRIMARY KEY autoincrement, - Response text NOT NULL, - ShortResponse text NOT NULL, Description text NOT NULL, - Active integer NOT NULL - );`, - - `CREATE TABLE IF NOT EXISTS FieldsAlternatives ( - Id integer PRIMARY KEY autoincrement, - IdFields integer NOT NULL, - IdAlternatives integer NOT NULL, - Active integer NOT NULL, - FOREIGN KEY (IdFields) REFERENCES Fields (Id), - FOREIGN KEY (IdAlternatives) REFERENCES Alternatives (Id) - );`, - - `CREATE TABLE IF NOT EXISTS FieldsAnswers ( - Id integer PRIMARY KEY autoincrement, - IdFields integer NOT NULL, - Value text NOT NULL, - DtFilling TIMESTAMP NOT NULL, - Active integer NOT NULL, - FOREIGN KEY (IdFields) REFERENCES Fields (Id) - );`, - - `CREATE TABLE IF NOT EXISTS UsersInformerFieldsAnswers ( - Id integer PRIMARY KEY autoincrement, - IdUsersInformer integer NOT NULL, - IdFieldsAnswers integer NOT NULL, - Latitude real NULL, - Longitude real NULL, - Active integer NOT NULL, - FOREIGN KEY (IdUsersInformer) REFERENCES Users (Id), - FOREIGN KEY (IdFieldsAnswers) REFERENCES FieldsAnswers (Id) - );`, - - `CREATE TABLE IF NOT EXISTS UsersEndorsementFieldsAnswers ( - Id integer PRIMARY KEY autoincrement, - IdUsersEndorsement integer NOT NULL, - IdFieldsAnswers integer NOT NULL, - Latitude real NULL, - Longitude real NULL, - IsTrustable integer NOT NULL, - Active integer NOT NULL, - FOREIGN KEY (IdUsersEndorsement) REFERENCES Users (Id), - FOREIGN KEY (IdFieldsAnswers) REFERENCES FieldsAnswers (Id) - );`, - - `CREATE TABLE IF NOT EXISTS PreliminaryData ( - Id integer PRIMARY KEY autoincrement, - IdFieldsAnswers integer NOT NULL, - DtInsert TIMESTAMP NOT NULL, - Active integer NOT NULL, - FOREIGN KEY (IdFieldsAnswers) REFERENCES FieldsAnswers (Id) - );`, - - `CREATE TABLE IF NOT EXISTS TrustedData ( - Id integer PRIMARY KEY autoincrement, - IdFieldsAnswers integer NOT NULL, - DtInsert TIMESTAMP NOT NULL, - Active integer NOT NULL, - FOREIGN KEY (IdFieldsAnswers) REFERENCES FieldsAnswers (Id) + Images text NOT NULL, + Latitude real NOT NULL, + Longitude real NOT NULL, + Passable INTERGER NOT NULL );`, ]; +// `CREATE TABLE IF NOT EXISTS Users ( +// Id integer PRIMARY KEY autoincrement, +// Email text NOT NULL, +// FirstName text NOT NULL, +// SurName text NOT NULL, +// Avatar text NOT NULL, +// Active integer NOT NULL +// );`, + +// `CREATE TABLE IF NOT EXISTS Profiles ( +// Id integer PRIMARY KEY autoincrement, +// Name text NOT NULL, +// Active integer NOT NULL +// );`, + +// `CREATE TABLE IF NOT EXISTS UsersProfiles ( +// Id integer PRIMARY KEY autoincrement, +// IdUsers integer NOT NULL, +// IdProfiles integer NOT NULL, +// Active integer NOT NULL, +// FOREIGN KEY (IdUsers) REFERENCES Users (Id), +// FOREIGN KEY (IdProfiles) REFERENCES Profiles (Id) +// );`, + +// `CREATE TABLE IF NOT EXISTS Permissions ( +// Id integer PRIMARY KEY autoincrement, +// Name text NOT NULL, +// Active integer NOT NULL +// );`, + +// `CREATE TABLE IF NOT EXISTS ProfilesPermissions ( +// Id integer PRIMARY KEY autoincrement, +// IdProfiles integer NOT NULL, +// IdPermissions integer NOT NULL, +// Active integer NOT NULL, +// FOREIGN KEY (IdProfiles) REFERENCES Profiles (Id), +// FOREIGN KEY (IdPermissions) REFERENCES Permissions (Id) +// );`, + +// `CREATE TABLE IF NOT EXISTS FormsOrigins ( +// Id integer PRIMARY KEY autoincrement, +// Name text NOT NULL, +// Active integer NOT NULL +// );`, + +// `CREATE TABLE IF NOT EXISTS Forms ( +// Id integer PRIMARY KEY autoincrement, +// IdFormsOrigins integer NOT NULL, +// Name text NOT NULL, +// Description text NOT NULL, +// DtCreation TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, +// Active integer NOT NULL, +// FOREIGN KEY (IdFormsOrigins) REFERENCES FormsOrigins (Id) +// );`, + +// `CREATE TABLE IF NOT EXISTS FieldsDataTypes ( +// Id integer PRIMARY KEY autoincrement, +// Name text NOT NULL, +// Description text NOT NULL, +// Active integer NOT NULL +// );`, + +// `CREATE TABLE IF NOT EXISTS Fields ( +// Id integer PRIMARY KEY autoincrement, +// IdFieldsDataTypes integer NOT NULL, +// Name text NOT NULL, +// Description text NOT NULL, +// FillingClue text NOT NULL, +// Active integer NOT NULL, +// FOREIGN KEY (IdFieldsDataTypes) REFERENCES FieldsDataTypes (Id) +// );`, + +// `CREATE TABLE IF NOT EXISTS FormsFields ( +// Id integer PRIMARY KEY autoincrement, +// IdForms integer NOT NULL, +// IdFields integer NOT NULL, +// Active integer NOT NULL, +// FOREIGN KEY (IdForms) REFERENCES Forms (Id), +// FOREIGN KEY (IdFields) REFERENCES Fields (Id) +// );`, + +// `CREATE TABLE IF NOT EXISTS Alternatives ( +// Id integer PRIMARY KEY autoincrement, +// Response text NOT NULL, +// ShortResponse text NOT NULL, +// Description text NOT NULL, +// Active integer NOT NULL +// );`, + +// `CREATE TABLE IF NOT EXISTS FieldsAlternatives ( +// Id integer PRIMARY KEY autoincrement, +// IdFields integer NOT NULL, +// IdAlternatives integer NOT NULL, +// Active integer NOT NULL, +// FOREIGN KEY (IdFields) REFERENCES Fields (Id), +// FOREIGN KEY (IdAlternatives) REFERENCES Alternatives (Id) +// );`, + +// `CREATE TABLE IF NOT EXISTS FieldsAnswers ( +// Id integer PRIMARY KEY autoincrement, +// IdFields integer NOT NULL, +// Value text NOT NULL, +// DtFilling TIMESTAMP NOT NULL, +// Active integer NOT NULL, +// FOREIGN KEY (IdFields) REFERENCES Fields (Id) +// );`, + +// `CREATE TABLE IF NOT EXISTS UsersInformerFieldsAnswers ( +// Id integer PRIMARY KEY autoincrement, +// IdUsersInformer integer NOT NULL, +// IdFieldsAnswers integer NOT NULL, +// Latitude real NULL, +// Longitude real NULL, +// Active integer NOT NULL, +// FOREIGN KEY (IdUsersInformer) REFERENCES Users (Id), +// FOREIGN KEY (IdFieldsAnswers) REFERENCES FieldsAnswers (Id) +// );`, + +// `CREATE TABLE IF NOT EXISTS UsersEndorsementFieldsAnswers ( +// Id integer PRIMARY KEY autoincrement, +// IdUsersEndorsement integer NOT NULL, +// IdFieldsAnswers integer NOT NULL, +// Latitude real NULL, +// Longitude real NULL, +// IsTrustable integer NOT NULL, +// Active integer NOT NULL, +// FOREIGN KEY (IdUsersEndorsement) REFERENCES Users (Id), +// FOREIGN KEY (IdFieldsAnswers) REFERENCES FieldsAnswers (Id) +// );`, + +// `CREATE TABLE IF NOT EXISTS PreliminaryData ( +// Id integer PRIMARY KEY autoincrement, +// IdFieldsAnswers integer NOT NULL, +// DtInsert TIMESTAMP NOT NULL, +// Active integer NOT NULL, +// FOREIGN KEY (IdFieldsAnswers) REFERENCES FieldsAnswers (Id) +// );`, + +// `CREATE TABLE IF NOT EXISTS TrustedData ( +// Id integer PRIMARY KEY autoincrement, +// IdFieldsAnswers integer NOT NULL, +// DtInsert TIMESTAMP NOT NULL, +// Active integer NOT NULL, +// FOREIGN KEY (IdFieldsAnswers) REFERENCES FieldsAnswers (Id) +// );`, +// ]; + export default init_queries; diff --git a/src/app/screens/MapFeedScreen.js b/src/app/screens/MapFeedScreen.js index 2c91c1a..0b299bc 100644 --- a/src/app/screens/MapFeedScreen.js +++ b/src/app/screens/MapFeedScreen.js @@ -1,63 +1,70 @@ -import React from "react"; +import React, { useState, useEffect } from "react"; import { StyleSheet, View } from "react-native"; import MapView, { Marker } from "react-native-maps"; import colors from "../config/colors"; +import "../config/globals"; -function MapFeedScreen(props) { - return ( - - - - - - - - - - - - ); +function parseResult(db_result) { + var warnings = []; + + for (let i = 0; i < db_result.rows.length; ++i) { + var row = db_result.rows.item(i); + warnings.push({ + key: row["Id"], + title: row["Passable"] == 0 ? "Transponível" : "Intransponível", + coordinate: { latitude: row["Latitude"], longitude: row["Longitude"] }, + image: require("../assets/pontos_alagamento_peq.png"), + description: row["Description"], + }); } - - const styles = StyleSheet.create({ - container: { - backgroundColor: colors.black, - flex: 1, - }, - mapStyle: { - position: 'absolute', - top: 0, - left: 0, - right: 0, - bottom: 0 - }, - }); - - export default MapFeedScreen; - \ No newline at end of file + + return warnings; +} + +function MapFeedScreen(props) { + const [warnings, setWarnings] = useState([]); + + useEffect(() => { + global.userDataBase.transaction((tx) => { + tx.executeSql("SELECT * FROM FloodZones;", [], (tx, results) => { + setWarnings(parseResult(results)); + }); + }); + }, []); + + return ( + + + {warnings.map((val) => { + return ; + })} + + + ); +} + +const styles = StyleSheet.create({ + container: { + backgroundColor: colors.black, + flex: 1, + }, + mapStyle: { + position: "absolute", + top: 0, + left: 0, + right: 0, + bottom: 0, + }, +}); + +export default MapFeedScreen; diff --git a/src/app/screens/SharingFloodZonesScreen.js b/src/app/screens/SharingFloodZonesScreen.js index 51b4887..b1d0974 100644 --- a/src/app/screens/SharingFloodZonesScreen.js +++ b/src/app/screens/SharingFloodZonesScreen.js @@ -9,9 +9,11 @@ import Screen from "../components/Screen"; import useLocation from "../hooks/useLocation"; import colors from "../config/colors"; import { TouchableNativeFeedback } from "react-native-gesture-handler"; +import insertFloodZone from "../database/databaseLoader"; -function submitForm(passable, image) { - console.log(passable, image); +function submitForm(props) { + // console.log(props); + insertFloodZone(props); } const validationSchema = Yup.object().shape({ @@ -25,7 +27,6 @@ const screen_width = Dimensions.get("window").width; function SharingFloodZonesScreen() { const [passable, setPassable] = useState(0); - const [image, setImage] = useState(); const location = useLocation(); return ( @@ -35,7 +36,7 @@ function SharingFloodZonesScreen() { images: [], description: "", }} - onSubmit={(values) => console.log({ ...values, passable, location })} + onSubmit={(values) => submitForm({ ...values, passable, location })} validationSchema={validationSchema} > @@ -88,16 +89,15 @@ const styles = StyleSheet.create({ }, image: { width: 150, - height: "100%", - resizeMode: "cover", - backgroundColor: "blue", + height: 80, + resizeMode: "contain", }, img_block: { - height: 100, + height: 120, padding: 10, borderRadius: 5, borderStyle: "dotted", - borderColor: "blue", + borderColor: colors.primary, justifyContent: "center", alignItems: "center", },