Skip to content

Commit 9135951

Browse files
Store settings.
1 parent 6daba64 commit 9135951

File tree

8 files changed

+82
-9
lines changed

8 files changed

+82
-9
lines changed

sdk/Notifo.SDK/CommandQueue/ICommandStore.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ internal interface ICommandStore
1818
ValueTask StoreAsync(QueuedCommand command);
1919

2020
ValueTask RemoveAsync(Guid id);
21+
22+
void Clear();
2123
}
2224
}

sdk/Notifo.SDK/INotifoMobilePush.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public partial interface INotifoMobilePush : INotifoClient
4141
/// </summary>
4242
bool IsConfigured { get; }
4343

44+
/// <summary>
45+
/// Clears all settings that are currently stored.
46+
/// </summary>
47+
void ClearAllSettings();
48+
4449
/// <summary>
4550
/// Sets the API key to use.
4651
/// </summary>

sdk/Notifo.SDK/NotifoIO.shared.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private static INotifoMobilePush SetupNotifoMobilePush()
4343
},
4444
10, TimeSpan.FromSeconds(5));
4545

46-
return new NotifoMobilePushImplementation(HttpClientFactory, settings, commandQueue);
46+
return new NotifoMobilePushImplementation(HttpClientFactory, settings, commandQueue, settings, settings);
4747
}
4848

4949
private static ILogger ConfigureLogger()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// ==========================================================================
2+
// Notifo.io
3+
// ==========================================================================
4+
// Copyright (c) Sebastian Stehle
5+
// All rights reserved. Licensed under the MIT license.
6+
// ==========================================================================
7+
8+
namespace Notifo.SDK.NotifoMobilePush
9+
{
10+
internal interface ICredentialsStore
11+
{
12+
string? ApiKey { get; set; }
13+
14+
string? ApiUrl { get; set; }
15+
16+
void Clear();
17+
}
18+
}

sdk/Notifo.SDK/NotifoMobilePush/ISeenNotificationsStore.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ internal interface ISeenNotificationsStore
1717
ValueTask AddSeenNotificationIdsAsync(int maxCapacity, IEnumerable<Guid> ids);
1818

1919
ValueTask<SlidingSet<Guid>> GetSeenNotificationIdsAsync();
20+
21+
void Clear();
2022
}
2123
}

sdk/Notifo.SDK/NotifoMobilePush/NotifoClientProvider.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ namespace Notifo.SDK.NotifoMobilePush
1212
{
1313
internal sealed class NotifoClientProvider
1414
{
15+
private const string CloudUrl = "https://app.notifo.io";
1516
private readonly Func<HttpClient> httpClientFactory;
17+
private readonly ICredentialsStore store;
1618
private readonly NotifoClientBuilder clientBuilder = NotifoClientBuilder.Create();
1719
private string? apiKey;
18-
private string apiUrl = "https://app.notifo.io";
20+
private string apiUrl;
1921
private INotifoClient? clientInstance;
2022

2123
public bool IsConfigured
@@ -32,8 +34,10 @@ public string? ApiKey
3234
{
3335
apiKey = value;
3436

35-
clientBuilder.SetApiKey(apiKey);
37+
clientBuilder.SetApiKey(value);
3638
clientInstance = null;
39+
40+
store.ApiKey = value;
3741
}
3842
}
3943
}
@@ -49,8 +53,10 @@ public string ApiUrl
4953
{
5054
apiUrl = value;
5155

52-
clientBuilder.SetApiUrl(apiUrl);
56+
clientBuilder.SetApiUrl(value);
5357
clientInstance = null;
58+
59+
store.ApiUrl = value;
5460
}
5561
}
5662
}
@@ -74,9 +80,14 @@ public HttpClient CreateHttpClient()
7480
return httpClientFactory();
7581
}
7682

