Skip to content

Commit 93694fe

Browse files
CopilotTheJoeFin
andcommitted
Move history storage from settings to separate file with migration support
Co-authored-by: TheJoeFin <7809853+TheJoeFin@users.noreply.github.com>
1 parent 9c23fe9 commit 93694fe

1 file changed

Lines changed: 65 additions & 8 deletions

File tree

‎Simple QR Code Maker/ViewModels/MainViewModel.cs‎

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -862,23 +862,80 @@ public async Task SaveCurrentStateToHistory()
862862
HistoryItems.Remove(historyItem);
863863
HistoryItems.Insert(0, historyItem);
864864

865-
await LocalSettingsService.SaveSettingAsync(nameof(HistoryItems), HistoryItems);
865+
await SaveHistoryToFile();
866866
}
867867

868-
private async Task LoadHistory()
868+
private async Task SaveHistoryToFile()
869869
{
870-
ObservableCollection<HistoryItem>? prevHistory = null;
870+
try
871+
{
872+
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
873+
StorageFile historyFile = await localFolder.CreateFileAsync("History.json", CreationCollisionOption.ReplaceExisting);
874+
875+
string json = System.Text.Json.JsonSerializer.Serialize(HistoryItems, new System.Text.Json.JsonSerializerOptions
876+
{
877+
WriteIndented = true
878+
});
879+
880+
await FileIO.WriteTextAsync(historyFile, json);
881+
}
882+
catch (Exception ex)
883+
{
884+
System.Diagnostics.Debug.WriteLine($"Failed to save history to file: {ex.Message}");
885+
}
886+
}
871887

888+
private async Task LoadHistory()
889+
{
890+
ObservableCollection<HistoryItem>? historyFromFile = null;
891+
892+
// First, try to load from file (new location)
872893
try
873894
{
874-
prevHistory = await LocalSettingsService.ReadSettingAsync<ObservableCollection<HistoryItem>>(nameof(HistoryItems));
895+
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
896+
StorageFile historyFile = await localFolder.GetFileAsync("History.json");
897+
string json = await FileIO.ReadTextAsync(historyFile);
898+
historyFromFile = System.Text.Json.JsonSerializer.Deserialize<ObservableCollection<HistoryItem>>(json);
899+
}
900+
catch
901+
{
902+
// File doesn't exist yet, that's okay
875903
}
876-
catch { }
877904

878-
if (prevHistory is null || prevHistory.Count == 0)
905+
// If we found history in the file, use it
906+
if (historyFromFile != null && historyFromFile.Count > 0)
907+
{
908+
foreach (HistoryItem hisItem in historyFromFile)
909+
HistoryItems.Add(hisItem);
879910
return;
911+
}
880912

881-
foreach (HistoryItem hisItem in prevHistory)
882-
HistoryItems.Add(hisItem);
913+
// Otherwise, try to migrate from old settings location
914+
ObservableCollection<HistoryItem>? historyFromSettings = null;
915+
try
916+
{
917+
historyFromSettings = await LocalSettingsService.ReadSettingAsync<ObservableCollection<HistoryItem>>(nameof(HistoryItems));
918+
}
919+
catch { }
920+
921+
if (historyFromSettings != null && historyFromSettings.Count > 0)
922+
{
923+
// Migrate: add to collection and save to file
924+
foreach (HistoryItem hisItem in historyFromSettings)
925+
HistoryItems.Add(hisItem);
926+
927+
// Save to new file location
928+
await SaveHistoryToFile();
929+
930+
// Clear from old settings location to free up space
931+
try
932+
{
933+
await LocalSettingsService.SaveSettingAsync(nameof(HistoryItems), new ObservableCollection<HistoryItem>());
934+
}
935+
catch (Exception ex)
936+
{
937+
System.Diagnostics.Debug.WriteLine($"Failed to clear old history from settings: {ex.Message}");
938+
}
939+
}
883940
}
884941
}

0 commit comments

Comments
 (0)