Skip to content

Commit

Permalink
Merge pull request #12 from michaelfromyeg/architecture
Browse files Browse the repository at this point in the history
Architecture
  • Loading branch information
oltimaloku authored Feb 3, 2024
2 parents 3c11c35 + 71e4f54 commit 5ecc1de
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 165 deletions.
2 changes: 1 addition & 1 deletion client/lib/common/custom_toast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CustomToast {
context: context,
builder: ((context) {
return PhotoPrompt(
geoSphereId: geoSphere.geoSphereId,
geosphere: geoSphere,
);
}));
},
Expand Down
6 changes: 3 additions & 3 deletions client/lib/features/auth/services/auth_service.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:georeal/global_variables.dart';
import 'package:georeal/constants/env_variables.dart';
import 'package:http/http.dart' as http;

class AuthService {
static Future<void> login(String email, String password) async {
var uri = Uri.parse('${GlobalVariables.URI}/auth/login');
var uri = Uri.parse('${EnvVariables.uri}/auth/login');
var response = await http.post(
uri,
body: {
Expand All @@ -21,7 +21,7 @@ class AuthService {

static Future<void> register(
String name, String email, String password) async {
var uri = Uri.parse('${GlobalVariables.URI}/auth/register');
var uri = Uri.parse('${EnvVariables.uri}/auth/register');
var response = await http.post(
uri,
body: {
Expand Down
23 changes: 1 addition & 22 deletions client/lib/features/gallery/services/gallery_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,10 @@ import 'dart:io';
import 'package:georeal/constants/env_variables.dart';
import 'package:http/http.dart' as http;

import '../../../models/gallery_model.dart';

/// Handles http requests for the Gallery
class GalleryService {
final Map<String, Gallery> _galleries = {};

void createGalleryForGeoSphere(String geoSphereId) {
_galleries[geoSphereId] = Gallery(id: geoSphereId);
}

void addPhotoToGallery(String geoSphereId, String photoPath) {
final gallery = _galleries[geoSphereId];
if (gallery != null) {
gallery.photoPaths.add(photoPath);
} else {
print("Gallery with this id does not exist!");
}
}

Future<void> uploadPhoto(String geoSphereId, File photo) async {
static Future<void> uploadPhoto(String geoSphereId, File photo) async {
var uri = Uri.parse('${EnvVariables.uri}/geofences');

var request = http.MultipartRequest('POST', uri)
Expand All @@ -42,8 +25,4 @@ class GalleryService {
throw Exception("Error occurred: $e");
}
}

List<String> getPhotosFromGallery(String geoSphereId) {
return _galleries[geoSphereId]?.photoPaths ?? [];
}
}
31 changes: 31 additions & 0 deletions client/lib/features/gallery/view_model/gallery_view_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/material.dart';

import '../../../models/gallery_model.dart';

class GalleryViewModel extends ChangeNotifier {
final Map<String, Gallery> _galleries = {};

get galleries => _galleries;

void createGallery(String geoSphereId) {
_galleries[geoSphereId] = Gallery(id: geoSphereId);
notifyListeners();
}

void addPhotoToGallery(String geoSphereId, String photoPath) {
final gallery = _galleries[geoSphereId];
if (gallery != null) {
gallery.photoPaths.add(photoPath);
notifyListeners();
} else {
Gallery newGallery = Gallery(id: geoSphereId);
newGallery.photoPaths.add(photoPath);
_galleries[geoSphereId] = newGallery;
notifyListeners();
}
}

List<String> getPhotosFromGallery(String geoSphereId) {
return _galleries[geoSphereId]?.photoPaths ?? [];
}
}
7 changes: 3 additions & 4 deletions client/lib/features/gallery/views/geo_sphere_gallery.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:georeal/features/gallery/view_model/gallery_view_model.dart';
import 'package:georeal/features/gallery/widgets/gallery_navbar.dart';
import 'package:georeal/models/geo_sphere_model.dart';
import 'package:provider/provider.dart';

import '../services/gallery_service.dart';

/// Gallery view for a specific GeoSphere
class GeoSphereGallery extends StatelessWidget {
Expand All @@ -19,9 +18,9 @@ class GeoSphereGallery extends StatelessWidget {

@override
Widget build(BuildContext context) {
final galleryService = Provider.of<GalleryService>(context, listen: false);
final galleryViewModel = context.watch<GalleryViewModel>();
List<String> photoPaths =
galleryService.getPhotosFromGallery(geoSphere.geoSphereId);
galleryViewModel.getPhotosFromGallery(geoSphere.geoSphereId);

return Scaffold(
body: SafeArea(
Expand Down
33 changes: 22 additions & 11 deletions client/lib/features/gallery/widgets/photo_prompt.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import 'dart:developer';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:georeal/features/gallery/view_model/gallery_view_model.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';

import '../../gallery/services/gallery_service.dart';
import '../../../models/geo_sphere_model.dart';
import '../views/geo_sphere_gallery.dart';

/// Prompt to add a photo to a GeoSphere
class PhotoPrompt extends StatefulWidget {
final String geoSphereId;
const PhotoPrompt({super.key, required this.geoSphereId});
final GeoSphere geosphere;
const PhotoPrompt({super.key, required this.geosphere});

@override
State<PhotoPrompt> createState() => _PhotoPromptState();
Expand All @@ -21,50 +24,58 @@ class PhotoPrompt extends StatefulWidget {
class _PhotoPromptState extends State<PhotoPrompt> {
File? image;

Future pickImage(ImageSource source) async {
Future pickImage(
ImageSource source, GalleryViewModel galleryViewModel) async {
try {
final image = await ImagePicker().pickImage(source: source);
if (image == null) return;

final imageTemporary = File(image.path);
this.image = imageTemporary;

log("beofre");
// Save the image path to the gallery
Provider.of<GalleryService>(context, listen: false)
.addPhotoToGallery(widget.geoSphereId, image.path);

galleryViewModel.addPhotoToGallery(
widget.geosphere.geoSphereId, image.path);
log("after");
/*
Provider.of<GalleryService>(context, listen: false)
.uploadPhoto(widget.geoSphereId, imageTemporary);*/
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => GeoSphereGallery(geoSphere: widget.geosphere),
),
);
} on PlatformException catch (e) {
print("Failed to pick image: $e");
}
}

@override
Widget build(BuildContext context) {
final galleryViewModel =
Provider.of<GalleryViewModel>(context, listen: false);
return AlertDialog(
title: const Text("Add a photo!"),
content: SizedBox(
height: 200,
child: Column(
children: [
ElevatedButton(
onPressed: () => pickImage(ImageSource.gallery),
onPressed: () => pickImage(ImageSource.gallery, galleryViewModel),
child: const Text("Pick Gallery"),
),
ElevatedButton(
onPressed: () async {
if (Platform.isAndroid) {
var status = await Permission.camera.request();
if (status.isGranted) {
pickImage(ImageSource.camera);
pickImage(ImageSource.camera, galleryViewModel);
} else {
print("bruh");
}
} else if (Platform.isIOS) {
// TODO: Implement iOS camera permission
pickImage(ImageSource.camera);
pickImage(ImageSource.camera, galleryViewModel);
}
},
child: const Text("Pick Camera"),
Expand Down
40 changes: 4 additions & 36 deletions client/lib/features/geo_sphere/services/geo_sphere_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,19 @@ import 'package:georeal/constants/env_variables.dart';
import 'package:http/http.dart' as http;

import '../../../models/geo_sphere_model.dart';
import '../../gallery/services/gallery_service.dart';

/// handles http requests for the GeoSpheres
class GeoSphereService {
final GalleryService _galleryService;
final List<GeoSphere> _geoSpheres = [];

GeoSphereService(this._galleryService) {
getAllGeoSpheres();
}

List<GeoSphere> get geoSpheres => _geoSpheres;

void createGeoSphere(
double latitude, double longitude, double radius, String name) {
var newGeoSphere = GeoSphere(
latitude: latitude,
longitude: longitude,
radiusInMeters: radius,
name: name,
);

_geoSpheres.add(newGeoSphere);
postGeoSphere(geoSphere: newGeoSphere);

_galleryService.createGalleryForGeoSphere(newGeoSphere.geoSphereId);
}

void postGeoSphere({
static void createGeoSphere({
required GeoSphere geoSphere,
}) async {
try {
log(geoSphere.geoSphereId);
log(geoSphere.toGeoJsonString());

http.Response res = await http.post(

Uri.parse('${EnvVariables.uri}/geofences'),

body: json
.encode({'name': geoSphere.name, 'geojson': geoSphere.toGeoJson()}),
headers: <String, String>{
Expand All @@ -63,20 +36,15 @@ class GeoSphereService {
}
}

Future<void> getAllGeoSpheres() async {
static Future<void> getAllGeoSpheres() async {
try {


var response = await http.get(Uri.parse('${EnvVariables.uri}/geofences'));


if (response.statusCode == 200) {
List<dynamic> geofencesData = json.decode(response.body);
// Process the data
// Assuming GeoSphere.fromMap() is a constructor that creates a GeoSphere from a Map
_geoSpheres.clear();

for (var geofenceData in geofencesData) {
_geoSpheres.add(GeoSphere.fromMap(geofenceData));
// TODO: handle geosjson data
}
} else {
print('Request failed with status: ${response.statusCode}.');
Expand Down
Loading

0 comments on commit 5ecc1de

Please sign in to comment.