-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceWizardViewModel.cs
154 lines (125 loc) · 5.46 KB
/
DeviceWizardViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using MvvmUtils.Reactive;
using AudioPlayer.Services;
using MvvmUtils;
using Plugin.Connectivity.Abstractions;
using CommonServiceLocator;
namespace AudioPlayer.ViewModels
{
public class DeviceWizardViewModel : AxisViewModelBase
{
private RelayCommand selectDeviceCommand;
private RelayCommand navigateToAddCustomDeviceCommand;
private RelayCommand cancelCommand;
private RelayCommand addDeviceCommand;
private IDisposable subscription;
private IDisposable subscription2;
private IDisposable subscription3;
private DiscoveryDevice selectedDevice;
private string searchText = String.Empty;
public DeviceWizardViewModel(IAppContext context,
IMessageBus messageBus,
IDeviceNavigator navigationService,
IDataService dataService,
IPopupService popupService,
IDeviceDiscoverer deviceDiscoverer,
IConnectivity connectivity)
: base(context, messageBus)
{
DataService = dataService;
Navigation = navigationService;
PopupService = popupService;
DeviceDiscoverer = deviceDiscoverer;
Connectivity = connectivity;
var deviceComparer = Comparer<DiscoveryDevice>.Create(deviceComparison);
AllDevices = new ReactiveSortedCollection<DiscoveryDevice>(deviceComparer);
Devices = new ReactiveSortedCollection<DiscoveryDevice>(deviceComparer);
}
public string SearchText
{
get => searchText;
set => SetProperty(ref searchText, value);
}
public Comparison<DiscoveryDevice> deviceComparison = new Comparison<DiscoveryDevice>((d1, d2) => d1.DisplayName.CompareTo(d2.DisplayName));
public ReactiveSortedCollection<DiscoveryDevice> AllDevices { get; }
public ReactiveSortedCollection<DiscoveryDevice> Devices { get; }
public DiscoveryDevice SelectedDevice
{
get => selectedDevice;
set => SetProperty(ref selectedDevice, value);
}
public ICommand SelectDeviceCommand => selectDeviceCommand ?? (selectDeviceCommand = RelayCommand.Create(async () =>
{
ServiceLocator.Current
.GetInstance<AddDeviceViewModel>()
.Initialize(SelectedDevice);
await Navigation.NavigateTo(Pages.AddDevice);
}));
public ICommand NavigateToAddCustomDeviceCommand
=> navigateToAddCustomDeviceCommand ?? (navigateToAddCustomDeviceCommand = new RelayCommand(async () =>
{
ServiceLocator.Current
.GetInstance<AddCustomDeviceViewModel>()
.Initialize();
await Navigation.NavigateTo(Pages.AddDeviceCustom);
}));
public ICommand CancelCommand => cancelCommand ?? (cancelCommand = RelayCommand.Create(async () => await Navigation.PopModal()));
private bool IsDeviceInFilter(DiscoveryDevice d)
{
if(string.IsNullOrEmpty(SearchText))
{
return true;
}
return d.DisplayName.ToLower().Contains(SearchText.ToLower()) || d.IPAddress.ToLower().Contains(SearchText.ToLower());
}
public async Task Initialize()
{
var whenDeviceDiscovered = DeviceDiscoverer.WhenDeviceDiscovered;
var whenSearchTextChanged = this.WhenAnyValue(vm => vm.SearchText);
var whenItemAddedToAllDevices = AllDevices.WhenItemInserted;
subscription = whenDeviceDiscovered
.Throttle(TimeSpan.FromMilliseconds(200))
.Where(IsDeviceInFilter)
.Subscribe((device) =>
{
if (!AllDevices.Any(d => d.IPAddress == device.IPAddress))
{
AllDevices.Add(device);
}
if (IsDeviceInFilter(device))
{
if (!Devices.Any(d => d.IPAddress == device.IPAddress))
{
Devices.Add(device);
}
}
});
subscription2 = whenSearchTextChanged
.DistinctUntilChanged()
.Throttle(TimeSpan.FromMilliseconds(800))
.Subscribe((searchText) =>
{
});
DeviceDiscoverer.Start();
SearchText = string.Empty;
}
public void Cleanup()
{
DeviceDiscoverer.Stop();
subscription.Dispose();
subscription2.Dispose();
AllDevices.Clear();
Devices.Clear();
}
public IDeviceDiscoverer DeviceDiscoverer { get; }
public IPopupService PopupService { get; }
public IDataService DataService { get; }
public IDeviceNavigator Navigation { get; }
public IConnectivity Connectivity { get; }
}
}