Skip to content

Commit

Permalink
Support backing up on first launch of the day
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjore committed Nov 27, 2024
1 parent 2d709fd commit e1634f9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions Fragments/Fragment_preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ 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; }
public bool DrawPOIOnGui { get; set; }
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; }
Expand Down
9 changes: 8 additions & 1 deletion Resources/Xml/Preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,15 @@
android:key="KeepNBackups"
android:inputType="numberDecimal"
android:digits="0123456789"
android:defaultValue="20"
android:defaultValue="10"
app:useSimpleSummaryProvider="true" />

<SwitchPreference
android:title="@string/EnableBackupAtStartup"
android:key="EnableBackupAtStartup"
android:defaultValue="true"
app:useSimpleSummaryProvider="true" />

</PreferenceCategory>

</PreferenceScreen>
1 change: 1 addition & 0 deletions Resources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<string name="ExportRogainingPOI">"Export Rogaining POI"</string>
<string name="BackupOptions">"Choose Backup Options"</string>
<string name="KeepNBackups">"Backups to Retain"</string>
<string name="EnableBackupAtStartup">"Backup on first launch of the day"</string>

<!-- Map Tile provider Start -->
<string name="MapTileSources">"Map Tile Sources"</string>
Expand Down
47 changes: 44 additions & 3 deletions Utils/Backup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 ", ""),
Expand All @@ -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);
Expand Down

0 comments on commit e1634f9

Please sign in to comment.