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

Crash fixes #229

Merged
merged 11 commits into from
Apr 21, 2024
23 changes: 21 additions & 2 deletions Diffusion.Database/DataStore.Album.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public IEnumerable<Album> GetAlbumsByLastUpdated(int limit)
return lists;
}

public IEnumerable<Album> GetAlbumsByName()
{
using var db = OpenConnection();

var lists = db.Query<Album>($"SELECT Id, Name, [Order], LastUpdated FROM {nameof(Album)} ORDER BY Name");

db.Close();

return lists;
}

public void RenameAlbum(int id, string name)
{
using var db = OpenConnection();
Expand All @@ -60,7 +71,7 @@ public void RenameAlbum(int id, string name)
db.Close();
}

public Album GetAlbum(int id)
public Album? GetAlbum(int id)
{
using var db = OpenConnection();

Expand All @@ -74,6 +85,8 @@ public Album GetAlbum(int id)

db.Close();

if (album.Count < 1)
return null;
return album[0];
}

Expand Down Expand Up @@ -157,8 +170,12 @@ public void RemoveAlbum(int id)
command.ExecuteNonQuery();
}

public void AddImagesToAlbum(int albumId, IEnumerable<int> imageId)
public bool AddImagesToAlbum(int albumId, IEnumerable<int> imageId)
{
//add a check to make sure that album exists
if (GetAlbum(albumId) == null)
return false;

using var db = OpenConnection();

db.BeginTransaction();
Expand All @@ -184,6 +201,8 @@ public void AddImagesToAlbum(int albumId, IEnumerable<int> imageId)
command.ExecuteNonQuery();

db.Commit();

return true;
}

public void RemoveImagesFromAlbum(int albumId, IEnumerable<int> imageId)
Expand Down
9 changes: 6 additions & 3 deletions Diffusion.Database/DataStore.Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,14 @@ public int CleanRemovedFolders(IEnumerable<string> watchedFolders)
{
using var db = OpenConnection();

var whereClause = string.Join(" AND ", watchedFolders.Select(f => "PATH NOT LIKE ? || '\\%'"));
var whereClause = string.Join(" AND ", watchedFolders.Select(f => $"PATH NOT LIKE '{f}\\%'"));

var query = $"DELETE FROM Image WHERE {whereClause}";
//first remove matching entries from AlbumImage
var albumImageQuery = $"DELETE FROM AlbumImage WHERE ImageId IN (SELECT Id FROM Image WHERE {whereClause})";
var albumImageQueryResult = db.Execute(albumImageQuery);

var result = db.Execute(query, watchedFolders.ToArray());
var query = $"DELETE FROM Image WHERE {whereClause}";
var result = db.Execute(query);

db.Close();

Expand Down
2 changes: 2 additions & 0 deletions Diffusion.Database/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class Filter
public int BatchPos { get; set; }

public bool UseAestheticScore { get; set; }
public bool NoAestheticScore { get; set; }
public string AestheticScoreOp { get; set; }
public double? AestheticScore { get; set; }

Expand Down Expand Up @@ -78,6 +79,7 @@ public class Filter
UseForDeletion ||
UseBatchSize ||
UseBatchPos ||
NoAestheticScore ||
UseAestheticScore ||
UsePath ||
UseCreationDate ||
Expand Down
22 changes: 8 additions & 14 deletions Diffusion.Database/QueryBuilder.Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,15 @@ private static void FilterHypernetStrength(Filter filter, List<KeyValuePair<stri
}
}



private static void FilterAestheticScore(Filter filter, List<KeyValuePair<string, object>> conditions)
{
if (filter.UseAestheticScore)
{
var oper = filter.AestheticScoreOp;

conditions.Add(new KeyValuePair<string, object>($"(AestheticScore {oper} ?)", filter.AestheticScore));
} else if (filter.NoAestheticScore)
{
conditions.Add(new KeyValuePair<string, object>($"(AestheticScore IS NULL)", null));
}
}

