Exemplo n.º 1
0
        /// <summary>
        /// Loads stream items from the souncloud api
        /// </summary>
        /// <param name="count">The amount of items to load</param>
        // ReSharper disable once RedundantAssignment
        public IAsyncOperation <LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
        {
            // Return a task that will get the items
            return(Task.Run(async() =>
            {
                _track = PlaybackService.Current.CurrentTrack;

                if (_track == null)
                {
                    return new LoadMoreItemsResult {
                        Count = 0
                    }
                }
                ;

                // We are loading
                await DispatcherHelper.ExecuteOnUIThreadAsync(() => { App.IsLoading = true; });

                try
                {
                    // Get the comments
                    var comments = await SoundByteService.Current.GetAsync <CommentListHolder>(string.Format("/tracks/{0}/comments", _track.Id), new Dictionary <string, string>
                    {
                        { "limit", "50" },
                        { "cursor", Token },
                        { "linked_partitioning", "1" }
                    });

                    // Parse uri for offset
                    var param = new QueryParameterCollection(comments.NextList);
                    var offset = param.FirstOrDefault(x => x.Key == "offset").Value;

                    // Get the comment offset
                    Token = string.IsNullOrEmpty(offset) ? "eol" : offset;

                    // Make sure that there are comments in the list
                    if (comments.Items.Count > 0)
                    {
                        // Set the count variable
                        count = (uint)comments.Items.Count;

                        // Loop though all the comments on the UI thread
                        await DispatcherHelper.ExecuteOnUIThreadAsync(() =>
                        {
                            comments.Items.ForEach(Add);
                        });
                    }
                    else
                    {
                        // There are no items, so we added no items
                        count = 0;

                        // Reset the token
                        Token = "eol";

                        // No items tell the user
                        await DispatcherHelper.ExecuteOnUIThreadAsync(async() =>
                        {
                            await new MessageDialog("Be the first to post a comment.", "No Comments").ShowAsync();
                        });
                    }
                }
                catch (SoundByteException ex)
                {
                    // Exception, most likely did not add any new items
                    count = 0;

                    // Reset the token
                    Token = "eol";

                    // Exception, display error to the user
                    await DispatcherHelper.ExecuteOnUIThreadAsync(async() =>
                    {
                        await new MessageDialog(ex.ErrorDescription, ex.ErrorTitle).ShowAsync();
                    });
                }

                // We are not loading
                await DispatcherHelper.ExecuteOnUIThreadAsync(() => { App.IsLoading = false; });

                // Return the result
                return new LoadMoreItemsResult {
                    Count = count
                };
            }).AsAsyncOperation());
        }
Exemplo n.º 2
0
 /// <summary>
 /// Get comments for a track
 /// </summary>
 /// <param name="track"></param>
 public CommentModel(Core.API.Endpoints.Track track)
 {
     _track = track;
 }