TimeTracker/lib/data.dart

121 lines
2.8 KiB
Dart

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;
}
}