Exemplo n.º 1
0
        private Task Initialize()
        {
            return(ExecuteSafe(async() =>
            {
                using (await _initializeAsyncLock.LockAsync())
                {
                    if (_isInitialized)
                    {
                        return;
                    }

                    var jsonAssets = await _storageService.GetAssetTextFileAsync(ReflectionHelper.GetAttributeOfEnum <DescriptionAttribute, FileKeys>(FileKeys.SourcesConfiguration).Description);
                    var feeds = JsonConvert.DeserializeObject <List <SourceModel> >(jsonAssets);
                    var recovered = false;
                    try
                    {
                        var json = await _storageService.GetRoamingTextFileAsync(ReflectionHelper.GetAttributeOfEnum <DescriptionAttribute, FileKeys>(FileKeys.SourcesUserConfiguration).Description);

                        if (!string.IsNullOrEmpty(json))
                        {
                            _sourceCacheEntity = JsonConvert.DeserializeObject <SourceCacheEntity>(json);
                            recovered = true;
                        }
                        else
                        {
                            _sourceCacheEntity = new SourceCacheEntity();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Instance.LogException(ex);
                        _sourceCacheEntity = new SourceCacheEntity();
                    }

                    var feedsToLoad = new List <FeedModel>();
                    foreach (var source in feeds)
                    {
                        if (!_sourceCacheEntity.IsEnabledDictionary.ContainsKey(source.Guid))
                        {
                            _sourceCacheEntity.IsEnabledDictionary[source.Guid] = false;
                            recovered = false;
                        }

                        SourceManager.AddSource(source, _sourceCacheEntity.IsEnabledDictionary[source.Guid]);
                        foreach (var feed in source.Feeds)
                        {
                            feed.Source = source;

                            if (!_sourceCacheEntity.IsEnabledDictionary.ContainsKey(feed.Guid))
                            {
                                _sourceCacheEntity.IsEnabledDictionary[feed.Guid] = false;
                                recovered = false;
                            }

                            SourceManager.AddFeed(feed, source, _sourceCacheEntity.IsEnabledDictionary[feed.Guid]);
                            if (_sourceCacheEntity.IsEnabledDictionary[feed.Guid])
                            {
                                feedsToLoad.Add(feed);
                            }
                        }
                    }

                    _isInitialized = true;

                    if (!recovered)
                    {
                        await SaveCache();
                    }

                    var tasks = feedsToLoad.Select(feedModel => LoadArticlesIntoFeed(feedModel, 12)).ToList();

                    await Task.WhenAll(tasks);
                }
            }));
        }