-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
395 lines (308 loc) · 13.2 KB
/
MainWindow.xaml.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using Hardcodet.Wpf.TaskbarNotification;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
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;
namespace FluxHueBridge
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private App _app;
private HueApiService _apiService;
private bool _hasColorChoiceInitialized = false;
private bool _hasNameTextInitialized = false;
private bool _nameUpdateTaskRunning = false;
private Stopwatch _nameTextChangeTimer = new Stopwatch();
private float _nameTextCooldown = 2.5f;
private Brush _defaultColor = new SolidColorBrush(Color.FromRgb(234, 238, 247));
private Brush _warningColor = new SolidColorBrush(Color.FromRgb(255, 234, 0));
private Brush _successColor = new SolidColorBrush(Color.FromRgb(87, 197, 104));
private Brush _errorColor = new SolidColorBrush(Color.FromRgb(237, 69, 69));
private string _status = "Waiting for data from f.lux";
public string Status { get => _status;
set
{
_status = value;
OnPropertyChanged();
}
}
private Brush _statusColor = new SolidColorBrush(Color.FromRgb(234, 238, 247));
public Brush StatusColor { get => _statusColor;
set
{
_statusColor = value;
OnPropertyChanged();
}
}
private List<SceneSwitchAction>? _sceneSwitchActions = null;
public List<SceneSwitchAction>? SceneSwitchActions
{
get
{
var json = Properties.Settings.Default.SceneSwitchJSON;
if (_sceneSwitchActions == null && !string.IsNullOrWhiteSpace(json))
_sceneSwitchActions = JsonSerializer.Deserialize<SceneSwitchList>(json)?.SceneSwitches;
return _sceneSwitchActions;
}
set
{
if (value == null) return;
var switchList = new SceneSwitchList() { SceneSwitches = value };
Properties.Settings.Default.SceneSwitchJSON = JsonSerializer.Serialize(switchList);
Properties.Settings.Default.Save();
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler? PropertyChanged;
#pragma warning disable CS8601
#pragma warning disable CS8602
public MainWindow()
{
if (!(Application.Current is App)) Application.Current.Shutdown();
_app = Application.Current as App;
if (_app.HueApiService == null) _app.Shutdown();
_apiService = _app.HueApiService;
InitializeComponent();
DataContext = this;
if (_apiService.HasReceivedData)
ConnectionSuccessState();
switch(MiredShift.GetMiredShiftType())
{
case MiredShift.MiredShiftType.QuiteABitWarmer: QuiteWarmerSetting.IsSelected = true; break;
case MiredShift.MiredShiftType.SlightlyWarmer: SlightlyWarmerSetting.IsSelected = true; break;
case MiredShift.MiredShiftType.MatchScreen: MatchScreenSetting.IsSelected = true; break;
}
var hasAppKey = !string.IsNullOrWhiteSpace(Properties.Settings.Default.AppKey);
var hasBridgeIP = !string.IsNullOrWhiteSpace(Properties.Settings.Default.BridgeIP);
NeedAccess.Visibility = hasAppKey ? Visibility.Collapsed : Visibility.Visible;
HasAccess.Visibility = hasAppKey ? Visibility.Visible : Visibility.Collapsed;
if (hasAppKey) return;
Status = "Connect to a Philips Hue Bridge to continue";
StatusColor = _warningColor;
BridgeSearchText.Visibility = hasBridgeIP ? Visibility.Collapsed : Visibility.Visible;
BridgeAppKeyRetrival.Visibility = hasBridgeIP ? Visibility.Visible : Visibility.Collapsed;
if (hasBridgeIP) return;
Task.Run(() => FindHueBridge());
}
#pragma warning restore CS8601
#pragma warning restore CS8602
protected void OnPropertyChanged([CallerMemberName]string? propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
public async Task FindHueBridge()
{
// delay to make user feel like finding bridge takes more time than a fraction of a second, is all about that UX baby
await Task.Delay(1500);
var foundIP = await _apiService.DiscoverHueBridgeIP();
Dispatcher.Invoke(() =>
{
BridgeSearchText.Visibility = Visibility.Collapsed;
if (foundIP)
BridgeAppKeyRetrival.Visibility = Visibility.Visible;
else
BridgeSearchFailedText.Visibility = Visibility.Visible;
});
}
private void ButtonQuit_Click(object sender, RoutedEventArgs e)
{
_app.Shutdown();
}
private void ConnectionButton_Click(object sender, RoutedEventArgs e)
{
ConnectionButtonPanel.Visibility = Visibility.Collapsed;
BridgeLinkingText.Visibility = Visibility.Visible;
Task.Run(() => Connect());
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
DragMove();
}
private void ResetButton_Click(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Reset();
Properties.Settings.Default.Save();
_app.Shutdown();
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void QuitButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private async Task Connect()
{
var generatedAppKey = await _apiService.GenerateAppKey();
await Dispatcher.Invoke(async () =>
{
if (generatedAppKey)
{
NeedAccess.Visibility = Visibility.Collapsed;
HasAccess.Visibility = Visibility.Visible;
Status = "Waiting for data from f.lux";
StatusColor = _defaultColor;
await _app.EstablishConnection();
}
else
{
AppKeyRetrievalHeaderText.Content = "Link failed, try again!";
AppKeyRetrievalHeaderText.Foreground = _errorColor;
ConfigureFluxDisclaimerText.Visibility = Visibility.Collapsed;
BridgeLinkingText.Visibility = Visibility.Collapsed;
ConnectionButtonPanel.Visibility = Visibility.Visible;
}
});
}
private void MiredShiftSelection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!_hasColorChoiceInitialized) { _hasColorChoiceInitialized = true; return; }
if (e.AddedItems.Count == 0) return;
var addedItem = e.AddedItems[0] as ComboBoxItem;
if (addedItem == null) return;
SetAPIConsumingControlsEnabled(false);
Status = "Changing color...";
StatusColor = _defaultColor;
if (addedItem == QuiteWarmerSetting)
MiredShift.SetMiredShiftType(MiredShift.MiredShiftType.QuiteABitWarmer);
else if (addedItem == SlightlyWarmerSetting)
MiredShift.SetMiredShiftType(MiredShift.MiredShiftType.SlightlyWarmer);
else if (addedItem == MatchScreenSetting)
MiredShift.SetMiredShiftType(MiredShift.MiredShiftType.MatchScreen);
Task.Run(async () =>
{
await _apiService.ForceSceneUpdate();
Dispatcher.Invoke(() =>
{
Status = "Color changed!";
StatusColor = _successColor;
SetAPIConsumingControlsEnabled(true);
});
});
}
private void NameTextbox_TextChanged(object sender, TextChangedEventArgs e)
{
if (!_hasNameTextInitialized) { _hasNameTextInitialized = true; return; }
var currName = Properties.Settings.Default.SceneName;
var originalTextBox = e.OriginalSource as TextBox;
var newTextBox = e.Source as TextBox;
if (newTextBox == null || originalTextBox == null) return;
_nameTextChangeTimer.Restart();
if (newTextBox.Text.Equals(currName) || _nameUpdateTaskRunning) return;
Status = "Waiting for user to finish typing...";
StatusColor = _defaultColor;
NameCheck.Visibility = Visibility.Collapsed;
NameSpinner.Visibility = Visibility.Visible;
_nameUpdateTaskRunning = true;
Task.Run(() => UpdateSceneName(currName));
}
private async Task UpdateSceneName(string oldName)
{
while (_nameTextChangeTimer.Elapsed.TotalSeconds < _nameTextCooldown)
{
var waitTimeSeconds = _nameTextCooldown - _nameTextChangeTimer.Elapsed.TotalSeconds;
await Task.Delay((int)(waitTimeSeconds * 1000));
}
var updatedName = false;
await Dispatcher.Invoke<Task>(async () =>
{
if (!NameTextBox.Text.Equals(oldName))
{
SetAPIConsumingControlsEnabled(false);
Status = "Applying new name to scenes...";
await _apiService.UpdateSceneNames(NameTextBox.Text);
updatedName = true;
}
else
{
Status = "No changes made to scene name";
StatusColor = _successColor;
}
});
_nameUpdateTaskRunning = false;
Dispatcher.Invoke(() =>
{
SetAPIConsumingControlsEnabled(true);
NameCheck.Visibility = Visibility.Visible;
NameSpinner.Visibility = Visibility.Collapsed;
if (updatedName)
{
Status = "Scene names updated!";
StatusColor = _successColor;
}
});
}
private void ApplySceneCheckbox_Check(object sender, RoutedEventArgs e)
{
var checkbox = sender as CheckBox;
if (checkbox == null || !(checkbox.DataContext is SceneSwitchAction)) return;
var action = checkbox.DataContext as SceneSwitchAction;
var switchActions = SceneSwitchActions;
if (action == null || switchActions == null || !(switchActions.Any(sw => sw.SceneId.Equals(action.SceneId)))) return;
switchActions.First(sw => sw.SceneId.Equals(action?.SceneId)).ShouldSwitch = action.ShouldSwitch;
var switchList = new SceneSwitchList() { SceneSwitches = switchActions };
Properties.Settings.Default.SceneSwitchJSON = JsonSerializer.Serialize(switchList);
Properties.Settings.Default.Save();
}
private void ApplyButton_Click(object sender, RoutedEventArgs e)
{
Status = "Applying scenes...";
StatusColor = _defaultColor;
SetAPIConsumingControlsEnabled(false);
Task.Run(async () =>
{
await _apiService.ApplyScenes();
Dispatcher.Invoke(() =>
{
Status = "Scenes applied!";
StatusColor = _successColor;
SetAPIConsumingControlsEnabled(true);
});
});
}
private void SetAPIConsumingControlsEnabled(bool enabled)
{
Keyboard.ClearFocus();
MiredShiftSelection.IsEnabled = enabled;
NameTextBox.IsEnabled = enabled;
ApplyButton.IsEnabled = enabled;
}
public void ConnectionSuccessState()
{
Status = "Receiving data from f.lux";
StatusColor = _successColor;
}
public void ConnectionErrorState()
{
Dispatcher.Invoke(() =>
{
StatusText.Visibility = Visibility.Collapsed;
NeedAccess.Visibility = Visibility.Collapsed;
HasAccess.Visibility = Visibility.Collapsed;
ConnectionFailed.Visibility = Visibility.Visible;
});
}
}
}