Skip to content

Commit

Permalink
monument check-in functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmohsin7 committed Aug 15, 2024
1 parent bba72c0 commit 8faf587
Show file tree
Hide file tree
Showing 18 changed files with 564 additions and 96 deletions.
Binary file added assets/desktop/checkedin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/desktop/checkin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import 'dart:math';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:geolocator/geolocator.dart';
import 'package:monumento/domain/entities/monument_entity.dart';
import 'package:monumento/domain/repositories/social_repository.dart';

part 'monument_checkin_event.dart';
part 'monument_checkin_state.dart';

class MonumentCheckinBloc
extends Bloc<MonumentCheckinEvent, MonumentCheckinState> {
final SocialRepository _socialRepository;
MonumentCheckinBloc(this._socialRepository)
: super(MonumentCheckinInitial()) {
on<CheckinMonument>(_mapCheckinMonumentToState);
on<CheckIfMonumentIsCheckedIn>(_mapCheckIfMonumentIsCheckedInToState);
}

Future<void> _mapCheckinMonumentToState(
CheckinMonument event, Emitter<MonumentCheckinState> emit) async {
try {
emit(MonumentCheckinLoading());
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
emit(const MonumentCheckinFailure(
message: "Location services are disabled"));
return;
}
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.deniedForever) {
emit(const MonumentCheckinFailure(
message:
"Location permissions are permanently denied, we cannot request permissions."));
return;
}
if (permission == LocationPermission.denied) {
emit(const MonumentCheckinFailure(
message: "Location permissions are denied"));
return;
}
}
var position = await Geolocator.getCurrentPosition();

double distance = calculateDistance(position.latitude, position.longitude,
event.monument.coordinates[0], event.monument.coordinates[1]);
if (distance < 200) {
emit(const MonumentCheckinFailure(
message: "You are not close enough to check in"));
return;
}

if (await _socialRepository.checkInStatus(
monumentId: event.monument.id)) {
emit(const MonumentCheckinFailure(message: "Already checked in"));
return;
}
await _socialRepository.monumentCheckIn(monumentId: event.monument.id);
emit(MonumentCheckinSuccess());
} catch (e) {
emit(MonumentCheckinFailure(message: e.toString()));
}
}

Future<void> _mapCheckIfMonumentIsCheckedInToState(
CheckIfMonumentIsCheckedIn event,
Emitter<MonumentCheckinState> emit) async {
try {
emit(MonumentCheckinLoading());

bool isCheckedIn =
await _socialRepository.checkInStatus(monumentId: event.monument.id);

if (isCheckedIn) {
emit(MonumentCheckedIn());
} else {
emit(MonumentNotCheckedIn());
}
} catch (e) {
emit(MonumentCheckinFailure(message: e.toString()));
}
}
}

double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
// Radius of the Earth in meters
const double earthRadius = 6371000;

// Convert latitude and longitude from degrees to radians
double dLat = (lat2 - lat1) * pi / 180;
double dLon = (lon2 - lon1) * pi / 180;

// Haversine formula for calculating distance
double a = sin(dLat / 2) * sin(dLat / 2) +
cos(lat1 * pi / 180) *
cos(lat2 * pi / 180) *
sin(dLon / 2) *
sin(dLon / 2);
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
// Distance in meters using the Earth's radius
double distance = earthRadius * c;
// Return the calculated distance
return distance;
}

