<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.Threading;
using System.Threading.Tasks;
using PocketCampus.Common.Services;
using PocketCampus.Events.Models;
using PocketCampus.Events.Services;
using PocketCampus.Mvvm;
using PocketCampus.Mvvm.Logging;

namespace PocketCampus.Events.ViewModels
{
    /// <summary>
    /// ViewModel for item details.
    /// </summary>
    [LogId( "/events/item" )]
    public sealed class EventItemViewModel : DataViewModel<long>
    {
        private readonly INavigationService _navigationService;
        private readonly IBrowserService _browserService;
        private readonly IEventsService _eventsService;
        private readonly IPluginSettings _settings;
        private readonly long _itemId;

        private EventItem _item;
        private EventPool[] _pools;
        private bool _isFavorite;


        /// <summary>
        /// Gets the item.
        /// </summary>
        public EventItem Item
        {
            get { return _item; }
            private set { SetProperty( ref _item, value ); }
        }

        /// <summary>
        /// Gets the item's child pools, if any.
        /// </summary>
        public EventPool[] Pools
        {
            get { return _pools; }
            private set { SetProperty( ref _pools, value ); }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the item is a favorite of the user.
        /// </summary>
        public bool IsFavorite
        {
            get { return _isFavorite; }
            set { SetProperty( ref _isFavorite, value ); }
        }


        /// <summary>
        /// Gets the command executed to view a child pool.
        /// </summary>
        [LogId( "ShowPool" )]
        [LogParameter( "$Param.LogId" )]
        public Command<EventPool> ViewPoolCommand
        {
            get
            {
                return GetCommand<EventPool>( pool =>
                {
                    if ( pool.OverrideTargetUrl == null )
                    {
                        _navigationService.NavigateTo<EventPoolViewModel, ViewPoolRequest>( new ViewPoolRequest( pool.Id ) );
                    }
                    else
                    {
                        _browserService.NavigateTo( pool.OverrideTargetUrl );
                    }
                } );
            }
        }

        /// <summary>
        /// Gets the command executed to open a link.
        /// </summary>
        [LogId( "OpenLink" )]
        public Command<string> OpenLinkCommand
        {
            get { return GetCommand<string>( _browserService.NavigateTo ); }
        }


        /// <summary>
        /// Creates a new EventItemViewModel.
        /// </summary>
        public EventItemViewModel( INavigationService navigationService, IBrowserService browserService,
                                   IEventsService eventsService, IPluginSettings settings,
                                   long itemId )
        {
            _navigationService = navigationService;
            _browserService = browserService;
            _eventsService = eventsService;
            _settings = settings;
            _itemId = itemId;
        }


        /// <summary>
        /// Refreshes the data.
        /// </summary>
        protected override async Task RefreshAsync( CancellationToken token, bool force )
        {
            if ( force )
            {
                var request = new EventItemRequest
                {
                    ItemId = _itemId,
                    UserTickets = _settings.UserTickets.ToArray(),
                    Language = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName
                };

                var response = await _eventsService.GetEventItemAsync( request );

                if ( response.Status != EventsStatus.Success )
                {
                    throw new Exception( "An error occured while fetching an event item." );
                }

                _settings.EventCategories = response.EventCategories;
                _settings.EventTags = response.EventTags;

                Pools = response.ChildrenPools == null ? new EventPool[0] : response.ChildrenPools.Values.ToArray();
                Item = response.Item;
                IsFavorite = _settings.FavoriteItemIds.Contains( Item.Id );
            }
        }


        /// <summary>
        /// Called when the user navigates away from the ViewModel.
        /// </summary>
        public override void OnNavigatedFrom()
        {
            if ( Item == null )
            {
                return;
            }

            if ( IsFavorite && !_settings.FavoriteItemIds.Contains( Item.Id ) )
            {
                _settings.FavoriteItemIds.Add( Item.Id );
            }
            else if ( !IsFavorite && _settings.FavoriteItemIds.Contains( Item.Id ) )
            {
                _settings.FavoriteItemIds.Remove( Item.Id );<commit_msg>[events.wp] Sort child pools by ID (fixes #152)<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.Threading;
using System.Threading.Tasks;
using PocketCampus.Common.Services;
using PocketCampus.Events.Models;
using PocketCampus.Events.Services;
using PocketCampus.Mvvm;
using PocketCampus.Mvvm.Logging;

namespace PocketCampus.Events.ViewModels
{
    /// <summary>
    /// ViewModel for item details.
    /// </summary>
    [LogId( "/events/item" )]
    public sealed class EventItemViewModel : DataViewModel<long>
    {
        private readonly INavigationService _navigationService;
        private readonly IBrowserService _browserService;
        private readonly IEventsService _eventsService;
        private readonly IPluginSettings _settings;
        private readonly long _itemId;

        private EventItem _item;
        private EventPool[] _pools;
        private bool _isFavorite;


        /// <summary>
        /// Gets the item.
        /// </summary>
        public EventItem Item
        {
            get { return _item; }
            private set { SetProperty( ref _item, value ); }
        }

        /// <summary>
        /// Gets the item's child pools, if any.
        /// </summary>
        public EventPool[] Pools
        {
            get { return _pools; }
            private set { SetProperty( ref _pools, value ); }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the item is a favorite of the user.
        /// </summary>
        public bool IsFavorite
        {
            get { return _isFavorite; }
            set { SetProperty( ref _isFavorite, value ); }
        }


        /// <summary>
        /// Gets the command executed to view a child pool.
        /// </summary>
        [LogId( "ShowPool" )]
        [LogParameter( "$Param.LogId" )]
        public Command<EventPool> ViewPoolCommand
        {
            get
            {
                return GetCommand<EventPool>( pool =>
                {
                    if ( pool.OverrideTargetUrl == null )
                    {
                        _navigationService.NavigateTo<EventPoolViewModel, ViewPoolRequest>( new ViewPoolRequest( pool.Id ) );
                    }
                    else
                    {
                        _browserService.NavigateTo( pool.OverrideTargetUrl );
                    }
                } );
            }
        }

        /// <summary>
        /// Gets the command executed to open a link.
        /// </summary>
        [LogId( "OpenLink" )]
        public Command<string> OpenLinkCommand
        {
            get { return GetCommand<string>( _browserService.NavigateTo ); }
        }


        /// <summary>
        /// Creates a new EventItemViewModel.
        /// </summary>
        public EventItemViewModel( INavigationService navigationService, IBrowserService browserService,
                                   IEventsService eventsService, IPluginSettings settings,
                                   long itemId )
        {
            _navigationService = navigationService;
            _browserService = browserService;
            _eventsService = eventsService;
            _settings = settings;
            _itemId = itemId;
        }


        /// <summary>
        /// Refreshes the data.
        /// </summary>
        protected override async Task RefreshAsync( CancellationToken token, bool force )
        {
            if ( force )
            {
                var request = new EventItemRequest
                {
                    ItemId = _itemId,
                    UserTickets = _settings.UserTickets.ToArray(),
                    Language = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName
                };

                var response = await _eventsService.GetEventItemAsync( request );

                if ( response.Status != EventsStatus.Success )
                {
                    throw new Exception( "An error occured while fetching an event item." );
                }

                _settings.EventCategories = response.EventCategories;
                _settings.EventTags = response.EventTags;

                var pools = response.ChildrenPools == null ? Enumerable.Empty<EventPool>() : response.ChildrenPools.Values;
                Pools = pools.OrderBy( p => p.Id ).ToArray();
                Item = response.Item;
                IsFavorite = _settings.FavoriteItemIds.Contains( Item.Id );
            }
        }


        /// <summary>
        /// Called when the user navigates away from the ViewModel.
        /// </summary>
        public override void OnNavigatedFrom()
        {
            if ( Item == null )
            {
                return;
            }

            if ( IsFavorite && !_settings.FavoriteItemIds.Contains( Item.Id ) )
            {
                _settings.FavoriteItemIds.Add( Item.Id );
            }
            else if ( !IsFavorite && _settings.FavoriteItemIds.Contains( Item.Id ) )
            {
                _settings.FavoriteItemIds.Remove( Item.Id );
{'fraction_non_alphanumeric': 0.08851884312007012, 'fraction_numerical': 0.0011685655857434998, 'mean_word_length': 1.4933236222384074, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3870498', 'n_tokens_mistral': 2605, 'n_tokens_neox': 2467, 'n_words': 644}