Skip to content

Commit 9a2e144

Browse files
CopilotTheJoeFin
andcommitted
Add history support for logo images with local storage persistence
Co-authored-by: TheJoeFin <[email protected]>
1 parent f7c676d commit 9a2e144

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
lines changed

Simple QR Code Maker/Models/HistoryItem.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public ErrorCorrectionLevel ErrorCorrection
3939
[JsonConverter(typeof(JsonStringEnumConverter<BarcodeFormat>))]
4040
public BarcodeFormat Format { get; set; } = BarcodeFormat.QR_CODE;
4141

42+
public string? LogoImagePath { get; set; }
43+
44+
public double LogoSizePercentage { get; set; } = 15;
45+
4246
public HistoryItem()
4347
{
4448

Simple QR Code Maker/ViewModels/MainViewModel.cs

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Simple_QR_Code_Maker.Helpers;
1111
using Simple_QR_Code_Maker.Models;
1212
using System.Collections.ObjectModel;
13+
using System.IO;
1314
using Windows.ApplicationModel.DataTransfer;
1415
using Windows.Storage;
1516
using Windows.Storage.Pickers;
@@ -116,9 +117,45 @@ partial void OnSelectedHistoryItemChanged(HistoryItem? value)
116117
ForegroundColor = value.Foreground;
117118
BackgroundColor = value.Background;
118119
SelectedOption = ErrorCorrectionLevels.First(x => x.ErrorCorrectionLevel == value.ErrorCorrection);
120+
121+
// Restore logo image and size if available
122+
if (!string.IsNullOrEmpty(value.LogoImagePath))
123+
{
124+
_ = LoadLogoFromHistory(value.LogoImagePath);
125+
}
126+
else
127+
{
128+
// Clear logo if history item has no logo
129+
LogoImage?.Dispose();
130+
LogoImage = null;
131+
}
132+
133+
LogoSizePercentage = value.LogoSizePercentage;
119134

120135
SelectedHistoryItem = null;
121136
}
137+
138+
private async Task LoadLogoFromHistory(string logoPath)
139+
{
140+
try
141+
{
142+
if (File.Exists(logoPath))
143+
{
144+
// Dispose old logo first
145+
LogoImage?.Dispose();
146+
147+
StorageFile file = await StorageFile.GetFileFromPathAsync(logoPath);
148+
using var stream = await file.OpenReadAsync();
149+
LogoImage = new System.Drawing.Bitmap(stream.AsStreamForRead());
150+
}
151+
}
152+
catch (Exception ex)
153+
{
154+
System.Diagnostics.Debug.WriteLine($"Failed to load logo from history: {ex.Message}");
155+
LogoImage?.Dispose();
156+
LogoImage = null;
157+
}
158+
}
122159

123160
public ObservableCollection<ErrorCorrectionOptions> ErrorCorrectionLevels { get; set; } = new(allCorrectionLevels);
124161

@@ -282,7 +319,7 @@ private void OnRequestPaneChange(object recipient, RequestPaneChange message)
282319
}
283320
}
284321

285-
private void OnSaveHistoryMessage(object recipient, SaveHistoryMessage message) => SaveCurrentStateToHistory();
322+
private async void OnSaveHistoryMessage(object recipient, SaveHistoryMessage message) => await SaveCurrentStateToHistory();
286323

287324
private void OnRequestShowMessage(object recipient, RequestShowMessage rsm)
288325
{
@@ -426,7 +463,7 @@ private async Task CopyPngToClipboard()
426463
if (QrCodeBitmaps.Count == 0)
427464
return;
428465

429-
SaveCurrentStateToHistory();
466+
await SaveCurrentStateToHistory();
430467

431468
StorageFolder folder = ApplicationData.Current.LocalCacheFolder;
432469
List<StorageFile> files = [];
@@ -475,7 +512,7 @@ private async Task CopySvgToClipboard()
475512
if (QrCodeBitmaps.Count == 0)
476513
return;
477514

478-
SaveCurrentStateToHistory();
515+
await SaveCurrentStateToHistory();
479516

480517
StorageFolder folder = ApplicationData.Current.LocalCacheFolder;
481518
List<StorageFile> files = [];
@@ -519,12 +556,12 @@ private async Task CopySvgToClipboard()
519556
}
520557

