private async Task LoadAudiosFromWall(AddAudioItem addItem, long ownerId, CancellationToken token) { int offset = 0; const int maxCount = 100; int allCount; int curCount = 0; Func <VKList <VKPost>, IEnumerable <VKAudio> > audioFilter = list => list.Items .Where(x => x.Attachments != null) .SelectMany(x => x.Attachments) .Where(x => x.Type == "audio") .Select(x => x.Audio) .Where(x => !String.IsNullOrWhiteSpace(x.Url)); var lHead = await WallAPI.GetAsync(ownerId, count : maxCount, offset : offset, token : token); if (lHead == null) { return; } foreach (var item in audioFilter(lHead)) { token.ThrowIfCancellationRequested(); await addItem(item); } curCount += maxCount; allCount = Convert.ToInt32(lHead.Count); while (curCount < allCount) { offset = curCount; var lCurr = await WallAPI.GetAsync(ownerId, count : maxCount, offset : offset, token : token); if (lCurr == null) { break; } foreach (var item in audioFilter(lCurr)) { token.ThrowIfCancellationRequested(); await addItem(item); } curCount += maxCount; token.ThrowIfCancellationRequested(); } }
private static async Task <IEnumerable <VKAudio> > GetAllAudioFromPost(long ownerId, CancellationToken token) { int offset = 0; const int maxCount = 100; int allCount; int curCount = 0; Func <VKList <VKPost>, IEnumerable <VKAudio> > audioFilter = list => list.Items .Where(x => x.Attachments != null) .SelectMany(x => x.Attachments) .Where(x => x.Type == "audio") .Select(x => x.Audio) .Where(x => !String.IsNullOrWhiteSpace(x.Url)); var lHead = await WallAPI.GetAsync(ownerId, count : maxCount, offset : offset, token : token); if (lHead == null) { return(null); } curCount += maxCount; allCount = Convert.ToInt32(lHead.Count); while (curCount < allCount) { offset = curCount; var lCurr = await WallAPI.GetAsync(ownerId, count : maxCount, offset : offset, token : token); if (lCurr == null) { break; } lHead.Items = lHead.Items.Union(lCurr.Items); curCount += maxCount; } return(audioFilter(lHead)); }