-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
301 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/SyncClipboard.Abstract/Notification/NotificationSessionBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
namespace SyncClipboard.Abstract.Notification; | ||
|
||
public abstract class NotificationSessionBase<NotificationIdType> : INotificationSession where NotificationIdType : notnull | ||
{ | ||
public abstract string Title { get; set; } | ||
public TimeSpan? Duration { get; set; } | ||
public Uri? Image { get; set; } | ||
public List<Button> Buttons { get; set; } = new(); | ||
|
||
protected abstract NotificationIdType? NativeNotificationId { get; } | ||
protected abstract void NativeRemove(); | ||
protected abstract void NativeShow(); | ||
protected abstract void NativeShowSilent(); | ||
|
||
private CancellationTokenSource? _durationCts; | ||
private readonly CallbackHandler<NotificationIdType> _callbackHandler; | ||
|
||
protected NotificationSessionBase(CallbackHandler<NotificationIdType> callbackHandler) | ||
{ | ||
_callbackHandler = callbackHandler; | ||
} | ||
|
||
public void CancelDurationTask() | ||
{ | ||
var oldCts = Interlocked.Exchange(ref _durationCts, null); | ||
oldCts?.Cancel(); | ||
oldCts?.Dispose(); | ||
} | ||
|
||
private CancellationToken CreateNewDurationCtk() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
var oldCts = Interlocked.Exchange(ref _durationCts, null); | ||
oldCts?.Cancel(); | ||
oldCts?.Dispose(); | ||
return cts.Token; | ||
} | ||
|
||
private async void SetNoficifationDuration() | ||
{ | ||
if (Duration is not null) | ||
{ | ||
var token = CreateNewDurationCtk(); | ||
try | ||
{ | ||
await Task.Delay(Duration.Value, token).ConfigureAwait(false); | ||
NativeRemoveAndClearCallbackHandler(); | ||
} | ||
catch { } | ||
} | ||
} | ||
|
||
public virtual void Show() | ||
{ | ||
NativeShow(); | ||
SetNoficifationDuration(); | ||
} | ||
|
||
public virtual void ShowSilent() | ||
{ | ||
NativeShowSilent(); | ||
SetNoficifationDuration(); | ||
} | ||
|
||
private void NativeRemoveAndClearCallbackHandler() | ||
{ | ||
NativeRemove(); | ||
if (NativeNotificationId is not null) | ||
_callbackHandler.OnClosed(NativeNotificationId); | ||
} | ||
|
||
public virtual void Remove() | ||
{ | ||
try | ||
{ | ||
NativeRemoveAndClearCallbackHandler(); | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.