Skip to content

Commit

Permalink
手动上传后可以发送通知
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed Jun 16, 2024
1 parent 40a03d1 commit dfc529f
Show file tree
Hide file tree
Showing 28 changed files with 301 additions and 111 deletions.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
v2.8.2.1
- 修复:不设置hash上传图片/文件到服务器后,桌面客户端会无限重复设置剪贴板
- 修复:远程文件不存在时,只报错一次,不再无限弹窗、图标错乱(#87)
- 功能:手动上传后可以发送通知(#82)

v2.8.2
- 修复:服务端产生大量图片文件
Expand Down
15 changes: 12 additions & 3 deletions src/SyncClipboard.Abstract/Notification/CallbackHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace SyncClipboard.Abstract.Notification;
public class CallbackHandler<NotificationIdType> where NotificationIdType : notnull
{
private readonly Dictionary<NotificationIdType, Dictionary<string, Button>> _handlerList = new();
private readonly Dictionary<NotificationIdType, NotificationSessionBase<NotificationIdType>> _sessionList = new();

public void OnActivated(NotificationIdType id, string buttonId)
{
Expand All @@ -13,15 +14,23 @@ public void OnActivated(NotificationIdType id, string buttonId)
button?.Invoke();
_handlerList.Remove(id);
}

_sessionList.Remove(id);
}

public void OnClosed(NotificationIdType id)
{
_handlerList.Remove(id);
_sessionList.Remove(id);
}

public void AddButton(NotificationIdType id, Button button)
public void AddButton(NotificationIdType id, Button button, NotificationSessionBase<NotificationIdType> session)
{
if (!_sessionList.ContainsKey(id))
{
_sessionList.Add(id, session);
}

if (!_handlerList.ContainsKey(id))
{
_handlerList.Add(id, new());
Expand All @@ -30,11 +39,11 @@ public void AddButton(NotificationIdType id, Button button)
buttonList.Add(button.Uid.ToString(), button);
}

public void AddButtons(NotificationIdType id, Button[] buttons)
public void AddButtons(NotificationIdType id, IEnumerable<Button> buttons, NotificationSessionBase<NotificationIdType> session)
{
foreach (var item in buttons)
{
AddButton(id, item);
AddButton(id, item, session);
}
}
}
2 changes: 1 addition & 1 deletion src/SyncClipboard.Abstract/Notification/INotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public interface INotification
{
public void SendText(string title, string text, params Button[] buttons);
public void SendImage(string title, string text, Uri uri, params Button[] buttons);
public void Send(NotificationPara para);
public void SendTemporary(NotificationPara para);
public IProgressBar CreateProgressNotification(string title);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public interface INotificationSession
{
public string Title { get; set; }
public Uri? Image { get; set; }
public List<Button> Buttons { get; set; }
}
82 changes: 82 additions & 0 deletions src/SyncClipboard.Abstract/Notification/NotificationSessionBase.cs
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
{
}
}
}
5 changes: 5 additions & 0 deletions src/SyncClipboard.Core/Clipboard/Profile/FileProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public override string ToolTip()
return StatusTip;
}

public override string ShowcaseText()
{
return FileName;
}

protected override bool Same(Profile rhs)
{
try
Expand Down
14 changes: 13 additions & 1 deletion src/SyncClipboard.Core/Clipboard/Profile/GroupProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,23 @@ protected override void SetNotification(INotification notification)
ArgumentNullException.ThrowIfNull(FullPath);
notification.SendText(
I18n.Strings.ClipboardFileUpdated,
string.Join("\n", _files.Select(file => Path.GetFileName(file))),
ShowcaseText(),
DefaultButton()
#if WINDOWS
, new Button(I18n.Strings.OpenFolder, () => Sys.OpenFolderInExplorer(FullPath[..^4] + "\\"))
#endif
);
}

public override string ShowcaseText()
{
if (_files is null)
return string.Empty;

if (_files.Length > 5)
{
return string.Join("\n", _files.Take(5).Select(file => Path.GetFileName(file))) + "\n...";
}
return string.Join("\n", _files.Select(file => Path.GetFileName(file)));
}
}
1 change: 1 addition & 0 deletions src/SyncClipboard.Core/Clipboard/Profile/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public abstract class Profile

public abstract ProfileType Type { get; }
public abstract string ToolTip();
public abstract string ShowcaseText();
public abstract Task UploadProfile(IWebDav webdav, CancellationToken cancelToken);

protected abstract IClipboardSetter<Profile> ClipboardSetter { get; }
Expand Down
9 changes: 9 additions & 0 deletions src/SyncClipboard.Core/Clipboard/Profile/TextProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public override string ToolTip()
return Text;
}

public override string ShowcaseText()
{
if (Text.Length > 500)
{
return Text[..500] + "\n...";
}
return Text;
}

protected override bool Same(Profile rhs)
{
try
Expand Down
5 changes: 5 additions & 0 deletions src/SyncClipboard.Core/Clipboard/Profile/UnknownProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ protected override ClipboardMetaInfomation CreateMetaInformation()
}

public override bool IsAvailableFromRemote() => false;

