Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit a627d1f

Browse files
David BritchDavid Britch
authored andcommitted
SettingsService now uses Application.Current.Properties dictionary.
Platform specific settings code removed.
1 parent f2a4f3a commit a627d1f

File tree

11 files changed

+811
-1001
lines changed

11 files changed

+811
-1001
lines changed

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Extensions/ObservableExtension.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable
1515
}
1616

1717
return collection;
18-
1918
}
2019
}
2120
}

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Settings/ISettingsService.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace eShopOnContainers.Core.Services.Settings
1+
using System.Threading.Tasks;
2+
3+
namespace eShopOnContainers.Core.Services.Settings
24
{
35
public interface ISettingsService
46
{
@@ -10,5 +12,10 @@ public interface ISettingsService
1012
string Latitude { get; set; }
1113
string Longitude { get; set; }
1214
bool AllowGpsLocation { get; set; }
15+
16+
bool GetValueOrDefault(string key, bool defaultValue);
17+
string GetValueOrDefault(string key, string defaultValue);
18+
Task AddOrUpdateValue(string key, bool value);
19+
Task AddOrUpdateValue(string key, string value);
1320
}
1421
}

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Settings/ISettingsServiceImplementation.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
using eShopOnContainers.Core.Services.Dependency;
1+
using System;
2+
using System.Threading.Tasks;
3+
using Xamarin.Forms;
24

35
namespace eShopOnContainers.Core.Services.Settings
46
{
57
public class SettingsService : ISettingsService
68
{
7-
private readonly ISettingsServiceImplementation _settingsService;
8-
9-
ISettingsServiceImplementation AppSettings
10-
{
11-
get { return _settingsService; }
12-
}
13-
14-
public SettingsService(IDependencyService dependencyService)
15-
{
16-
_settingsService = dependencyService.Get<ISettingsServiceImplementation>();
17-
}
18-
199
#region Setting Constants
2010

2111
private const string AccessToken = "access_token";
@@ -37,52 +27,113 @@ public SettingsService(IDependencyService dependencyService)
3727

3828
#endregion
3929

30+
#region Settings Properties
31+
4032
public string AuthAccessToken
4133
{
42-
get => AppSettings.GetValueOrDefault(AccessToken, AccessTokenDefault);
43-
set => AppSettings.AddOrUpdateValue(AccessToken, value);
34+
get => GetValueOrDefault(AccessToken, AccessTokenDefault);
35+
set => AddOrUpdateValue(AccessToken, value);
4436
}
4537

4638
public string AuthIdToken
4739
{
48-
get => AppSettings.GetValueOrDefault(IdToken, IdTokenDefault);
49-
set => AppSettings.AddOrUpdateValue(IdToken, value);
40+
get => GetValueOrDefault(IdToken, IdTokenDefault);
41+
set => AddOrUpdateValue(IdToken, value);
5042
}
5143

5244
public bool UseMocks
5345
{
54-
get => AppSettings.GetValueOrDefault(IdUseMocks, UseMocksDefault);
55-
set => AppSettings.AddOrUpdateValue(IdUseMocks, value);
46+
get => GetValueOrDefault(IdUseMocks, UseMocksDefault);
47+
set => AddOrUpdateValue(IdUseMocks, value);
5648
}
5749

5850
public string UrlBase
5951
{
60-
get => AppSettings.GetValueOrDefault(IdUrlBase, UrlBaseDefault);
61-
set => AppSettings.AddOrUpdateValue(IdUrlBase, value);
52+
get => GetValueOrDefault(IdUrlBase, UrlBaseDefault);
53+
set => AddOrUpdateValue(IdUrlBase, value);
6254
}
6355

6456
public bool UseFakeLocation
6557
{
66-
get => AppSettings.GetValueOrDefault(IdUseFakeLocation, UseFakeLocationDefault);
67-
set => AppSettings.AddOrUpdateValue(IdUseFakeLocation, value);
58+
get => GetValueOrDefault(IdUseFakeLocation, UseFakeLocationDefault);
59+
set => AddOrUpdateValue(IdUseFakeLocation, value);
6860
}
6961

7062
public string Latitude
7163
{
72-
get => AppSettings.GetValueOrDefault(IdLatitude, FakeLatitudeDefault.ToString());
73-
set => AppSettings.AddOrUpdateValue(IdLatitude, value);
64+
get => GetValueOrDefault(IdLatitude, FakeLatitudeDefault.ToString());
65+
set => AddOrUpdateValue(IdLatitude, value);
7466
}
7567

7668
public string Longitude
7769
{
78-
get => AppSettings.GetValueOrDefault(IdLongitude, FakeLongitudeDefault.ToString());
79-
set => AppSettings.AddOrUpdateValue(IdLongitude, value);
70+
get => GetValueOrDefault(IdLongitude, FakeLongitudeDefault.ToString());
71+
set => AddOrUpdateValue(IdLongitude, value);
8072
}
8173

8274
public bool AllowGpsLocation
8375
{
84-
get => AppSettings.GetValueOrDefault(IdAllowGpsLocation, AllowGpsLocationDefault);
85-
set => AppSettings.AddOrUpdateValue(IdAllowGpsLocation, value);
76+
get => GetValueOrDefault(IdAllowGpsLocation, AllowGpsLocationDefault);
77+
set => AddOrUpdateValue(IdAllowGpsLocation, value);
8678
}
79+
80+
#endregion
81+
82+
#region Public Methods
83+
84+
public Task AddOrUpdateValue(string key, bool value) => AddOrUpdateValueInternal(key, value);
85+
public Task AddOrUpdateValue(string key, string value) => AddOrUpdateValueInternal(key, value);
86+
public bool GetValueOrDefault(string key, bool defaultValue) => GetValueOrDefaultInternal(key, defaultValue);
87+
public string GetValueOrDefault(string key, string defaultValue) => GetValueOrDefaultInternal(key, defaultValue);
88+
89+
#endregion
90+
91+
#region Internal Implementation
92+
93+
async Task AddOrUpdateValueInternal<T>(string key, T value)
94+
{
95+
if (value == null)
96+
{
97+
await Remove(key);
98+
}
99+
100+
Application.Current.Properties[key] = value;
101+
try
102+
{
103+
await Application.Current.SavePropertiesAsync();
104+
}
105+
catch (Exception ex)
106+
{
107+
Console.WriteLine("Unable to save: " + key, " Message: " + ex.Message);
108+
}
109+
}
110+
111+
T GetValueOrDefaultInternal<T>(string key, T defaultValue = default(T))
112+
{
113+
object value = null;
114+
if (Application.Current.Properties.ContainsKey(key))
115+
{
116+
value = Application.Current.Properties[key];
117+
}
118+
return null != value ? (T)value : defaultValue;
119+
}
120+
121+
async Task Remove(string key)
122+
{
123+
try
124+
{
125+
if (Application.Current.Properties[key] != null)
126+
{
127+
Application.Current.Properties.Remove(key);
128+
await Application.Current.SavePropertiesAsync();
129+
}
130+
}
131+
catch (Exception ex)
132+
{
133+
Console.WriteLine("Unable to remove: " + key, " Message: " + ex.Message);
134+
}
135+
}
136+
137+
#endregion
87138
}
88139
}

