From 0a3fbbc944bb3c15e8223f6ac8f4f9b7c7112c01 Mon Sep 17 00:00:00 2001 From: zontreck Date: Sun, 15 Jun 2025 02:25:23 -0700 Subject: [PATCH] Add the patch method for protocol 2 --- lib/consts.dart | 2 +- lib/data.dart | 40 ++++++++++++++++++++++++++++++++++++++++ lib/pages/HomePage.dart | 6 ++++++ pubspec.yaml | 2 +- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/consts.dart b/lib/consts.dart index 6ddee55..0f49b9f 100644 --- a/lib/consts.dart +++ b/lib/consts.dart @@ -9,7 +9,7 @@ class TTConsts { static get SESSION_SERVER => "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 UpdateChannel UPDATE_CHANNEL = UpdateChannel.beta; diff --git a/lib/data.dart b/lib/data.dart index cdc8bc4..cdc1c51 100644 --- a/lib/data.dart +++ b/lib/data.dart @@ -52,6 +52,9 @@ class SessionData { /// This is the version number of the recording as specified by the server. 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 static bool isWeb = false; @@ -189,6 +192,37 @@ class SessionData { } //** 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 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 _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 Login() async { final androidConfig = FlutterBackgroundAndroidConfig( notificationTitle: "Time Tracker", @@ -256,6 +290,12 @@ class SessionData { SessionData.SaveCacheState(); SessionData.Calls.dispatch(); + + var curMiles = GetTotalMiles(); + if (LastTotalMiles + 0.25 < curMiles) { + _performPatch(); + LastTotalMiles = curMiles; + } }); return true; diff --git a/lib/pages/HomePage.dart b/lib/pages/HomePage.dart index 8edbac1..dc61513 100644 --- a/lib/pages/HomePage.dart +++ b/lib/pages/HomePage.dart @@ -136,6 +136,7 @@ class _HomePageState extends State { setState(() { SessionData.ResetAppSession(); SessionData.DirtyState = true; + SessionData.ActionPerformed(); }); }, ), @@ -182,6 +183,7 @@ class _HomePageState extends State { setState(() {}); SessionData.DirtyState = true; + SessionData.ActionPerformed(); }, child: Text("ENGAGE"), ), @@ -228,6 +230,7 @@ class _HomePageState extends State { setState(() {}); SessionData.DirtyState = true; + SessionData.ActionPerformed(); }, child: Text("Yes"), ), @@ -273,6 +276,7 @@ class _HomePageState extends State { setState(() {}); SessionData.DirtyState = true; + SessionData.ActionPerformed(); }, child: Text("Yes"), ), @@ -316,6 +320,7 @@ class _HomePageState extends State { setState(() {}); SessionData.DirtyState = true; + SessionData.ActionPerformed(); }, child: Text("Yes"), ), @@ -386,6 +391,7 @@ class _HomePageState extends State { setState(() {}); SessionData.DirtyState = true; + SessionData.ActionPerformed(); }, child: Text("Yes"), ), diff --git a/pubspec.yaml b/pubspec.yaml index 6891933..dbc3b3a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 # 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. -version: 1.0.0-beta.32 +version: 1.0.0-beta.33 environment: sdk: ^3.7.2