From e1634f921856edf416be68e00a02adc5e8b77be5 Mon Sep 17 00:00:00 2001 From: johnjore Date: Wed, 27 Nov 2024 18:37:46 +1100 Subject: [PATCH] Support backing up on first launch of the day --- Fragments/Fragment_preferences.cs | 1 + MainActivity.cs | 9 ++++-- Models/Settings.cs | 3 +- Resources/Xml/Preferences.xml | 9 +++++- Resources/values/strings.xml | 1 + Utils/Backup.cs | 47 +++++++++++++++++++++++++++++-- 6 files changed, 62 insertions(+), 8 deletions(-) diff --git a/Fragments/Fragment_preferences.cs b/Fragments/Fragment_preferences.cs index 84f798e..fe439ca 100644 --- a/Fragments/Fragment_preferences.cs +++ b/Fragments/Fragment_preferences.cs @@ -43,6 +43,7 @@ public class Fragment_Preferences : PreferenceFragmentCompat, ISharedPreferences public readonly static bool DrawTracksOnGui_b = true; //Draw Tracks on GUI at Startup public readonly static bool DisableMapRotate_b = false; //Disable Map Rotate. Lock North up public readonly static int KeepNBackups = 10; //Backup copies to keep + public readonly static bool EnableBackupAtStartup = true; //Enable backup when starting app (once per day) //Runtime only public const string COGGeoTiffServer = "https://elevation-tiles-prod.s3.amazonaws.com/geotiff/"; //Cloud Optimized GeoTiff Server diff --git a/MainActivity.cs b/MainActivity.cs index 80b3d89..faf10f6 100644 --- a/MainActivity.cs +++ b/MainActivity.cs @@ -58,9 +58,6 @@ protected override async void OnCreate(Bundle? savedInstanceState) //Clear out all backups //Utils.Misc.EmptyFolder(Fragment_Preferences.Backups); - Preferences.Clear("KeepNBackups"); - - //Logging string _Path = System.IO.Path.Combine(Fragment_Preferences.rootPath, Fragment_Preferences.logFile); Log.Logger = new LoggerConfiguration() @@ -179,6 +176,12 @@ protected override async void OnCreate(Bundle? savedInstanceState) //Create alarm Utilities.Alarms.CreateAlarm(); + + Log.Information($"Daily Backup?"); + if (Preferences.Get("EnableBackupAtStartup", Fragment_Preferences.EnableBackupAtStartup)) + { + MainThread.BeginInvokeOnMainThread(() => Backup.RunDailyBackup()); + } } catch (Exception ex) { diff --git a/Models/Settings.cs b/Models/Settings.cs index 8e4ecd8..4218152 100644 --- a/Models/Settings.cs +++ b/Models/Settings.cs @@ -6,7 +6,9 @@ public class DefaultPrefSettings public bool BackupRouteTrackData { get; set; } public bool BackupPOIData { get; set; } public bool BackupMapTiles { get; set; } + public int KeepNBackups { get; set; } public bool BackupElevationData { get; set; } + public bool EnableBackupAtStartup { get; set; } public bool MapLockNorth { get; set; } public bool TrackLocation { get; set; } public bool RecordingTrack { get; set; } @@ -14,7 +16,6 @@ public class DefaultPrefSettings public string? mapUTMZone { get; set; } public long mapScale { get; set; } public int freq_s_OffRoute { get; set; } - public int KeepNBackups { get; set; } public bool EnableOffRouteWarning { get; set; } public int OffTrackDistanceWarning_m { get; set; } public int OffTrackRouteSnooze_m { get; set; } diff --git a/Resources/Xml/Preferences.xml b/Resources/Xml/Preferences.xml index 527c907..67b40c6 100644 --- a/Resources/Xml/Preferences.xml +++ b/Resources/Xml/Preferences.xml @@ -116,8 +116,15 @@ android:key="KeepNBackups" android:inputType="numberDecimal" android:digits="0123456789" - android:defaultValue="20" + android:defaultValue="10" app:useSimpleSummaryProvider="true" /> + + + diff --git a/Resources/values/strings.xml b/Resources/values/strings.xml index c488246..5ec69de 100644 --- a/Resources/values/strings.xml +++ b/Resources/values/strings.xml @@ -97,6 +97,7 @@ "Export Rogaining POI" "Choose Backup Options" "Backups to Retain" + "Backup on first launch of the day" "Map Tile Sources" diff --git a/Utils/Backup.cs b/Utils/Backup.cs index ed8efba..91bbb09 100644 --- a/Utils/Backup.cs +++ b/Utils/Backup.cs @@ -21,6 +21,40 @@ namespace hajk { internal class Backup { + public static void RunDailyBackup() + { + bool[] checkedItems = [ + Preferences.Get("BackupPreferences", true), + Preferences.Get("BackupRoute&TrackData", true), + Preferences.Get("BackupPOIData", true), + Preferences.Get("BackupMapTiles", true), + Preferences.Get("BackupElevationData", true), + ]; + + //All files + var backupFiles = Directory.GetFiles(Fragment_Preferences.Backups); + + //Backup taken today? + string todaysDate = (DateTime.Now).ToString("yyMMdd"); + if (backupFiles.Where(x => x.Contains("/" + todaysDate + "-")).FirstOrDefault() != null) + { + Serilog.Log.Information($"Backup already taken today '{todaysDate}'"); + return; + } + + //Create progress bar + _ = Progressbar.UpdateProgressBar.CreateGUIAsync("Backup Progress"); + Progressbar.UpdateProgressBar.Progress = 0; + Progressbar.UpdateProgressBar.MessageBody = $"Starting backup"; + int ProgressBarIncrement = (int)Math.Ceiling((double)(100 / (checkedItems.Count(c => c == true) + 2))); //2 = Compress and remove old backup files + + Task.Run(() => + { + PerformBackups(ProgressBarIncrement); + }); + } + + public static void ShowBackupDialog() { //Size of live data @@ -91,6 +125,12 @@ public static void ShowBackupDialog() builder.SetNegativeButton(Resource.String.Cancel, (senderDialog, args) => { + //Update/save preferences + Preferences.Set("BackupPreferences", checkedItems[0]); + Preferences.Set("BackupRoute&TrackData", checkedItems[1]); + Preferences.Set("BackupPOIData", checkedItems[2]); + Preferences.Set("BackupMapTiles", checkedItems[3]); + Preferences.Set("BackupElevationData", checkedItems[4]); }); builder.Create(); @@ -149,7 +189,6 @@ private static bool PerformBackups(int ProgressBarIncrement) { Progressbar.UpdateProgressBar.MessageBody = "POI Data..."; r3 = BackupPOIData(BackupFolder); - r3 = false; Progressbar.UpdateProgressBar.Progress += ProgressBarIncrement; Progressbar.UpdateProgressBar.MessageBody = "Done with POI Data"; if (r3 == false) @@ -249,7 +288,6 @@ private static bool BackupPreferences(string BackupFolder) DrawTrackOnGui = Preferences.Get("DrawTrackOnGui", Fragment_Preferences.DrawTrackOnGui_b), freq = int.Parse(Preferences.Get("freq", Fragment_Preferences.freq_s.ToString())), MapLockNorth = Preferences.Get("MapLockNorth", Fragment_Preferences.DisableMapRotate_b), - KeepNBackups = int.Parse(Preferences.Get("KeepNBackups", Fragment_Preferences.KeepNBackups.ToString())), OSM_BulkDownload_Source = Preferences.Get("Tile Bulk Download Source", Fragment_Preferences.TileBulkDownloadSource), OSM_Browse_Source = Preferences.Get("OSM_Browse_Source", Fragment_Preferences.TileBrowseSource), CustomServerURL = Preferences.Get("CustomServerURL ", ""), @@ -260,11 +298,14 @@ private static bool BackupPreferences(string BackupFolder) mapScale = Preferences.Get("mapScale", Fragment_Preferences.DefaultMapScale), mapUTMZone = Preferences.Get("mapUTMZone", Fragment_Preferences.DefaultUTMZone), + //Backup Settings BackupPreferences = Preferences.Get("BackupPreferences", true), BackupRouteTrackData = Preferences.Get("BackupRoute&TrackData", true), BackupPOIData = Preferences.Get("BackupPOIData", true), BackupMapTiles = Preferences.Get("BackupMapTiles", true), - BackupElevationData = Preferences.Get("BackupElevationData", true), + BackupElevationData = Preferences.Get("BackupElevationData", true), + KeepNBackups = int.Parse(Preferences.Get("KeepNBackups", Fragment_Preferences.KeepNBackups.ToString())), + EnableBackupAtStartup = Preferences.Get("BackupElevationData", Fragment_Preferences.EnableBackupAtStartup), }; AppContext.SetSwitch("System.Reflection.NullabilityInfoContext.IsSupported", true);