Skip to content

Commit b90f408

Browse files
committed
refactor: cleanup
1 parent 9c30e47 commit b90f408

4 files changed

Lines changed: 23 additions & 87 deletions

File tree

src/ByteSync.Client/ViewModels/Sessions/Inventories/InventoryAnalysisViewModel.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ByteSync.ViewModels.Sessions.Inventories;
1212

1313
public class InventoryAnalysisViewModel : ActivatableViewModelBase
1414
{
15-
private readonly IInventoryService _inventoryService;
15+
private readonly IInventoryService _inventoryService = null!;
1616
private readonly IThemeService _themeService = null!;
1717

1818
public InventoryAnalysisViewModel()
@@ -82,25 +82,25 @@ private void HandleActivation(CompositeDisposable disposables)
8282
case InventoryTaskStatus.Cancelled:
8383
case InventoryTaskStatus.NotLaunched:
8484
AnalysisIcon = "SolidXCircle";
85-
AnalysisIconBrush = _themeService?.GetBrush("MainSecondaryColor");
85+
AnalysisIconBrush = _themeService.GetBrush("MainSecondaryColor");
8686

8787
break;
8888
case InventoryTaskStatus.Success:
8989
AnalysisIcon = errors > 0 ? "RegularError" : "SolidCheckCircle";
9090
AnalysisIconBrush = errors > 0
91-
? _themeService?.GetBrush("MainSecondaryColor")
92-
: _themeService?.GetBrush("HomeCloudSynchronizationBackGround");
91+
? _themeService.GetBrush("MainSecondaryColor")
92+
: _themeService.GetBrush("HomeCloudSynchronizationBackGround");
9393

9494
break;
9595
case InventoryTaskStatus.Pending:
9696
case InventoryTaskStatus.Running:
9797
AnalysisIcon = "None";
98-
AnalysisIconBrush = _themeService?.GetBrush("HomeCloudSynchronizationBackGround");
98+
AnalysisIconBrush = _themeService.GetBrush("HomeCloudSynchronizationBackGround");
9999

100100
break;
101101
default:
102102
AnalysisIcon = "None";
103-
AnalysisIconBrush = _themeService?.GetBrush("HomeCloudSynchronizationBackGround");
103+
AnalysisIconBrush = _themeService.GetBrush("HomeCloudSynchronizationBackGround");
104104

105105
break;
106106
}

src/ByteSync.Client/ViewModels/Sessions/Inventories/InventoryMainStatusViewModel.cs

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System.Reactive;
22
using System.Reactive.Disposables;
33
using System.Reactive.Linq;
4-
using Avalonia;
54
using Avalonia.Media;
6-
using Avalonia.Styling;
75
using ByteSync.Assets.Resources;
86
using ByteSync.Business;
97
using ByteSync.Business.Inventories;
@@ -33,13 +31,14 @@ public InventoryMainStatusViewModel()
3331
}
3432

3533
public InventoryMainStatusViewModel(IInventoryService inventoryService, ISessionService sessionService,
36-
ITimeTrackingCache timeTrackingCache, IDialogService dialogService, ILogger<InventoryMainStatusViewModel> logger,
37-
IInventoryStatisticsService inventoryStatisticsService)
34+
ITimeTrackingCache timeTrackingCache, IDialogService dialogService, IThemeService themeService,
35+
IInventoryStatisticsService inventoryStatisticsService, ILogger<InventoryMainStatusViewModel> logger)
3836
{
3937
_inventoryService = inventoryService;
4038
_sessionService = sessionService;
4139
_timeTrackingCache = timeTrackingCache;
4240
_dialogService = dialogService;
41+
_themeService = themeService;
4342
_logger = logger;
4443

4544
AbortIventoryCommand = ReactiveCommand.CreateFromTask(AbortInventory);
@@ -162,7 +161,7 @@ public InventoryMainStatusViewModel(IInventoryService inventoryService, ISession
162161
{
163162
GlobalMainIcon = v.Icon;
164163
GlobalMainStatusText = v.Text;
165-
GlobalMainIconBrush = GetBrushSafe(v.BrushKey);
164+
GlobalMainIconBrush = themeService.GetBrush(v.BrushKey);
166165
})
167166
.DisposeWith(disposables);
168167

@@ -181,16 +180,6 @@ public InventoryMainStatusViewModel(IInventoryService inventoryService, ISession
181180
});
182181
}
183182

