mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-21 05:43:10 -07:00
Added export history feature
Signed-off-by: Ajay Nair <ajaypnair99@gmail.com>
This commit is contained in:
parent
09a39a500e
commit
8bfb61a374
2 changed files with 79 additions and 1 deletions
|
@ -176,6 +176,14 @@
|
||||||
Content=""
|
Content=""
|
||||||
ToolTipService.ToolTip="{utils:ResourceString Name=ClearHistory/[using:Windows.UI.Xaml.Controls]ToolTipService/ToolTip}"
|
ToolTipService.ToolTip="{utils:ResourceString Name=ClearHistory/[using:Windows.UI.Xaml.Controls]ToolTipService/ToolTip}"
|
||||||
Visibility="{x:Bind Model.ItemsCount, Mode=OneWay, Converter={StaticResource ItemSizeToVisibilityNegationConverter}}"/>
|
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>
|
||||||
</Grid>
|
</Grid>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
using CalculatorApp.ViewModel;
|
using CalculatorApp.ViewModel;
|
||||||
using CalculatorApp.ViewModel.Common;
|
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;
|
||||||
using Windows.UI.Xaml.Controls;
|
using Windows.UI.Xaml.Controls;
|
||||||
|
|
||||||
|
@ -77,6 +82,71 @@ namespace CalculatorApp
|
||||||
Model.DeleteItem(swipedItem);
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue