1- using eShopOnContainers . Core . Services . Dependency ;
1+ using System ;
2+ using System . Threading . Tasks ;
3+ using Xamarin . Forms ;
24
35namespace 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}
0 commit comments