Skip to content

Commit 9c384a0

Browse files
Update 'seen notification' storing mechanism.
1 parent 45b8d15 commit 9c384a0

File tree

3 files changed

+21
-38
lines changed

3 files changed

+21
-38
lines changed

sdk/Notifo.SDK/NotifoMobilePush/NotifoMobilePushImplementation.shared.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,10 @@ private async Task<ICollection<NotificationDto>> GetPendingNotificationsAsync()
261261
try
262262
{
263263
var allNotifications = await Notifications.GetNotificationsAsync();
264+
var seenNotifications = settings.GetSeenNotifications();
264265
var pendingNotifications = allNotifications
265266
.Items
266-
.Where(x => !settings.IsNotificationSeen(x.Id))
267+
.Where(x => !seenNotifications.Contains(x.Id))
267268
.OrderBy(x => x.Created)
268269
.ToArray();
269270

sdk/Notifo.SDK/Services/ISettings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Collections.Generic;
1010
using System.Threading.Tasks;
11+
using Notifo.SDK.Helpers;
1112

1213
namespace Notifo.SDK.Services
1314
{
@@ -21,6 +22,6 @@ internal interface ISettings
2122

2223
Task TrackNotificationsAsync(IEnumerable<Guid> ids);
2324

24-
bool IsNotificationSeen(Guid id);
25+
SlidingSet<Guid> GetSeenNotifications();
2526
}
2627
}

sdk/Notifo.SDK/Services/Settings.cs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal class Settings : ISettings
2020
{
2121
private static readonly string PrimaryPackageName = Regex.Replace(AppInfo.PackageName, @"\.([^.]*)ServiceExtension$", string.Empty);
2222
private static readonly string SharedName = $"group.{PrimaryPackageName}.notifo";
23+
private static readonly string SeenNotificationsKey = "SeenNotifications";
2324

2425
private static readonly SemaphoreSlim Semaphore = new SemaphoreSlim(1, 1);
2526

@@ -35,60 +36,40 @@ public bool IsTokenRefreshed
3536
set => Preferences.Set(nameof(IsTokenRefreshed), value, SharedName);
3637
}
3738

38-
private SlidingSet<Guid> seenNotifications;
39-
private SlidingSet<Guid> SeenNotifications
39+
public SlidingSet<Guid> GetSeenNotifications()
4040
{
41-
get
42-
{
43-
if (seenNotifications == null)
44-
{
45-
seenNotifications = new SlidingSet<Guid>(capacity: 500);
41+
var seenNotifications = new SlidingSet<Guid>(capacity: 500);
4642

47-
var serialized = Preferences.Get(nameof(SeenNotifications), string.Empty, SharedName);
48-
if (!string.IsNullOrWhiteSpace(serialized))
49-
{
50-
seenNotifications = JsonConvert.DeserializeObject<SlidingSet<Guid>>(serialized) ?? seenNotifications;
51-
}
52-
}
53-
54-
return seenNotifications;
55-
}
56-
set
43+
var serialized = Preferences.Get(SeenNotificationsKey, string.Empty, SharedName);
44+
if (!string.IsNullOrWhiteSpace(serialized))
5745
{
58-
seenNotifications = value;
59-
60-
var serialized = JsonConvert.SerializeObject(seenNotifications);
61-
Preferences.Set(nameof(SeenNotifications), serialized, SharedName);
46+
seenNotifications = JsonConvert.DeserializeObject<SlidingSet<Guid>>(serialized) ?? seenNotifications;
6247
}
63-
}
6448

65-
public bool IsNotificationSeen(Guid id) => SeenNotifications.Contains(id);
49+
return seenNotifications;
50+
}
6651

67-
public async Task TrackNotificationAsync(Guid id)
52+
private void SetSeenNotifications(SlidingSet<Guid> seenNotifications)
6853
{
69-
await Semaphore.WaitAsync();
70-
try
71-
{
72-
SeenNotifications.Add(id);
73-
await Task.Run(() => SeenNotifications = SeenNotifications);
74-
}
75-
finally
76-
{
77-
Semaphore.Release();
78-
}
54+
var serialized = JsonConvert.SerializeObject(seenNotifications);
55+
Preferences.Set(SeenNotificationsKey, serialized, SharedName);
7956
}
8057

58+
public Task TrackNotificationAsync(Guid id) => TrackNotificationsAsync(new Guid[] { id });
59+
8160
public async Task TrackNotificationsAsync(IEnumerable<Guid> ids)
8261
{
8362
await Semaphore.WaitAsync();
8463
try
8564
{
65+
var seenNotifications = GetSeenNotifications();
66+
8667
foreach (var id in ids)
8768
{
88-
SeenNotifications.Add(id);
69+
seenNotifications.Add(id);
8970
}
9071

91-
await Task.Run(() => SeenNotifications = SeenNotifications);
72+
await Task.Run(() => SetSeenNotifications(seenNotifications));
9273
}
9374
finally
9475
{

0 commit comments

Comments
 (0)