Skip to content

Commit

Permalink
Sync alarm with radius
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdeltwabMF committed Jan 28, 2023
1 parent 999f838 commit d9a9584
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
15 changes: 4 additions & 11 deletions lib/screen/home.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_ringtone_player/flutter_ringtone_player.dart';
import 'package:loc/style/colors.dart';
import 'package:loc/utils/location.dart';
import 'package:loc/utils/states.dart';
Expand All @@ -12,7 +11,6 @@ class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final provider = Provider.of<AppStates>(context);
final providerNoListening = Provider.of<AppStates>(context, listen: false);

return Scaffold(
resizeToAvoidBottomInset: false,
Expand Down Expand Up @@ -161,12 +159,7 @@ class HomeScreen extends StatelessWidget {
'Latitude, Longitude, and Radius are required!');
return;
}

FlutterRingtonePlayer.playNotification(
looping: true,
volume: 0.1,
asAlarm: true,
);
provider.setListening(true);

getCurrentLocation().then((value) {
provider.setCurrLatitude(value.latitude);
Expand Down Expand Up @@ -201,14 +194,14 @@ class HomeScreen extends StatelessWidget {
),
),
),
provider.isListening && providerNoListening.isLocValid() == false
provider.isListening && provider.isLocValid() == false
? Container(
padding: const EdgeInsets.only(
top: 16,
left: 16,
right: 16,
),
child: LinearProgressIndicator(
child: const LinearProgressIndicator(
value: null,
),
)
Expand Down Expand Up @@ -336,7 +329,7 @@ class HomeScreen extends StatelessWidget {
),
child: Text(
provider.isListening
? '${calcDistance(context).toString()} KM'
? '${toKiloMeter(calcDistance(context))} KM'
: '',
textAlign: TextAlign.center,
),
Expand Down
24 changes: 18 additions & 6 deletions lib/utils/location.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,27 @@ Future<geo.Position> getCurrentLocation() async {
void listenToLocationUpdate(BuildContext context) {
final provider = Provider.of<AppStates>(context, listen: false);

geo.LocationSettings settings = geo.LocationSettings(
geo.LocationSettings settings = const geo.LocationSettings(
accuracy: geo.LocationAccuracy.high,
distanceFilter: double.parse(provider.radiusText.text).toInt(),
distanceFilter: 50,
);

final subscription =
geo.Geolocator.getPositionStream(locationSettings: settings)
.listen((position) {
provider.setCurrLatitude(position.latitude);
provider.setCurrLongitude(position.longitude);

if (calcDistance(context) <= double.parse(provider.radiusText.text)) {
FlutterRingtonePlayer.playAlarm(
looping: true,
volume: 0.1,
asAlarm: true,
);
}
});

provider.setPositionStream(subscription);
provider.setListening(true);
}

void cancelLocationUpdate(BuildContext context) {
Expand All @@ -56,7 +64,7 @@ void cancelLocationUpdate(BuildContext context) {

double calcDistance(BuildContext context) {
final provider = Provider.of<AppStates>(context, listen: false);
if (provider.isLocValid() == false) return 0.0;
if (provider.isLocValid() == false) return 0;

final currentLatitude = provider.currLatitude!;
final currentLongitude = provider.currLongitude!;
Expand All @@ -66,10 +74,14 @@ double calcDistance(BuildContext context) {
final inMeters = geo.Geolocator.distanceBetween(
destLatitude, destLongitude, currentLatitude, currentLongitude);

return toKiloMeter(inMeters);
return inMeters;
}

double toKiloMeter(double distanceInMeters) {
if (distanceInMeters <= 1000) {
return distanceInMeters;
}
distanceInMeters /= 1000;
return double.parse(distanceInMeters.toStringAsFixed(2));

return double.parse(distanceInMeters.toStringAsPrecision(3));
}
2 changes: 1 addition & 1 deletion test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:loc/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpWidget(const App());

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Expand Down

0 comments on commit d9a9584

Please sign in to comment.