Skip to content

Commit be90f80

Browse files
author
Adam Pedley
committed
ConcurrentDictionary used and counter locked
1 parent a466278 commit be90f80

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

Toasts.Forms.Plugin.Abstractions/Options/NotificationOptions.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
namespace Plugin.Toasts
22
{
33
using System;
4-
using System.Collections.Generic;
4+
using System.Collections.Concurrent;
5+
using System.Collections.Generic;
56

67
public class NotificationOptions : INotificationOptions
78
{
@@ -17,7 +18,7 @@ public class NotificationOptions : INotificationOptions
1718

1819
public IiOSOptions iOSOptions { get; set; } = new iOSOptions();
1920

20-
public IDictionary<string, string> CustomArgs { get; set; } = new Dictionary<string, string>();
21+
public IDictionary<string, string> CustomArgs { get; set; } = new ConcurrentDictionary<string, string>();
2122

2223
public bool ClearFromHistory { get; set; } = false;
2324

Toasts.Forms.Plugin.Droid/NotificationBuilder.cs

+12-6
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
using System.Threading;
88
using System.Xml.Serialization;
99
using Plugin.Toasts.Interfaces;
10+
using System.Collections.Concurrent;
1011

1112
namespace Plugin.Toasts
1213
{
1314
class NotificationBuilder
1415
{
1516
public static string PackageName { get; set; }
1617

17-
public static IDictionary<string, ManualResetEvent> ResetEvent = new Dictionary<string, ManualResetEvent>();
18-
public static IDictionary<string, NotificationResult> EventResult = new Dictionary<string, NotificationResult>();
18+
public static IDictionary<string, ManualResetEvent> ResetEvent = new ConcurrentDictionary<string, ManualResetEvent>();
19+
public static IDictionary<string, NotificationResult> EventResult = new ConcurrentDictionary<string, NotificationResult>();
1920
public static List<string> Channels = new List<string>();
2021

2122
public const string NotificationId = "NOTIFICATION_ID";
@@ -154,10 +155,15 @@ public INotificationResult Notify(Activity activity, INotificationOptions option
154155
INotificationResult notificationResult = null;
155156
if (options != null)
156157
{
157-
var notificationId = _count;
158-
var id = _count.ToString();
159-
_count++;
160-
158+
var notificationId = 0;
159+
var id = "";
160+
lock (_lock)
161+
{
162+
notificationId = _count;
163+
id = _count.ToString();
164+
_count++;
165+
}
166+
161167
int smallIcon;
162168

163169
if (options.AndroidOptions.SmallDrawableIcon.HasValue)

Toasts.Forms.Plugin.Droid/SnackbarNotification.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ namespace Plugin.Toasts
55
using Android.Text;
66
using Android.Views;
77
using System;
8-
using System.Collections.Generic;
8+
using System.Collections.Concurrent;
9+
using System.Collections.Generic;
910
using System.Threading;
1011

1112
internal class SnackbarNotification
1213
{
13-
private IDictionary<string, ManualResetEvent> _resetEvents = new Dictionary<string, ManualResetEvent>();
14-
private IDictionary<string, NotificationResult> _eventResult = new Dictionary<string, NotificationResult>();
14+
private IDictionary<string, ManualResetEvent> _resetEvents = new ConcurrentDictionary<string, ManualResetEvent>();
15+
private IDictionary<string, NotificationResult> _eventResult = new ConcurrentDictionary<string, NotificationResult>();
1516
private IList<Snackbar> _snackBars = new List<Snackbar>();
1617

1718
private int _count = 0;

Toasts.Forms.Plugin.UWP/ToastNotification.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
using System;
99
using Windows.ApplicationModel.Activation;
1010
using System.Xml;
11+
using System.Collections.Concurrent;
1112

12-
public class ToastNotification : IToastNotificator
13+
public class ToastNotification : IToastNotificator
1314
{
1415

15-
private IDictionary<string, ManualResetEvent> _resetEvents = new Dictionary<string, ManualResetEvent>();
16-
private IDictionary<string, NotificationResult> _eventResult = new Dictionary<string, NotificationResult>();
17-
private IDictionary<string, INotificationOptions> _notificationOptions = new Dictionary<string, INotificationOptions>();
16+
private IDictionary<string, ManualResetEvent> _resetEvents = new ConcurrentDictionary<string, ManualResetEvent>();
17+
private IDictionary<string, NotificationResult> _eventResult = new ConcurrentDictionary<string, NotificationResult>();
18+
private IDictionary<string, INotificationOptions> _notificationOptions = new ConcurrentDictionary<string, INotificationOptions>();
1819
private int _count = 0;
1920

2021
public static void Init() { }

Toasts.Forms.Plugin.iOS/UNNotificationManager.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
using Extensions;
44
using Foundation;
55
using System;
6-
using System.Collections.Generic;
6+
using System.Collections.Concurrent;
7+
using System.Collections.Generic;
78
using System.Threading;
89
using UserNotifications;
910

1011
public class UNNotificationManager
1112
{
12-
private IDictionary<string, ManualResetEvent> _resetEvents = new Dictionary<string, ManualResetEvent>();
13-
private IDictionary<string, NotificationResult> _eventResult = new Dictionary<string, NotificationResult>();
13+
private IDictionary<string, ManualResetEvent> _resetEvents = new ConcurrentDictionary<string, ManualResetEvent>();
14+
private IDictionary<string, NotificationResult> _eventResult = new ConcurrentDictionary<string, NotificationResult>();
1415
private int _count = 0;
1516
private static object _lock = new object();
1617

0 commit comments

Comments
 (0)