Skip to content

Commit

Permalink
use clipId to update database
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzzerd committed Sep 29, 2023
1 parent 9434c5a commit 4ccbf38
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
7 changes: 4 additions & 3 deletions SharpFM.App/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@
<MenuItem Header="_File">
<MenuItem Command="{Binding NewEmptyItem}" Header="_New" />
<Separator />
<MenuItem Command="{Binding SaveToDb}" Header="Save All" />
<Separator />
<MenuItem Command="{Binding ExitApplication}" Header="_Exit" />
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Command="{Binding CopySelectedToClip}" Header="Copy" />
<MenuItem Command="{Binding PasteFileMakerClipData}" Header="Paste" />
<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 SaveToDb}" Header="Save Db" />
<MenuItem Command="{Binding ClearDb}" Header="Clear Db" />
</MenuItem>
<MenuItem Header="{Binding Version}" />
Expand Down
16 changes: 15 additions & 1 deletion SharpFM.App/ViewModels/ClipViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,23 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")

public FileMakerClip Clip { get; set; }

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

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

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

public string ClipType
Expand Down
25 changes: 15 additions & 10 deletions SharpFM.App/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ public MainWindowViewModel()

Console.WriteLine($"Database path: {_context.DbPath}.");

Keys = new ObservableCollection<ClipViewModel>();
FileMakerClips = new ObservableCollection<ClipViewModel>();

foreach (var clip in _context.Clips)
{
Keys.Add(new ClipViewModel(
FileMakerClips.Add(new ClipViewModel(
new FileMakerClip(
clip.ClipName,
clip.ClipType,
clip.ClipXml
)
),
clip.ClipId
)
);
}
Expand All @@ -50,11 +51,12 @@ public void SaveToDb()
{
var dbClips = _context.Clips.ToList();

foreach (var clip in Keys)
foreach (var clip in FileMakerClips)
{
if (dbClips.Any(dbc => dbc.ClipName == clip.Name))
var dbClip = dbClips.FirstOrDefault(dbc => dbc.ClipName == clip.Name);

if (dbClip is not null)
{
var dbClip = dbClips.First(dbc => dbc.ClipName == clip.Name);
dbClip.ClipType = clip.ClipType;
dbClip.ClipXml = clip.ClipXml;
}
Expand Down Expand Up @@ -99,7 +101,7 @@ public void NewEmptyItem()
var clip = new FileMakerClip("New", FileMakerClip.ClipTypes.First()?.KeyId ?? "", Array.Empty<byte>());
var clipVm = new ClipViewModel(clip);

Keys.Add(clipVm);
FileMakerClips.Add(clipVm);
}
catch (Exception e)

Check warning on line 106 in SharpFM.App/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / CI Build (linux)

The variable 'e' is declared but never used

Check warning on line 106 in SharpFM.App/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / CI Build (linux)

The variable 'e' is declared but never used

Check warning on line 106 in SharpFM.App/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / CI Build (macOS)

The variable 'e' is declared but never used

Check warning on line 106 in SharpFM.App/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / CI Build (macOS)

The variable 'e' is declared but never used
{
Expand Down Expand Up @@ -160,12 +162,12 @@ public async Task PasteFileMakerClipData()

// don't bother adding a duplicate. For some reason entries were getting entered twice per clip
// this is not the most efficient method to detect it, but it works well enough for now
if (Keys.Any(k => k.ClipXml == clip.XmlData))
if (FileMakerClips.Any(k => k.ClipXml == clip.XmlData))
{
continue;
}

Keys.Add(new ClipViewModel(clip));
FileMakerClips.Add(new ClipViewModel(clip));
}
}
catch (Exception e)

Check warning on line 173 in SharpFM.App/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / CI Build (linux)

The variable 'e' is declared but never used

Check warning on line 173 in SharpFM.App/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / CI Build (linux)

The variable 'e' is declared but never used

Check warning on line 173 in SharpFM.App/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / CI Build (macOS)

The variable 'e' is declared but never used

Check warning on line 173 in SharpFM.App/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / CI Build (macOS)

The variable 'e' is declared but never used
Expand Down Expand Up @@ -198,6 +200,9 @@ public async Task CopySelectedToClip()
}
}

/// <summary>
/// SharpFM Version.
/// </summary>
public string Version
{
get
Expand All @@ -208,7 +213,7 @@ public string Version
}
}

public ObservableCollection<ClipViewModel> Keys { get; set; }
public ObservableCollection<ClipViewModel> FileMakerClips { get; set; }

private ClipViewModel? _selectedClip;
public ClipViewModel? SelectedClip
Expand Down

0 comments on commit 4ccbf38

Please sign in to comment.