Browse Source

Finishing an MVP from the SharingFloodZone screen with integration on MapFeedScreen.

master
GabrielTrettel 4 years ago
parent
commit
4867999dbb
  1. 36
      src/app/database/databaseLoader.js
  2. 296
      src/app/database/db.js
  3. 79
      src/app/screens/MapFeedScreen.js
  4. 18
      src/app/screens/SharingFloodZonesScreen.js

36
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;

296
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, Id integer PRIMARY KEY autoincrement,
Response text NOT NULL,
ShortResponse text NOT NULL,
Description 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; export default init_queries;

79
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 { StyleSheet, View } from "react-native";
import MapView, { Marker } from "react-native-maps"; import MapView, { Marker } from "react-native-maps";
import colors from "../config/colors"; import colors from "../config/colors";
import "../config/globals";
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"],
});
}
return warnings;
}
function MapFeedScreen(props) { function MapFeedScreen(props) {
const [warnings, setWarnings] = useState([]);
useEffect(() => {
global.userDataBase.transaction((tx) => {
tx.executeSql("SELECT * FROM FloodZones;", [], (tx, results) => {
setWarnings(parseResult(results));
});
});
}, []);
return ( return (
<View style={styles.container}> <View style={styles.container}>
<MapView <MapView
style={styles.mapStyle} style={styles.mapStyle}
initialRegion={{ initialRegion={{
latitude: -23.657090,
longitude: -46.699260,
// TODO: make this flexible
latitude: -22.1070263,
longitude: -51.3948396,
latitudeDelta: 0.0922, latitudeDelta: 0.0922,
longitudeDelta: 0.0421, longitudeDelta: 0.0421,
}}>
<Marker
coordinate={{ latitude: -23.657090, longitude: -46.699260 }}
image={require("../assets/chuva_peq.png")}
/>
<Marker
coordinate={{ latitude: -23.656282, longitude: -46.682768 }}
image={require("../assets/chuva_peq.png")}
/>
<Marker
coordinate={{ latitude: -23.666712, longitude: -46.687650 }}
image={require("../assets/chuva_peq.png")}
/>
<Marker
coordinate={{ latitude: -23.660848, longitude: -46.704396 }}
image={require("../assets/chuva_peq.png")}
/>
<Marker
coordinate={{ latitude: -23.634700, longitude: -46.721960 }}
image={require("../assets/pontos_alagamento_peq.png")}
/>
<Marker
coordinate={{ latitude: -23.650861, longitude: -46.721775 }}
image={require("../assets/pontos_alagamento_peq.png")}
/>
}}
>
{warnings.map((val) => {
return <MapView.Marker {...val} />;
})}
</MapView> </MapView>
</View> </View>
); );
}
}
const styles = StyleSheet.create({
const styles = StyleSheet.create({
container: { container: {
backgroundColor: colors.black, backgroundColor: colors.black,
flex: 1, flex: 1,
}, },
mapStyle: { mapStyle: {
position: 'absolute',
position: "absolute",
top: 0, top: 0,
left: 0, left: 0,
right: 0, right: 0,
bottom: 0
bottom: 0,
}, },
});
export default MapFeedScreen;
});
export default MapFeedScreen;

18
src/app/screens/SharingFloodZonesScreen.js

@ -9,9 +9,11 @@ import Screen from "../components/Screen";
import useLocation from "../hooks/useLocation"; import useLocation from "../hooks/useLocation";
import colors from "../config/colors"; import colors from "../config/colors";
import { TouchableNativeFeedback } from "react-native-gesture-handler"; 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({ const validationSchema = Yup.object().shape({
@ -25,7 +27,6 @@ const screen_width = Dimensions.get("window").width;
function SharingFloodZonesScreen() { function SharingFloodZonesScreen() {
const [passable, setPassable] = useState(0); const [passable, setPassable] = useState(0);
const [image, setImage] = useState();
const location = useLocation(); const location = useLocation();
return ( return (
@ -35,7 +36,7 @@ function SharingFloodZonesScreen() {
images: [], images: [],
description: "", description: "",
}} }}
onSubmit={(values) => console.log({ ...values, passable, location })}
onSubmit={(values) => submitForm({ ...values, passable, location })}
validationSchema={validationSchema} validationSchema={validationSchema}
> >
<View style={styles.imgs_container}> <View style={styles.imgs_container}>
@ -88,16 +89,15 @@ const styles = StyleSheet.create({
}, },
image: { image: {
width: 150, width: 150,
height: "100%",
resizeMode: "cover",
backgroundColor: "blue",
height: 80,
resizeMode: "contain",
}, },
img_block: { img_block: {
height: 100,
height: 120,
padding: 10, padding: 10,
borderRadius: 5, borderRadius: 5,
borderStyle: "dotted", borderStyle: "dotted",
borderColor: "blue",
borderColor: colors.primary,
justifyContent: "center", justifyContent: "center",
alignItems: "center", alignItems: "center",
}, },

Loading…
Cancel
Save