// Function to check if a coordinate is within a certain range (in meters) of another coordinate
bool isWithinRange(
double lat1, double lon1, double lat2, double lon2, double rangeInMeters) {
double distance = calculateDistance(lat1, lon1, lat2, lon2);
return distance <= rangeInMeters;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
part of 'monument_checkin_bloc.dart';

sealed class MonumentCheckinEvent extends Equatable {
const MonumentCheckinEvent();

@override
List<Object> get props => [];
}

final class CheckinMonument extends MonumentCheckinEvent {
final MonumentEntity monument;

const CheckinMonument({required this.monument});

@override
List<Object> get props => [monument];
}

final class CheckIfMonumentIsCheckedIn extends MonumentCheckinEvent {
final MonumentEntity monument;

const CheckIfMonumentIsCheckedIn({required this.monument});

@override
List<Object> get props => [monument];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
part of 'monument_checkin_bloc.dart';

sealed class MonumentCheckinState extends Equatable {
const MonumentCheckinState();

@override
List<Object> get props => [];
}

final class MonumentCheckinInitial extends MonumentCheckinState {}

final class MonumentCheckinLoading extends MonumentCheckinState {}

final class MonumentCheckinSuccess extends MonumentCheckinState {}

final class MonumentCheckinFailure extends MonumentCheckinState {
final String message;

const MonumentCheckinFailure({required this.message});

@override
List<Object> get props => [message];
}

final class MonumentCheckedIn extends MonumentCheckinState {}

final class MonumentNotCheckedIn extends MonumentCheckinState {}
73 changes: 73 additions & 0 deletions lib/data/repositories/firebase_social_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class FirebaseSocialRepository implements SocialRepository {
QuerySnapshot snap = await _database
.collection("posts")
.where("postByUid", isEqualTo: userId)
// .where("postType", isEqualTo: 0)
.orderBy("timeStamp", descending: true)
.limit(10)
.get();
Expand Down Expand Up @@ -725,4 +726,76 @@ class FirebaseSocialRepository implements SocialRepository {
.toList();
return notifications;
}

@override
Future<bool> monumentCheckIn(
{required String monumentId, String? title}) async {
var (userLoggedIn, user) = await authenticationRepository.getUser();
if (!userLoggedIn) {
throw Exception("User not logged in");
}
// get monument details
DocumentSnapshot monumentDoc =
await _database.collection("monuments").doc(monumentId).get();

// check if the user has already checked in
QuerySnapshot snap = await _database
.collection("users")
.doc(user?.uid)
.collection("checkIns")
.where("monumentId", isEqualTo: monumentId)
.get();

if (snap.docs.isNotEmpty) {
return false;
}

// add the check-in to the user's check-ins
await _database
.collection("users")
.doc(user?.uid)
.collection("checkIns")
.add({
"monumentId": monumentId,
"title": title ?? "",
"timeStamp": DateTime.now().millisecondsSinceEpoch,
});

// add the check-in as a post
int timeStamp = DateTime.now().millisecondsSinceEpoch;
DocumentReference doc = await _database.collection("posts").add({
"title": title ?? "",
"location": monumentDoc['city'] + ", " + monumentDoc['country'],
"imageUrl": "",
"author": {
"name": user?.name,
"username": user?.username,
"uid": user?.uid,
"profilePictureUrl": user?.profilePictureUrl,
"email": user?.email,
},
"timeStamp": timeStamp,
"postType": 2,
"postByUid": user?.uid,
"likesCount": 0,
"commentsCount": 0,
});

return true;
}

@override
Future<bool> checkInStatus({required String monumentId}) async {
var (userLoggedIn, user) = await authenticationRepository.getUser();
if (!userLoggedIn) {
throw Exception("User not logged in");
}
QuerySnapshot snap = await _database
.collection("users")
.doc(user?.uid)
.collection("checkIns")
.where("monumentId", isEqualTo: monumentId)
.get();
return snap.docs.isNotEmpty;
}
}
4 changes: 4 additions & 0 deletions lib/domain/repositories/social_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ abstract interface class SocialRepository {

Future<List<NotificationModel>> getMoreNotifications(
{required String startAfterDocId});

Future<bool> monumentCheckIn({required String monumentId, String? title});

Future<bool> checkInStatus({required String monumentId});
}
43 changes: 36 additions & 7 deletions lib/presentation/feed/desktop/widgets/feed_post_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,42 @@ class _FeedPostCardState extends State<FeedPostCard>
height: 24,
),
widget.post.imageUrl == null
? Text(
widget.post.title,
style: AppTextStyles.s16(
color: AppColor.appSecondary,
fontType: FontType.REGULAR,
),
)
? (widget.post.postType == 2
? Container(
width: double.infinity,
height: 210,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
),
child: Column(
children: [
Image.asset(
"assets/desktop/checkedin.png",
width: 160,
height: 160,
),
const SizedBox(
height: 8,
),
Text(
widget.post.title.isEmpty
? "${widget.post.author.name} visited ${widget.post.location ?? "a place"}"
: widget.post.title,
style: AppTextStyles.s16(
color: AppColor.appSecondary,
fontType: FontType.REGULAR,
),
),
],
),
)
: Text(
widget.post.title,
style: AppTextStyles.s16(
color: AppColor.appSecondary,
fontType: FontType.REGULAR,
),
))
: Container(
width: double.infinity,
height: 380,
Expand Down
Loading

0 comments on commit 8faf587

Please sign in to comment.