Skip to content

Commit

Permalink
Add ability to hide screenshots with or without assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Neakita committed Sep 14, 2023
1 parent c35947b commit d96a8e6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DynamicData;
using DynamicData.Aggregation;
using DynamicData.Binding;
using ReactiveUI;
using SightKeeper.Application.Annotating;
using SightKeeper.Commons;
using SightKeeper.Domain.Model;
Expand All @@ -17,10 +23,35 @@ namespace SightKeeper.Avalonia.ViewModels.Annotating;
public sealed partial class AnnotatorScreenshotsViewModel : ViewModel
{
public SelectedScreenshotViewModel SelectedScreenshotViewModel { get; }
public IReadOnlyList<ScreenshotViewModel> Screenshots { get; }

public IReadOnlyList<ScreenshotViewModel> Screenshots
{
get => _screenshots;
private set => SetProperty(ref _screenshots, value);
}

public IObservable<int> TotalScreenshotsCount { get; }
public IObservable<int> ScreenshotsWithoutAssetsCount { get; }
public IObservable<int> ScreenshotsWithAssetsCount { get; }
public bool ShowScreenshotsWithAssets
{
get => _showScreenshotsWithAssets;
set
{
if (SetProperty(ref _showScreenshotsWithAssets, value))
SetScreenshots();
}
}

public bool ShowScreenshotsWithoutAssets
{
get => _showScreenshotsWithoutAssets;
set
{
if (SetProperty(ref _showScreenshotsWithoutAssets, value))
SetScreenshots();
}
}

public IEnumerable<SortingRule<Screenshot>> SortingRules { get; } = new[]
{
Expand All @@ -43,19 +74,32 @@ public AnnotatorScreenshotsViewModel(ScreenshotImageLoader imageLoader, Screensh
_sortingRule = SortingRules.First();
var sortingRule = this.WhenAnyValue(viewModel => viewModel.SortingRule)
.Select(rule => rule.Comparer);
_screenshots.Connect()
_screenshotsSource.Connect()
.Sort(sortingRule)
.Transform(screenshot => new ScreenshotViewModel(imageLoader, screenshot))
.Bind(out var screenshots)
.PopulateInto(_screenshotViewModels);
TotalScreenshotsCount = _screenshots.Connect().Count();
Screenshots = screenshots;
TotalScreenshotsCount = _screenshotsSource.Connect().Count();
_allScreenshots = screenshots;
ScreenshotsWithAssetsCount = _screenshotViewModels.Connect()
.FilterOnObservable(screenshot => screenshot.IsAssetObservable)
.Count();
ScreenshotsWithoutAssetsCount = _screenshotViewModels.Connect()
.FilterOnObservable(screenshot => screenshot.IsAssetObservable.Select(isAsset => !isAsset))
.Count();
_screenshotViewModels.Connect()
.AutoRefreshOnObservable(_ => _actualizeFilterSubject)
.Filter(screenshot => screenshot.IsAsset)
.Bind(out var screenshotsWithAssets)
.Subscribe();
_screenshotsWithAssetsAndSelected = screenshotsWithAssets;
_screenshotViewModels.Connect()
.AutoRefreshOnObservable(_ => _actualizeFilterSubject)
.Filter(screenshot => !screenshot.IsAsset)
.Bind(out var screenshotsWithoutAssets)
.Subscribe();
_screenshotsWithoutAssets = screenshotsWithoutAssets;
SetScreenshots();
}

public void ScrollScreenshot(bool reverse)
Expand All @@ -66,21 +110,26 @@ public void ScrollScreenshot(bool reverse)
}

private readonly ScreenshotsDataAccess _screenshotsDataAccess;
private readonly SourceList<Screenshot> _screenshots = new();
private readonly SourceList<Screenshot> _screenshotsSource = new();
private readonly ReadOnlyObservableCollection<ScreenshotViewModel> _allScreenshots;
private readonly ReadOnlyObservableCollection<ScreenshotViewModel> _screenshotsWithAssetsAndSelected;
private readonly ReadOnlyObservableCollection<ScreenshotViewModel> _screenshotsWithoutAssets;
private readonly SourceList<ScreenshotViewModel> _screenshotViewModels = new();

[ObservableProperty, NotifyPropertyChangedFor(nameof(TotalScreenshotsCount))]
private DataSet? _dataSet;

private CompositeDisposable? _dataSetDisposable;
private bool _isLoading;
private int? _screenshotsWithAssetsCount;
private int? _screenshotsWithoutAssetsCount;
private bool _showScreenshotsWithAssets = true;
private bool _showScreenshotsWithoutAssets = true;
private IReadOnlyList<ScreenshotViewModel> _screenshots;
private readonly Subject<Unit> _actualizeFilterSubject = new();

partial void OnDataSetChanged(DataSet? value)
{
_dataSetDisposable?.Dispose();
_screenshots.Clear();
_screenshotsSource.Clear();
if (value == null)
return;
LoadScreenshots(value);
Expand All @@ -94,15 +143,32 @@ partial void OnDataSetChanged(DataSet? value)
OnPropertyChanged(nameof(TotalScreenshotsCount));
}

private async void LoadScreenshots(DataSet dataSet)
private void LoadScreenshots(DataSet dataSet)
{
IsLoading = true;
_screenshotsDataAccess.Load(
dataSet.ScreenshotsLibrary,
SortingRule.Direction == SortDirection.Descending)
.Subscribe(screenshotsPartition => _screenshots.AddRange(screenshotsPartition), () => IsLoading = false);
.Subscribe(screenshotsPartition => _screenshotsSource.AddRange(screenshotsPartition), () => IsLoading = false);
}

private void OnScreenshotAdded(Screenshot newScreenshot) => _screenshotsSource.Add(newScreenshot);
private void OnScreenshotRemoved(Screenshot removedScreenshot) => _screenshotsSource.Remove(removedScreenshot);

[MemberNotNull(nameof(_screenshots))]
private void SetScreenshots()
{
_screenshots = null!;
if (ShowScreenshotsWithAssets && ShowScreenshotsWithoutAssets)
Screenshots = _allScreenshots;
else if (ShowScreenshotsWithAssets)
Screenshots = _screenshotsWithAssetsAndSelected;
else if (ShowScreenshotsWithoutAssets)
Screenshots = _screenshotsWithoutAssets;
else
Screenshots = Array.Empty<ScreenshotViewModel>();
}

private void OnScreenshotAdded(Screenshot newScreenshot) => _screenshots.Add(newScreenshot);
private void OnScreenshotRemoved(Screenshot removedScreenshot) => _screenshots.Remove(removedScreenshot);
[RelayCommand]
private void ActualizeFiltering() => _actualizeFilterSubject.OnNext(Unit.Default);
}
26 changes: 21 additions & 5 deletions SightKeeper.Avalonia/Views/Annotating/AnnotatorScreenshots.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:annotating="clr-namespace:SightKeeper.Avalonia.ViewModels.Annotating"
xmlns:avalonia="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
xmlns:icons="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="450"
x:Class="SightKeeper.Avalonia.Views.Annotating.AnnotatorScreenshots"
x:DataType="annotating:AnnotatorScreenshotsViewModel">
Expand All @@ -27,6 +27,22 @@
</TextBlock>
</ToolTip.Tip>
</TextBlock>
<Button Content="{icons:MaterialIconExt Cog}">
<Button.Flyout>
<Flyout>
<StackPanel>
<CheckBox Content="Show screenshots without assets"
IsChecked="{Binding ShowScreenshotsWithoutAssets}"/>
<CheckBox Content="Show screenshots with assets"
IsChecked="{Binding ShowScreenshotsWithAssets}"/>
<Button Content="{icons:MaterialIconExt Refresh}"
Command="{Binding ActualizeFilteringCommand}"
ToolTip.Tip="Actualize filtering"/>
</StackPanel>
</Flyout>
</Button.Flyout>
</Button>
</WrapPanel>

<ComboBox Grid.Row="1"
HorizontalAlignment="Right"
Expand All @@ -51,18 +67,18 @@
<Panel Name="Panel">
<Image Source="{Binding Image^.Content, Converter={StaticResource BytesToBitmapConverter}}"
Margin="1"/>
<avalonia:MaterialIcon Kind="BookmarkCheck"
<icons:MaterialIcon Kind="BookmarkCheck"
Width="30" Height="30"
Foreground="Green"
VerticalAlignment="Top"
HorizontalAlignment="Right"
Margin="0 -5 20 0"
ToolTip.Tip="Asset"
IsVisible="{Binding IsAssetObservable^}">
<avalonia:MaterialIcon.Clip>
<icons:MaterialIcon.Clip>
<RectangleGeometry Rect="0, 5, 30, 30"/>
</avalonia:MaterialIcon.Clip>
</avalonia:MaterialIcon>
</icons:MaterialIcon.Clip>
</icons:MaterialIcon>
<TextBlock Text="{Binding CreationDate, StringFormat=G}"
VerticalAlignment="Bottom"
HorizontalAlignment="Left"
Expand Down

0 comments on commit d96a8e6

Please sign in to comment.