Skip to content

Commit 53cf1a6

Browse files
committed
Simplified factory usage
Made clip model immutable from outside Possibly fixed a bug where is clip is created multiple times when added from clipboard
1 parent 5ae31ae commit 53cf1a6

17 files changed

Lines changed: 46 additions & 93 deletions

File tree

ClipboardMachinery/ClipboardMachinery.Core/DataStorage/Impl/ClipLazyProvider.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Data;
3+
using System.Linq;
34
using System.Threading.Tasks;
45
using ClipboardMachinery.Core.DataStorage.Schema;
56
using ServiceStack.OrmLite;
@@ -48,6 +49,7 @@ protected override Task OnQueryBuildingStarts(SqlExpression<Clip> query) {
4849
}
4950

5051
protected override Task OnQueryOrdering(SqlExpression<Clip> query) {
52+
// Order clips by ID column
5153
query.OrderByDescending(clip => clip.Id);
5254
return Task.CompletedTask;
5355
}
@@ -56,12 +58,7 @@ protected override async Task OnBatchLoaded(IDbConnection db, List<Clip> batch)
5658
await base.OnBatchLoaded(db, batch);
5759

5860
// Go thought every single clip in the batch
59-
foreach (Clip clip in batch) {
60-
// Skip empty tags
61-
if (clip.Tags == null) {
62-
continue;
63-
}
64-
61+
foreach (Clip clip in batch.Where(clip => clip.Tags != null)) {
6562
// Load nested references for clip tags
6663
foreach (Tag tag in clip.Tags) {
6764
await db.LoadReferencesAsync(tag);

ClipboardMachinery/ClipboardMachinery/ClipboardMachinery.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,8 @@
195195
<DependentUpon>TagTypesView.xaml</DependentUpon>
196196
</Compile>
197197
<Compile Include="Pages\TagTypes\TagTypesViewModel.cs" />
198-
<Compile Include="Plumbing\Factories\IClipFactory.cs" />
199198
<Compile Include="Plumbing\Factories\ITagKindFactory.cs" />
200-
<Compile Include="Plumbing\Factories\IViewModelFactory.cs" />
201-
<Compile Include="Plumbing\Factories\IColorGalleryFactory.cs" />
199+
<Compile Include="Plumbing\Factories\IClipFactory.cs" />
202200
<Compile Include="Plumbing\Factories\IDialogOverlayFactory.cs" />
203201
<Compile Include="Components\DialogOverlay\IOverlayDialog.cs" />
204202
<Compile Include="Components\DialogOverlay\Impl\Portal\DialogOverlayPortalView.xaml.cs">

ClipboardMachinery/ClipboardMachinery/Common/Screen/ClipPageBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public abstract class ClipPageBase : LazyPageBase<ClipViewModel, ClipModel>, IHa
1515

1616
protected readonly IDataRepository dataRepository;
1717
protected readonly IEventAggregator eventAggregator;
18-
protected readonly IViewModelFactory vmFactory;
18+
protected readonly IClipFactory vmFactory;
1919

2020
#endregion
2121

22-
protected ClipPageBase(int batchSize, IDataRepository dataRepository, IEventAggregator eventAggregator, IViewModelFactory vmFactory)
22+
protected ClipPageBase(int batchSize, IDataRepository dataRepository, IEventAggregator eventAggregator, IClipFactory vmFactory)
2323
: base(dataRepository.CreateLazyClipProvider(batchSize)) {
2424

2525
this.dataRepository = dataRepository;

ClipboardMachinery/ClipboardMachinery/Components/Clip/ClipModel.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using Caliburn.Micro;
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Caliburn.Micro;
24
using ClipboardMachinery.Components.Tag;
3-
using System;
45

56
namespace ClipboardMachinery.Components.Clip {
67

@@ -10,7 +11,7 @@ public class ClipModel : PropertyChangedBase {
1011

1112
public int Id {
1213
get => id;
13-
set {
14+
private set {
1415
if (id == value) {
1516
return;
1617
}
@@ -22,7 +23,7 @@ public int Id {
2223

2324
public string Content {
2425
get => rawContent;
25-
set {
26+
private set {
2627
if (rawContent == value) {
2728
return;
2829
}
@@ -33,7 +34,7 @@ public string Content {
3334
}
3435

3536
public BindableCollection<TagModel> Tags {
36-
set; get;
37+
get;
3738
}
3839

3940
#endregion
@@ -45,8 +46,10 @@ public BindableCollection<TagModel> Tags {
4546

4647
#endregion
4748

48-
public ClipModel() {
49-
Tags = new BindableCollection<TagModel>();
49+
public ClipModel(int id, string content, IEnumerable<TagModel> tags) {
50+
Id = id;
51+
Content = content;
52+
Tags = new BindableCollection<TagModel>(tags ?? Enumerable.Empty<TagModel>());
5053
}
5154

5255
}

ClipboardMachinery/ClipboardMachinery/Components/ColorGallery/ColorGalleryViewModel.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using ClipboardMachinery.Components.Buttons.ActionButton;
33
using ClipboardMachinery.Plumbing.Factories;
44
using System;
5+
using System.Collections.Generic;
56
using System.Linq;
67
using System.Threading;
78
using System.Threading.Tasks;
@@ -54,17 +55,14 @@ public ActionButtonViewModel NextPageButton {
5455

5556
#region Fields
5657

57-
private readonly IColorGalleryFactory colorGalleryFactory;
5858

5959
private IColorPalette selectedPreset;
6060
private Color selectedColor;
6161

6262
#endregion
6363

64-
public ColorGalleryViewModel(IColorGalleryFactory colorGalleryFactory, Func<ActionButtonViewModel> actionButtonFactory) {
65-
this.colorGalleryFactory = colorGalleryFactory;
66-
67-
Presets = new BindableCollection<IColorPalette>(colorGalleryFactory.GetAllPresets());
64+
public ColorGalleryViewModel(IEnumerable<IColorPalette> colorPalettes, Func<ActionButtonViewModel> actionButtonFactory) {
65+
Presets = new BindableCollection<IColorPalette>(colorPalettes);
6866
SelectedPreset = Presets.FirstOrDefault();
6967

7068
// Create control buttons
@@ -79,16 +77,6 @@ public ColorGalleryViewModel(IColorGalleryFactory colorGalleryFactory, Func<Acti
7977
NextPageButton.ClickAction = HandleNextPresetClick;
8078
}
8179

82-
protected override async Task OnDeactivateAsync(bool close, CancellationToken cancellationToken) {
83-
if (close) {
84-
foreach (IColorPalette preset in Presets) {
85-
colorGalleryFactory.Release(preset);
86-
}
87-
}
88-
89-
await base.OnDeactivateAsync(close, cancellationToken);
90-
}
91-
9280
#region Actions
9381

9482
public void SelectColor(Color newColor) {

ClipboardMachinery/ClipboardMachinery/Components/Navigator/NavigatorViewModel.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,22 @@ public string SelectedPageTitle
4545
#endregion
4646

4747
public NavigatorViewModel(
48-
IScreenPage[] availablePages, ILogger logger,
48+
IEnumerable<IScreenPage> availablePages, ILogger logger,
4949
Func<ActionButtonViewModel> actionButtonFactory, Func<SelectableButtonViewModel> selectableButtonFactory) {
5050

5151
Logger = logger;
52+
List<IScreenPage> pages = availablePages.ToList();
53+
54+
// Print out available pages into log
5255
Logger.Info("Listing available pages for the navigator:");
53-
foreach (IScreenPage page in availablePages) {
56+
foreach (IScreenPage page in pages) {
5457
Logger.Info($" - Title={page.Title}, Type={page.GetType().FullName}");
5558
}
5659

57-
// Automatically create pages from ViewModels that implements IScreenPage
58-
List<IScreenPage> pages = availablePages.ToList();
60+
// Order pages by it's defined order
5961
pages.Sort((x, y) => x.Order.CompareTo(y.Order));
6062

63+
// Create view models from available page definitions
6164
Pages = new BindableCollection<SelectableButtonViewModel>(
6265
pages.Select(page => {
6366
SelectableButtonViewModel button = selectableButtonFactory.Invoke();

ClipboardMachinery/ClipboardMachinery/Components/Tag/TagModel.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Caliburn.Micro;
33
using System.Windows.Media;
44
using ClipboardMachinery.Core;
5-
using ClipboardMachinery.Core.DataStorage;
65

76
namespace ClipboardMachinery.Components.Tag {
87

ClipboardMachinery/ClipboardMachinery/Components/TagTypeLister/TagTypeListerViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ private set {
3030

3131
#region Fields
3232

33-
private readonly IViewModelFactory vmFactory;
33+
private readonly IClipFactory vmFactory;
3434
private TagTypeModel selectedItem;
3535

3636
#endregion
3737

38-
public TagTypeListerViewModel(IDataRepository dataRepository, IViewModelFactory vmFactory) : base(dataRepository.CreateLazyTagTypeProvider(15)) {
38+
public TagTypeListerViewModel(IDataRepository dataRepository, IClipFactory vmFactory) : base(dataRepository.CreateLazyTagTypeProvider(15)) {
3939
this.vmFactory = vmFactory;
4040
}
4141

ClipboardMachinery/ClipboardMachinery/Pages/Favorites/FavoritesViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public byte Order
2323

2424
#endregion
2525

26-
public FavoritesViewModel(IDataRepository dataRepository, IEventAggregator eventAggregator, IViewModelFactory vmFactory) : base(15, dataRepository, eventAggregator, vmFactory) {
26+
public FavoritesViewModel(IDataRepository dataRepository, IEventAggregator eventAggregator, IClipFactory vmFactory) : base(15, dataRepository, eventAggregator, vmFactory) {
2727
((ClipLazyProvider) DataProvider).ApplyTagFilter("category", "favorite");
2828
}
2929

ClipboardMachinery/ClipboardMachinery/Pages/History/HistoryViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public byte Order
2525

2626
#endregion
2727

28-
public HistoryViewModel(IDataRepository dataRepository, IEventAggregator eventAggregator, IViewModelFactory vmFactory) : base(15, dataRepository, eventAggregator, vmFactory) {
28+
public HistoryViewModel(IDataRepository dataRepository, IEventAggregator eventAggregator, IClipFactory vmFactory) : base(15, dataRepository, eventAggregator, vmFactory) {
2929
}
3030

3131
#region Logic

0 commit comments

Comments
 (0)