Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
HelloWRC committed Sep 30, 2024
1 parent 0ac68ce commit d102ad8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ public interface INotificationHostService : IHostedService, INotifyPropertyChang
/// <param name="id">提醒提供方 ID</param>
/// <param name="settings">要保存的设置</param>
void WriteNotificationProviderSettings<T>(Guid id, T settings);

internal void CancelAllNotifications();
}
2 changes: 1 addition & 1 deletion ClassIsland/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ private async void MenuItemDebugFitSize_OnClick(object sender, RoutedEventArgs e

private void MenuItemClearAllNotifications_OnClick(object sender, RoutedEventArgs e)
{
NotificationHostService.CurrentRequest?.CancellationTokenSource.Cancel();
NotificationHostService.CancelAllNotifications();
}

private void MenuItemNotificationSettings_OnClick(object sender, RoutedEventArgs e)
Expand Down
18 changes: 10 additions & 8 deletions ClassIsland/Services/NotificationHostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,6 @@ public TimeState CurrentState
public NotificationRequest GetRequest()
{
CurrentRequest = RequestQueue.Dequeue();
CurrentRequest.CancellationTokenSource.Token.Register(() =>
{
while (RequestQueue.Count > 0)
{
var r = RequestQueue.Dequeue();
r.CompletedTokenSource.Cancel();
}
});
return CurrentRequest;
}

Expand Down Expand Up @@ -238,6 +230,16 @@ public void WriteNotificationProviderSettings<T>(Guid id, T settings)
Settings.NotificationProvidersSettings[id.ToString()] = settings;
}

public void CancelAllNotifications()
{
CurrentRequest?.CancellationTokenSource.Cancel();
while (RequestQueue.Count > 0)
{
var r = RequestQueue.Dequeue();
r.CompletedTokenSource.Cancel();
}
}

public event PropertyChangedEventHandler? PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ private string FormatTeacher(Subject subject)
var name = subject.GetFirstName();
return string.IsNullOrWhiteSpace(name) ? string.Empty : $"由{name}老师任教";
}

private NotificationRequest? _onClassNotificationRequest;

private INotificationHostService NotificationHostService { get; }

Expand Down Expand Up @@ -88,14 +90,19 @@ private void UpdateTimerTick(object? sender, EventArgs e)
var settingsDeltaTime = LessonsService.NextClassSubject.IsOutDoor
? settingsOutDoorClassPreparingDeltaTime
: settingsInDoorClassPreparingDeltaTime;
if (settingsIsClassOnPreparingNotificationEnabled &&
tClassDelta > TimeSpan.Zero &&
tClassDelta <= TimeSpan.FromSeconds(settingsDeltaTime) &&
!IsClassPreparingNotified && LessonsService.CurrentState is TimeState.Breaking or TimeState.None)
if (!settingsIsClassOnPreparingNotificationEnabled ||
LessonsService.CurrentState is not (TimeState.Breaking or TimeState.None))
return;
if (tClassDelta > TimeSpan.Zero &&
tClassDelta <= TimeSpan.FromSeconds(settingsDeltaTime))
{
if (IsClassPreparingNotified)
{
return;
}
var deltaTime = TimeSpan.FromSeconds(settingsDeltaTime) - tClassDelta > TimeSpan.FromSeconds(10) ? tClassDelta : TimeSpan.FromSeconds(settingsDeltaTime);
IsClassPreparingNotified = true;
var onClassPrepareRequest = new NotificationRequest
var onClassPrepareRequest = _onClassNotificationRequest = new NotificationRequest
{
MaskSpeechContent = $"距上课还剩{TimeSpanFormatHelper.Format(deltaTime)}。",
MaskContent = new ClassNotificationProviderControl("ClassPrepareNotifyMask")
Expand All @@ -114,6 +121,15 @@ private void UpdateTimerTick(object? sender, EventArgs e)
};
NotificationHostService.ShowNotification(onClassPrepareRequest);
}
else
{
if (!IsClassPreparingNotified)
{
return;
}
_onClassNotificationRequest?.CancellationTokenSource.Cancel();
IsClassPreparingNotified = false;
}
}

private void OnBreakingTime(object? sender, EventArgs e)
Expand Down

0 comments on commit d102ad8

Please sign in to comment.