Implement error handling and notification

This commit is contained in:
Alexander Horner
2022-12-07 23:20:36 +00:00
parent 1e1187e71f
commit ecbde9509f
3 changed files with 188 additions and 68 deletions

View File

@@ -19,10 +19,29 @@ namespace ntfysh_client
{
_notificationListener = notificationListener;
_notificationListener.OnNotificationReceive += OnNotificationReceive;
_notificationListener.OnConnectionMultiAttemptFailure += OnConnectionMultiAttemptFailure;
_notificationListener.OnConnectionCredentialsFailure += OnConnectionCredentialsFailure;
InitializeComponent();
}
private void OnNotificationReceive(object sender, NotificationReceiveEventArgs e)
{
notifyIcon.ShowBalloonTip(3000, e.Title, e.Message, ToolTipIcon.Info);
}
private void OnConnectionMultiAttemptFailure(NotificationListener sender, SubscribedTopic topic)
{
MessageBox.Show($"Connecting to topic ID '{topic.TopicId}' on server '{topic.ServerUrl}' failed after multiple attempts.\n\nThis topic ID will be ignored and you will not receive notifications for it until you restart the application.", "Connection Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void OnConnectionCredentialsFailure(NotificationListener sender, SubscribedTopic topic)
{
string reason = string.IsNullOrWhiteSpace(topic.Username) ? "credentials are required but were not provided" : "the entered credentials are incorrect";
MessageBox.Show($"Connecting to topic ID '{topic.TopicId}' on server '{topic.ServerUrl}' failed because {reason}.\n\nThis topic ID will be ignored and you will not receive notifications for it until you correct the credentials.", "Connection Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void MainForm_Load(object sender, EventArgs e) => LoadTopics();
private void subscribeNewTopic_Click(object sender, EventArgs e)
@@ -137,7 +156,7 @@ namespace ntfysh_client
}
//Assemble new format
List<SubscribedTopic> newTopics = legacyTopics.Select(lt => new SubscribedTopic(lt, "https://ntfy.sh", null, null, null, null)).ToList();
List<SubscribedTopic> newTopics = legacyTopics.Select(lt => new SubscribedTopic(lt, "https://ntfy.sh", null, null)).ToList();
string newFormatSerialised = JsonConvert.SerializeObject(newTopics, Formatting.Indented);
@@ -196,11 +215,6 @@ namespace ntfysh_client
}
}
private void OnNotificationReceive(object sender, NotificationReceiveEventArgs e)
{
notifyIcon.ShowBalloonTip(3000, e.Title, e.Message, ToolTipIcon.Info);
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
notifyIcon.Dispose();