async Task<PivotItem> MapViewModel(ViewModelBase viewModel)
{
var cancelToken = CancelSource.Token;
var rvm = viewModel as LinkedPictureViewModel.LinkedPicture;
string domain = rvm.Url;
try
{
var uri = new Uri(rvm.Url);
domain = uri.Host;
}
catch{}
Messenger.Default.Send<LoadingMessage>(new LoadingMessage { Loading = true, Percentage = 0, Message = "loading from " + domain });
try
{
var imageBytes = await SimpleHttpService.GetBytesWithProgress(cancelToken, rvm.Url, (progress) =>
{
Messenger.Default.Send<LoadingMessage>(new LoadingMessage { Loading = true, Percentage = progress, Message = "loading from " + domain });
});
Messenger.Default.Send<LoadingMessage>(new LoadingMessage { Loading = false });
bool isGif = false;
if (imageBytes != null && imageBytes.Length >= 6)
{
isGif =
imageBytes[0] == 0x47 && // G
imageBytes[1] == 0x49 && // I
imageBytes[2] == 0x46 && // F
imageBytes[3] == 0x38 && // 8
(imageBytes[4] == 0x39 || imageBytes[4] == 0x37) && // 9 or 7
imageBytes[5] == 0x61; // a
}
else
{
ServiceLocator.Current.GetInstance<INotificationService>().CreateNotification("failed to load image: unknown");
return null;
}
rvm.ImageSource = imageBytes;
rvm.IsGif = isGif;
}
catch (Exception ex)
{
if (!(ex is TaskCanceledException))
ServiceLocator.Current.GetInstance<INotificationService>().CreateNotification("failed to load image: " + ex.Message);
else
{
Messenger.Default.Send<LoadingMessage>(new LoadingMessage { Loading = false });
}
}
return new PivotItem { DataContext = viewModel };
}