Skip to content

Commit 6974d3e

Browse files
committed
Fix intellisense messages
1 parent 1a85679 commit 6974d3e

File tree

4 files changed

+65
-53
lines changed

4 files changed

+65
-53
lines changed

src/Bible.Alarm/Platforms/Windows/Services/UI/WindowsNotificationService.cs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Bible.Alarm.Platforms.Windows.Services.UI
1313
{
14-
public class WindowsNotificationService(WindowsAlarmHandler windowsAlarmHandler) : INotificationService
14+
public sealed partial class WindowsNotificationService(WindowsAlarmHandler windowsAlarmHandler) : INotificationService
1515
{
1616
private readonly WindowsAlarmHandler _windowsAlarmHandler = windowsAlarmHandler;
1717

@@ -68,10 +68,10 @@ public Task RemoveAsync(int scheduleId)
6868
try
6969
{
7070
var notifier = GetToastNotifier();
71-
if (notifier == null) return Task.CompletedTask;
71+
if (notifier is null) return Task.CompletedTask;
7272

7373
var toRemove = FindScheduledToast(notifier, scheduleId);
74-
if (toRemove != null)
74+
if (toRemove is not null)
7575
{
7676
notifier.RemoveFromSchedule(toRemove);
7777
}
@@ -89,7 +89,7 @@ public Task<bool> IsScheduledAsync(int scheduleId)
8989
try
9090
{
9191
var notifier = GetToastNotifier();
92-
if (notifier == null) return Task.FromResult(false);
92+
if (notifier is null) return Task.FromResult(false);
9393

9494
return Task.FromResult(IsNotificationScheduled(notifier, scheduleId));
9595
}
@@ -147,22 +147,38 @@ private static XmlDocument CreateToastXml(string title, string body, int schedul
147147
private static bool IsNotificationScheduled(ToastNotifier notifier, int scheduleId)
148148
{
149149
var scheduledToasts = notifier.GetScheduledToastNotifications();
150-
return scheduledToasts.Any(t => t.Id == scheduleId.ToString());
150+
var scheduleIdString = scheduleId.ToString();
151+
foreach (var toast in scheduledToasts)
152+
{
153+
if (toast.Id == scheduleIdString)
154+
{
155+
return true;
156+
}
157+
}
158+
return false;
151159
}
152160

153161
private static ScheduledToastNotification? FindScheduledToast(ToastNotifier notifier, int scheduleId)
154162
{
155163
var scheduledToasts = notifier.GetScheduledToastNotifications();
156-
return scheduledToasts.FirstOrDefault(t => t.Id == scheduleId.ToString());
164+
var scheduleIdString = scheduleId.ToString();
165+
foreach (var toast in scheduledToasts)
166+
{
167+
if (toast.Id == scheduleIdString)
168+
{
169+
return toast;
170+
}
171+
}
172+
return null;
157173
}
158174

159175
private static ToastNotifier? GetToastNotifier()
160176
{
161177
var notifier = TryCreateNotifierWithoutParameters();
162-
if (notifier != null) return notifier;
178+
if (notifier is not null) return notifier;
163179

164180
notifier = TryCreateNotifierWithAumid();
165-
if (notifier != null) return notifier;
181+
if (notifier is not null) return notifier;
166182

167183
Serilog.Log.Error(
168184
"Unable to create toast notifier. Scheduled notifications will not work. " +
@@ -177,7 +193,7 @@ private static bool IsNotificationScheduled(ToastNotifier notifier, int schedule
177193
{
178194
Serilog.Log.Debug("Attempting to create toast notifier without parameters...");
179195
var notifier = ToastNotificationManager.CreateToastNotifier();
180-
if (notifier != null)
196+
if (notifier is not null)
181197
{
182198
Serilog.Log.Debug("Successfully created toast notifier without parameters");
183199
return notifier;
@@ -202,17 +218,17 @@ private static bool IsNotificationScheduled(ToastNotifier notifier, int schedule
202218
var package = Package.Current;
203219
var packageId = package.Id;
204220

205-
var aumidFormats = new[]
206-
{
221+
string[] aumidFormats =
222+
[
207223
$"{packageId.FamilyName}!App",
208224
packageId.FamilyName,
209225
packageId.Name,
210-
};
226+
];
211227

212228
foreach (var aumid in aumidFormats)
213229
{
214230
var notifier = TryCreateNotifierWithAumid(aumid);
215-
if (notifier != null) return notifier;
231+
if (notifier is not null) return notifier;
216232
}
217233

218234
Serilog.Log.Warning(
@@ -239,7 +255,7 @@ private static bool IsNotificationScheduled(ToastNotifier notifier, int schedule
239255
{
240256
Serilog.Log.Debug("Trying to create toast notifier with AUMID: {AUMID}", aumid);
241257
var notifier = ToastNotificationManager.CreateToastNotifier(aumid);
242-
if (notifier != null)
258+
if (notifier is not null)
243259
{
244260
Serilog.Log.Information("Successfully created toast notifier with AUMID: {AUMID}", aumid);
245261
return notifier;
@@ -254,6 +270,7 @@ private static bool IsNotificationScheduled(ToastNotifier notifier, int schedule
254270

255271
public void Dispose()
256272
{
273+
GC.SuppressFinalize(this);
257274
}
258275
}
259276
}

src/Bible.Alarm/Platforms/Windows/Services/UI/WindowsToastService.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,26 @@
1313

1414
namespace Bible.Alarm.Platforms.Windows.Services.UI
1515
{
16-
public class WindowsToastService : ToastService
16+
public sealed partial class WindowsToastService(TaskScheduler taskScheduler) : ToastService
1717
{
18-
private static readonly SemaphoreSlim Lock = new SemaphoreSlim(1);
18+
private static readonly SemaphoreSlim Lock = new(1);
1919

2020
private static TaskCompletionSource<bool>? clearRequest;
21-
private readonly TaskScheduler _taskScheduler;
22-
23-
public WindowsToastService(TaskScheduler taskScheduler)
24-
{
25-
_taskScheduler = taskScheduler;
26-
}
21+
private readonly TaskScheduler _taskScheduler = taskScheduler;
2722

2823
public override Task Clear()
2924
{
30-
if (clearRequest != null)
25+
if (clearRequest is { } request)
3126
{
32-
clearRequest.SetResult(true);
27+
request.SetResult(true);
3328
}
3429

3530
return Task.CompletedTask;
3631
}
3732

3833
public override async Task ShowMessage(string message, int seconds)
3934
{
40-
if (clearRequest != null)
35+
if (clearRequest is not null)
4136
{
4237
return;
4338
}
@@ -62,13 +57,13 @@ private static async Task ShowAlert(string message, double seconds)
6257
try
6358
{
6459
var currentWindow = GetNativeWindow();
65-
if (currentWindow == null)
60+
if (currentWindow is null)
6661
{
6762
return;
6863
}
6964

7065
var targetElement = FindTargetElement(currentWindow);
71-
if (targetElement == null)
66+
if (targetElement is null)
7267
{
7368
return;
7469
}
@@ -89,11 +84,12 @@ private static async Task ShowAlert(string message, double seconds)
8984
var currentWindow = Window.Current;
9085

9186
// If Window.Current is null, try to get it from MAUI Application
92-
if (currentWindow == null)
87+
if (currentWindow is null)
9388
{
94-
var mauiWindow = Microsoft.Maui.Controls.Application.Current?.Windows?.FirstOrDefault();
95-
if (mauiWindow != null)
89+
var windows = Microsoft.Maui.Controls.Application.Current?.Windows;
90+
if (windows is not null && windows.Count > 0)
9691
{
92+
var mauiWindow = windows[0];
9793
var handler = mauiWindow.Handler;
9894
if (handler?.PlatformView is Window nativeWindow)
9995
{
@@ -131,7 +127,7 @@ private static async Task ShowAlert(string message, double seconds)
131127
}
132128

133129
var parent = VisualTreeHelper.GetParent(depObj);
134-
while (parent != null)
130+
while (parent is not null)
135131
{
136132
if (parent is FrameworkElement frameworkElement)
137133
{
@@ -163,9 +159,9 @@ private static async Task ShowFlyoutAsync(Flyout flyout, FrameworkElement target
163159
flyout.OverlayInputPassThroughElement = targetElement;
164160
flyout.ShowAt(targetElement);
165161

166-
if (clearRequest != null)
162+
if (clearRequest is { } request)
167163
{
168-
await Task.WhenAny(clearRequest.Task, Task.Delay((int)(seconds * 1000))).ConfigureAwait(true);
164+
await Task.WhenAny(request.Task, Task.Delay((int)(seconds * 1000))).ConfigureAwait(true);
169165
}
170166
else
171167
{

src/Bible.Alarm/Services/Media/PlaybackService.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class PlaybackService : IPlaybackService
2121
private int _currentTrackIndex = -1;
2222
private int? _currentScheduleId;
2323
private bool _isAlarm;
24-
private System.Timers.Timer? _progressSaveTimer;
25-
private HashSet<int> _manuallyVisitedTrackIndices = new();
24+
private readonly System.Timers.Timer? _progressSaveTimer;
25+
private readonly HashSet<int> _manuallyVisitedTrackIndices = [];
2626

2727
public int? CurrentScheduleId => _currentScheduleId;
2828

@@ -32,12 +32,12 @@ public class PlaybackService : IPlaybackService
3232
_audioPlayer.Status == PlayStatus.Paused;
3333

3434
public bool CanPlayNext =>
35-
_playlist != null &&
35+
_playlist is not null &&
3636
_currentTrackIndex >= 0 &&
3737
_currentTrackIndex < _playlist.Count - 1;
3838

3939
public bool CanPlayPrevious =>
40-
_playlist != null &&
40+
_playlist is not null &&
4141
_currentTrackIndex > 0;
4242

4343
public PlaybackService(
@@ -56,7 +56,7 @@ public PlaybackService(
5656
_audioPlayer.MediaEnded += OnMediaEnded;
5757
_audioPlayer.MediaFailed += OnMediaFailed;
5858

59-
_progressSaveTimer = new System.Timers.Timer(1000);
59+
_progressSaveTimer = new(1000);
6060
_progressSaveTimer.Elapsed += OnProgressSaveTimerElapsed;
6161
_progressSaveTimer.AutoReset = true;
6262
}
@@ -77,7 +77,7 @@ public async Task PrepareAndPlayAsync(int scheduleId, bool isAlarm)
7777
_isAlarm = isAlarm;
7878
_playlist = await _preparePlaybackService.PrepareTracksAsync(scheduleId);
7979

80-
if (_playlist == null)
80+
if (_playlist is null)
8181
{
8282
_logger.Warning($"Failed to prepare tracks for schedule {scheduleId}");
8383
await HandlePlaybackFailureAsync();
@@ -106,7 +106,7 @@ public async Task PrepareAndPlayAsync(int scheduleId, bool isAlarm)
106106

107107
public async Task PlayAsync()
108108
{
109-
if (_currentTrackIndex < 0 || _playlist == null || _currentTrackIndex >= _playlist.Count)
109+
if (_currentTrackIndex < 0 || _playlist is null || _currentTrackIndex >= _playlist.Count)
110110
return;
111111

112112
if (_audioPlayer.Status == PlayStatus.Paused)
@@ -136,7 +136,7 @@ public async Task PauseAsync()
136136

137137
public async Task PlayNextAsync()
138138
{
139-
if (_playlist == null || _playlist.Count == 0)
139+
if (_playlist is null || _playlist.Count == 0)
140140
return;
141141

142142
if (_currentTrackIndex < _playlist.Count - 1)
@@ -158,7 +158,7 @@ public async Task PlayNextAsync()
158158

159159
public async Task PlayPreviousAsync()
160160
{
161-
if (_playlist == null || _playlist.Count == 0)
161+
if (_playlist is null || _playlist.Count == 0)
162162
return;
163163

164164
if (_currentTrackIndex > 0)
@@ -310,7 +310,7 @@ private async void OnMediaEnded(object? sender, EventArgs e)
310310
_progressSaveTimer?.Stop();
311311
await MarkCurrentTrackAsFinishedAsync();
312312

313-
if (_playlist != null && _currentTrackIndex < _playlist.Count - 1)
313+
if (_playlist is not null && _currentTrackIndex < _playlist.Count - 1)
314314
{
315315
_currentTrackIndex++;
316316
await PlayCurrentTrackAsync();
@@ -333,7 +333,7 @@ private async void OnMediaFailed(object? sender, EventArgs e)
333333
{
334334
_logger.Warning($"Media failed for track at index {_currentTrackIndex}");
335335

336-
if (_playlist != null && _currentTrackIndex < _playlist.Count - 1)
336+
if (_playlist is not null && _currentTrackIndex < _playlist.Count - 1)
337337
{
338338
_currentTrackIndex++;
339339
await PlayCurrentTrackAsync();
@@ -453,7 +453,7 @@ private async Task PlayFallbackAlarmSoundAsync()
453453
try
454454
{
455455
var fallbackTrack = await _fallbackAlarmSoundService.GetFallbackAlarmTrackAsync();
456-
if (fallbackTrack == null)
456+
if (fallbackTrack is null)
457457
{
458458
_logger.Error("Failed to get fallback alarm track");
459459
WeakReferenceMessenger.Default.Send(new HideAlarmModalMessage());
@@ -462,7 +462,7 @@ private async Task PlayFallbackAlarmSoundAsync()
462462
return;
463463
}
464464

465-
_playlist = new List<AudioPlayerTrack> { fallbackTrack };
465+
_playlist = [fallbackTrack];
466466
_currentTrackIndex = 0;
467467
await PlayCurrentTrackAsync();
468468
}
@@ -477,11 +477,11 @@ private async Task PlayFallbackAlarmSoundAsync()
477477

478478
public void Dispose()
479479
{
480-
if (_progressSaveTimer != null)
480+
if (_progressSaveTimer is { } timer)
481481
{
482-
_progressSaveTimer.Elapsed -= OnProgressSaveTimerElapsed;
483-
_progressSaveTimer.Stop();
484-
_progressSaveTimer.Dispose();
482+
timer.Elapsed -= OnProgressSaveTimerElapsed;
483+
timer.Stop();
484+
timer.Dispose();
485485
}
486486
_audioPlayer.Dispose();
487487
}

src/Bible.Alarm/Services/Scheduler/AlarmService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Bible.Alarm.Services.Scheduler;
77

8-
public class AlarmService(
8+
public sealed partial class AlarmService(
99
INotificationService notificationService)
1010
: IAlarmService
1111
{
@@ -41,7 +41,6 @@ private void RemoveNotification(int scheduleId)
4141

4242
public void Dispose()
4343
{
44-
// Note: notificationService is a singleton
45-
// and should not be disposed here as it is managed by the DI container
44+
GC.SuppressFinalize(this);
4645
}
4746
}

0 commit comments

Comments
 (0)