Skip to content

Commit

Permalink
feat: location service implemented (#51)
Browse files Browse the repository at this point in the history
* feat: location service implemented
  • Loading branch information
enescevik authored Feb 15, 2023
1 parent bb8adc0 commit 6124857
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 4 deletions.
6 changes: 6 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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. -->

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

<application
android:name="${applicationName}"
android:label="Nylo"
Expand Down
6 changes: 6 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,11 @@
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your location</string>
</dict>
</plist>
20 changes: 18 additions & 2 deletions lib/app/services/firestore_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -31,9 +34,22 @@ class FirestoreService implements IDatabaseService {
}

Future<bool> 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<void> 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 {
Expand Down
57 changes: 57 additions & 0 deletions lib/app/services/location_service.dart
Original file line number Diff line number Diff line change
@@ -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<LocationData>? _locationSub;

Future<void> 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<LocationData> getLocation() async {
return await _location.getLocation();
}

Future<void> dispose() async {
_locationSub?.cancel();
}
}
5 changes: 4 additions & 1 deletion lib/resources/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -23,11 +24,13 @@ class _HomePageState extends NyState<HomePage> {

@override
init() async {
await LocationService().init();
super.init();
}

@override
void dispose() {
dispose() async {
await LocationService().dispose();
super.dispose();
}

Expand Down
1 change: 0 additions & 1 deletion lib/resources/pages/register_page.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
6 changes: 6 additions & 0 deletions macos/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your location</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your location</string>
</dict>
</plist>
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6124857

Please sign in to comment.