public static Task<ReadableArticleViewModel> LoadAtLeastOne(ISimpleHttpService httpService, string url, string linkId)
{
TaskCompletionSource<ReadableArticleViewModel> result = new TaskCompletionSource<ReadableArticleViewModel>();
var articleViewModel = new ReadableArticleViewModel { ArticleUrl = url, ArticleParts = new ObservableCollection<object>(), LinkId = linkId, ContentIsFocused = true };
LoadOneImpl(httpService, url, articleViewModel.ArticleParts).ContinueWith(async (task) =>
{
try
{
if (task.IsCompleted)
{
Tuple<string, string> tpl = await task;
var nextPage = tpl.Item1;
articleViewModel.Title = tpl.Item2;
result.SetResult(articleViewModel);
if (!string.IsNullOrEmpty(nextPage))
{
var remainingParts = await Task.Run(() => LoadFullyImpl(httpService, nextPage));
foreach (var part in remainingParts.Item2)
{
articleViewModel.ArticleParts.Add(part);
}
}
}
else if (task.Exception != null)
result.SetException(task.Exception);
}
catch (Exception ex)
{
result.SetException(ex);
}
}, TaskScheduler.FromCurrentSynchronizationContext());
return result.Task;
}