184-
// // Overload with theme service for color selection
185-
// public InventoryMainStatusViewModel(IInventoryService inventoryService, ISessionService sessionService,
186-
// ITimeTrackingCache timeTrackingCache, IDialogService dialogService, ILogger<InventoryMainStatusViewModel> logger,
187-
// IInventoryStatisticsService inventoryStatisticsService, IThemeService themeService)
188-
// : this(inventoryService, sessionService, timeTrackingCache, dialogService, logger, inventoryStatisticsService)
189-
// {
190-
// _themeService = themeService;
191-
// GlobalMainIconBrush = _themeService?.GetBrush("HomeCloudSynchronizationBackGround");
192-
// }
193-
194183
private void HandleActivation(CompositeDisposable disposables)
195184
{
196185
_inventoryService.InventoryProcessData.GlobalMainStatus
@@ -275,26 +264,4 @@ private async Task AbortInventory()
275264
await _inventoryService.AbortInventory();
276265
}
277266
}
278-
279-
private IBrush? GetBrushSafe(string resourceName)
280-
{
281-
var brush = _themeService?.GetBrush(resourceName);
282-
283-
if (brush != null) return brush;
284-
285-
var themeVariant = Application.Current?.RequestedThemeVariant ?? ThemeVariant.Default;
286-
if (Application.Current?.Styles.TryGetResource(resourceName, themeVariant, out var res) == true)
287-
{
288-
if (res is IBrush b) return b;
289-
if (res is Color c) return new SolidColorBrush(c);
290-
}
291-
292-
if (Application.Current?.Styles.TryGetResource(resourceName, ThemeVariant.Default, out var res2) == true)
293-
{
294-
if (res2 is IBrush b2) return b2;
295-
if (res2 is Color c2) return new SolidColorBrush(c2);
296-
}
297-
298-
return null;
299-
}
300267
}

src/ByteSync.Client/ViewModels/Sessions/Inventories/InventoryProcessViewModel.cs

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,27 @@
88
using ByteSync.Interfaces.Dialogs;
99
using ReactiveUI;
1010
using ReactiveUI.Fody.Helpers;
11-
using Serilog;
1211

1312
namespace ByteSync.ViewModels.Sessions.Inventories;
1413

1514
public class InventoryProcessViewModel : ActivatableViewModelBase
1615
{
1716
private readonly IInventoryService _inventoryService;
1817
private readonly IDialogService _dialogService;
18+
private readonly ILogger<InventoryProcessViewModel> _logger;
1919

2020
public InventoryProcessViewModel()
2121
{
2222
}
2323

2424
public InventoryProcessViewModel(InventoryMainStatusViewModel inventoryMainStatusViewModel,
2525
InventoryIdentificationViewModel inventoryIdentificationViewModel, InventoryAnalysisViewModel inventoryAnalysisViewModel,
26-
InventoryBeforeStartViewModel inventoryBeforeStartViewModel, IInventoryService inventoryService, IDialogService dialogService)
26+
InventoryBeforeStartViewModel inventoryBeforeStartViewModel, IInventoryService inventoryService, IDialogService dialogService,
27+
ILogger<InventoryProcessViewModel> logger)
2728
{
2829
_inventoryService = inventoryService;
2930
_dialogService = dialogService;
31+
_logger = logger;
3032

3133
InventoryMainStatusViewModel = inventoryMainStatusViewModel;
3234
InventoryIdentificationViewModel = inventoryIdentificationViewModel;
@@ -48,46 +50,8 @@ private void HandleActivation(CompositeDisposable disposables)
4850
.DisposeWith(disposables);
4951
}
5052

