Fix background svc; Add end time logging to trips
This commit is contained in:
parent
1e01384d9b
commit
35863780f6
6 changed files with 99 additions and 15 deletions
|
@ -5,6 +5,7 @@ import 'dart:ui';
|
|||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_background/flutter_background.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:libac_dart/nbt/Stream.dart';
|
||||
import 'package:timetrack/consts.dart';
|
||||
|
@ -25,6 +26,7 @@ class SessionData {
|
|||
static String LastSessionID = "";
|
||||
static String DisplayError = "";
|
||||
static double? TotalPay;
|
||||
static bool ContainsTripTimes = true;
|
||||
|
||||
/// Is true if the try-catch is tripped or if not running on Android
|
||||
static bool isWeb = false;
|
||||
|
@ -51,6 +53,27 @@ class SessionData {
|
|||
return total;
|
||||
}
|
||||
|
||||
static String GetPaidHours() {
|
||||
return Duration2Notation(_GetPaidHours());
|
||||
}
|
||||
|
||||
static Duration _GetPaidHours() {
|
||||
Duration stamp = Duration();
|
||||
for (var trip in Trips) {
|
||||
stamp += trip.EndTime.difference(trip.StartTime);
|
||||
}
|
||||
|
||||
return stamp;
|
||||
}
|
||||
|
||||
static String GetUnpaidHours() {
|
||||
// This is the inverted value of paid hours. We get total hours and subtract it from the paid hours. This gives us the unpaid hours.
|
||||
Duration totalTime = EndTime.difference(StartTime);
|
||||
Duration unpaid = _GetPaidHours() - totalTime;
|
||||
|
||||
return Duration2Notation(unpaid);
|
||||
}
|
||||
|
||||
static String GetTotalMilesAsString() {
|
||||
double miles = GetTotalMiles();
|
||||
if (miles == 0) return "0.0";
|
||||
|
@ -126,7 +149,21 @@ class SessionData {
|
|||
}
|
||||
//** End AI Generated code */
|
||||
|
||||
static Future<void> Login() async {
|
||||
static Future<bool> Login() async {
|
||||
final androidConfig = FlutterBackgroundAndroidConfig(
|
||||
notificationTitle: "Time Tracker",
|
||||
notificationText:
|
||||
"Background notification for keeping TimeTrack running in the background",
|
||||
notificationImportance: AndroidNotificationImportance.normal,
|
||||
);
|
||||
bool success = await FlutterBackground.initialize(
|
||||
androidConfig: androidConfig,
|
||||
);
|
||||
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
StartTime = DateTime.now();
|
||||
IsOnTheClock = true;
|
||||
|
||||
|
@ -136,7 +173,7 @@ class SessionData {
|
|||
hasGPS = await Geolocator.isLocationServiceEnabled();
|
||||
if (!hasGPS) {
|
||||
IsOnTheClock = false;
|
||||
return Future.error("Location services are disabled");
|
||||
return await Future.error("Location services are disabled");
|
||||
}
|
||||
|
||||
perm = await Geolocator.checkPermission();
|
||||
|
@ -144,13 +181,13 @@ class SessionData {
|
|||
perm = await Geolocator.requestPermission();
|
||||
if (perm == LocationPermission.denied) {
|
||||
IsOnTheClock = false;
|
||||
return Future.error("Location permissions are denied");
|
||||
return await Future.error("Location permissions are denied");
|
||||
}
|
||||
}
|
||||
|
||||
if (perm == LocationPermission.deniedForever) {
|
||||
IsOnTheClock = false;
|
||||
return Future.error(
|
||||
return await Future.error(
|
||||
"Location permissions are denied permanently. Login cannot proceed.",
|
||||
);
|
||||
}
|
||||
|
@ -166,6 +203,8 @@ class SessionData {
|
|||
|
||||
SessionData.Calls.dispatch();
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static Future<void> Logout() async {
|
||||
|
@ -305,11 +344,14 @@ class SessionData {
|
|||
/// [b] is the end time
|
||||
static String GetTotalTimeWorked(DateTime a, DateTime b) {
|
||||
Duration diff = b.difference(a);
|
||||
return Duration2Notation(diff);
|
||||
}
|
||||
|
||||
int days = diff.inDays;
|
||||
int hours = diff.inHours.remainder(24);
|
||||
int minutes = diff.inMinutes.remainder(60);
|
||||
int seconds = diff.inSeconds.remainder(60);
|
||||
static String Duration2Notation(Duration time) {
|
||||
int days = time.inDays;
|
||||
int hours = time.inHours.remainder(24);
|
||||
int minutes = time.inMinutes.remainder(60);
|
||||
int seconds = time.inSeconds.remainder(60);
|
||||
|
||||
List<String> parts = [];
|
||||
|
||||
|
@ -361,6 +403,7 @@ class Trip {
|
|||
List<Delivery> deliveries = [];
|
||||
|
||||
DateTime StartTime = DateTime(0);
|
||||
DateTime EndTime = DateTime(0);
|
||||
|
||||
Trip() {
|
||||
StartTime = DateTime.now();
|
||||
|
@ -374,7 +417,10 @@ class Trip {
|
|||
}
|
||||
|
||||
Map<String, dynamic> toJsonMap() {
|
||||
Map<String, Object> trip = {"start": StartTime.toString()};
|
||||
Map<String, Object> trip = {
|
||||
"start": StartTime.toString(),
|
||||
"end": EndTime.toString(),
|
||||
};
|
||||
List<Map<String, dynamic>> dropOffs = [];
|
||||
for (var delivery in deliveries) {
|
||||
dropOffs.add(delivery.toJsonMap());
|
||||
|
@ -388,6 +434,10 @@ class Trip {
|
|||
static Trip fromJsonMap(Map<String, dynamic> jsx) {
|
||||
Trip trip = Trip();
|
||||
trip.StartTime = DateTime.parse(jsx['start'] as String);
|
||||
if (jsx.containsKey("end")) {
|
||||
trip.EndTime = DateTime.parse(jsx['end'] as String);
|
||||
} else
|
||||
SessionData.ContainsTripTimes = false;
|
||||
trip.deliveries = [];
|
||||
List<dynamic> dropOffs = jsx['deliveries'] as List<dynamic>;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue