mirror of
https://github.com/Stone-Red-Code/ClipCMD.git
synced 2026-09-07 23:38:47 +02:00
Add import and export functionality
This commit is contained in:
@@ -38,7 +38,8 @@ public partial class App : Application
|
|||||||
{
|
{
|
||||||
desktop.MainWindow = new MainWindow();
|
desktop.MainWindow = new MainWindow();
|
||||||
ClipCmdCommandHandler clipCmdCommandHandler = new ClipCmdCommandHandler(desktop.MainWindow);
|
ClipCmdCommandHandler clipCmdCommandHandler = new ClipCmdCommandHandler(desktop.MainWindow);
|
||||||
desktop.MainWindow.DataContext = new MainViewModel(clipCmdCommandHandler);
|
ImportExportService importExportService = new ImportExportService(desktop.MainWindow, clipCmdCommandHandler);
|
||||||
|
desktop.MainWindow.DataContext = new MainViewModel(clipCmdCommandHandler, importExportService);
|
||||||
|
|
||||||
desktop.MainWindow.Closing += (sender, e) =>
|
desktop.MainWindow.Closing += (sender, e) =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Platform.Storage;
|
||||||
|
|
||||||
|
using ClipCmd.Models;
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ClipCmd.Utilities;
|
||||||
|
|
||||||
|
public class ImportExportService(Window window, ClipCmdCommandHandler clipCmdCommandHandler)
|
||||||
|
{
|
||||||
|
private readonly TopLevel topLevel = window;
|
||||||
|
|
||||||
|
public async Task Import()
|
||||||
|
{
|
||||||
|
// Start async operation to open the dialog.
|
||||||
|
IReadOnlyList<IStorageFile> files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
|
||||||
|
{
|
||||||
|
Title = "Import ClipCMD commands",
|
||||||
|
AllowMultiple = false,
|
||||||
|
FileTypeFilter = [new FilePickerFileType("ccmd")
|
||||||
|
{
|
||||||
|
Patterns = ["*.ccmd"]
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
|
||||||
|
if (files.Count < 1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await using Stream stream = await files[0].OpenReadAsync();
|
||||||
|
using StreamReader streamReader = new StreamReader(stream);
|
||||||
|
|
||||||
|
string json = await streamReader.ReadToEndAsync();
|
||||||
|
|
||||||
|
clipCmdCommandHandler.ClearCommands();
|
||||||
|
|
||||||
|
Dictionary<string, ClipCmdCommand>? commands = JsonSerializer.Deserialize<Dictionary<string, ClipCmdCommand>>(json);
|
||||||
|
|
||||||
|
if (commands is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (KeyValuePair<string, ClipCmdCommand> command in commands)
|
||||||
|
{
|
||||||
|
clipCmdCommandHandler.Commands.Add(command.Key, command.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Export()
|
||||||
|
{
|
||||||
|
// Start async operation to open the dialog.
|
||||||
|
IStorageFile? file = await topLevel.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
|
||||||
|
{
|
||||||
|
Title = "Export ClipCMD commands",
|
||||||
|
SuggestedFileName = "export.ccmd",
|
||||||
|
FileTypeChoices = [new FilePickerFileType("ccmd")
|
||||||
|
{
|
||||||
|
Patterns = ["*.ccmd"]
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
|
||||||
|
if (file is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string json = JsonSerializer.Serialize(clipCmdCommandHandler.Commands);
|
||||||
|
|
||||||
|
await using Stream stream = await file.OpenWriteAsync();
|
||||||
|
using StreamWriter streamWriter = new StreamWriter(stream);
|
||||||
|
await streamWriter.WriteAsync(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ namespace ClipCmd.ViewModels;
|
|||||||
public class MainViewModel : ViewModelBase
|
public class MainViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
private readonly ClipCmdCommandHandler clipCmdCommandHandler;
|
private readonly ClipCmdCommandHandler clipCmdCommandHandler;
|
||||||
|
private readonly ImportExportService importExportService;
|
||||||
|
|
||||||
public List<ClipCmdCommandItemViewModel> Commands => [.. clipCmdCommandHandler.Commands.Keys.Select(x => new ClipCmdCommandItemViewModel(x, clipCmdCommandHandler))];
|
public List<ClipCmdCommandItemViewModel> Commands => [.. clipCmdCommandHandler.Commands.Keys.Select(x => new ClipCmdCommandItemViewModel(x, clipCmdCommandHandler))];
|
||||||
|
|
||||||
@@ -105,10 +106,13 @@ public class MainViewModel : ViewModelBase
|
|||||||
|
|
||||||
public ICommand AddCommand => ReactiveCommand.Create(Add);
|
public ICommand AddCommand => ReactiveCommand.Create(Add);
|
||||||
|
|
||||||
public MainViewModel(ClipCmdCommandHandler clipCmdCommandHandler)
|
public ICommand ExportCommand => ReactiveCommand.Create(importExportService.Export);
|
||||||
|
public ICommand ImportCommand => ReactiveCommand.Create(importExportService.Import);
|
||||||
|
|
||||||
|
public MainViewModel(ClipCmdCommandHandler clipCmdCommandHandler, ImportExportService importExportService)
|
||||||
{
|
{
|
||||||
this.clipCmdCommandHandler = clipCmdCommandHandler;
|
this.clipCmdCommandHandler = clipCmdCommandHandler;
|
||||||
|
this.importExportService = importExportService;
|
||||||
clipCmdCommandHandler.Commands.CollectionChanged += (sender, e) =>
|
clipCmdCommandHandler.Commands.CollectionChanged += (sender, e) =>
|
||||||
{
|
{
|
||||||
this.RaisePropertyChanged(nameof(Commands));
|
this.RaisePropertyChanged(nameof(Commands));
|
||||||
@@ -119,6 +123,7 @@ public class MainViewModel : ViewModelBase
|
|||||||
public MainViewModel()
|
public MainViewModel()
|
||||||
{
|
{
|
||||||
clipCmdCommandHandler = new ClipCmdCommandHandler(new());
|
clipCmdCommandHandler = new ClipCmdCommandHandler(new());
|
||||||
|
importExportService = new(new(), clipCmdCommandHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void Add()
|
private async void Add()
|
||||||
|
|||||||
@@ -53,14 +53,14 @@
|
|||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Grid ColumnDefinitions="* 20 *" Grid.Column="2" Grid.Row="2">
|
<Grid ColumnDefinitions="* 20 *" Grid.Column="2" Grid.Row="2">
|
||||||
<Button>
|
<Button Command="{Binding ImportCommand}">
|
||||||
<StackPanel Spacing="4" Orientation="Horizontal">
|
<StackPanel Spacing="4" Orientation="Horizontal">
|
||||||
<i:Icon Value="fa-file-upload" />
|
<i:Icon Value="fa-file-upload" />
|
||||||
<TextBlock>Import</TextBlock>
|
<TextBlock>Import</TextBlock>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button Grid.Column="2">
|
<Button Grid.Column="2" Command="{Binding ExportCommand}">
|
||||||
<StackPanel Orientation="Horizontal">
|
<StackPanel Orientation="Horizontal">
|
||||||
<i:Icon Value="fa-file-download" />
|
<i:Icon Value="fa-file-download" />
|
||||||
<TextBlock>Export</TextBlock>
|
<TextBlock>Export</TextBlock>
|
||||||
|
|||||||
Reference in New Issue
Block a user