Expand All @@ -188,17 +188,11 @@ private static void FilterRating(Filter filter, List<KeyValuePair<string, object
{
if (filter.UseRating)
{

if (filter.Unrated)
{
conditions.Add(new KeyValuePair<string, object>($"(Rating IS NULL)", null));
}
else
{
var oper = filter.RatingOp;
conditions.Add(new KeyValuePair<string, object>($"(Rating {oper} ?)", filter.Rating));
}

var oper = filter.RatingOp;
conditions.Add(new KeyValuePair<string, object>($"(Rating {oper} ?)", filter.Rating));
} else if (filter.Unrated)
{
conditions.Add(new KeyValuePair<string, object>($"(Rating IS NULL)", null));
}
}

Expand Down
15 changes: 10 additions & 5 deletions Diffusion.Toolkit/Controls/FilterControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
lex:LocalizeDictionary.DefaultProvider="{StaticResource LocalizationProvider}"
d:DataContext="{d:DesignInstance local:FilterControlModel, IsDesignTimeCreatable=True}"
mc:Ignorable="d"
d:DesignHeight="660" d:DesignWidth="600">
d:DesignHeight="720" d:DesignWidth="600">
<UserControl.Resources>
<ResourceDictionary>
<converters:IsValueBoolConverter x:Key="isValueBool"></converters:IsValueBoolConverter>
Expand Down Expand Up @@ -150,28 +150,33 @@
<ColumnDefinition Width="130"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<CheckBox IsChecked="{Binding UseAestheticScore}" VerticalContentAlignment="Center">Aesthetic Score</CheckBox>
<CheckBox Name="chkAesthetic" IsChecked="{Binding UseAestheticScore}" VerticalContentAlignment="Center">Aesthetic Score</CheckBox>
<StackPanel Orientation="Horizontal" Grid.Column="1">
<RadioButton Click="ButtonBase_OnClick" GroupName="AestheticScore" IsChecked="{Binding AestheticScoreOp, Mode=TwoWay, Converter={StaticResource isStrValueBool}, ConverterParameter='='}" Margin="0,0,10,0" VerticalContentAlignment="Center">=</RadioButton>
<RadioButton Click="ButtonBase_OnClick" GroupName="AestheticScore" IsChecked="{Binding AestheticScoreOp, Mode=TwoWay, Converter={StaticResource isStrValueBool}, ConverterParameter='&lt;='}" Margin="0,0,10,0" VerticalContentAlignment="Center">&lt;=</RadioButton>
<RadioButton Click="ButtonBase_OnClick" GroupName="AestheticScore" IsChecked="{Binding AestheticScoreOp, Mode=TwoWay, Converter={StaticResource isStrValueBool}, ConverterParameter='&gt;='}" Margin="0,0,10,0" VerticalContentAlignment="Center">&gt;=</RadioButton>
<RadioButton Click="ButtonBase_OnClick" GroupName="AestheticScore" IsChecked="{Binding AestheticScoreOp, Mode=TwoWay, Converter={StaticResource isStrValueBool}, ConverterParameter='!='}" Margin="0,0,10,0" VerticalContentAlignment="Center">!=</RadioButton>
<TextBox Text="{Binding AestheticScore}" KeyDown="UIElement_OnKeyDown" Height="20" Width="50"></TextBox>
<TextBox Text="{Binding AestheticScore}" KeyDown="UIElement_OnKeyDown" Height="20" Width="50" Margin="0,0,10,0" ></TextBox>
<CheckBox Name="chkNoAestheticScore"
IsChecked="{Binding NoAestheticScore}"
VerticalContentAlignment="Center">No Score</CheckBox>
</StackPanel>
</Grid>
<Grid Margin="0,0,0,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="130"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<CheckBox IsChecked="{Binding UseRating}" VerticalContentAlignment="Center">Rating</CheckBox>
<CheckBox Name="chkRating" IsChecked="{Binding UseRating}" VerticalContentAlignment="Center">Rating</CheckBox>
<StackPanel Orientation="Horizontal" Grid.Column="1">
<RadioButton IsEnabled="{Binding Unrated, Converter={StaticResource notBool}}" Click="ButtonBase_OnClick" GroupName="Rating" IsChecked="{Binding RatingOp, Mode=TwoWay, Converter={StaticResource isStrValueBool}, ConverterParameter='='}" Margin="0,0,10,0" VerticalContentAlignment="Center">=</RadioButton>
<RadioButton IsEnabled="{Binding Unrated, Converter={StaticResource notBool}}" Click="ButtonBase_OnClick" GroupName="Rating" IsChecked="{Binding RatingOp, Mode=TwoWay, Converter={StaticResource isStrValueBool}, ConverterParameter='&lt;='}" Margin="0,0,10,0" VerticalContentAlignment="Center">&lt;=</RadioButton>
<RadioButton IsEnabled="{Binding Unrated, Converter={StaticResource notBool}}" Click="ButtonBase_OnClick" GroupName="Rating" IsChecked="{Binding RatingOp, Mode=TwoWay, Converter={StaticResource isStrValueBool}, ConverterParameter='&gt;='}" Margin="0,0,10,0" VerticalContentAlignment="Center">&gt;=</RadioButton>
<RadioButton IsEnabled="{Binding Unrated, Converter={StaticResource notBool}}" Click="ButtonBase_OnClick" GroupName="Rating" IsChecked="{Binding RatingOp, Mode=TwoWay, Converter={StaticResource isStrValueBool}, ConverterParameter='!='}" Margin="0,0,10,0" VerticalContentAlignment="Center">!=</RadioButton>
<TextBox IsEnabled="{Binding Unrated, Converter={StaticResource notBool}}" Text="{Binding Rating}" KeyDown="UIElement_OnKeyDown" Height="20" Width="50" Margin="0,0,10,0" ></TextBox>
<CheckBox IsChecked="{Binding Unrated}" VerticalContentAlignment="Center">Unrated</CheckBox>
<CheckBox IsChecked="{Binding Unrated}"
VerticalContentAlignment="Center"
Name="chkUnratedCheckbox">Unrated</CheckBox>
</StackPanel>
</Grid>
<Grid Margin="0,0,0,5">
Expand Down
9 changes: 9 additions & 0 deletions Diffusion.Toolkit/Controls/FilterControlModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class FilterControlModel : BaseNotify
private int _batchSize;
private bool _useBatchPos;
private int _batchPos;
private bool _noAestheticScore;
private bool _useAestheticScore;
private string _aestheticScoreOp;
private double? _aestheticScore;
Expand Down Expand Up @@ -283,6 +284,12 @@ public bool UseAestheticScore
set => SetField(ref _useAestheticScore, value);
}

