diff --git a/lib/main.dart b/lib/main.dart index 42f7080..ada5f56 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_broadcast_receiver/flutter_broadcast_receiver.dart'; +import 'package:geolocator/geolocator.dart'; import 'package:isar/isar.dart'; import 'package:path_provider/path_provider.dart'; import 'package:stadtbote/model/message.dart'; @@ -29,14 +30,11 @@ Future main() async { MessageSchema, ], directory: dir.path); - runZonedGuarded(() { - }, (error, stackTrace) { + runZonedGuarded(() {}, (error, stackTrace) { _traceUncaughtExceptions(error, stackTrace); }); - MqttCore.setup(); - - runApp(const MyApp()); + runApp(MyApp()); } void _traceUncaughtExceptions(Object error, StackTrace stackTrace) { @@ -45,10 +43,16 @@ void _traceUncaughtExceptions(Object error, StackTrace stackTrace) { } class MyApp extends StatelessWidget { - const MyApp({super.key}); + MyApp({super.key}) { + MqttCore.setup(); + + _startLocationService(); + } static late Isar isarInstance; + static Position? currentPosition; + @override Widget build(BuildContext context) { return MaterialApp( @@ -59,6 +63,56 @@ class MyApp extends StatelessWidget { home: const Main(), ); } + + _startLocationService() async { + var result = await _handleLocationPermissions(); + + if (result) { + LocationSettings locationSettings = const LocationSettings( + accuracy: LocationAccuracy.high, + distanceFilter: 0, + ); + + Geolocator.getPositionStream(locationSettings: locationSettings).listen((Position? position) { + MyApp.currentPosition = position; + + Util.logInfo(position == null ? 'Unknown' : '${position.latitude.toString()}, ${position.longitude.toString()}'); + }); + } + } + + Future _handleLocationPermissions() async { + // Test if location services are enabled. + var serviceEnabled = await Geolocator.isLocationServiceEnabled(); + if (!serviceEnabled) { + // Location services are not enabled don't continue + // accessing the position and request users of the + // App to enable the location services. + return false; + //return Future.error('Location services are disabled.'); + } + + var permission = await Geolocator.checkPermission(); + if (permission == LocationPermission.denied) { + permission = await Geolocator.requestPermission(); + if (permission == LocationPermission.denied) { + // Permissions are denied, next time you could try + // requesting permissions again (this is also where + // Android's shouldShowRequestPermissionRationale + // returned true. According to Android guidelines + // your App should show an explanatory UI now. + //return Future.error('Location permissions are denied'); + return false; + } + } + + if (permission == LocationPermission.deniedForever) { + //return Future.error('Location permissions are permanently denied, we cannot request permissions.'); + return false; + } + + return true; + } } class Main extends StatefulWidget { @@ -134,9 +188,7 @@ class _MainState extends State
{ key: GlobalKey(), child: PopScope( canPop: contentStack.isNotEmpty, - onPopInvoked: (didPop) => { - _popContentView() - }, + onPopInvoked: (didPop) => {_popContentView()}, child: Scaffold( appBar: PreferredSize( preferredSize: const Size.fromHeight(100), @@ -203,9 +255,7 @@ class _MainState extends State
{ SizedBox( height: 100, width: 300, - child: Center( - heightFactor: 1, - child: _currentTitleWidget), + child: Center(heightFactor: 1, child: _currentTitleWidget), ), ], ), @@ -224,11 +274,13 @@ class _MainState extends State
{ height: 30, color: Colors.red, child: const Center( - child: Text("*** OFFLINE ***", + child: Text( + "*** OFFLINE ***", style: TextStyle( color: Colors.white, fontSize: 15.0, - ),), + ), + ), ), ), ); @@ -236,7 +288,7 @@ class _MainState extends State
{ ), Expanded(child: _currentContent), ], - ),// This trailing comma makes auto-formatting nicer for build methods. + ), // This trailing comma makes auto-formatting nicer for build methods. ), ), ); diff --git a/lib/popups/accept_jobs_popup.dart b/lib/popups/accept_jobs_popup.dart index 0090d43..7d440f6 100644 --- a/lib/popups/accept_jobs_popup.dart +++ b/lib/popups/accept_jobs_popup.dart @@ -163,38 +163,9 @@ Future> _createListEntries() async { return entries; } -/* -private void SendAnswer(int item) - { - JObject payloadObject; - - var location = App.CurrentLocation; - if (location != null) - payloadObject = new JObject( - new JProperty("reaction", new JObject( - new JProperty("id", _jobViewModel.IdString), - new JProperty("ver", _jobViewModel.Version), - new JProperty("item", item), - new JProperty("track", new JObject( - new JProperty("time", DateTime.Now.ToString("o")), - new JProperty("pos", new JObject( - new JProperty("lat", location.Latitude), - new JProperty("long", location.Longitude), - new JProperty("acc", location.Accuracy)))))))); - else - payloadObject = new JObject( - new JProperty("reaction", new JObject( - new JProperty("id", _jobViewModel.IdString), - new JProperty("ver", _jobViewModel.Version), - new JProperty("item", item), - new JProperty("track", new JObject( - new JProperty("time", DateTime.Now.ToString("o"))))))); - - App.Core.SendJsonMessageAsync("job", payloadObject); - } - */ - _sendAnswer(Job job, int item) { + var currentPosition = MyApp.currentPosition; + var message = AcceptJobMessage(); message.reaction = Reaction(); message.reaction.id = job.id.toString(); @@ -203,9 +174,9 @@ _sendAnswer(Job job, int item) { message.reaction.track = Track(); message.reaction.track.time = DateTime.now().toUtc().toIso8601String(); message.reaction.track.pos = Pos(); - message.reaction.track.pos.lat = 0; - message.reaction.track.pos.long = 0; - message.reaction.track.pos.acc = 1; + message.reaction.track.pos.lat = currentPosition != null ? currentPosition.latitude : 0.0; + message.reaction.track.pos.long = currentPosition != null ? currentPosition.longitude : 0.0; + message.reaction.track.pos.acc = currentPosition != null ? currentPosition.accuracy : 0.0; MqttCore.instance.publishPayloadMessage("job", message.toJson()); }