Skip to content

Commit

Permalink
feat: Add ProximityLocationAlarm.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Sep 9, 2023
1 parent a966a19 commit a19d055
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 4 deletions.
24 changes: 24 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,30 @@
}
}
},
"locationAlarm_proximityLocation_notificationTitle_whenEnter": "{name} is within {proximity}m",
"@locationAlarm_proximityLocation_notificationTitle_whenEnter": {
"description": "Notification title when entering a proximity location; Try to keep this short, will be truncated to 80 characters",
"placeholders": {
"name": {
"type": "String"
},
"proximity": {
"type": "int"
}
}
},
"locationAlarm_proximityLocation_notificationTitle_whenLeave": "{name} is outside of {proximity}m",
"@locationAlarm_proximityLocation_notificationTitle_whenLeave": {
"description": "Notification title when leaving a proximity location; Try to keep this short, will be truncated to 80 characters",
"placeholders": {
"name": {
"type": "String"
},
"proximity": {
"type": "int"
}
}
},
"locationAlarm_notification_description": "Tap for more information",
"androidNotificationChannel_locationAlarms_name": "Location Alarms",
"androidNotificationChannel_locationAlarms_description": "Receive notifications for location alarms",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class LocationAlarmServiceBase {
LocationAlarmTriggerType check(
final LocationPointService previousLocation,
final LocationPointService nextLocation, {
final LocationPointService userLocation,
required final LocationPointService userLocation,
});

String getStorageKey() => "location_alarm_service:$IDENTIFIER:$id";
Expand Down
153 changes: 153 additions & 0 deletions lib/services/location_alarm_service/ProximityLocationAlarm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import 'package:apple_maps_flutter/apple_maps_flutter.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:geolocator/geolocator.dart';

import 'package:locus/services/location_alarm_service/enums.dart';

import 'package:locus/services/location_point_service.dart';

import 'LocationAlarmServiceBase.dart';

enum ProximityLocationAlarmType {
whenEnter,
whenLeave,
}

class ProximityLocationAlarm extends LocationAlarmServiceBase {
// Radius in meters
final int radius;
final ProximityLocationAlarmType type;

const ProximityLocationAlarm({
required this.radius,
required this.type,
required String id,
}) : super(id);

@override
LocationAlarmType get IDENTIFIER => LocationAlarmType.proximityLocation;

factory ProximityLocationAlarm.fromJSON(
final Map<String, dynamic> data,
) =>
ProximityLocationAlarm(
radius: data["radius"],
type: ProximityLocationAlarmType.values[data["alarmType"]],
id: data["id"],
);

factory ProximityLocationAlarm.create({
required final int radius,
required final ProximityLocationAlarmType type,
}) =>
ProximityLocationAlarm(
radius: radius,
type: type,
id: uuid.v4(),
);

LocationAlarmTriggerType _wasInside(
final LocationPointService location,
final LocationPointService userLocation,
) {
final fullDistance = Geolocator.distanceBetween(
location.latitude,
location.longitude,
userLocation.latitude,
userLocation.longitude,
);

if (fullDistance < radius && location.accuracy < radius) {
return LocationAlarmTriggerType.yes;
}

if (fullDistance - location.accuracy - radius > 0) {
return LocationAlarmTriggerType.no;
}

return LocationAlarmTriggerType.maybe;
}

@override
LocationAlarmTriggerType check(
LocationPointService previousLocation,
LocationPointService nextLocation, {
required LocationPointService userLocation,
}) {
final previousInside = _wasInside(previousLocation, userLocation);
final nextInside = _wasInside(nextLocation, userLocation);

switch (type) {
case ProximityLocationAlarmType.whenEnter:
if (previousInside == LocationAlarmTriggerType.no &&
nextInside == LocationAlarmTriggerType.yes) {
return LocationAlarmTriggerType.yes;
}

if (previousInside == LocationAlarmTriggerType.maybe &&
nextInside == LocationAlarmTriggerType.yes) {
return LocationAlarmTriggerType.yes;
}

if (previousInside == LocationAlarmTriggerType.no &&
nextInside == LocationAlarmTriggerType.maybe) {
return LocationAlarmTriggerType.maybe;
}

if (previousInside == LocationAlarmTriggerType.maybe &&
nextInside == LocationAlarmTriggerType.maybe) {
return LocationAlarmTriggerType.maybe;
}
break;
case ProximityLocationAlarmType.whenLeave:
if (previousInside == LocationAlarmTriggerType.yes &&
nextInside == LocationAlarmTriggerType.no) {
return LocationAlarmTriggerType.yes;
}

if (previousInside == LocationAlarmTriggerType.maybe &&
nextInside == LocationAlarmTriggerType.no) {
return LocationAlarmTriggerType.yes;
}

if (previousInside == LocationAlarmTriggerType.yes &&
nextInside == LocationAlarmTriggerType.maybe) {
return LocationAlarmTriggerType.maybe;
}

if (previousInside == LocationAlarmTriggerType.maybe &&
nextInside == LocationAlarmTriggerType.maybe) {
return LocationAlarmTriggerType.maybe;
}
break;
}

return LocationAlarmTriggerType.no;
}

@override
String createNotificationTitle(AppLocalizations l10n, String viewName) {
switch (type) {
case ProximityLocationAlarmType.whenEnter:
return l10n.locationAlarm_proximityLocation_notificationTitle_whenEnter(
viewName,
radius,
);
case ProximityLocationAlarmType.whenLeave:
return l10n.locationAlarm_proximityLocation_notificationTitle_whenLeave(
viewName,
radius,
);
}
}

@override
Map<String, dynamic> toJSON() {
return {
"_IDENTIFIER": IDENTIFIER.name,
"radius": radius,
"alarmType": type.index,
"id": id,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class RadiusBasedRegionLocationAlarm extends LocationAlarmServiceBase {
LocationAlarmType get IDENTIFIER => LocationAlarmType.radiusBasedRegion;

factory RadiusBasedRegionLocationAlarm.fromJSON(
final Map<String, dynamic> data) =>
final Map<String, dynamic> data,
) =>
RadiusBasedRegionLocationAlarm(
center: LatLng.fromJson(data["center"]),
radius: data["radius"],
Expand Down Expand Up @@ -74,10 +75,14 @@ class RadiusBasedRegionLocationAlarm extends LocationAlarmServiceBase {
switch (type) {
case RadiusBasedRegionLocationAlarmType.whenEnter:
return l10n.locationAlarm_radiusBasedRegion_notificationTitle_whenEnter(
viewName, zoneName);
viewName,
zoneName,
);
case RadiusBasedRegionLocationAlarmType.whenLeave:
return l10n.locationAlarm_radiusBasedRegion_notificationTitle_whenLeave(
viewName, zoneName);
viewName,
zoneName,
);
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/services/location_alarm_service/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ enum LocationAlarmTriggerType {

enum LocationAlarmType {
radiusBasedRegion,
proximityLocation,
}
3 changes: 3 additions & 0 deletions lib/services/view_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import '../api/get-locations.dart' as get_locations_api;
import '../constants/values.dart';
import 'location_alarm_service/LocationAlarmServiceBase.dart';
import 'location_alarm_service/ProximityLocationAlarm.dart';
import 'location_alarm_service/RadiusBasedRegionLocationAlarm.dart';
import 'location_alarm_service/enums.dart';
import 'task_service/mixins.dart';
Expand Down Expand Up @@ -102,6 +103,8 @@ class TaskView extends ChangeNotifier with LocationBase {
switch (identifier) {
case LocationAlarmType.radiusBasedRegion:
return RadiusBasedRegionLocationAlarm.fromJSON(alarm);
case LocationAlarmType.proximityLocation:
return ProximityLocationAlarm.fromJSON(alarm);
}
}),
),
Expand Down

0 comments on commit a19d055

Please sign in to comment.