Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Calculator/Views/HistoryList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@
Content=""
ToolTipService.ToolTip="{utils:ResourceString Name=ClearHistory/[using:Windows.UI.Xaml.Controls]ToolTipService/ToolTip}"
Visibility="{x:Bind Model.ItemsCount, Mode=OneWay, Converter={StaticResource ItemSizeToVisibilityNegationConverter}}"/>

<Button x:Name="ExportButton"
Grid.Row="0"
Style="{ThemeResource HistoryButtonStyle}"
AutomationProperties.Name="Export History"
Click="ExportButton_Click">
<SymbolIcon Symbol="Download"/>
</Button>
</Grid>
</Grid>
</UserControl>
72 changes: 71 additions & 1 deletion src/Calculator/Views/HistoryList.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using CalculatorApp.ViewModel;
using CalculatorApp.ViewModel.Common;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

Expand Down Expand Up @@ -77,6 +82,71 @@ private void OnDeleteSwipeInvoked(MUXC.SwipeItem sender, MUXC.SwipeItemInvokedEv
Model.DeleteItem(swipedItem);
}
}

private async void ExportButton_Click(object sender, RoutedEventArgs e)
{
var history = (CalculatorApp.ViewModel.HistoryViewModel)this.DataContext;


if (history.ItemsCount == 0)
{
return; // No history to export
}

Debug.WriteLine("Opening save picker");

var savePicker = new FileSavePicker
{
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
SuggestedFileName = "CalculatorHistory"
};

Debug.WriteLine("Adding choice");

savePicker.FileTypeChoices.Add("Plain Text", new List<string> { ".txt" });
savePicker.FileTypeChoices.Add("CSV File", new List<string> { ".csv" });

StorageFile file = await savePicker.PickSaveFileAsync();

if (file == null)
{
return; // User cancelled
}

try
{
var content = new StringBuilder();
bool isCsv = file.FileType.Equals(".csv", System.StringComparison.OrdinalIgnoreCase);

if (isCsv)
{
content.AppendLine("Expression,Result");
}

foreach (var item in history.Items)
{
if (isCsv)
{
// Escape quotes and commas for CSV
var expression = item.Expression.Replace("\"", "\"\"");
var result = item.Result.Replace("\"", "\"\"");
content.AppendLine($"\"{expression}\",\"{result}\"");
}
else
{
content.AppendLine($"{item.Expression} = {item.Result}");
}
}

await FileIO.WriteTextAsync(file, content.ToString());
}
catch (Exception ex)
{
// Log error and show user-friendly message
Debug.WriteLine($"Error exporting history: {ex.Message}");
// You might want to show a dialog here to inform the user
}
}
}
}