521558
[RelayCommand]
522-
private void CopySvgTextToClipboard()
559+
private async Task CopySvgTextToClipboard()
523560
{
524561
if (QrCodeBitmaps.Count == 0)
525562
return;
526563

527-
SaveCurrentStateToHistory();
564+
await SaveCurrentStateToHistory();
528565

529566
List<string> textStrings = [];
530567
foreach (BarcodeImageItem qrCodeItem in QrCodeBitmaps)
@@ -596,7 +633,7 @@ private async Task SavePng()
596633
if (QrCodeBitmaps.Count == 0)
597634
return;
598635

599-
SaveCurrentStateToHistory();
636+
await SaveCurrentStateToHistory();
600637

601638
await SaveAllFiles(FileKind.PNG);
602639

@@ -615,7 +652,7 @@ private async Task SaveSvg()
615652
if (QrCodeBitmaps.Count == 0)
616653
return;
617654

618-
SaveCurrentStateToHistory();
655+
await SaveCurrentStateToHistory();
619656

620657
await SaveAllFiles(FileKind.SVG);
621658

@@ -769,23 +806,61 @@ public async void OnNavigatedTo(object parameter)
769806
public void OnNavigatedFrom()
770807
{
771808
if (!string.IsNullOrWhiteSpace(UrlText))
772-
SaveCurrentStateToHistory();
809+
_ = SaveCurrentStateToHistory();
773810
}
774811

775-
public void SaveCurrentStateToHistory()
812+
public async Task SaveCurrentStateToHistory()
776813
{
814+
string? logoImagePath = null;
815+
816+
// Save logo image to local app storage if present
817+
if (LogoImage != null)
818+
{
819+
try
820+
{
821+
StorageFolder logoFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("LogoImages", CreationCollisionOption.OpenIfExists);
822+
string fileName = $"logo_{DateTime.Now:yyyyMMddHHmmss}_{Guid.NewGuid():N}.png";
823+
StorageFile logoFile = await logoFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
824+
825+
using (var stream = await logoFile.OpenAsync(FileAccessMode.ReadWrite))
826+
{
827+
using (var outputStream = stream.GetOutputStreamAt(0))
828+
{
829+
using (var dataWriter = new DataWriter(outputStream))
830+
{
831+
using (MemoryStream ms = new())
832+
{
833+
LogoImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
834+
byte[] bytes = ms.ToArray();
835+
dataWriter.WriteBytes(bytes);
836+
await dataWriter.StoreAsync();
837+
}
838+
}
839+
}
840+
}
841+
842+
logoImagePath = logoFile.Path;
843+
}
844+
catch (Exception ex)
845+
{
846+
System.Diagnostics.Debug.WriteLine($"Failed to save logo image: {ex.Message}");
847+
}
848+
}
849+
777850
HistoryItem historyItem = new()
778851
{
779852
CodesContent = UrlText,
780853
Foreground = ForegroundColor,
781854
Background = BackgroundColor,
782855
ErrorCorrection = SelectedOption.ErrorCorrectionLevel,
856+
LogoImagePath = logoImagePath,
857+
LogoSizePercentage = LogoSizePercentage,
783858
};
784859

785860
HistoryItems.Remove(historyItem);
786861
HistoryItems.Insert(0, historyItem);
787862

788-
LocalSettingsService.SaveSettingAsync(nameof(HistoryItems), HistoryItems);
863+
await LocalSettingsService.SaveSettingAsync(nameof(HistoryItems), HistoryItems);
789864
}
790865

791866
private async Task LoadHistory()

0 commit comments

Comments
 (0)