From 911c2738aa93cf72fd9dd5bc6bfb5b4af30a762e Mon Sep 17 00:00:00 2001
From: bytedream <bytedream@protonmail.com>
Date: Wed, 27 Mar 2024 01:03:01 +0100
Subject: [PATCH] added map live locating

---
 android/app/src/main/AndroidManifest.xml |  2 +
 lib/state/mapState.dart                  | 18 +++---
 lib/view/home.dart                       | 50 +++++++++++++--
 pubspec.lock                             | 80 ++++++++++++++++++++++++
 pubspec.yaml                             |  1 +
 5 files changed, 136 insertions(+), 15 deletions(-)

diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 32ea881..ea15bda 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -41,4 +41,6 @@
             <data android:mimeType="text/plain"/>
         </intent>
     </queries>
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 </manifest>
diff --git a/lib/state/mapState.dart b/lib/state/mapState.dart
index 9da94ee..945733b 100644
--- a/lib/state/mapState.dart
+++ b/lib/state/mapState.dart
@@ -1,22 +1,18 @@
 import 'package:flutter/cupertino.dart';
 import 'package:flutter_map/flutter_map.dart';
+import 'package:geolocator/geolocator.dart';
 
 class MapState extends ChangeNotifier {
-  bool _isLocationLock = false;
-  bool _isCurrentlyFetchin = false;
+  bool _isCurrentlyLoading = false;
   final MapController _mapController = MapController();
+  final Geolocator _geolocator = Geolocator();
 
-  bool get followLocation => _isLocationLock;
-  bool get isCurrentlyFetching => _isCurrentlyFetchin;
+  bool get isCurrentlyLoading => _isCurrentlyLoading;
   MapController get getMapController => _mapController;
+  Geolocator get getGeolocator => _geolocator;
 
-  toggleLocationLock() {
-    _isLocationLock = !_isLocationLock;
-    notifyListeners();
-  }
-
-  set setNetworkActivity(bool active) {
-    _isCurrentlyFetchin = active;
+  void setLoading(bool loading) {
+    _isCurrentlyLoading = loading;
     notifyListeners();
   }
 }
\ No newline at end of file
diff --git a/lib/view/home.dart b/lib/view/home.dart
index 9bc7c9a..15a1e2a 100644
--- a/lib/view/home.dart
+++ b/lib/view/home.dart
@@ -1,4 +1,3 @@
-
 import 'package:app/extensions/obtainProviderExtension.dart';
 import 'package:app/state/mapState.dart';
 import 'package:app/state/timeStatusState.dart';
@@ -8,6 +7,8 @@ import 'package:app/view/appInfo.dart';
 import 'package:app/view/status.dart';
 import 'package:app/view/map.dart';
 import 'package:flutter/material.dart';
+import 'package:geolocator/geolocator.dart';
+import 'package:latlong2/latlong.dart';
 import 'package:provider/provider.dart';
 
 class HomeView extends StatefulWidget {
@@ -18,6 +19,30 @@ class HomeView extends StatefulWidget {
 }
 
 class _HomePageState extends State<HomeView> {
+  bool locationEnabled = false;
+
+  Future<LatLng?> getLatLng() async {
+    LocationPermission permissionStatus = await Geolocator.checkPermission();
+    if (permissionStatus == LocationPermission.denied) {
+      permissionStatus = await Geolocator.requestPermission();
+      if (permissionStatus == LocationPermission.denied || permissionStatus == LocationPermission.deniedForever) {
+        return null;
+      }
+    }
+
+    Position? position;
+    try {
+      position = await Geolocator.getCurrentPosition();
+    } catch (e) {
+      position = await Geolocator.getLastKnownPosition();
+    }
+
+    if (position != null) {
+      return LatLng(position.latitude, position.longitude);
+    }
+    return null;
+  }
+
   @override
   Widget build(BuildContext context) {
     return Scaffold(
@@ -43,8 +68,25 @@ class _HomePageState extends State<HomeView> {
         ],
       ),
       floatingActionButton: FloatingActionButton(
-        child: WatchState<MapState>((context, state) => Icon(state.followLocation ? Icons.my_location : Icons.location_disabled)),
-        onPressed: () => context.obtainState<MapState>().toggleLocationLock(),
+        child: WatchState<MapState>((context, state) => Icon(
+            locationEnabled
+                ? state.isCurrentlyLoading
+                    ? Icons.location_searching
+                    : Icons.my_location
+                : Icons.location_disabled
+        )),
+        onPressed: () async {
+          MapState mapState = context.obtainState<MapState>();
+          mapState.setLoading(true);
+          LatLng? latLng = await getLatLng();
+          if (latLng != null) {
+            mapState.getMapController.move(latLng, 16);
+            setState(() {
+              locationEnabled = true;
+            });
+          }
+          mapState.setLoading(false);
+        },
       ),
       body: Column(
         children: [
@@ -61,7 +103,7 @@ class _HomePageState extends State<HomeView> {
               builder: (context, state, child) {
                 return LoadingContainer(
                   loading: child == null,
-                  fetching: state.isCurrentlyFetching,
+                  fetching: state.isCurrentlyLoading,
                   child: const MapView(),
                 );
               },
diff --git a/pubspec.lock b/pubspec.lock
index 1a142e6..4769651 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -121,6 +121,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "1.3.1"
+  fixnum:
+    dependency: transitive
+    description:
+      name: fixnum
+      sha256: "25517a4deb0c03aa0f32fd12db525856438902d9c16536311e76cdc57b31d7d1"
+      url: "https://pub.dev"
+    source: hosted
+    version: "1.1.0"
   flutter:
     dependency: "direct main"
     description: flutter
@@ -176,6 +184,54 @@ packages:
     description: flutter
     source: sdk
     version: "0.0.0"
+  geolocator:
+    dependency: "direct main"
+    description:
+      name: geolocator
+      sha256: "694ec58afe97787b5b72b8a0ab78c1a9244811c3c10e72c4362ef3c0ceb005cd"
+      url: "https://pub.dev"
+    source: hosted
+    version: "11.0.0"
+  geolocator_android:
+    dependency: transitive
+    description:
+      name: geolocator_android
+      sha256: f15d1536cd01b1399578f1da1eb5d566e7a718db6a3648f2c24d2e2f859f0692
+      url: "https://pub.dev"
+    source: hosted
+    version: "4.5.4"
+  geolocator_apple:
+    dependency: transitive
+    description:
+      name: geolocator_apple
+      sha256: bc2aca02423ad429cb0556121f56e60360a2b7d694c8570301d06ea0c00732fd
+      url: "https://pub.dev"
+    source: hosted
+    version: "2.3.7"
+  geolocator_platform_interface:
+    dependency: transitive
+    description:
+      name: geolocator_platform_interface
+      sha256: "009a21c4bc2761e58dccf07c24f219adaebe0ff707abdfd40b0a763d4003fab9"
+      url: "https://pub.dev"
+    source: hosted
+    version: "4.2.2"
+  geolocator_web:
+    dependency: transitive
+    description:
+      name: geolocator_web
+      sha256: "49d8f846ebeb5e2b6641fe477a7e97e5dd73f03cbfef3fd5c42177b7300fb0ed"
+      url: "https://pub.dev"
+    source: hosted
+    version: "3.0.0"
+  geolocator_windows:
+    dependency: transitive
+    description:
+      name: geolocator_windows
+      sha256: "53da08937d07c24b0d9952eb57a3b474e29aae2abf9dd717f7e1230995f13f0e"
+      url: "https://pub.dev"
+    source: hosted
+    version: "0.2.3"
   html:
     dependency: transitive
     description:
@@ -344,6 +400,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "6.0.2"
+  plugin_platform_interface:
+    dependency: transitive
+    description:
+      name: plugin_platform_interface
+      sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
+      url: "https://pub.dev"
+    source: hosted
+    version: "2.1.8"
   pointycastle:
     dependency: transitive
     description:
@@ -389,6 +453,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "1.10.0"
+  sprintf:
+    dependency: transitive
+    description:
+      name: sprintf
+      sha256: "1fc9ffe69d4df602376b52949af107d8f5703b77cda567c4d7d86a0693120f23"
+      url: "https://pub.dev"
+    source: hosted
+    version: "7.0.0"
   stack_trace:
     dependency: transitive
     description:
@@ -453,6 +525,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "2.2.2"
+  uuid:
+    dependency: transitive
+    description:
+      name: uuid
+      sha256: cd210a09f7c18cbe5a02511718e0334de6559871052c90a90c0cca46a4aa81c8
+      url: "https://pub.dev"
+    source: hosted
+    version: "4.3.3"
   vector_math:
     dependency: transitive
     description:
diff --git a/pubspec.yaml b/pubspec.yaml
index e373df3..990f68f 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -42,6 +42,7 @@ dependencies:
   latlong2: ^0.9.0
   http: ^1.2.1
   flutter_timer_countdown: ^1.0.7
+  geolocator: ^11.0.0
 
 dev_dependencies:
   flutter_test: