Skip to content

Commit

Permalink
Added dart files with new login/register pages
Browse files Browse the repository at this point in the history
  • Loading branch information
oltimaloku committed Jan 5, 2024
1 parent a9c3486 commit 8404d1b
Show file tree
Hide file tree
Showing 16 changed files with 946 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dist/
downloads/
eggs/
.eggs/
lib/
#lib/
lib64/
parts/
sdist/
Expand Down
6 changes: 6 additions & 0 deletions flutter_client/lib/constants/global_variables.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ignore: constant_identifier_names
class GlobalVariables {
static const String google_api_key =
'AIzaSyBeghIHy3KuYoSFcZ0rzxFv-H32jNwYesI';
static const String uri = 'http://localhost:3000';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'dart:convert';
import 'dart:developer';

import 'package:http/http.dart' as http;

import '../../../constants/global_variables.dart';
import '../../../models/geo_sphere_model.dart';

class GeoSphereServices {
void createGeoSphere({
required GeoSphere geoSphere,
}) async {
try {
print("geosphere ${geoSphere.toGeoJsonString()}");
http.Response res = await http.post(
Uri.parse('${GlobalVariables.uri}/geofences'),
body: json
.encode({'name': geoSphere.name, 'geojson': geoSphere.toGeoJson()}),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8'
},
);
log("Geo Sphere Successfully logged! ");
} catch (e) {
throw Exception("Error occured: $e");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import 'package:flutter/material.dart';
import 'package:georeal/features/add_geo_sphere/view_model/geo_sphere_view_model.dart';
import 'package:georeal/global_variables.dart';
import 'package:location/location.dart';
import 'package:provider/provider.dart';

class AddGeoSphereModal extends StatefulWidget {
static const routeName = 'addGeoSphere';
const AddGeoSphereModal({super.key});

@override
State<AddGeoSphereModal> createState() => _AddGeoSphereModalState();
}

class _AddGeoSphereModalState extends State<AddGeoSphereModal> {
double sliderValue = 0;
Location location = Location();
TextEditingController nameController = TextEditingController();

@override
Widget build(BuildContext context) {
final viewModel = Provider.of<GeoSphereViewModel>(context);
return SafeArea(
child: Padding(
padding: const EdgeInsets.only(left: 20.0, right: 20),
child: Column(
children: [
Container(
width: 40,
height: 5,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(12.0),
),
margin: const EdgeInsets.only(
top: 5,
bottom:
10.0), // Optional spacing from the rest of the modal content
),
const Text(
"Add a Geo-Sphere!",
style: GlobalVariables.headerStyle,
),
const SizedBox(height: 20),
const Text(
"When you are in the desired location, select a radius and press the button to create your new geo-sphere",
style: GlobalVariables.bodyStyleRegular,
),
const SizedBox(height: 20),
const Text(
"Please select the radius of your geo-sphere in meters:",
),
Slider(
value: sliderValue,
min: 0,
max: 50,
divisions: 20,
onChanged: (p0) {
setState(() {
sliderValue = p0;
});
},
label: "${(sliderValue).toString()} m",
),
const Text("Give your Geo-Sphere a name:"),
TextField(
controller: nameController,
),
Expanded(
child: Align(
alignment: AlignmentDirectional.bottomCenter,
child: SizedBox(
width: MediaQuery.of(context).size.width - 40,
height: 50,
child: ElevatedButton(
onPressed: () async {
await viewModel.setAndCreateGeoSphere(
sliderValue, nameController.text);
Navigator.of(context).pop();
},
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context)
.primaryColor, // this is the background color
foregroundColor: Colors.white, // this is the text color
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
),
child: const Text("Create Geo-Sphere"),
),
),
),
)
],
),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:georeal/features/add_geo_sphere/services/geo_sphere_services.dart';
import 'package:georeal/models/geo_sphere_model.dart';
import 'package:location/location.dart';

class GeoSphereViewModel extends ChangeNotifier {
List<GeoSphere> geoSpheres = [];

void createGeoSphere(
double latitude, double longitude, double radius, String name) {
GeoSphere newGeoSphere = GeoSphere(
latitude: latitude,
longitude: longitude,
radiusInMeters: radius,
name: name,
);
GeoSphereServices geoSphereServices = GeoSphereServices();
geoSphereServices.createGeoSphere(geoSphere: newGeoSphere);
geoSpheres.add(newGeoSphere);

notifyListeners();
}

Future<void> setAndCreateGeoSphere(double radius, String name) async {
Location location = Location();
bool serviceEnabled;
PermissionStatus permissionGranted;
LocationData locationData;
serviceEnabled = await location.serviceEnabled();
if (!serviceEnabled) {
serviceEnabled = await location.requestService();
if (!serviceEnabled) {
return;
}
}

permissionGranted = await location.hasPermission();

if (permissionGranted == PermissionStatus.denied) {
permissionGranted = await location.requestPermission();
if (permissionGranted != PermissionStatus.granted) {
return;
}
}

locationData = await location.getLocation();
double latitude = locationData.latitude!;
double longitude = locationData.longitude!;
createGeoSphere(latitude, longitude, radius, name);
notifyListeners();
}

// Calculates the angular distance between two points on the surface of a sphere
// Haversine (or great circle)
double _haversineDistance(double startLatitude, double startLongitude,
double endLatitude, double endLongitude) {
const earthRadiusInKM = 6371; // Earth radius in kilometers

// Differences in coordinates converted to radians
var deltaLatitudeRadians = _degreesToRadians(endLatitude - startLatitude);
var deltaLongitudeRadians =
_degreesToRadians(endLongitude - startLongitude);

// Convert starting and ending latitudes from degrees to radians
startLatitude = _degreesToRadians(startLatitude);
endLatitude = _degreesToRadians(endLatitude);

// Haversine formula calculation
var haversineOfCentralAngle =
sin(deltaLatitudeRadians / 2) * sin(deltaLatitudeRadians / 2) +
sin(deltaLongitudeRadians / 2) *
sin(deltaLongitudeRadians / 2) *
cos(startLatitude) *
cos(endLatitude);
var centralAngle = 2 *
atan2(sqrt(haversineOfCentralAngle), sqrt(1 - haversineOfCentralAngle));

// Return distance using the Earth's radius
return earthRadiusInKM * centralAngle;
}

double _degreesToRadians(double degrees) {
return degrees * pi / 180;
}

bool isPointInGeoSphere(double pointLat, double pointLon) {
for (GeoSphere geoSphere in geoSpheres) {
double distanceFromCenter = _haversineDistance(
geoSphere.latitude, geoSphere.longitude, pointLat, pointLon);
if (distanceFromCenter <= geoSphere.radiusInMeters) {
return true;
}
}
return false;
}
}
Loading

0 comments on commit 8404d1b

Please sign in to comment.