public bool NoAestheticScore
{
get => _noAestheticScore;
set => SetField(ref _noAestheticScore, value);
}

public string AestheticScoreOp
{
get => _aestheticScoreOp;
Expand Down Expand Up @@ -383,6 +390,7 @@ public bool NoMetadata
UseForDeletion ||
UseBatchSize ||
UseBatchPos ||
NoAestheticScore ||
UseAestheticScore ||
UsePath ||
UseCreationDate ||
Expand Down Expand Up @@ -411,6 +419,7 @@ public void Clear()
ForDeletion = false;
BatchSize = 0;
BatchPos = 0;
NoAestheticScore = false;
AestheticScoreOp = String.Empty;
AestheticScore = null;
Path = String.Empty;
Expand Down
2 changes: 1 addition & 1 deletion Diffusion.Toolkit/Controls/ThumbnailView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
</ListView.InputBindings>
<ListView.ContextMenu>
<ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem ItemsSource="{Binding AlbumMenuItems}" x:Name="AlbumMenu" Header="{lex:Loc Thumbnail.ContextMenu.AddToAlbum}">
<MenuItem ItemsSource="{Binding AlbumMenuItems}" x:Name="AlbumMenu" Header="{lex:Loc Thumbnail.ContextMenu.AddToAlbum}" >
<!--<MenuItem Header="_New Album" Click="CreateAlbum_OnClick"></MenuItem>
<Separator></Separator>-->
</MenuItem>
Expand Down
4 changes: 1 addition & 3 deletions Diffusion.Toolkit/Controls/ThumbnailView.xaml.Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ public void ReloadThumbnailsView(double offset)
{
var wrapPanel = GetChildOfType<WrapPanel>(this)!;

if (wrapPanel.Children.Count == 0)
{
if (wrapPanel == null || wrapPanel.Children.Count == 0)
return;
}

var scrollViewer = GetChildOfType<ScrollViewer>(this)!;

Expand Down
49 changes: 29 additions & 20 deletions Diffusion.Toolkit/Controls/ThumbnailView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,23 @@ public void ReloadAlbums()
{
Header = GetLocalizedText("Thumbnail.ContextMenu.AddToAlbum.NewAlbum"),
};

albumMenuItem.Click += CreateAlbum_OnClick;

var refreshAlbumMenuItem = new MenuItem()
{
Header = GetLocalizedText("Menu.View.Refresh"),
};
refreshAlbumMenuItem.Click += RefreshAlbum_OnClick;

Model.AlbumMenuItems = new ObservableCollection<Control>(new List<Control>()
{
albumMenuItem,
refreshAlbumMenuItem,
new Separator()
});


var albums = DataStore.GetAlbumsByLastUpdated(10);

var albums = DataStore.GetAlbumsByName();

foreach (var album in albums)
{
Expand Down Expand Up @@ -255,13 +260,15 @@ private void ThumbnailListView_OnKeyDown(object sender, KeyEventArgs e)
public void ShowItem(int index, bool focus = false)
{
var wrapPanel = GetChildOfType<WrapPanel>(this)!;
var item = wrapPanel.Children[index] as ListViewItem;
if (wrapPanel == null) return;

ListViewItem? item = wrapPanel.Children[index] as ListViewItem;
if (item == null) return;

ThumbnailListView.ScrollIntoView(item);
item.BringIntoView();
if (focus)
{
item.Focus();
}
}


Expand Down Expand Up @@ -522,26 +529,23 @@ private async void RemoveEntry()

private void DeleteSelected()
{
if (ThumbnailListView.SelectedItems != null)
if (ThumbnailListView.SelectedItems != null && ThumbnailListView.SelectedItems.Count > 0)
{
if (ThumbnailListView.SelectedItems != null)
{
var imageEntries = ThumbnailListView.SelectedItems.Cast<ImageEntry>().ToList();
var imageEntries = ThumbnailListView.SelectedItems.Cast<ImageEntry>().ToList();

var delete = !imageEntries.GroupBy(e => e.ForDeletion).OrderByDescending(g => g.Count()).First().Key;
var delete = !imageEntries.GroupBy(e => e.ForDeletion).OrderByDescending(g => g.Count()).First().Key;

foreach (var entry in imageEntries)
foreach (var entry in imageEntries)
{
entry.ForDeletion = delete;
if (Model.CurrentImage != null && Model.CurrentImage.Path == entry.Path)
{
entry.ForDeletion = delete;
if (Model.CurrentImage != null && Model.CurrentImage.Path == entry.Path)
{
Model.CurrentImage.ForDeletion = delete;
}
Model.CurrentImage.ForDeletion = delete;
}

var ids = imageEntries.Select(x => x.Id).ToList();
DataStore.SetDeleted(ids, delete);
}

var ids = imageEntries.Select(x => x.Id).ToList();
DataStore.SetDeleted(ids, delete);
}
}

Expand Down Expand Up @@ -749,6 +753,11 @@ private void CreateAlbum_OnClick(object sender, RoutedEventArgs e)
AddAlbumCommand?.Execute(null);
}

private void RefreshAlbum_OnClick(object sender, RoutedEventArgs e)
{
ReloadAlbums();
}

private void AddToAlbum_OnClick(object sender, RoutedEventArgs e)
{
AddToAlbumCommand?.Execute(sender);
Expand Down
28 changes: 18 additions & 10 deletions Diffusion.Toolkit/MainWindow.xaml.Albums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ private void InitAlbums()
{
var album = (Album)((MenuItem)o).Tag;
var images = _model.SelectedImages.Select(x => x.Id).ToList();
_dataStore.AddImagesToAlbum(album.Id, images);
Toast($"{images.Count} image{(images.Count == 1 ? "" : "s")} added to \"{album.Name} \".", "Add to Album");
LoadAlbums();
_search.ReloadMatches(null);
if (_dataStore.AddImagesToAlbum(album.Id, images))
{
Toast($"{images.Count} image{(images.Count == 1 ? "" : "s")} added to \"{album.Name} \".", "Add to Album");
LoadAlbums();
_search.ReloadMatches(null);
}
else
MessageBox.Show("Album not found, please refresh and try again", "No Album");
});

_model.RemoveFromAlbumCommand = new RelayCommand<object>((o) =>
Expand Down Expand Up @@ -171,14 +175,18 @@ private void AddSelectedImagesToAlbum(IAlbumInfo album)
if (_model.SelectedImages != null)
{
var images = _model.SelectedImages.Select(x => x.Id).ToList();
_dataStore.AddImagesToAlbum(album.Id, images);
Toast($"{images.Count} image{(images.Count == 1 ? "" : "s")} added to \"{album.Name}\".", "Add to Album");
LoadAlbums();
foreach (var image in _model.SelectedImages)
if(_dataStore.AddImagesToAlbum(album.Id, images))
{
image.AlbumCount++;
Toast($"{images.Count} image{(images.Count == 1 ? "" : "s")} added to \"{album.Name}\".", "Add to Album");
LoadAlbums();
foreach (var image in _model.SelectedImages)
{
image.AlbumCount++;
}
//_search.ReloadMatches(null);
}
//_search.ReloadMatches(null);
else
MessageBox.Show("Album not found, please refresh and try again", "No Album");
}
}

Expand Down
4 changes: 2 additions & 2 deletions Diffusion.Toolkit/MainWindow.xaml.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ private void RemoveMarked(object obj)
_model.CurrentProgress = count;
});

_dataStore.DeleteImage(imagePath.Id);

File.Delete(imagePath.Path);
var dir = Path.GetDirectoryName(imagePath.Path);
var fileName = Path.GetFileNameWithoutExtension(imagePath.Path);
Expand All @@ -122,8 +124,6 @@ private void RemoveMarked(object obj)
File.Delete(textFilePath);
}

_dataStore.DeleteImage(imagePath.Id);

}
catch (Exception e)
{
Expand Down
Loading
Loading