Add the patch method for protocol 2

This commit is contained in:
zontreck 2025-06-15 02:25:23 -07:00
parent 8adaf6169a
commit 0a3fbbc944
4 changed files with 48 additions and 2 deletions

View file

@ -9,7 +9,7 @@ class TTConsts {
static get SESSION_SERVER => static get SESSION_SERVER =>
"https://api.zontreck.com/timetrack/$UPDATE_CHANNEL/timetrack.php"; "https://api.zontreck.com/timetrack/$UPDATE_CHANNEL/timetrack.php";
static const VERSION = "1.0.0-beta.32"; static const VERSION = "1.0.0-beta.33";
static bool UPDATE_AVAILABLE = false; static bool UPDATE_AVAILABLE = false;
static UpdateChannel UPDATE_CHANNEL = UpdateChannel.beta; static UpdateChannel UPDATE_CHANNEL = UpdateChannel.beta;

View file

@ -52,6 +52,9 @@ class SessionData {
/// This is the version number of the recording as specified by the server. /// This is the version number of the recording as specified by the server.
static int RecordingVersion = 0; static int RecordingVersion = 0;
/// This is used for the patch method detection. Do not edit this value, it will be overwritten.
static double LastTotalMiles = 0.0;
/// Is true if the try-catch is tripped or if not running on Android /// Is true if the try-catch is tripped or if not running on Android
static bool isWeb = false; static bool isWeb = false;
@ -189,6 +192,37 @@ class SessionData {
} }
//** End AI Generated code */ //** End AI Generated code */
/// A simple function that performs some immediate actions when a action has been performed. This does not replace the dirty state handler, which can perform additional tasks, such as upload of the patch data.
static Future<void> ActionPerformed() async {
await SaveCacheState();
DirtyState = true;
}
/// This replaces _upload. This method will serialize the current data as is with no alterations. It will then upload the data to the server in a patch.
static Future<void> _performPatch() async {
var data = await _serializeToNBT();
Dio dio = Dio();
// This is where we send the data to the server. In the server reply to a patch will be the new version number. This is not currently needed.
var packet = json.encode({
"cmd": "patch",
"id": LastSessionID,
"data": base64.encode(await NbtIo.writeToStream(data)),
});
var reply = await dio.post(TTConsts.SESSION_SERVER, data: packet);
var replyData = reply.data as String;
var replyJs = json.decode(replyData);
if (replyJs['status'] == "patched") {
RecordingVersion = replyJs['version'] as int;
return;
} else {
DisplayError = "Error: Patch upload failed. Retrying...";
Timer(Duration(seconds: 5), () async {
_performPatch();
});
}
}
static Future<bool> Login() async { static Future<bool> Login() async {
final androidConfig = FlutterBackgroundAndroidConfig( final androidConfig = FlutterBackgroundAndroidConfig(
notificationTitle: "Time Tracker", notificationTitle: "Time Tracker",
@ -256,6 +290,12 @@ class SessionData {
SessionData.SaveCacheState(); SessionData.SaveCacheState();
SessionData.Calls.dispatch(); SessionData.Calls.dispatch();
var curMiles = GetTotalMiles();
if (LastTotalMiles + 0.25 < curMiles) {
_performPatch();
LastTotalMiles = curMiles;
}
}); });
return true; return true;

View file

@ -136,6 +136,7 @@ class _HomePageState extends State<HomePage> {
setState(() { setState(() {
SessionData.ResetAppSession(); SessionData.ResetAppSession();
SessionData.DirtyState = true; SessionData.DirtyState = true;
SessionData.ActionPerformed();
}); });
}, },
), ),
@ -182,6 +183,7 @@ class _HomePageState extends State<HomePage> {
setState(() {}); setState(() {});
SessionData.DirtyState = true; SessionData.DirtyState = true;
SessionData.ActionPerformed();
}, },
child: Text("ENGAGE"), child: Text("ENGAGE"),
), ),
@ -228,6 +230,7 @@ class _HomePageState extends State<HomePage> {
setState(() {}); setState(() {});
SessionData.DirtyState = true; SessionData.DirtyState = true;
SessionData.ActionPerformed();
}, },
child: Text("Yes"), child: Text("Yes"),
), ),
@ -273,6 +276,7 @@ class _HomePageState extends State<HomePage> {
setState(() {}); setState(() {});
SessionData.DirtyState = true; SessionData.DirtyState = true;
SessionData.ActionPerformed();
}, },
child: Text("Yes"), child: Text("Yes"),
), ),
@ -316,6 +320,7 @@ class _HomePageState extends State<HomePage> {
setState(() {}); setState(() {});
SessionData.DirtyState = true; SessionData.DirtyState = true;
SessionData.ActionPerformed();
}, },
child: Text("Yes"), child: Text("Yes"),
), ),
@ -386,6 +391,7 @@ class _HomePageState extends State<HomePage> {
setState(() {}); setState(() {});
SessionData.DirtyState = true; SessionData.DirtyState = true;
SessionData.ActionPerformed();
}, },
child: Text("Yes"), child: Text("Yes"),
), ),

View file

@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts # In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix. # of the product and file versions while build-number is used as the build suffix.
version: 1.0.0-beta.32 version: 1.0.0-beta.33
environment: environment:
sdk: ^3.7.2 sdk: ^3.7.2