public override string ShowcaseText()
{
return "Do not support this type of clipboard";
}
}
18 changes: 18 additions & 0 deletions src/SyncClipboard.Core/I18n/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/SyncClipboard.Core/I18n/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,10 @@
<data name="CheckUpdateForBetaChannel" xml:space="preserve">
<value>Check Update for Beta Channel</value>
</data>
<data name="Uploaded" xml:space="preserve">
<value>Clipboard Uploaded</value>
</data>
<data name="SendNotificationAfterManuallyUpload" xml:space="preserve">
<value>Send notification after manually upload</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/SyncClipboard.Core/I18n/Strings.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,10 @@
<data name="CheckUpdateForBetaChannel" xml:space="preserve">
<value>更新到预览版</value>
</data>
<data name="Uploaded" xml:space="preserve">
<value>已上传剪贴板</value>
</data>
<data name="SendNotificationAfterManuallyUpload" xml:space="preserve">
<value>手动上传剪贴板后发送通知</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/SyncClipboard.Core/Models/UserConfigs/SyncConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public record class SyncConfig
public bool UseLocalServer { get; set; } = false;
public bool DeletePreviousFilesOnPush { get; set; } = true;
public bool NotifyOnDownloaded { get; set; } = false;
public bool NotifyOnManualUpload { get; set; } = false;
public bool TrustInsecureCertificate { get; set; } = false;
public uint MaxFileByte { get; set; } = 1024 * 1024 * 20; // 20MB
public uint IntervalTime { get; set; } = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void SwitchImageAssistant(bool isOn)
Duration = TimeSpan.FromSeconds(2),
Title = isOn ? I18n.Strings.SwitchOnImageAssistant : I18n.Strings.SwitchOffImageAssistant
};
_notificationManager.Send(para);
_notificationManager.SendTemporary(para);
}
#endregion Hotkey

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void SwitchClipboardSyncing(bool isOn)
Duration = TimeSpan.FromSeconds(2),
Title = isOn ? I18n.Strings.SwitchOnClipboardSyncing : I18n.Strings.SwitchOffClipboardSyncing,
};
_notificationManager.Send(para);
_notificationManager.SendTemporary(para);
}

private void SwitchBuiltInServer(bool isOn)
Expand All @@ -96,7 +96,7 @@ private void SwitchBuiltInServer(bool isOn)
Duration = TimeSpan.FromSeconds(2),
Title = isOn ? I18n.Strings.SwitchOnBuiltInServer : I18n.Strings.SwitchOffBuiltInServer
};
_notificationManager.Send(para);
_notificationManager.SendTemporary(para);
}

private void SwitchMixedClientMode(bool isOn)
Expand All @@ -107,7 +107,7 @@ private void SwitchMixedClientMode(bool isOn)
Duration = TimeSpan.FromSeconds(2),
Title = isOn ? I18n.Strings.SwitchOnMixedClientMode : I18n.Strings.SwitchOffMixedClientMode
};
_notificationManager.Send(para);
_notificationManager.SendTemporary(para);
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ protected override bool SwitchOn
}
}

private bool NotifyOnManualUpload => _syncConfig.NotifyOnManualUpload;

private bool _downServiceChangingLocal = false;
private Profile? _profileCache;

Expand Down Expand Up @@ -286,6 +288,8 @@ private async void QuickUpload()
var meta = await _clipboardFactory.GetMetaInfomation(token);
var profile = await _clipboardFactory.CreateProfileFromMeta(meta, token);
await HandleClipboard(meta, profile, token);
if (NotifyOnManualUpload)
_notificationManager.SendTemporary(new(I18n.Strings.Uploaded, profile.ShowcaseText()));
}
catch { }
}
Expand Down
6 changes: 6 additions & 0 deletions src/SyncClipboard.Core/ViewModels/SyncSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ partial void OnServerConfigChanged(ServerConfig value)
private bool notifyOnDownloaded;
partial void OnNotifyOnDownloadedChanged(bool value) => ClientConfig = ClientConfig with { NotifyOnDownloaded = value };

[ObservableProperty]
private bool notifyOnManualUpload;
partial void OnNotifyOnManualUploadChanged(bool value) => ClientConfig = ClientConfig with { NotifyOnManualUpload = value };

[ObservableProperty]
private bool trustInsecureCertificate;
partial void OnTrustInsecureCertificateChanged(bool value) => ClientConfig = ClientConfig with { TrustInsecureCertificate = value };
Expand All @@ -76,6 +80,7 @@ partial void OnClientConfigChanged(SyncConfig value)
MaxFileSize = value.MaxFileByte / 1024 / 1024;
AutoDeleleServerFile = value.DeletePreviousFilesOnPush;
NotifyOnDownloaded = value.NotifyOnDownloaded;
NotifyOnManualUpload = value.NotifyOnManualUpload;
TrustInsecureCertificate = value.TrustInsecureCertificate;
_configManager.SetConfig(value);
}
Expand Down Expand Up @@ -140,6 +145,7 @@ public SyncSettingViewModel(ConfigManager configManager, MainViewModel mainViewM
maxFileSize = clientConfig.MaxFileByte / 1024 / 1024;
autoDeleleServerFile = clientConfig.DeletePreviousFilesOnPush;
notifyOnDownloaded = clientConfig.NotifyOnDownloaded;
notifyOnManualUpload = clientConfig.NotifyOnManualUpload;
trustInsecureCertificate = clientConfig.TrustInsecureCertificate;
}

Expand Down
Loading

0 comments on commit dfc529f

Please sign in to comment.