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_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <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 <!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method. calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide 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); _subject.sink.add(response);
} }
stopTracking(String lat, String lng) async { stopTracking() async {
ResponseModel response = await _repository.stopTracking(); ResponseModel response = await _repository.stopTracking();
_subject.sink.add(response); _subject.sink.add(response);
} }

View File

@ -1,6 +1,8 @@
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'package:background_location/background_location.dart'; import 'package:background_location/background_location.dart';
import 'package:device_info/device_info.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart'; import 'package:fluttertoast/fluttertoast.dart';
import 'package:geo_app/bloc/position_bloc.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/model/position.dart';
import 'package:geo_app/page/setting.dart'; import 'package:geo_app/page/setting.dart';
import 'package:google_maps_flutter/google_maps_flutter.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 { class Map extends StatefulWidget {
Map({Key key, this.title}) : super(key: key); Map({Key key, this.title}) : super(key: key);
@ -29,17 +35,21 @@ class _MapState extends State<Map> {
Position _position; Position _position;
PositionBloc _positionBloc; PositionBloc _positionBloc;
SettingBloc _settingBloc; SettingBloc _settingBloc;
PublishSubject<bool> _checkDevice;
bool isChecking = true;
String id; String id;
String host; String host;
bool isGranted = false; bool isGranted = false;
bool isTracking = false; bool isTracking = false;
int androidSDKVersion = 0;
@override _MapState() {
void initState() { _checkDevice = PublishSubject<bool>();
super.initState(); }
BackgroundLocation.checkPermissions();
getPermission() {
BackgroundLocation.getPermissions( BackgroundLocation.getPermissions(
onDenied: () { onDenied: () {
isGranted = false; isGranted = false;
@ -50,6 +60,121 @@ class _MapState extends State<Map> {
print("Granted"); 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 = new SettingBloc();
_settingBloc.getSetting(); _settingBloc.getSetting();
@ -67,59 +192,13 @@ class _MapState extends State<Map> {
_positionBloc = new PositionBloc(settingModel); _positionBloc = new PositionBloc(settingModel);
_positionBloc.subject.listen((position) { _positionBloc.subject.listen((position) {
if (position.isError()) { if (position.isError()) {
BackgroundLocation.stopLocationService(); stopLocationService();
settingPage(); 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( static final CameraPosition _kGooglePlex = CameraPosition(
@ -153,7 +232,9 @@ class _MapState extends State<Map> {
color: (isTracking) ? Colors.redAccent : Colors.green, color: (isTracking) ? Colors.redAccent : Colors.green,
onPressed: toggleTracking, onPressed: toggleTracking,
child: Text( child: Text(
(isTracking) ? "Stop Tracking" : "Start Tracking", (isChecking)
? "Pleasewait"
: ((isTracking) ? "Stop Tracking" : "Start Tracking"),
style: TextStyle(color: Colors.white), 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() { toggleTracking() {
if (isGranted) { if (!isChecking) {
if (isTracking) { if (isGranted) {
BackgroundLocation.stopLocationService(); if (isTracking) {
stopLocationService();
} else {
startLocationService();
}
setState(() {
isTracking = !isTracking;
});
} else { } 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() { void dispose() {
print("Disposed"); print("Disposed");
super.dispose(); super.dispose();
BackgroundLocation.stopLocationService(); stopLocationService();
} }
} }

View File

@ -8,13 +8,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.1" 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: analyzer:
dependency: transitive dependency: transitive
description: description:
@ -43,6 +36,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.4.0" 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: background_location:
dependency: "direct main" dependency: "direct main"
description: description:
@ -127,6 +127,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.3" 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: dio:
dependency: "direct main" dependency: "direct main"
description: description:
@ -139,6 +146,13 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" 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: flutter_test:
dependency: "direct dev" dependency: "direct dev"
description: flutter description: flutter
@ -276,7 +290,7 @@ packages:
source: hosted source: hosted
version: "1.8.0+1" version: "1.8.0+1"
permission_handler: permission_handler:
dependency: transitive dependency: "direct main"
description: description:
name: permission_handler name: permission_handler
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"

View File

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