private Task Initialize() { return ExecuteSafe(async () => { using (await _initializeAsyncLock.LockAsync()) { if (_isInitialized) return; var jsonAssets = await _storageService.GetAssetTextFileAsync( ReflectionHelper.GetAttributeOfEnum<DescriptionAttribute, FileKeys>( FileKeys.SettingsConfiguration).Description); var defaultSettings = JsonConvert.DeserializeObject<List<SettingEntity>>(jsonAssets); var recovered = false; SettingCacheEntity cache = new SettingCacheEntity(); try { var json = await _storageService.GetCachedTextFileAsync( ReflectionHelper.GetAttributeOfEnum<DescriptionAttribute, FileKeys>( FileKeys.SettingsUserConfiguration).Description); if (!string.IsNullOrEmpty(json)) { cache = JsonConvert.DeserializeObject<SettingCacheEntity>(json); recovered = true; } } catch (Exception ex) { LogHelper.Instance.LogException(ex); } foreach (var defaultSetting in defaultSettings) { var savedSetting = cache.SettingCacheItemEntities.FirstOrDefault(s => s.Guid == defaultSetting.Guid); if (savedSetting != null && !defaultSetting.IsImmutable) defaultSetting.Value = savedSetting.Value; SettingManager.AddSetting(defaultSetting); } _isInitialized = true; //do saving fire&forget to free lockings #pragma warning disable 4014 if (!recovered) SaveSettingsAsync(); #pragma warning restore 4014 } }); }
public static SettingCacheEntity GetSettingCache() { var cache = new SettingCacheEntity(); foreach (var baseSettingModel in AllSettings) { cache.SettingCacheItemEntities.Add(new SettingCacheItemEntity() { Guid = baseSettingModel.Guid, Value = baseSettingModel.Value }); } return cache; }