Esempio n. 1
0
        public async void LoadSubscriptions()
        {
            if (!ApplicationSetting.GetLocalSetting("google_is_loggedin", false))
            {
                return;
            }

            if (_selfSubscriptionVideoCollection != null)
            {
                _selfSubscriptionVideoCollection.Clear();
            }

            // Static global variable data to be referenced by the LoadMoreItems function
            string pagination      = null;
            string firstpagination = null;

            bool isLoadComplete = false;

            // Function
            selfSubscriptionVideoCollection = new IncrementalLoadingCollection <Subscription>((CancellationToken cts, uint count) =>
            {
                return(Task.Run <ObservableCollection <Subscription> >(async() =>
                {
                    ObservableCollection <Subscription> newcollection = new ObservableCollection <Subscription>();

                    if (!isLoadComplete)
                    {
                        string uri = string.Format("https://www.googleapis.com/youtube/v3/subscriptions?part={0}&mine={1}&maxResults={2}",
                                                   "contentDetails%2CsubscriberSnippet%2Csnippet",
                                                   "true",
                                                   50);
                        if (pagination != null)
                        {
                            uri += "&pageToken=" + pagination;
                        }

                        object obj = await YouTubeAuthenticatedAPI.RequestedAuthenticatedAPICall(uri, typeof(SubscriptionResultObject));
                        if (obj != null)
                        {
                            SubscriptionResultObject deserialize = (SubscriptionResultObject)obj;

                            if (pagination != null && firstpagination == pagination) // Is this the end?
                            {
                                isLoadComplete = true;
                                return newcollection;
                            }
                            else
                            {
                                // Set pagination
                                pagination = deserialize.nextPageToken;

                                if (firstpagination == null)
                                {
                                    firstpagination = pagination; // no choice, we might load the first at the end again.. there's no pagination information for the first page
                                }

                                // Add to list of subscription videos
                                foreach (Subscription s in deserialize.items)
                                {
                                    newcollection.Add(s);
                                }
                            }
                        }

                        //var videoInfoRequest = YoutubeService.service.Subscriptions.List("contentDetails,id,snippet,subscriberSnippet");

                        /*    videoInfoRequest.RequestParameters.Add("access_token",
                         *      new Parameter()
                         *      {
                         *           Name = "access_token",
                         *            DefaultValue = await ApplicationSetting.GetEncryptedLocalStringValueOrDefault("google_access_token", ""),
                         *             IsRequired = true,
                         *      });*/
                        //videoInfoRequest.Mine = true;
                        //videoInfoRequest.OauthToken = await ApplicationSetting.GetEncryptedLocalStringValueOrDefault("google_authorization_token", "");

                        /*if (pagination != null)
                         * {
                         *  videoInfoRequest.PageToken = pagination;
                         * }
                         *
                         * try
                         * {
                         *  var videoResultResponse = await videoInfoRequest.ExecuteAsync();
                         *
                         *  // Set new pagination
                         *  pagination = videoResultResponse.NextPageToken;
                         *  if (firstpagination == null)
                         *  {
                         *      firstpagination = pagination; // no choice, we might load the first at the end again.. there's no pagination information for the first page
                         *  }
                         *  else if (firstpagination == pagination)
                         *  {
                         *      isLoadComplete = true;
                         *      return newcollection;
                         *  }
                         *
                         *  foreach (Subscription t in videoResultResponse.Items)
                         *  {
                         *      newcollection.Add(t);
                         *  }
                         * }
                         * catch (Exception exp)
                         * {
                         *  Debug.WriteLine(exp.ToString());
                         * }*/
                    }
                    return newcollection;
                }));
            });
            await selfSubscriptionVideoCollection.LoadMoreItemsAsync(10);
        }