Begin implementing GPS related functions, and initial UI for trip and delivery
This commit is contained in:
parent
061fd46f89
commit
770c1e7c74
6 changed files with 276 additions and 3 deletions
121
lib/data.dart
Normal file
121
lib/data.dart
Normal file
|
@ -0,0 +1,121 @@
|
|||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:libac_dart/utils/TimeUtils.dart';
|
||||
import 'package:timetrack/consts.dart';
|
||||
|
||||
class SessionData {
|
||||
static int StartTime = 0;
|
||||
|
||||
static get StartTimeAsDateTime =>
|
||||
DateTime.fromMillisecondsSinceEpoch(StartTime * 1000);
|
||||
|
||||
static bool IsOnTheClock = false;
|
||||
|
||||
static List<Trip> Trips = [];
|
||||
|
||||
static Delivery? currentDelivery;
|
||||
static Trip? currentTrip;
|
||||
List<Position> positions = [];
|
||||
|
||||
static Future<void> Login() async {
|
||||
StartTime = TimeUtils.getUnixTimestamp();
|
||||
IsOnTheClock = true;
|
||||
|
||||
bool hasGPS;
|
||||
|
||||
LocationPermission perm;
|
||||
hasGPS = await Geolocator.isLocationServiceEnabled();
|
||||
if (!hasGPS) {
|
||||
IsOnTheClock = false;
|
||||
return Future.error("Location services are disabled");
|
||||
}
|
||||
|
||||
perm = await Geolocator.checkPermission();
|
||||
if (perm == LocationPermission.denied) {
|
||||
perm = await Geolocator.requestPermission();
|
||||
if (perm == LocationPermission.denied) {
|
||||
IsOnTheClock = false;
|
||||
return Future.error("Location permissions are denied");
|
||||
}
|
||||
}
|
||||
|
||||
if (perm == LocationPermission.deniedForever) {
|
||||
IsOnTheClock = false;
|
||||
return Future.error(
|
||||
"Location permissions are denied permanently. Login cannot proceed.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> Logout() async {
|
||||
IsOnTheClock = false;
|
||||
|
||||
// TODO: Do other tasks to finalize the saved work data.
|
||||
}
|
||||
|
||||
static Future<Position> GetNewLocation() async {
|
||||
Position pos = await Geolocator.getCurrentPosition(
|
||||
locationSettings: TTConsts.LOCATION_SETTINGS,
|
||||
);
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
static Trip GetNewTrip({required double basePay}) {
|
||||
currentTrip = Trip(BasePay: basePay);
|
||||
return currentTrip!;
|
||||
}
|
||||
|
||||
static Delivery GetNewDelivery() {
|
||||
if (currentTrip != null) {
|
||||
var dropOff = currentTrip!.startNewDelivery();
|
||||
;
|
||||
currentDelivery = dropOff;
|
||||
return dropOff;
|
||||
} else {
|
||||
throw Exception("A delivery cannot exist without a trip");
|
||||
}
|
||||
}
|
||||
|
||||
static void EndTrip() {
|
||||
currentDelivery = null;
|
||||
currentTrip = null;
|
||||
}
|
||||
}
|
||||
|
||||
class Delivery {
|
||||
double TipAmount = 0;
|
||||
late Position endLocation;
|
||||
int StartTime = 0;
|
||||
DateTime get StartTimeAsDateTime =>
|
||||
DateTime.fromMillisecondsSinceEpoch(StartTime * 1000);
|
||||
|
||||
Delivery() {
|
||||
StartTime = TimeUtils.getUnixTimestamp();
|
||||
}
|
||||
|
||||
Future<void> MarkEndLocation() async {
|
||||
var pos = await SessionData.GetNewLocation();
|
||||
endLocation = pos;
|
||||
}
|
||||
}
|
||||
|
||||
class Trip {
|
||||
List<Delivery> deliveries = [];
|
||||
|
||||
int StartTime = 0;
|
||||
DateTime get StartTimeAsDateTime =>
|
||||
DateTime.fromMillisecondsSinceEpoch(StartTime * 1000);
|
||||
|
||||
double BasePay = 0.0;
|
||||
|
||||
Trip({required this.BasePay}) {
|
||||
StartTime = TimeUtils.getUnixTimestamp();
|
||||
}
|
||||
|
||||
Delivery startNewDelivery() {
|
||||
var delivery = Delivery();
|
||||
deliveries.add(delivery);
|
||||
|
||||
return delivery;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue