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,39 +60,40 @@ class _MapState extends State<Map> {
print("Granted");
},
);
_settingBloc = new SettingBloc();
_settingBloc.getSetting();
_settingBloc.subject.listen((settingModel) {
this.id = settingModel.id;
this.host = settingModel.host;
if (settingModel.isNullId()) {
settingPage();
}
if (this.host != null && this.host != "") {
setState(() {
_positionBloc = new PositionBloc(settingModel);
_positionBloc.subject.listen((position) {
if (position.isError()) {
BackgroundLocation.stopLocationService();
settingPage();
}
@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;
}
print("Location Updated");
this.latitude = location.latitude;
this.longitude = location.longitude;
this.accuracy = location.accuracy;
@ -98,7 +109,7 @@ class _MapState extends State<Map> {
type: "user",
);
} else {
BackgroundLocation.stopLocationService();
stopLocationService();
settingPage();
}
@ -119,6 +130,74 @@ class _MapState extends State<Map> {
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();
_settingBloc.subject.listen((settingModel) {
this.id = settingModel.id;
this.host = settingModel.host;
if (settingModel.isNullId()) {
settingPage();
}
if (this.host != null && this.host != "") {
setState(() {
_positionBloc = new PositionBloc(settingModel);
_positionBloc.subject.listen((position) {
if (position.isError()) {
stopLocationService();
settingPage();
}
});
});
}
});
}
@ -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,12 +246,34 @@ 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 (!isChecking) {
if (isGranted) {
if (isTracking) {
BackgroundLocation.stopLocationService();
stopLocationService();
} else {
BackgroundLocation.startLocationService();
startLocationService();
}
setState(() {
isTracking = !isTracking;
@ -181,6 +284,7 @@ class _MapState extends State<Map> {
);
}
}
}
settingPage() {
Navigator.of(context).pushReplacement(new MaterialPageRoute(
@ -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: