forked from AOSSIE-Org/Monumento
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
564 additions
and
96 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions
114
lib/application/popular_monuments/monument_checkin/monument_checkin_bloc.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
26 changes: 26 additions & 0 deletions
26
lib/application/popular_monuments/monument_checkin/monument_checkin_event.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
27 changes: 27 additions & 0 deletions
27
lib/application/popular_monuments/monument_checkin/monument_checkin_state.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.