51-
private IObservable<(InventoryMonitorData, InventoryTaskStatus)> SampledMonitorData
52-
{
53-
get
54-
{
55-
var source = InventoryProcessData.InventoryMonitorObservable.CombineLatest(InventoryProcessData.IdentificationStatus);
56-
57-
Func<(InventoryMonitorData, InventoryTaskStatus), bool> canSkip =
58-
tuple =>
59-
{
60-
var inventoryMonitorData = tuple.Item1;
61-
var localInventoryPartStatus = tuple.Item2;
62-
63-
return inventoryMonitorData.HasNonZeroProperty() &&
64-
localInventoryPartStatus.In(InventoryTaskStatus.Running);
65-
};
66-
67-
// Share the source so that it's not subscribed multiple times
68-
var sharedSource = source.Publish().RefCount();
69-
70-
// Sample the source observable every 0.52 seconds, but only for values that can be skipped
71-
var sampled = sharedSource
72-
.Where(canSkip)
73-
.Sample(TimeSpan.FromSeconds(0.5));
74-
75-
// Get the values from the shared source that can not be skipped
76-
var notSkipped = sharedSource
77-
.Where(value => !canSkip(value));
78-
79-
// Merge the sampled and notSkipped sequences
80-
var merged = sampled.Merge(notSkipped);
81-
82-
return merged;
83-
}
84-
}
85-
8653
public extern bool HasLocalInventoryStarted { [ObservableAsProperty] get; }
8754

88-
[Reactive]
89-
public InventoryMonitorData MonitorData { get; set; }
90-
9155
[Reactive]
9256
public InventoryProcessData InventoryProcessData { get; set; }
9357

@@ -110,9 +74,9 @@ private async Task AbortInventory()
11074

11175
if (result == MessageBoxResult.Yes)
11276
{
113-
Log.Information("inventory aborted on user request");
77+
_logger.LogInformation("inventory aborted on user request");
11478

115-
_inventoryService.InventoryProcessData?.RequestInventoryAbort();
79+
_inventoryService.InventoryProcessData.RequestInventoryAbort();
11680

11781
await _inventoryService.AbortInventory();
11882
}

tests/ByteSync.Client.Tests/ViewModels/Sessions/Inventories/InventoryMainStatusViewModelTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using ByteSync.Business.Misc;
55
using ByteSync.Business.Sessions;
66
using ByteSync.Interfaces.Controls.Inventories;
7+
using ByteSync.Interfaces.Controls.Themes;
78
using ByteSync.Interfaces.Controls.TimeTracking;
89
using ByteSync.Interfaces.Dialogs;
910
using ByteSync.Interfaces.Services.Sessions;
@@ -26,6 +27,7 @@ public class InventoryMainStatusViewModelTests
2627
private Mock<ISessionService> _sessionService = null!;
2728
private Mock<ITimeTrackingCache> _timeTrackingCache = null!;
2829
private Mock<IDialogService> _dialogService = null!;
30+
private Mock<IThemeService> _themeService = null!;
2931
private Mock<ILogger<InventoryMainStatusViewModel>> _logger = null!;
3032

3133
[SetUp]
@@ -40,6 +42,8 @@ public void Setup()
4042
_statsService = new Mock<IInventoryStatisticsService>();
4143
_statsService.SetupGet(x => x.Statistics).Returns(_statsSubject.AsObservable());
4244

45+
_themeService = new Mock<IThemeService>();
46+
4347
_sessionService = new Mock<ISessionService>();
4448
_sessionService.SetupGet(x => x.SessionId).Returns("test-session");
4549
_sessionStatusSubject = new Subject<SessionStatus>();
@@ -64,8 +68,9 @@ private InventoryMainStatusViewModel CreateVm()
6468
_sessionService.Object,
6569
_timeTrackingCache.Object,
6670
_dialogService.Object,
67-
_logger.Object,
68-
_statsService.Object);
71+
_themeService.Object,
72+
_statsService.Object,
73+
_logger.Object);
6974
vm.Activator.Activate();
7075

7176
return vm;

0 commit comments

Comments
 (0)