Esempio n. 1
0
        private async Task <SpotlightFeedReader> InitializeAsync_(IFeedCache <string> cache)
        {
            // DEBUGGING ONLY: a hardcoded delay to make sure the loading page is working correctly
            //await Task.Run(() =>
            //{
            //    Thread.Sleep(5000);
            //});

            // if the cache has not yet been initialized, initialize it
            if (cache == null)
            {
                cache = await SpotlightFeedCacheFactory.CreateSpotlightFeedCache();
            }

            m_cache = cache;

            SpotlightItemRoot root = null;

            // try to load the feed from the web first
            var json = await LoadJsonFromWeb_(m_url);

            if (json != null)
            {
                // try to deserialize
                root = await DeserializeJson_(json);

                if (root == null)
                {
                    // invalid json, clear it and try to fetch from cache
                    json = null;
                }
                else
                {
                    // if there are web-based images, download them and re-serialize to json
                    json = await SerializeJsonWithLocalImages_(root);

                    // store the result back in cache; at this point we know we have valid json
                    await cache.PutFeedAsync(json);
                }
            }

            // if we didn't get a valid feed from the web, try to fetch from cache
            if (json == null)
            {
                json = cache.GetFeed();
                if (json != null)
                {
                    // try to deserialize
                    root = await DeserializeJson_(json);

                    if (root == null)
                    {
                        json = null;
                    }
                }
            }

            m_root = root;

            return(this);
        }