77-
public NotifoClientProvider(Func<HttpClient> httpClientFactory)
83+
public NotifoClientProvider(Func<HttpClient> httpClientFactory, ICredentialsStore store)
7884
{
7985
this.httpClientFactory = httpClientFactory;
86+
87+
apiKey = store.ApiKey;
88+
apiUrl = store.ApiUrl ?? CloudUrl;
89+
90+
this.store = store;
8091
}
8192
}
8293
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ internal partial class NotifoMobilePushImplementation : INotifoMobilePush
1818
{
1919
private readonly ISeenNotificationsStore seenNotificationsStore;
2020
private readonly ICommandQueue commandQueue;
21+
private readonly ICommandStore commandStore;
22+
private readonly ICredentialsStore credentialsStore;
2123
private readonly NotifoClientProvider clientProvider;
2224
private IPushEventsProvider? pushEventsProvider;
2325
private string? token;
@@ -85,13 +87,19 @@ internal partial class NotifoMobilePushImplementation : INotifoMobilePush
8587
/// <inheritdoc/>
8688
public bool IsConfigured => clientProvider.IsConfigured;
8789

88-
public NotifoMobilePushImplementation(Func<HttpClient> httpClientFactory,
89-
ISeenNotificationsStore seenNotificationsStore, ICommandQueue commandQueue)
90+
public NotifoMobilePushImplementation(
91+
Func<HttpClient> httpClientFactory,
92+
ISeenNotificationsStore seenNotificationsStore,
93+
ICommandQueue commandQueue,
94+
ICommandStore commandStore,
95+
ICredentialsStore credentialsStore)
9096
{
9197
this.seenNotificationsStore = seenNotificationsStore;
9298
this.commandQueue = commandQueue;
9399
this.commandQueue.OnError += CommandQueue_OnError;
94-
this.clientProvider = new NotifoClientProvider(httpClientFactory);
100+
this.commandStore = commandStore;
101+
this.credentialsStore = credentialsStore;
102+
this.clientProvider = new NotifoClientProvider(httpClientFactory, credentialsStore);
95103

96104
SetupPlatform();
97105

@@ -103,6 +111,14 @@ public NotifoMobilePushImplementation(Func<HttpClient> httpClientFactory,
103111

104112
partial void SetupPlatform();
105113

114+
/// <inheritdoc/>
115+
public void ClearAllSettings()
116+
{
117+
commandStore.Clear();
118+
credentialsStore.Clear();
119+
seenNotificationsStore.Clear();
120+
}
121+
106122
/// <inheritdoc/>
107123
public INotifoMobilePush SetApiKey(string apiKey)
108124
{

sdk/Notifo.SDK/Services/Settings.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
namespace Notifo.SDK.Services
2020
{
21-
internal sealed class Settings : ISeenNotificationsStore, ICommandStore
21+
internal sealed class Settings : ISeenNotificationsStore, ICommandStore, ICredentialsStore
2222
{
2323
private const string KeyCommand = "CommandV2";
24+
private const string KeyApiKey = nameof(ApiKey);
25+
private const string KeyApiUrl = nameof(ApiUrl);
2426
private const string KeySeenNotifications = "SeenNotificationsV2";
2527
private static readonly string PrimaryPackageName = Regex.Replace(AppInfo.PackageName, @"\.([^.]*)ServiceExtension$", string.Empty);
2628
private static readonly string SharedName = $"group.{PrimaryPackageName}.notifo";
@@ -30,6 +32,23 @@ internal sealed class Settings : ISeenNotificationsStore, ICommandStore
3032
TypeNameHandling = TypeNameHandling.Auto
3133
};
3234

35+
public void Clear()
36+
{
37+
Preferences.Clear(SharedName);
38+
}
39+
40+
public string? ApiKey
41+
{
42+
get => Preferences.Get(KeyApiKey, null, SharedName);
43+
set => Preferences.Set(KeyApiKey, value);
44+
}
45+
46+
public string? ApiUrl
47+
{
48+
get => Preferences.Get(KeyApiUrl, null, SharedName);
49+
set => Preferences.Set(KeyApiUrl, value);
50+
}
51+
3352
public async ValueTask AddSeenNotificationIdsAsync(int maxCapacity, IEnumerable<Guid> ids)
3453
{
3554
await Task.Run(() =>

0 commit comments

Comments
 (0)