Ejemplo n.º 1
0
        private async void LoadFavoritesAudios(CancellationToken token)
        {
            IsWorking = true;
            OnTaskStarted("favorites");

            FavoritesTracks = new ObservableCollection<Audio>();

            try
            {
                int offset = 0;
                int count = 50;
                int requestsCount = 0;

                while (FavoritesTracks != null && FavoritesTracks.Count < MAX_WALL_AUDIOS)
                {
                    var a = await DataService.GetFavoritesAudio(count, offset, 0, token);
                    if (a == null || a.Count == 0)
                        break;
                    else if (a.Count > 0)
                    {
                        OnTaskFinished("favorites");
                    }

                    if (token.IsCancellationRequested)
                    {
                        Debug.WriteLine("Favorites audios cancelled");
                        break;
                    }

                    offset += count;

                    foreach (var audio in a)
                    {
                        FavoritesTracks.Add(audio);
                    }

                    requestsCount++;

                    if (requestsCount >= 2) //не больше 2-х запросов в секунду
                    {
                        requestsCount = 0;
                        await Task.Delay(1000);
                    }

                    Debug.WriteLine("Loading more audios from favorites");
                }

                if (FavoritesTracks.IsNullOrEmpty() && !token.IsCancellationRequested)
                    OnTaskError("favorites", ErrorResources.LoadAudiosErrorEmpty);
            }
            catch (Exception ex)
            {
                LoggingService.Log(ex);

                OnTaskError("favorites", ErrorResources.LoadAudiosErrorCommon);
            }

            IsWorking = false;
            OnTaskFinished("favorites");
        }
Ejemplo n.º 2
0
        private async void LoadNewsAudios(CancellationToken token)
        {
            IsWorking = true;
            OnTaskStarted("news");

            NewsTracks = new ObservableCollection<Audio>();

            try
            {
                var nextFrom = string.Empty;
                int count = 150;
                int requestsCount = 0;

                while (NewsTracks != null && NewsTracks.Count < MAX_NEWS_AUDIOS)
                {
                    if (token.IsCancellationRequested)
                    {
                        Debug.WriteLine("News audio cancelled");
                        break;
                    }

                    var a = await DataService.GetNewsAudio(count, nextFrom, token);
                    if (a.Items.IsNullOrEmpty())
                        break;
                    else if (a.Items.Count > 0)
                    {
                        OnTaskFinished("news");
                    }

                    if (token.IsCancellationRequested)
                    {
                        Debug.WriteLine("News audio cancelled");
                        break;
                    }

                    nextFrom = a.NextFrom;

                    foreach (var audio in a.Items)
                    {
                        NewsTracks.Add(audio);
                    }

                    requestsCount++;

                    if (requestsCount >= 2) //не больше 2-х запросов в секунду
                    {
                        requestsCount = 0;
                        await Task.Delay(1000);
                    }

                    Debug.WriteLine("Loading more audios from news");
                }

                if (NewsTracks.IsNullOrEmpty() && !token.IsCancellationRequested)
                    OnTaskError("news", ErrorResources.LoadAudiosErrorEmpty);
            }
            catch (Exception ex)
            {
                LoggingService.Log(ex);
                OnTaskError("news", ErrorResources.LoadAudiosErrorCommon);
            }

            IsWorking = false;
            OnTaskFinished("news");
        }