Skip to content

Commit

Permalink
feat: clicking on user profile shows geo spheres created by that user
Browse files Browse the repository at this point in the history
  • Loading branch information
oltimaloku committed May 11, 2024
1 parent c5d49cb commit 0c2fa98
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 4 deletions.
42 changes: 39 additions & 3 deletions client/lib/features/friends/view/user_profile_screen.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,70 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:flutter/material.dart';
import 'package:georeal/features/friends/widgets/profile_layout.dart';
import 'package:georeal/features/geo_sphere/view_model/geo_sphere_view_model.dart';
import 'package:georeal/features/geo_sphere/widgets/geo_sphere_widget.dart';
import 'package:georeal/global_variables.dart';
import 'package:georeal/models/other_user.dart';
import 'package:georeal/providers/user_provider';
import 'package:provider/provider.dart';

class UserProfileScreen extends StatelessWidget {
class UserProfileScreen extends StatefulWidget {
OtherUser user;
UserProfileScreen({
super.key,
required this.user,
});

@override
State<UserProfileScreen> createState() => _UserProfileScreenState();
}

class _UserProfileScreenState extends State<UserProfileScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
Future.microtask(() =>
Provider.of<GeoSphereViewModel>(context, listen: false)
.fetchUserGeoSpheres(widget.user.id));
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: GlobalVariables.backgroundColor,
title: Text(
user.username,
widget.user.username,
style: Theme.of(context)
.textTheme
.titleMedium
?.copyWith(fontWeight: FontWeight.bold),
),
),
backgroundColor: GlobalVariables.backgroundColor,
body: const SafeArea(
body: SafeArea(
child: Column(
children: [
ProfileLayout(),
Expanded(
child: Consumer<GeoSphereViewModel>(
builder: (context, geoSphereViewModel, child) {
// geoSphereViewModel.fetchUserGeoSpheres(user.id);
return ListView.builder(
itemCount: geoSphereViewModel.selectedUserGeoSpheres.length,
itemBuilder: (context, index) {
var geoSphere =
geoSphereViewModel.selectedUserGeoSpheres[index];
return Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 8),
child: GeoSphereWidget(geoSphere: geoSphere),
);
},
);
},
),
),
],
),
),
Expand Down
21 changes: 21 additions & 0 deletions client/lib/features/geo_sphere/services/geo_sphere_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ class GeoSphereService {
}
}

static Future<List<GeoSphere>?> getGeoSpheresByUserId(int userId) async {
try {
var response =
await http.get(Uri.parse('${EnvVariables.uri}/geofences/$userId'));

if (response.statusCode == 200) {
List<dynamic> geofencesData = json.decode(response.body);
List<GeoSphere> geoSpheres = [];
for (var geofenceData in geofencesData) {
geoSpheres.add(GeoSphere.fromJson(geofenceData));
}
return geoSpheres;
} else {
throw Exception(
'Failed to get geospheres. Status code: ${response.statusCode}');
}
} catch (e) {
throw Exception("Error occurred: $e");
}
}

static Future<void> deleteGeoSphere(int id) async {
log('Deleting geosphere with id: $id');
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ class GeoSphereViewModel extends ChangeNotifier {

final LocationViewModel _locationViewModel;
final List<GeoSphere> _geoSpheres = [];
List<GeoSphere> _selectedUserGeoSpheres = [];

GeoSphereViewModel(this._locationViewModel) {
fetchGeoSpheres();
}

List<GeoSphere> get geoSpheres => _geoSpheres;
List<GeoSphere> get selectedUserGeoSpheres => _selectedUserGeoSpheres;

Future<void> fetchGeoSpheres() async {
List<GeoSphere>? geoSpheres = await GeoSphereService.getAllGeoSpheres();
Expand All @@ -34,6 +36,15 @@ class GeoSphereViewModel extends ChangeNotifier {
}
}

Future<void> fetchUserGeoSpheres(int userID) async {
List<GeoSphere>? userGeoSpheres =
await GeoSphereService.getGeoSpheresByUserId(userID);
if (userGeoSpheres != null) {
_selectedUserGeoSpheres = userGeoSpheres;
notifyListeners();
}
}

Future<void> setAndCreateGeoSphere(
double radius, String name, int userID) async {
LocationData? locationData = _locationViewModel.currentLocation;
Expand Down
37 changes: 36 additions & 1 deletion client/lib/features/profile/views/profile_screen.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import 'package:flutter/material.dart';
import 'package:georeal/common/profile_photo.dart';
import 'package:georeal/features/geo_sphere/view_model/geo_sphere_view_model.dart';
import 'package:georeal/features/geo_sphere/widgets/geo_sphere_widget.dart';
import 'package:georeal/features/profile/views/friend_request_screen.dart';
import 'package:georeal/global_variables.dart';
import 'package:georeal/providers/user_provider';
import 'package:provider/provider.dart';

class ProfileScreen extends StatelessWidget {
class ProfileScreen extends StatefulWidget {
const ProfileScreen({super.key});

@override
State<ProfileScreen> createState() => _ProfileScreenState();
}

class _ProfileScreenState extends State<ProfileScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
Future.microtask(() =>
Provider.of<GeoSphereViewModel>(context, listen: false)
.fetchUserGeoSpheres(
Provider.of<UserProvider>(context, listen: false).user!.id));
}

@override
Widget build(BuildContext context) {
final user = Provider.of<UserProvider>(context, listen: false).user!;
Expand Down Expand Up @@ -102,6 +119,24 @@ class ProfileScreen extends StatelessWidget {
),
),
const Divider(),
Expanded(
child: Consumer<GeoSphereViewModel>(
builder: (context, geoSphereViewModel, child) {
// geoSphereViewModel.fetchUserGeoSpheres(user.id);
return ListView.builder(
itemCount: geoSphereViewModel.selectedUserGeoSpheres.length,
itemBuilder: (context, index) {
var geoSphere =
geoSphereViewModel.selectedUserGeoSpheres[index];
return Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 8),
child: GeoSphereWidget(geoSphere: geoSphere),
);
},
);
},
),
),
],
),
),
Expand Down
16 changes: 16 additions & 0 deletions georeal/routes/geofences.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ def get_geofences() -> tuple[Response, int]:
'radius': g.radius,
'creator_id': g.creator_id,
} for g in geofences]), 200

@geofences.route("/geofences/<int:user_id>", methods=["GET"])
def get_geofences_by_user(user_id: int) -> tuple[Response, int]:
"""
Get all regions that are geofenced by a specific user.
"""
geofences = Geofence.query.filter_by(creator_id=user_id).all()
return jsonify([{
'id': g.id,
'name': g.name,
'latitude': g.latitude,
'longitude': g.longitude,
'radius': g.radius,
'creator_id': g.creator_id,
} for g in geofences]), 200


@geofences.route("/geofences", methods=["POST"])
def create_geofence():
Expand Down

0 comments on commit 0c2fa98

Please sign in to comment.