Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clips): use file system to store clips in files. #64

Merged
merged 7 commits into from
Mar 9, 2024
Merged
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
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"filemaker",
"fmxmlsnippet",
"Mvvm",
"nlog",
"xdoc",
"XMFD",
"XMSC",
Expand Down
19 changes: 19 additions & 0 deletions App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using SharpFM.ViewModels;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using System;
using SharpFM.Services;

namespace SharpFM;

Expand All @@ -24,8 +27,24 @@ public override void OnFrameworkInitializationCompleted()
{
DataContext = new MainWindowViewModel(logger)
};

var services = new ServiceCollection();

services.AddSingleton(x => new FolderService(desktop.MainWindow));

Services = services.BuildServiceProvider();
}

base.OnFrameworkInitializationCompleted();
}

/// <summary>
/// Get a reference to the current app.
/// </summary>
public new static App? Current => Application.Current as App;

/// <summary>
/// Gets the <see cref="IServiceProvider"/> instance to resolve application services.
/// </summary>
public IServiceProvider? Services { get; private set; }
}
30 changes: 20 additions & 10 deletions MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,41 @@
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Command="{Binding NewEmptyItem}" Header="_New" />
<MenuItem Header="_New">
<MenuItem Command="{Binding NewEmptyItem}" Header="Blank Clip" />
<MenuItem Command="{Binding PasteFileMakerClipData}" Header="From Clipboard (copied from FileMaker)" />
</MenuItem>
<Separator />
<MenuItem Command="{Binding SaveToDb}" Header="Save All" />
<MenuItem Command="{Binding OpenFolderPicker}" Header="Open Folder" />
<Separator />
<MenuItem Header="Save">
<MenuItem Command="{Binding SaveClipsStorage}" Header="Save All To Folder" />
<MenuItem Command="{Binding CopySelectedToClip}" Header="Selected clip to Clipboard (to paste into FileMaker)" />
</MenuItem>
<Separator />
<MenuItem Command="{Binding ExitApplication}" Header="_Exit" />
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Command="{Binding CopySelectedToClip}" Header="Copy as FileMaker Blob" />
<MenuItem Command="{Binding PasteFileMakerClipData}" Header="Paste From FileMaker Blob" />
</MenuItem>
<MenuItem Header="Transform">
<MenuItem Command="{Binding CopyAsClass}" Header="Copy as C# Class" />
</MenuItem>
<MenuItem Header="Storage">
<MenuItem Command="{Binding ClearDb}" Header="Clear Db" />
</MenuItem>
<MenuItem Header="{Binding Version}" />
</Menu>
<TextBlock />
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="225" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Margin="0,8">
<TextBox Text="{Binding CurrentPath, Mode=OneWay}" />
</StackPanel>

<ListBox
Grid.Row="1"
Grid.Column="0"
ItemsSource="{Binding FileMakerClips}"
SelectedItem="{Binding SelectedClip}">
Expand All @@ -65,6 +74,7 @@

<AvaloniaEdit:TextEditor
x:Name="avaloniaEditor"
Grid.Row="1"
Grid.Column="1"
FontFamily="Cascadia Code,Consolas,Menlo,Monospace"
ShowLineNumbers="True"
Expand Down
5 changes: 0 additions & 5 deletions Models/Clip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ namespace SharpFM.Models;
/// </summary>
public class Clip
{
/// <summary>
/// Database Id
/// </summary>
public int ClipId { get; set; }

/// <summary>
/// Display name for clip may match Name inside the xml data or may not.
/// </summary>
Expand Down
36 changes: 0 additions & 36 deletions Models/ClipDbContext.cs

This file was deleted.

71 changes: 71 additions & 0 deletions Models/ClipRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace SharpFM.Models;

/// <summary>
/// Clip File Repository.
/// </summary>
public class ClipRepository
{
/// <summary>
/// Clips stored in the specified folder.
/// </summary>
public ICollection<Clip> Clips { get; init; }

/// <summary>
/// Database path.
/// </summary>
public string ClipPath { get; }

/// <summary>
/// Constructor.
/// </summary>
public ClipRepository(string path)
{
// ensure the directory exists
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}

ClipPath = path;

// init clips to empty
Clips = [];
}

/// <summary>
/// Load clips from the path specified by <see cref="ClipPath"/>.
/// </summary>
public void LoadClips()
{
foreach (var clipFile in Directory.EnumerateFiles(ClipPath))
{
var fi = new FileInfo(clipFile);

var clip = new Clip
{
ClipName = fi.Name.Replace(fi.Extension, string.Empty),
ClipType = fi.Extension.Replace(".", string.Empty),
ClipXml = File.ReadAllText(clipFile)
};

Clips.Add(clip);
}
}

/// <summary>
/// Write all clips to their associated clip type files in the path specified by <see cref="ClipPath"/>.
/// </summary>
public void SaveChanges()
{
foreach (var clip in Clips)
{
var clipPath = Path.Combine(ClipPath, $"{clip.ClipName}.{clip.ClipType}");

File.WriteAllText(clipPath, clip.ClipXml);
}
}
}
29 changes: 29 additions & 0 deletions Services/FolderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Platform.Storage;

namespace SharpFM.Services;

public class FolderService(Window target)
{
private readonly Window _target = target;

public async Task<string> GetFolderAsync()
{
// Get top level from the current control. Alternatively, you can use Window reference instead.
var topLevel = TopLevel.GetTopLevel(_target) ?? throw new ArgumentNullException(nameof(_target), "Window Target.");

// Start async operation to open the dialog.
var folders = await topLevel.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
AllowMultiple = false,
Title = "Select a Folder",
});

var folder = folders.SingleOrDefault();

return folder?.TryGetLocalPath() ?? throw new ArgumentException("Could not load local path.");
}
}
2 changes: 0 additions & 2 deletions SharpFM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.9" />
<PackageReference Include="FluentAvaloniaUI" Version="2.0.5" />

<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />

<PackageReference Include="MinVer" Version="5.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
16 changes: 1 addition & 15 deletions ViewModels/ClipViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,9 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")

public FileMakerClip Clip { get; set; }

public ClipViewModel(FileMakerClip clip) : this(clip, null) { }

public ClipViewModel(FileMakerClip clip, int? clipId)
public ClipViewModel(FileMakerClip clip)
{
Clip = clip;
ClipId = clipId;
}

private int? _clipId;
public int? ClipId
{
get => _clipId;
set
{
_clipId = value;
NotifyPropertyChanged();
}
}

public string ClipType
Expand Down
Loading