Exemple #1
0
        public MagicBinderCardViewModel(
            IMagicCardDefinition definition,
            MagicBinderCard card)
        {
            _notificationCenter = NotificationCenter.Instance;
            _definition         = definition;
            _card  = card;
            _price = StaticPriceDatabase.FindPrice(_definition, false, false, "", false);
            _price.PriceChanged += OnPricePriceChanged;

            UpdatePrice();
        }
        public static void OnImageChanged(
            DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            var instance = d as CardImage;

            if (instance != null)
            {
                instance.imageControl.Source = instance._emptyImage;

                // Trigger download and display
                var card = instance.SelectedCard;

                instance.SetDefinition = card != null
                    ? StaticMagicData.SetDefinitionsBySetCode[card.SetCode]
                    : null;

                MagicCardPrice cardPrice    = null;
                var            cardFileName = string.Empty;
                Task.Factory.StartNew(() =>
                {
                    cardPrice = StaticPriceDatabase.FindPrice(card, false, false, "CardImage control", false);

                    var download = new CardImageDownload(instance._notificationCenter);
                    cardFileName = download.DownloadImage(card, cardPrice);
                }).ContinueWith(task =>
                {
                    // Lookup card price:
                    instance.CardPrice = cardPrice;

                    if (string.IsNullOrWhiteSpace(cardFileName))
                    {
                        instance.imageControl.Source = instance._emptyImage;
                    }
                    else
                    {
                        var uri    = new Uri(cardFileName);
                        var bitmap = new BitmapImage(uri);
                        bitmap.Freeze();
                        instance.imageControl.Source = bitmap;
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext());
            }
        }
        public string DownloadImage(MagicCardDefinition card, MagicCardPrice cardPrice)
        {
            if (card == null)
            {
                return(null);
            }

            if (cardPrice == null)
            {
                cardPrice = StaticPriceDatabase.FindPrice(card, false, false, "CardImage download", false);
            }

            // Add default image path if needed
            cardPrice.BuildDefaultMkmImagePath(card);

            FileInfo localStorage = null;
            string   fullUrl      = null;

            try
            {
                var cache = PathHelper.CardImageCacheFolder;
                localStorage = new FileInfo(Path.Combine(cache, CreateCardIdPart(card, '\\', true, true).TrimStart('\\')));
                if (localStorage.Exists)
                {
                    return(localStorage.FullName);
                }

                var url = CreateCardIdPart(card, '/', false, false);
                if (url == null)
                {
                    return(null);
                }

                var stopwatch = Stopwatch.StartNew();

                if (!localStorage.Directory.Exists)
                {
                    localStorage.Directory.Create();
                }

                using (var client = new WebClient())
                {
                    var rootUrl = card.MagicCardType != MagicCardType.Token
                        ? "http://magiccards.info/scans/en"
                        : "http://magiccards.info/extras/token";

                    fullUrl = !string.IsNullOrWhiteSpace(cardPrice.ImagePath)
                        ? "http://www.magickartenmarkt.de/" + cardPrice.ImagePath
                        : rootUrl + url;

                    client.DownloadFile(new Uri(fullUrl), localStorage.FullName);
                }

                stopwatch.Stop();
                _notificationCenter.FireNotification(
                    LogLevel.Debug,
                    string.Format("Downloaded image for '{0}[{1}]' in {2}", card.NameEN, card.SetCode, stopwatch.Elapsed));
            }
            catch (Exception error)
            {
                _notificationCenter.FireNotification(
                    LogLevel.Debug,
                    string.Format("Error downloading image for '{0}[{1}]': {2} ({3})", card.NameEN, card.SetCode, error.Message, fullUrl));

                return(null);
            }

            return(localStorage.FullName);
        }