<commit_before>// Copyright (c) PocketCampus.Org 2014
// See LICENSE file for more details
// File author: <NAME>

using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using PocketCampus.Common;
using PocketCampus.Food.Models;
using PocketCampus.Food.Resources;

namespace PocketCampus.Food
{
    /// <summary>
    /// Converts a price to a display-friendly version.
    /// </summary>
    public sealed class PriceToStringConverter : ValueConverter<double?, string>
    {
        private const string IntegerPriceFormat = "{0}.- CHF";
        private const string DecimalPriceFormat = "{0:0.00} CHF";

        public override string Convert( double? value )
        {
            if ( value == null || value == 0.0 )
            {
                return PluginResources.UnknownPrice;
            }
            if ( Math.Round( (double) value ) == value )
            {
                return string.Format( CultureInfo.InvariantCulture, IntegerPriceFormat, value );
            }
            return string.Format( CultureInfo.InvariantCulture, DecimalPriceFormat, value );
        }
    }

    /// <summary>
    /// Converts meals to their price according to the current settings.
    /// </summary>
    /// <remarks>
    /// Since it needs IPluginSettings, it can't be a generic ValueConverter.
    /// </remarks>
    public sealed class MealPriceConverter : DependencyObject, IValueConverter
    {
        private PriceToStringConverter _priceToString = new PriceToStringConverter();

        public IPluginSettings Settings
        {
            get { return (IPluginSettings) GetValue( SettingsProperty ); }
            set { SetValue( SettingsProperty, value ); }
        }

        public static readonly DependencyProperty SettingsProperty =
            DependencyProperty.Register( "Settings", typeof( IPluginSettings ), typeof( MealPriceConverter ), new PropertyMetadata( null ) );

        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {
            var meal = (Meal) value;

            // Settings == null in design mode for some reason
            var target = Settings == null ? PriceTarget.All : Settings.PriceTarget;
            return _priceToString.Convert( meal.GetPrice( target ) );
        }

        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            throw new NotSupportedException();
        }
    }

    /// <summary>
    /// Converts meals to images of their main type.
    /// </summary>
    public sealed class MealToImageConverter : ValueConverter<Meal, ImageSource>
    {
        public override ImageSource Convert( Meal value )
        {
            string type = value.MealTypes.OrderByDescending( x => x ).First().ToString();
            return new BitmapImage( new Uri( string.Format( "/Assets/MealTypes_{0}.png", type ), UriKind.Relative ) );
        }
    }

    /// <summary>
    /// HACK: Allows Restaurants to be used with LongListSelector.
    /// </summary>
    public sealed class RestaurantsToGroupsConverter : ValueConverter<Restaurant[], RestaurantAsGroup[]>
    {
        public override RestaurantAsGroup[] Convert( Restaurant[] value )
        {
            if ( value == null )
            {
                return null;
            }

            return value.Select( r => new RestaurantAsGroup( r ) ).ToArray();
        }
    }
}<commit_msg>[food.wp] Fixed a problem with meal images.<commit_after>// Copyright (c) PocketCampus.Org 2014
// See LICENSE file for more details
// File author: <NAME>

using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using PocketCampus.Common;
using PocketCampus.Food.Models;
using PocketCampus.Food.Resources;

namespace PocketCampus.Food
{
    /// <summary>
    /// Converts a price to a display-friendly version.
    /// </summary>
    public sealed class PriceToStringConverter : ValueConverter<double?, string>
    {
        private const string IntegerPriceFormat = "{0}.- CHF";
        private const string DecimalPriceFormat = "{0:0.00} CHF";

        public override string Convert( double? value )
        {
            if ( value == null || value == 0.0 )
            {
                return PluginResources.UnknownPrice;
            }
            if ( Math.Round( (double) value ) == value )
            {
                return string.Format( CultureInfo.InvariantCulture, IntegerPriceFormat, value );
            }
            return string.Format( CultureInfo.InvariantCulture, DecimalPriceFormat, value );
        }
    }

    /// <summary>
    /// Converts meals to their price according to the current settings.
    /// </summary>
    /// <remarks>
    /// Since it needs IPluginSettings, it can't be a generic ValueConverter.
    /// </remarks>
    public sealed class MealPriceConverter : DependencyObject, IValueConverter
    {
        private PriceToStringConverter _priceToString = new PriceToStringConverter();

        public IPluginSettings Settings
        {
            get { return (IPluginSettings) GetValue( SettingsProperty ); }
            set { SetValue( SettingsProperty, value ); }
        }

        public static readonly DependencyProperty SettingsProperty =
            DependencyProperty.Register( "Settings", typeof( IPluginSettings ), typeof( MealPriceConverter ), new PropertyMetadata( null ) );

        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {
            var meal = (Meal) value;

            // Settings == null in design mode for some reason
            var target = Settings == null ? PriceTarget.All : Settings.PriceTarget;
            return _priceToString.Convert( meal.GetPrice( target ) );
        }

        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            throw new NotSupportedException();
        }
    }

    /// <summary>
    /// Converts meals to images of their main type.
    /// </summary>
    public sealed class MealToImageConverter : ValueConverter<Meal, ImageSource>
    {
        private EnumToImageSourceConverter _converter = new EnumToImageSourceConverter();

        public override ImageSource Convert( Meal value )
        {
            MealType type = value.MealTypes.OrderByDescending( x => x ).First();
            return _converter.Convert( type );
        }
    }

    /// <summary>
    /// HACK: Allows Restaurants to be used with LongListSelector.
    /// </summary>
    public sealed class RestaurantsToGroupsConverter : ValueConverter<Restaurant[], RestaurantAsGroup[]>
    {
        public override RestaurantAsGroup[] Convert( Restaurant[] value )
        {
            if ( value == null )
            {
                return null;
            }

            return value.Select( r => new RestaurantAsGroup( r ) ).ToArray();
        }
    }
}
{'fraction_non_alphanumeric': 0.09556747600225861, 'fraction_numerical': 0.003246753246753247, 'mean_word_length': 2.3014911463187326, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2642197', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1743, 'n_words': 543}