From 82971fd188a5015dc1aae4312d02bbeef0efae40 Mon Sep 17 00:00:00 2001 From: Marcel Rehkemper Date: Thu, 27 Jan 2022 19:01:35 +0100 Subject: [PATCH] single item viewer moved to dedicated control --- .../Pages/ViewModels/LibraryPageVm.cs | 54 ++--------- .../Pages/Views/LibraryPageV.xaml | 39 +++----- .../Pages/Views/LibraryPageV.xaml.cs | 35 ++----- .../UserControls/ViewModels/ItemActionsVm.cs | 8 +- .../ViewModels/SingleItemViewerVm.cs | 93 +++++++++++++++++++ .../UserControls/Views/SingleItemViewerV.xaml | 40 ++++++++ .../Views/SingleItemViewerV.xaml.cs | 60 ++++++++++++ .../Views/ItemControls/ItemViewerHeader.xaml | 15 +++ 8 files changed, 239 insertions(+), 105 deletions(-) create mode 100644 Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/ViewModels/SingleItemViewerVm.cs create mode 100644 Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/Views/SingleItemViewerV.xaml create mode 100644 Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/Views/SingleItemViewerV.xaml.cs diff --git a/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/ViewModels/LibraryPageVm.cs b/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/ViewModels/LibraryPageVm.cs index ef2c8a01..131317c7 100644 --- a/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/ViewModels/LibraryPageVm.cs +++ b/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/ViewModels/LibraryPageVm.cs @@ -33,26 +33,18 @@ public LibraryPageVm( IItemList itemList, IFilteredListHeader listHeader, IItemListFilter filter, - IItemModelViewer modelViewer, - IItemActions actions, - IItemDataViewer dataViewer, - IItemViewerHeader viewerHeader, - IItemFileList fileList, - IGallery gallery, - IGalleryService galleryService) + IGalleryService galleryService, + ISingleItemViewer singleItemViewer, + IItemActions itemActions) { _galleryService = galleryService; ItemList = itemList.DisposeWith(_disposables); ListHeader = listHeader; Filter = filter; - ModelViewer = modelViewer; - Actions = actions; - Actions.GenerateThumbnailsByViewportCommand = ModelViewer.GenerateThumbnailCommand; - Actions.SelectedItems = ItemList.SelectedItems; - DataViewer = dataViewer; - ViewerHeader = viewerHeader; - Gallery = gallery; - FileList = fileList.DisposeWith(_disposables); + SingleItemViewer = singleItemViewer; + ItemActions = itemActions; + + ItemActions.GenerateThumbnailsByViewportCommand = singleItemViewer.GenerateThumbnailCommand; var selection = ItemList.SelectedItems .Connect() @@ -69,19 +61,8 @@ public LibraryPageVm( .RefCount(); this.WhenActivated(() => new[] { - itemChanges - .BindTo(this, vm => vm.ModelViewer.Item), - itemChanges - .BindTo(this, vm => vm.DataViewer.Item), - itemChanges - .BindTo(this, vm => vm.ViewerHeader.Item), - itemChanges - .BindTo(this, vm => vm.FileList.Item), - itemChanges - .BindTo(this, vm => vm.Gallery.Item), - itemChanges - .BindTo(this, vm => vm.Actions.Item), filter.FilterChanges.BindTo(this, vm=>vm.ItemList.Filter), + itemChanges.BindTo(this, vm=>vm.SingleItemViewer.Item), this._selectionErrorText = selection .Select(items=> items.Count switch @@ -98,12 +79,8 @@ public LibraryPageVm( public IItemList ItemList { get; } public IFilteredListHeader ListHeader { get; } public IItemListFilter Filter { get; } - public IItemModelViewer ModelViewer { get; } - public IItemActions Actions { get; } - public IItemDataViewer DataViewer { get; } - public IItemViewerHeader ViewerHeader { get; } - public IGallery Gallery { get; } - public IItemFileList FileList { get; } + public ISingleItemViewer SingleItemViewer { get; } + public IItemActions ItemActions { get; } public ViewModelActivator Activator { get; } = new(); public PackIconKind? Icon => PackIconKind.Bookshelf; public Name Title => Name.From("Item Library"); @@ -113,16 +90,5 @@ public LibraryPageVm( public Message SelectionErrorText => _selectionErrorText.Value; public void Dispose() => _disposables.Dispose(); - - public void HandleFileDrop(FilePath[] filePaths) - { - var images = - filePaths - .Where(file => file.IsImage()) - .Select(img => img.ToImagePath()) - .ToArray(); - var item = ItemList.SelectedItems.Items.FirstOrDefault(); - _galleryService.AddImagesToItem(item,images); - } } } \ No newline at end of file diff --git a/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/Views/LibraryPageV.xaml b/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/Views/LibraryPageV.xaml index 62066ce8..1ae9c2ec 100644 --- a/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/Views/LibraryPageV.xaml +++ b/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/Views/LibraryPageV.xaml @@ -51,38 +51,10 @@ Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" /> - - - - - - - - - - - - - - - - - - + + + + diff --git a/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/Views/LibraryPageV.xaml.cs b/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/Views/LibraryPageV.xaml.cs index 209088eb..66f5b6af 100644 --- a/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/Views/LibraryPageV.xaml.cs +++ b/Domain/TableTopCrucible.Domain.Library.Wpf/Pages/Views/LibraryPageV.xaml.cs @@ -20,46 +20,23 @@ public LibraryPageV() this.Bind(ViewModel, vm => vm.ItemList, v => v.ItemList.ViewModel), + this.Bind(ViewModel, + vm => vm.SingleItemViewer, + v => v.SingleItemViewer.ViewModel), + this.Bind(ViewModel, + vm => vm.ItemActions, + v => v.Actions.ViewModel), this.Bind(ViewModel, vm => vm.ListHeader, v => v.ListHeader.ViewModel), this.Bind(ViewModel, vm => vm.Filter, v => v.Filter.ViewModel), - this.Bind(ViewModel, - vm => vm.ModelViewer, - v => v.ModelViewer.ViewModel), - this.Bind(ViewModel, - vm => vm.DataViewer, - v => v.DataViewer.ViewModel), - this.Bind(ViewModel, - vm => vm.Actions, - v => v.Actions.ViewModel), - this.Bind(ViewModel, - vm => vm.ViewerHeader, - v => v.ViewerHeader.ViewModel), - this.Bind(ViewModel, - vm => vm.FileList, - v => v.FileList.ViewModel), - this.Bind(ViewModel, - vm => vm.Gallery, - v => v.Gallery.ViewModel), this.OneWayBind(ViewModel, vm=>vm.SelectionErrorText, v=>v.SelectionErrorDisplay.Text), }); } - private void UIElement_OnDrop(object sender, DragEventArgs e) - { - if (e.Data.GetDataPresent("FileDrop")) - { - var paths = - (e.Data.GetData("FileDrop") as string[]) - .Select(FilePath.From).ToArray() - .ToArray(); - ViewModel.HandleFileDrop(paths); - } - } } } \ No newline at end of file diff --git a/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/ViewModels/ItemActionsVm.cs b/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/ViewModels/ItemActionsVm.cs index 6c61900c..bbd2734f 100644 --- a/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/ViewModels/ItemActionsVm.cs +++ b/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/ViewModels/ItemActionsVm.cs @@ -48,7 +48,7 @@ public class ItemActionsVm : ReactiveObject, IItemActions, IActivatableViewModel [Reactive] public ReactiveCommand GenerateThumbnailsByViewportCommand { get; set; } [Reactive] - public ICommand GenerateThumbnailsCommand { get; private set; } + public ReactiveCommand GenerateThumbnailsCommand { get; private set; } public ICommand PickThumbnailsCommand { get; } public Interaction DeletionConfirmation { get; } = new(); public Interaction> SelectImages { get; } = new(); @@ -108,16 +108,16 @@ public ItemActionsVm( } ); }); - this.WhenActivated(() => new[] + this.WhenActivated(() => new [] { - ReactiveCommandHelper.Create(() => + GenerateThumbnailsCommand = ReactiveCommand.Create(() => { if (SelectedItems!.Items.Count() == 1) GenerateThumbnailsByViewportCommand!.Execute(); else thumbnailGenerationService.GenerateManyAsync(SelectedItems.Items, null, true); - },GenerateThumbnailsByViewportCommand!.CanExecute,cmd=>GenerateThumbnailsCommand = cmd) + },GenerateThumbnailsByViewportCommand!.CanExecute.Select(_=>false)) }); } diff --git a/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/ViewModels/SingleItemViewerVm.cs b/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/ViewModels/SingleItemViewerVm.cs new file mode 100644 index 00000000..d378e055 --- /dev/null +++ b/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/ViewModels/SingleItemViewerVm.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reactive; +using System.Reactive.Disposables; +using System.Reactive.Linq; +using System.Text; +using System.Threading.Tasks; +using ReactiveUI; +using ReactiveUI.Fody.Helpers; +using TableTopCrucible.Core.DependencyInjection.Attributes; +using TableTopCrucible.Core.ValueTypes; +using TableTopCrucible.Infrastructure.Models.Entities; +using TableTopCrucible.Shared.Services; +using TableTopCrucible.Shared.Wpf.UserControls.ViewModels; +using TableTopCrucible.Shared.Wpf.UserControls.ViewModels.ItemControls; + +namespace TableTopCrucible.Domain.Library.Wpf.UserControls.ViewModels +{ + [Transient] + public interface ISingleItemViewer:IDisposable + { + public Item Item { get; set; } + ReactiveCommand GenerateThumbnailCommand { get; } + } + public class SingleItemViewerVm:ReactiveObject, IActivatableViewModel, ISingleItemViewer + { + private readonly IGalleryService _galleryService; + private readonly CompositeDisposable _disposables = new(); + public IItemActions Actions { get; } + public IItemFileList FileList { get; } + public IGallery Gallery { get; } + public IItemDataViewer DataViewer { get; } + public IItemModelViewer ModelViewer { get; } + public IItemViewerHeader ViewerHeader { get; } + public ViewModelActivator Activator { get; } = new(); + + public SingleItemViewerVm( + IItemActions actions, + IItemFileList fileList, + IGallery gallery, + IItemDataViewer dataViewer, + IItemModelViewer modelViewer, + IItemViewerHeader viewerHeader, + IGalleryService galleryService) + { + _galleryService = galleryService; + Actions = actions; + FileList = fileList.DisposeWith(_disposables); + Gallery = gallery; + DataViewer = dataViewer; + ModelViewer = modelViewer; + ViewerHeader = viewerHeader; + Actions.GenerateThumbnailsByViewportCommand = ModelViewer.GenerateThumbnailCommand; + + var itemChanges = this.WhenAnyValue(vm => vm.Item) + .Publish() + .RefCount(); + this.WhenActivated(()=>new [] + { + itemChanges + .BindTo(this, vm => vm.ModelViewer.Item), + itemChanges + .BindTo(this, vm => vm.DataViewer.Item), + itemChanges + .BindTo(this, vm => vm.ViewerHeader.Item), + itemChanges + .BindTo(this, vm => vm.FileList.Item), + itemChanges + .BindTo(this, vm => vm.Gallery.Item), + itemChanges + .BindTo(this, vm => vm.Actions.Item) + }); + } + [Reactive] + public Item Item { get; set; } + + public ReactiveCommand GenerateThumbnailCommand => ModelViewer.GenerateThumbnailCommand; + + public void Dispose() + => _disposables.Dispose(); + public void HandleFileDrop(FilePath[] filePaths) + { + var images = + filePaths + .Where(file => file.IsImage()) + .Select(img => img.ToImagePath()) + .ToArray(); + _galleryService.AddImagesToItem(Item, images); + } + } + +} diff --git a/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/Views/SingleItemViewerV.xaml b/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/Views/SingleItemViewerV.xaml new file mode 100644 index 00000000..8343a17e --- /dev/null +++ b/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/Views/SingleItemViewerV.xaml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/Views/SingleItemViewerV.xaml.cs b/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/Views/SingleItemViewerV.xaml.cs new file mode 100644 index 00000000..564bb8c3 --- /dev/null +++ b/Domain/TableTopCrucible.Domain.Library.Wpf/UserControls/Views/SingleItemViewerV.xaml.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using ReactiveUI; +using TableTopCrucible.Core.ValueTypes; + +namespace TableTopCrucible.Domain.Library.Wpf.UserControls.Views +{ + /// + /// Interaction logic for SingleItemViewerV.xaml + /// + public partial class SingleItemViewerV + { + public SingleItemViewerV() + { + InitializeComponent(); + this.WhenActivated(() =>new[] + { + this.Bind(ViewModel, + vm => vm.ModelViewer, + v => v.ModelViewer.ViewModel), + this.Bind(ViewModel, + vm => vm.DataViewer, + v => v.DataViewer.ViewModel), + this.Bind(ViewModel, + vm => vm.ViewerHeader, + v => v.ViewerHeader.ViewModel), + this.Bind(ViewModel, + vm => vm.FileList, + v => v.FileList.ViewModel), + this.Bind(ViewModel, + vm => vm.Gallery, + v => v.Gallery.ViewModel), + }); + } + private void UIElement_OnDrop(object sender, DragEventArgs e) + { + if (!e.Data.GetDataPresent("FileDrop")) + return; + + var paths = + (e.Data.GetData("FileDrop") as string[]) + .Select(FilePath.From).ToArray() + .ToArray(); + ViewModel.HandleFileDrop(paths); + } + + } +} diff --git a/Shared/TableTopCrucible.Shared.Wpf/UserControls/Views/ItemControls/ItemViewerHeader.xaml b/Shared/TableTopCrucible.Shared.Wpf/UserControls/Views/ItemControls/ItemViewerHeader.xaml index 16cdfd23..67fe7fa1 100644 --- a/Shared/TableTopCrucible.Shared.Wpf/UserControls/Views/ItemControls/ItemViewerHeader.xaml +++ b/Shared/TableTopCrucible.Shared.Wpf/UserControls/Views/ItemControls/ItemViewerHeader.xaml @@ -14,6 +14,7 @@ + + + + + + + \ No newline at end of file