Skip to content

Commit

Permalink
refactor: Add PublishTaskPositionsOnUpdate.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Oct 12, 2023
1 parent 7113c5d commit efb029e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
58 changes: 58 additions & 0 deletions lib/app_wrappers/PublishTaskPositionsOnUpdate.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:locus/services/current_location_service.dart';
import 'package:locus/services/location_point_service.dart';
import 'package:locus/services/task_service/index.dart';
import 'package:provider/provider.dart';

class PublishTaskPositionsOnUpdate extends StatefulWidget {
const PublishTaskPositionsOnUpdate({super.key});

@override
State<PublishTaskPositionsOnUpdate> createState() =>
_PublishTaskPositionsOnUpdateState();
}

class _PublishTaskPositionsOnUpdateState
extends State<PublishTaskPositionsOnUpdate> {
late final CurrentLocationService _currentLocation;
late final StreamSubscription _stream;

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

_currentLocation = context.read<CurrentLocationService>();

_stream = _currentLocation.stream.listen((position) async {
final taskService = context.read<TaskService>();

final runningTasks = await taskService.getRunningTasks().toList();

if (runningTasks.isEmpty) {
return;
}

final locationData = await LocationPointService.fromPosition(position);

for (final task in runningTasks) {
await task.publisher.publishOutstandingPositions();
await task.publisher.publishLocation(
locationData.copyWithDifferentId(),
);
}
});
}

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

@override
Widget build(BuildContext context) {
return const SizedBox.shrink();
}
}
4 changes: 4 additions & 0 deletions lib/app_wrappers/RegisterBackgroundListeners.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class _RegisterBackgroundListenersState
}

void _updateListeners() async {
if (!_settings.userHasSeenWelcomeScreen) {
return;
}

FlutterLogs.logInfo(
LOG_TAG,
"Register Background Listeners",
Expand Down
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:locus/app_wrappers/CheckViewAlarmsLive.dart';
import 'package:locus/app_wrappers/HandleNotifications.dart';
import 'package:locus/app_wrappers/InitCurrentLocationFromSettings.dart';
import 'package:locus/app_wrappers/LocationHistoryUpdater.dart';
import 'package:locus/app_wrappers/PublishTaskPositionsOnUpdate.dart';
import 'package:locus/app_wrappers/RegisterBackgroundListeners.dart';
import 'package:locus/app_wrappers/ManageQuickActions.dart';
import 'package:locus/app_wrappers/ShowUpdateDialog.dart';
Expand Down Expand Up @@ -116,6 +117,7 @@ void main() async {
ManageQuickActions(),
InitCurrentLocationFromSettings(),
ShowUpdateDialog(),
PublishTaskPositionsOnUpdate(),
App(),
],
),
Expand Down
16 changes: 0 additions & 16 deletions lib/screens/LocationsOverviewScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>
);

_positionStream!.listen((position) async {
final taskService = context.read<TaskService>();
final currentLocation = context.read<CurrentLocationService>();

currentLocation.updateCurrentPosition(position);
Expand All @@ -309,21 +308,6 @@ class _LocationsOverviewScreenState extends State<LocationsOverviewScreen>
lastPosition = position;
locationStatus = LocationStatus.active;
});

final runningTasks = await taskService.getRunningTasks().toList();

if (runningTasks.isEmpty) {
return;
}

final locationData = await LocationPointService.fromPosition(position);

for (final task in runningTasks) {
await task.publisher.publishOutstandingPositions();
await task.publisher.publishLocation(
locationData.copyWithDifferentId(),
);
}
});
}

Expand Down

0 comments on commit efb029e

Please sign in to comment.