src/Mobile/eShopOnContainers/eShopOnContainers.Droid/Extensions/LocationExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal static DateTimeOffset GetTimestamp(this Android.Locations.Location loca
3232
{
3333
return new DateTimeOffset(Epoch.AddMilliseconds(location.Time));
3434
}
35-
catch (Exception e)
35+
catch (Exception)
3636
{
3737
return new DateTimeOffset(Epoch);
3838
}

src/Mobile/eShopOnContainers/eShopOnContainers.Droid/Services/SettingsServiceImplementation.cs

Lines changed: 0 additions & 149 deletions
This file was deleted.

src/Mobile/eShopOnContainers/eShopOnContainers.Droid/eShopOnContainers.Droid.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
2020
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
2121
<AndroidUseLatestPlatformSdk>true</AndroidUseLatestPlatformSdk>
22-
<TargetFrameworkVersion>v8.0</TargetFrameworkVersion>
22+
<TargetFrameworkVersion>v8.1</TargetFrameworkVersion>
2323
<AndroidSupportedAbis>armeabi,armeabi-v7a,x86</AndroidSupportedAbis>
2424
<AndroidStoreUncompressedFileExtensions />
2525
<MandroidI18n />
@@ -211,7 +211,6 @@
211211
<Compile Include="Effects\CircleEffect.cs" />
212212
<Compile Include="Effects\BaseContainerEffect.cs" />
213213
<Compile Include="Activities\SplashActivity.cs" />
214-
<Compile Include="Services\SettingsServiceImplementation.cs" />
215214
<Compile Include="Services\PermissionsService.cs" />
216215
<Compile Include="Services\LocationServiceImplementation.cs" />
217216
<Compile Include="Services\GeolocationSingleListener.cs" />

0 commit comments

Comments
 (0)