@@ -20,6 +20,7 @@ internal class Settings : ISettings
20
20
{
21
21
private static readonly string PrimaryPackageName = Regex . Replace ( AppInfo . PackageName , @"\.([^.]*)ServiceExtension$" , string . Empty ) ;
22
22
private static readonly string SharedName = $ "group.{ PrimaryPackageName } .notifo";
23
+ private static readonly string SeenNotificationsKey = "SeenNotifications" ;
23
24
24
25
private static readonly SemaphoreSlim Semaphore = new SemaphoreSlim ( 1 , 1 ) ;
25
26
@@ -35,60 +36,40 @@ public bool IsTokenRefreshed
35
36
set => Preferences . Set ( nameof ( IsTokenRefreshed ) , value , SharedName ) ;
36
37
}
37
38
38
- private SlidingSet < Guid > seenNotifications ;
39
- private SlidingSet < Guid > SeenNotifications
39
+ public SlidingSet < Guid > GetSeenNotifications ( )
40
40
{
41
- get
42
- {
43
- if ( seenNotifications == null )
44
- {
45
- seenNotifications = new SlidingSet < Guid > ( capacity : 500 ) ;
41
+ var seenNotifications = new SlidingSet < Guid > ( capacity : 500 ) ;
46
42
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 ) )
57
45
{
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 ;
62
47
}
63
- }
64
48
65
- public bool IsNotificationSeen ( Guid id ) => SeenNotifications . Contains ( id ) ;
49
+ return seenNotifications ;
50
+ }
66
51
67
- public async Task TrackNotificationAsync ( Guid id )
52
+ private void SetSeenNotifications ( SlidingSet < Guid > seenNotifications )
68
53
{
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 ) ;
79
56
}
80
57
58
+ public Task TrackNotificationAsync ( Guid id ) => TrackNotificationsAsync ( new Guid [ ] { id } ) ;
59
+
81
60
public async Task TrackNotificationsAsync ( IEnumerable < Guid > ids )
82
61
{
83
62
await Semaphore . WaitAsync ( ) ;
84
63
try
85
64
{
65
+ var seenNotifications = GetSeenNotifications ( ) ;
66
+
86
67
foreach ( var id in ids )
87
68
{
88
- SeenNotifications . Add ( id ) ;
69
+ seenNotifications . Add ( id ) ;
89
70
}
90
71
91
- await Task . Run ( ( ) => SeenNotifications = SeenNotifications ) ;
72
+ await Task . Run ( ( ) => SetSeenNotifications ( seenNotifications ) ) ;
92
73
}
93
74
finally
94
75
{
0 commit comments