Big Changes

This commit is contained in:
Supan Adit Pratama 2020-01-05 20:04:50 +07:00
parent 9959084e59
commit 4f3b80f87f
5 changed files with 194 additions and 75 deletions

View File

@ -4,8 +4,6 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide

View File

@ -23,7 +23,7 @@ class PositionBloc {
_subject.sink.add(response);
}
stopTracking(String lat, String lng) async {
stopTracking() async {
ResponseModel response = await _repository.stopTracking();
_subject.sink.add(response);
}

View File

@ -1,6 +1,8 @@
import 'dart:async';
import 'dart:io';
import 'package:background_location/background_location.dart';
import 'package:device_info/device_info.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:geo_app/bloc/position_bloc.dart';
@ -8,6 +10,10 @@ import 'package:geo_app/bloc/setting.dart';
import 'package:geo_app/model/position.dart';
import 'package:geo_app/page/setting.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart'
as bg;
import 'package:permission_handler/permission_handler.dart';
import 'package:rxdart/rxdart.dart';
class Map extends StatefulWidget {
Map({Key key, this.title}) : super(key: key);
@ -29,17 +35,21 @@ class _MapState extends State<Map> {
Position _position;
PositionBloc _positionBloc;
SettingBloc _settingBloc;
PublishSubject<bool> _checkDevice;
bool isChecking = true;
String id;
String host;
bool isGranted = false;
bool isTracking = false;
int androidSDKVersion = 0;
@override
void initState() {
super.initState();
BackgroundLocation.checkPermissions();
_MapState() {
_checkDevice = PublishSubject<bool>();
}
getPermission() {
BackgroundLocation.getPermissions(
onDenied: () {
isGranted = false;
@ -50,6 +60,121 @@ class _MapState extends State<Map> {
print("Granted");
},
);
}
@override
void initState() {
super.initState();
PermissionHandler()
.requestPermissions([PermissionGroup.location]).then((permissions) {
permissions.forEach((group, status) {
print("$group With Status $status");
});
});
if (Platform.isAndroid) {
PermissionHandler()
.shouldShowRequestPermissionRationale(PermissionGroup.location)
.then((isShow) {
print("Is Show $isShow");
});
DeviceInfoPlugin().androidInfo.then((androidInfo) {
var release = androidInfo.version.release;
var sdkInt = androidInfo.version.sdkInt;
var manufacturer = androidInfo.manufacturer;
var model = androidInfo.model;
print('Android $release (SDK $sdkInt), $manufacturer $model');
androidSDKVersion = sdkInt;
if (sdkInt > 21) {
getPermission();
BackgroundLocation.getLocationUpdates((location) async {
print("Location Updated");
if (!isTracking) {
this.isTracking = true;
}
if (!isGranted) {
this.isGranted = true;
}
this.latitude = location.latitude;
this.longitude = location.longitude;
this.accuracy = location.accuracy;
this.altitude = location.altitude;
this.bearing = location.bearing;
this.speed = location.speed;
if (this.id != null) {
this._position = Position(
id: this.id,
lat: this.latitude.toString(),
lng: this.longitude.toString(),
type: "user",
);
} else {
stopLocationService();
settingPage();
}
if (this._position != null) {
if (this._position.isValid()) {
if (_positionBloc != null) {
_positionBloc.sendPosition(
this._position.lat.toString(),
this._position.lng.toString(),
);
} else {
Fluttertoast.showToast(
msg: "Failed to start position service",
);
settingPage();
}
} else {
print("Invalid Position");
}
}
setState(() {
isChecking = false;
});
});
}
if (sdkInt <= 21) {
bg.BackgroundGeolocation.onLocation((bg.Location location) {
print('[location] - $location');
});
bg.BackgroundGeolocation.onMotionChange((bg.Location location) {
print('[motionchange] - $location');
});
bg.BackgroundGeolocation.onProviderChange(
(bg.ProviderChangeEvent event) {
print('[providerchange] - $event');
});
bg.BackgroundGeolocation.ready(
bg.Config(
desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
distanceFilter: 10.0,
stopOnTerminate: false,
startOnBoot: true,
debug: true,
logLevel: bg.Config.LOG_LEVEL_VERBOSE,
),
).then(
(bg.State state) {
if (!state.enabled) {
isGranted = true;
isChecking = false;
}
},
);
}
});
} else {
getPermission();
setState(() {
isChecking = false;
});
}
_settingBloc = new SettingBloc();
_settingBloc.getSetting();
@ -67,59 +192,13 @@ class _MapState extends State<Map> {
_positionBloc = new PositionBloc(settingModel);
_positionBloc.subject.listen((position) {
if (position.isError()) {
BackgroundLocation.stopLocationService();
stopLocationService();
settingPage();
}
});
});
}
});
BackgroundLocation.getLocationUpdates((location) async {
if (!isTracking) {
this.isTracking = true;
}
if (!isGranted) {
this.isGranted = true;
}
print("Location Updated");
this.latitude = location.latitude;
this.longitude = location.longitude;
this.accuracy = location.accuracy;
this.altitude = location.altitude;
this.bearing = location.bearing;
this.speed = location.speed;
if (this.id != null) {
this._position = Position(
id: this.id,
lat: this.latitude.toString(),
lng: this.longitude.toString(),
type: "user",
);
} else {
BackgroundLocation.stopLocationService();
settingPage();
}
if (this._position != null) {
if (this._position.isValid()) {
if (_positionBloc != null) {
_positionBloc.sendPosition(
this._position.lat.toString(),
this._position.lng.toString(),
);
} else {
Fluttertoast.showToast(
msg: "Failed to start position service",
);
settingPage();
}
} else {
print("Invalid Position");
}
}
});
}
static final CameraPosition _kGooglePlex = CameraPosition(
@ -153,7 +232,9 @@ class _MapState extends State<Map> {
color: (isTracking) ? Colors.redAccent : Colors.green,
onPressed: toggleTracking,
child: Text(
(isTracking) ? "Stop Tracking" : "Start Tracking",
(isChecking)
? "Pleasewait"
: ((isTracking) ? "Stop Tracking" : "Start Tracking"),
style: TextStyle(color: Colors.white),
),
),
@ -165,20 +246,43 @@ class _MapState extends State<Map> {
);
}
stopLocationService() {
if (androidSDKVersion > 21 || !Platform.isAndroid) {
BackgroundLocation.stopLocationService();
}
if (androidSDKVersion <= 21) {
bg.BackgroundGeolocation.stop();
}
if (_positionBloc != null) {
_positionBloc.stopTracking();
}
}
startLocationService() {
if (androidSDKVersion > 21 || !Platform.isAndroid) {
BackgroundLocation.startLocationService();
}
if (androidSDKVersion <= 21) {
bg.BackgroundGeolocation.start();
}
}
toggleTracking() {
if (isGranted) {
if (isTracking) {
BackgroundLocation.stopLocationService();
if (!isChecking) {
if (isGranted) {
if (isTracking) {
stopLocationService();
} else {
startLocationService();
}
setState(() {
isTracking = !isTracking;
});
} else {
BackgroundLocation.startLocationService();
Fluttertoast.showToast(
msg: "Make sure you have turn on location services",
);
}
setState(() {
isTracking = !isTracking;
});
} else {
Fluttertoast.showToast(
msg: "Make sure you have turn on location services",
);
}
}
@ -192,6 +296,6 @@ class _MapState extends State<Map> {
void dispose() {
print("Disposed");
super.dispose();
BackgroundLocation.stopLocationService();
stopLocationService();
}
}

View File

@ -8,13 +8,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
adhara_socket_io:
dependency: "direct main"
description:
name: adhara_socket_io
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.1"
analyzer:
dependency: transitive
description:
@ -43,6 +36,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
background_fetch:
dependency: transitive
description:
name: background_fetch
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.0"
background_location:
dependency: "direct main"
description:
@ -127,6 +127,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.3"
device_info:
dependency: "direct main"
description:
name: device_info
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.1+4"
dio:
dependency: "direct main"
description:
@ -139,6 +146,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_background_geolocation:
dependency: "direct main"
description:
name: flutter_background_geolocation
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
flutter_test:
dependency: "direct dev"
description: flutter
@ -276,7 +290,7 @@ packages:
source: hosted
version: "1.8.0+1"
permission_handler:
dependency: transitive
dependency: "direct main"
description:
name: permission_handler
url: "https://pub.dartlang.org"

View File

@ -31,6 +31,9 @@ dependencies:
rxdart: ^0.23.1
dio: ^3.0.7
fluttertoast: ^3.1.3
device_info: ^0.4.1+4
flutter_background_geolocation: ^1.5.0
permission_handler: ^4.0.0
dev_dependencies:
flutter_test: