Example #1
0
        private async Task<RootObject> GetJsonFeedAsync()
        {
            int post_count = 0;
            HttpClient client = new HttpClient();

            //TODO: save these also in temp state when downloaded otherwise it takes a while to start up
            await this.CacheCategories(client);
            await this.CacheTags(client);

            try
            {
                String response = null;

                response = await LoadFeedFromCache();

                if (response == null)
                {
                    #region Get the number of posts (apparently the JSON API doesn't support retrieving ONLY the post count)

                    // I used this URI because it returns the smallest amount of data
                    Uri post_count_uri = new Uri(App.BLOG_URL + "?json=get_recent_posts&include=id");

                    try
                    {
                        var post_count_response = await client.GetStringAsync(post_count_uri);
                        RootObject post_count_object = await JsonConvert.DeserializeObjectAsync<RootObject>(post_count_response);
                        post_count = post_count_object.count_total;
                    }
                    catch (Exception)
                    {
                        return null;
                    }

                    #endregion

                    Uri feedUri = new Uri(App.BLOG_URL + "?json=get_recent_posts&count=" + post_count);

                    response = await client.GetStringAsync(feedUri);
                    SaveFeedToCache(response);  //Note: no need to wait here as resonse is read-only
                    var local = Windows.Storage.ApplicationData.Current.LocalSettings;
                    local.Values["lastFeedSave"] = DateTime.Now.ToString();
                }

                RootObject postList = await JsonConvert.DeserializeObjectAsync<RootObject>(response);
                RootObject feedData = new RootObject();

                
                //Update the tile with the most recent posts
                //For the moment, just do the first one.
                //Boolean tileUpdated = false;

                foreach (Post post in postList.posts)
                {
                    if (post.title != null)
                    {
                        /*if (!tileUpdated)                     //J: Do we need this anymore? Already defined in app.xaml.cs
                        {
                            TileUpdater updater = Windows.UI.Notifications.TileUpdateManager.CreateTileUpdaterForApplication();
                            updater.StartPeriodicUpdate(new Uri("http://www.kraigbrockschmidt.com/blog/tileupdate.php"), PeriodicUpdateRecurrence.Hour);
                        }*/

                        post.title = post.title.Replace("&#8220;", "\"");
                        post.title = post.title.Replace("&#8221;", "\"");
                        post.title = post.title.Replace("&#8243;", "\"");
                        post.title = post.title.Replace("&#8230;", "...");
                        post.title = post.title.Replace("&#038;", "&");
                        post.title = post.title.Replace("&#8217;", "'");
                        post.title = post.title.Replace("&#8211;", "-");
                        post.title = post.title.Replace("&lt;", "<");
                        post.title = post.title.Replace("&gt;", ">");
                    }

                    post.real_comment_count = post.comments.Count.ToString() + " comments";
                    feedData.posts.Add(post);
                }

                return feedData;
            }

            catch (Exception)
            {
                return null;
            }
        }
Example #2
0
        private async Task<RootObject> GetJsonFeedAsync(string filter, string index)
        {
            int post_count = 0;
            System.Net.Http.HttpClient client = new HttpClient();

            #region Get the number of posts (apparently the JSON API doesn't support retrieving ONLY the post count)

            Uri post_count_uri;
            if (filter == "category")
                post_count_uri = new Uri(App.BLOG_URL + "?json=get_category_posts&include=id&slug=" + index);
            else if (filter == "tag")
                post_count_uri = new Uri(App.BLOG_URL + "?json=get_tag_posts&include=id&slug=" + index);
            else if (filter == "date")
                post_count_uri = new Uri(App.BLOG_URL + "?json=get_date_posts&include=id&date=" + index);
            else
                post_count_uri = new Uri(App.BLOG_URL + "?json=get_search_results&include=id&search=" + index);

            try
            {
                var post_count_response = await client.GetStringAsync(post_count_uri);
                RootObject post_count_object = await JsonConvert.DeserializeObjectAsync<RootObject>(post_count_response);
                post_count = post_count_object.count_total;
            }
            catch (Exception)
            {
                return null;
            }

            #endregion

            Uri feedUri;
            if (filter == "category")
                feedUri = new Uri(App.BLOG_URL + "?json=get_category_posts&slug=" + index + "&count=" + post_count);
            else if (filter == "tag")
                feedUri = new Uri(App.BLOG_URL + "?json=get_tag_posts&slug=" + index + "&count=" + post_count);
            else if (filter == "date")
                feedUri = new Uri(App.BLOG_URL + "?json=get_date_posts&date=" + index + "&count=" + post_count);
            else
                feedUri = new Uri(App.BLOG_URL + "?json=get_search_results&search=" + index + "&count=" + post_count);

            try
            {
                var response = await client.GetStringAsync(feedUri);
                RootObject postList = await JsonConvert.DeserializeObjectAsync<RootObject>(response);
                RootObject feedData = new RootObject();

                foreach (Post post in postList.posts)
                {
                    if (post.title != null)
                    {
                        post.title = post.title.Replace("&#8220;", "\"");
                        post.title = post.title.Replace("&#8221;", "\"");
                        post.title = post.title.Replace("&#8243;", "\"");
                        post.title = post.title.Replace("&#8230;", "...");
                        post.title = post.title.Replace("&#038;", "&");
                        post.title = post.title.Replace("&#8217;", "'");
                        post.title = post.title.Replace("&#8211;", "-");
                        post.title = post.title.Replace("&lt;", "<");
                        post.title = post.title.Replace("&gt;", ">");
                    }

                    post.real_comment_count = post.comments.Count.ToString() + " comments";
                    feedData.posts.Add(post);
                }
                return feedData;
            }

            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected async override void LoadState(Object navigationParameter, Dictionary <String, Object> pageState)
        {
            navParam       = navigationParameter as postDate;
            pageTitle.Text = "Posts for " + navParam.month_name + " " + navParam.year;
            ViewCommentsButton.Visibility = Visibility.Collapsed;
            progressRing.Visibility       = Visibility.Visible;
            Windows.UI.Xaml.Media.Animation.Storyboard sb =
                this.FindName("PopInStoryBoard") as Windows.UI.Xaml.Media.Animation.Storyboard;
            if (sb != null)
            {
                sb.Begin();
            }

            // TODO: Assign a bindable group to this.DefaultViewModel["Group"]
            // TODO: Assign a collection of bindable items to this.DefaultViewModel["Items"]
            FeedDataSource feedDataSource = (FeedDataSource)App.Current.Resources["feedDataSource"];

            if (feedDataSource != null)
            {
                await feedDataSource.GetFeedsAsync("date", navParam.year + navParam.month);
            }
            RootObject feedData = FeedDataSource.GetFeed();

            if (feedData != null)
            {
                this.DefaultViewModel["Feed"]  = feedData;
                this.DefaultViewModel["Items"] = feedData.posts;
                if (feedData.posts.Count == 0)
                {
                    itemTitle.Text = "There are no posts yet.";
                }
            }

            if (pageState == null)
            {
                // When this is a new page, select the first item automatically unless logical page
                // navigation is being used (see the logical page navigation #region below.)
                if (!this.UsingLogicalPageNavigation() && this.itemsViewSource.View != null)
                {
                    this.itemsViewSource.View.MoveCurrentToFirst();
                }
                else
                {
                    //this.itemsViewSource.View.MoveCurrentToPosition(-1);
                }
            }
            else
            {
                // Restore the previously saved state associated with this page
                if (pageState.ContainsKey("SelectedItem") && this.itemsViewSource.View != null)
                {
                    // TODO: Invoke this.itemsViewSource.View.MoveCurrentTo() with the selected
                    //       item as specified by the value of pageState["SelectedItem"]
                    string itemTitle    = (string)pageState["SelectedItem"];
                    Post   selectedItem = FeedDataSource.GetItem(itemTitle);
                    this.itemsViewSource.View.MoveCurrentTo(selectedItem);
                }
            }
            if (feedData.posts.Count == 0)
            {
                ViewCommentsButton.Visibility = Visibility.Collapsed;
            }
            else
            {
                ViewCommentsButton.Visibility = Visibility.Visible;
            }
            progressRing.Visibility = Visibility.Collapsed;
        }