Skip to content

Commit

Permalink
refactor: Add CheckViewAlarmsLive.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Oct 11, 2023
1 parent 6a8e6d2 commit 39d981a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
56 changes: 56 additions & 0 deletions lib/app_wrappers/CheckViewAlarmsLive.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:locus/services/current_location_service.dart';
import 'package:locus/services/location_point_service.dart';
import 'package:locus/services/manager_service/helpers.dart';
import 'package:locus/services/view_service/index.dart';
import 'package:provider/provider.dart';

import 'package:flutter_gen/gen_l10n/app_localizations.dart';

/// Checks view alarms while the app is in use
class CheckViewAlarmsLive extends StatefulWidget {
const CheckViewAlarmsLive({super.key});

@override
State<CheckViewAlarmsLive> createState() => _CheckViewAlarmsLiveState();
}

class _CheckViewAlarmsLiveState extends State<CheckViewAlarmsLive> {
late final CurrentLocationService _currentLocation;
late final StreamSubscription<Position> _subscription;

@override
void initState() {
super.initState();

_subscription = _currentLocation.stream.listen((position) async {
final l10n = AppLocalizations.of(context);
final viewService = context.read<ViewService>();
final userLocation = await LocationPointService.fromPosition(position);

if (!mounted) {
return;
}

checkViewAlarms(
l10n: l10n,
viewService: viewService,
userLocation: userLocation,
);
});
}

@override
void dispose() {
_subscription.cancel();
super.dispose();
}

@override
Widget build(BuildContext context) {
return const Placeholder();
}
}
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_logs/flutter_logs.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:locus/App.dart';
import 'package:locus/app_wrappers/CheckViewAlarmsLive.dart';
import 'package:locus/app_wrappers/HandleNotifications.dart';
import 'package:locus/app_wrappers/LocationHistoryUpdater.dart';
import 'package:locus/app_wrappers/RegisterBackgroundListeners.dart';
Expand Down Expand Up @@ -108,6 +109,7 @@ void main() async {
RegisterBackgroundListeners(),
UpdateLocaleToSettings(),
HandleNotifications(),
CheckViewAlarmsLive(),
App(),
],
),
Expand Down
20 changes: 0 additions & 20 deletions lib/screens/LocationsOverviewScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -313,24 +313,6 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>
);
}

void _checkViewAlarms(
final Position position,
) async {
final l10n = AppLocalizations.of(context);
final viewService = context.read<ViewService>();
final userLocation = await LocationPointService.fromPosition(position);

if (!mounted) {
return;
}

checkViewAlarms(
l10n: l10n,
viewService: viewService,
userLocation: userLocation,
);
}

void _initLiveLocationUpdate() {
if (_positionStream != null) {
return;
Expand All @@ -346,8 +328,6 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>

currentLocation.updateCurrentPosition(position);

_checkViewAlarms(position);

setState(() {
lastPosition = position;
locationStatus = LocationStatus.active;
Expand Down

0 comments on commit 39d981a

Please sign in to comment.