You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.4 KiB
Dart
101 lines
3.4 KiB
Dart
import 'dart:collection';
|
|
|
|
import 'package:driverapp/config/config.dart';
|
|
import 'package:driverapp/core/model/initiate_order_delivey/request_initiate_order_delivery.dart';
|
|
import 'package:driverapp/core/model/orders/deliverd_order_res_model.dart';
|
|
import 'package:driverapp/core/model/orders/next_order_request_model.dart';
|
|
import 'package:driverapp/core/model/orders/pending_orders_req_model.dart';
|
|
import 'package:driverapp/core/model/orders/pending_orders_res_model.dart';
|
|
import 'package:driverapp/core/model/orders/update_order_status_request_model.dart';
|
|
import 'package:driverapp/core/model/scan_qr/scan_qr_request_model.dart';
|
|
import 'package:driverapp/core/model/update_driver_location/request_update_driver_location.dart';
|
|
import 'package:driverapp/core/service/base_service.dart';
|
|
import 'package:driverapp/uitl/utils.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:latlong/latlong.dart';
|
|
import 'package:location/location.dart';
|
|
|
|
|
|
class DeliveryTrackingServices extends BaseService {
|
|
var _distanceFilter = 5; // [Meters] (Call update API if distance covered from last location is greater then 'value' )
|
|
|
|
var initiateOrderDeliveryResponse = null;
|
|
|
|
Future<Object> initiateOrderDelivery(int orderID) async {
|
|
hasError = false;
|
|
|
|
var order = RequestInitiateOrderDelivery(orderID: orderID);
|
|
|
|
try {
|
|
await baseAppClient.post(PING_SERVICE,
|
|
onSuccess: (dynamic response, int statusCode) {
|
|
initiateOrderDeliveryResponse = response;
|
|
|
|
}, onFailure: (String error, int statusCode) {
|
|
super.hasError = true;
|
|
super.error = error;
|
|
}, body: order.toJson());
|
|
|
|
} catch (e) {
|
|
hasError = true;
|
|
super.error = error;
|
|
throw e;
|
|
}
|
|
|
|
return initiateOrderDeliveryResponse;
|
|
}
|
|
|
|
_updateDriverLocation(int orderID, LocationData location) async {
|
|
if(location != null){
|
|
debugPrint("Updating driver location");
|
|
var order = RequestUpdateDriverLocation(orderID: orderID, location: location);
|
|
await baseAppClient.post(UPDATE_DRIVER_LOCATION,
|
|
onSuccess: (dynamic response, int statusCode) {
|
|
|
|
}, onFailure: (String errorMessage, int statusCode) {
|
|
if(statusCode == 200){
|
|
// Server responded but check failed (stop updating location)
|
|
// stopLocationUpdate(orderID);
|
|
// Show error message that you received in response
|
|
}
|
|
}, body: order.toJson());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
List _currentRunnings = List<int>();
|
|
Future<Object> startLocationUpdate(int orderID, {int frequencyInSeconds = 3}) async {
|
|
_currentRunnings.remove(orderID); // remove existing if its already running
|
|
_currentRunnings.add(orderID);
|
|
|
|
while(_currentRunnings.contains(orderID)){
|
|
await Future.delayed(Duration(seconds: frequencyInSeconds));
|
|
Utils.possibleToGetLocation((value) async{
|
|
if(value){
|
|
var location = await Utils.getLocation();
|
|
if (_haveEnoughLocationUpdate(location))
|
|
await _updateDriverLocation(orderID, location);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
stopLocationUpdate(int orderID){
|
|
_currentRunnings.remove(orderID);
|
|
}
|
|
|
|
LocationData _lastLocation;
|
|
bool _haveEnoughLocationUpdate(LocationData location){
|
|
if(_lastLocation == null) {
|
|
_lastLocation = location;
|
|
return true;
|
|
}else{
|
|
var distanceCovered = Utils.distanceBetween(_lastLocation, location, LengthUnit.Meter);
|
|
return (distanceCovered > _distanceFilter);
|
|
}
|
|
}
|
|
|
|
}
|