From 61248577725b969adb421c71130aedf4112543bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enes=20=C3=87EV=C4=B0K?= Date: Wed, 15 Feb 2023 22:43:52 +0300 Subject: [PATCH] feat: location service implemented (#51) * feat: location service implemented --- android/app/src/main/AndroidManifest.xml | 6 +++ ios/Runner/Info.plist | 6 +++ lib/app/services/firestore_service.dart | 20 ++++++++- lib/app/services/location_service.dart | 57 ++++++++++++++++++++++++ lib/resources/pages/home_page.dart | 5 ++- lib/resources/pages/register_page.dart | 1 - macos/Runner/Info.plist | 6 +++ pubspec.lock | 24 ++++++++++ pubspec.yaml | 1 + 9 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 lib/app/services/location_service.dart diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 24ad7d2..74d3b06 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -5,6 +5,12 @@ In most cases you can leave this as-is, but you if you want to provide additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. --> + + + + + + UIApplicationSupportsIndirectInputEvents + NSLocationAlwaysAndWhenInUseUsageDescription + $(PRODUCT_NAME) would like to access your location + NSLocationAlwaysUsageDescription + $(PRODUCT_NAME) would like to access your location + NSLocationWhenInUseUsageDescription + $(PRODUCT_NAME) would like to access your location diff --git a/lib/app/services/firestore_service.dart b/lib/app/services/firestore_service.dart index 89ed5a4..e56394c 100644 --- a/lib/app/services/firestore_service.dart +++ b/lib/app/services/firestore_service.dart @@ -22,7 +22,10 @@ class FirestoreService implements IDatabaseService { if (isExist) { return; } - await firestore.collection(FirestoreCollectionPath.dev.name).doc(translator.uuid).set(translator.toJson()); + await firestore + .collection(FirestoreCollectionPath.dev.name) + .doc(translator.uuid) + .set(translator.toJson()); } catch (e) { print(e); log("Write user exception:", error: e); @@ -31,9 +34,22 @@ class FirestoreService implements IDatabaseService { } Future isUserExist({required Translator translator}) async { - final snapshot = await firestore.collection(FirestoreCollectionPath.dev.name).doc(translator.uuid).get(); + final snapshot = await firestore + .collection(FirestoreCollectionPath.dev.name) + .doc(translator.uuid) + .get(); return snapshot.exists; } + + Future updateUserLocation(GeoPoint location) async { + var userId = AuthService().currentUser?.uid; + if (userId != null) { + await firestore + .collection(FirestoreCollectionPath.dev.name) + .doc(userId) + .update({"location": location}); + } + } } abstract class IDatabaseService { diff --git a/lib/app/services/location_service.dart b/lib/app/services/location_service.dart new file mode 100644 index 0000000..cfd69b1 --- /dev/null +++ b/lib/app/services/location_service.dart @@ -0,0 +1,57 @@ +import 'dart:async'; + +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter_app/app/services/firestore_service.dart'; +import 'package:location/location.dart'; + +class LocationService { + final Location _location = Location(); + + StreamSubscription? _locationSub; + + Future init() async { + try { + final isServiceEnabled = await _location.serviceEnabled(); + if (!isServiceEnabled) { + final isServiceEnabled = await _location.requestService(); + if (!isServiceEnabled) { + return; + } + } + + final isPermissionGranted = await _location.hasPermission(); + if (isPermissionGranted == PermissionStatus.denied) { + final isPermissionGranted = await _location.requestPermission(); + if (isPermissionGranted != PermissionStatus.granted) { + return; + } + } + + _location.changeSettings( + accuracy: LocationAccuracy.high, + interval: Duration(minutes: 30).inMilliseconds, + distanceFilter: 10, + ); + + if (!kIsWeb) { + _location.enableBackgroundMode(enable: true); + } + + _locationSub = _location.onLocationChanged.listen((currentLocation) { + FirestoreService().updateUserLocation( + GeoPoint(currentLocation.latitude!, currentLocation.longitude!)); + }); + } catch (e) { + print(e); + } + } + + Future getLocation() async { + return await _location.getLocation(); + } + + Future dispose() async { + _locationSub?.cancel(); + } +} diff --git a/lib/resources/pages/home_page.dart b/lib/resources/pages/home_page.dart index 4c133ae..b5d212d 100644 --- a/lib/resources/pages/home_page.dart +++ b/lib/resources/pages/home_page.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_app/app/controllers/home_controller.dart'; +import 'package:flutter_app/app/services/location_service.dart'; import 'package:flutter_app/resources/themes/styles/light_theme_colors.dart'; import 'package:flutter_app/resources/widgets/atoms/custom_expandable_card.dart'; import 'package:flutter_app/resources/widgets/molecules/contact_us_card.dart'; @@ -23,11 +24,13 @@ class _HomePageState extends NyState { @override init() async { + await LocationService().init(); super.init(); } @override - void dispose() { + dispose() async { + await LocationService().dispose(); super.dispose(); } diff --git a/lib/resources/pages/register_page.dart b/lib/resources/pages/register_page.dart index b09c4c6..15e158b 100644 --- a/lib/resources/pages/register_page.dart +++ b/lib/resources/pages/register_page.dart @@ -1,4 +1,3 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_app/resources/extensions/dynamic_size_extension.dart'; import 'package:flutter_app/resources/extensions/padding_extension.dart'; diff --git a/macos/Runner/Info.plist b/macos/Runner/Info.plist index 4789daa..5fe61cc 100644 --- a/macos/Runner/Info.plist +++ b/macos/Runner/Info.plist @@ -28,5 +28,11 @@ MainMenu NSPrincipalClass NSApplication + NSLocationWhenInUseUsageDescription + $(PRODUCT_NAME) would like to access your location + NSLocationAlwaysAndWhenInUseUsageDescription + $(PRODUCT_NAME) would like to access your location + NSLocationAlwaysUsageDescription + $(PRODUCT_NAME) would like to access your location diff --git a/pubspec.lock b/pubspec.lock index 780080e..d54c889 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -701,6 +701,30 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.1" + location: + dependency: "direct main" + description: + name: location + sha256: "9051959f6f2ccadd887b28b66e9cbbcc25b6838e37cf9e894c421ccc0ebf80b5" + url: "https://pub.dev" + source: hosted + version: "4.4.0" + location_platform_interface: + dependency: transitive + description: + name: location_platform_interface + sha256: "62eeaf1658e92e4459b727f55a3c328eccbac8ba043fa6d262ac5286ad48384c" + url: "https://pub.dev" + source: hosted + version: "2.3.0" + location_web: + dependency: transitive + description: + name: location_web + sha256: "6c08c408a040534c0269c4ff9fe17eebb5a36dea16512fbaf116b9c8bc21545b" + url: "https://pub.dev" + source: hosted + version: "3.1.1" logger: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 97affae..c7edb10 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -41,6 +41,7 @@ dependencies: google_sign_in: 5.4.4 dropdown_search: 5.0.5 flutter_linkify: 5.0.2 + location: 4.4.0 dev_dependencies: build_runner: 2.3.3