<commit_before>﻿using System;
using System.Threading.Tasks;
using Windows.Media.PlayTo;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Caliburn.Micro;
using PropertyChanged;
using XboxMediaRemote.App.Events;
using XboxMediaRemote.App.Resources;

namespace XboxMediaRemote.App.ViewModels
{
    [ImplementPropertyChanged]
    public class PlayToViewModel : ViewModelBase, IHandle<MediaSelectedEventArgs>
    {
        private readonly IEventAggregator eventAggregator;
        private PlayToManager playToManager;
        private PlayToConnection currentConnection;

        public PlayToViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            playToManager = PlayToManager.GetForCurrentView();

            playToManager.SourceRequested += OnSourceRequsted;
            playToManager.SourceSelected += OnSourceSelected;

            eventAggregator.Subscribe(this);
        }

        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);

            playToManager.SourceRequested -= OnSourceRequsted;
            playToManager.SourceSelected -= OnSourceSelected;

            eventAggregator.Unsubscribe(this);
        }

        private void OnSourceRequsted(PlayToManager sender, PlayToSourceRequestedEventArgs args)
        {
            var deferral = args.SourceRequest.GetDeferral();

            Execute.OnUIThread(async () =>
                {
                    if (CurrentFile != null)
                    {
                        var source = await CreateSourceAsync();

                        if (source == null)
                            args.SourceRequest.DisplayErrorString(Strings.PlayToInvalidMediaType);
                        else
                        {
                            args.SourceRequest.SetSource(source);

                            CurrentConnection = source.Connection;
                        }
                    }
                    else
                    {
                        args.SourceRequest.DisplayErrorString(Strings.PlayToNoFile);
                    }

                    deferral.Complete();
                });
        }

        private void OnSourceSelected(PlayToManager sender, PlayToSourceSelectedEventArgs args)
        {
            Execute.OnUIThread(() =>
            {
                SourceName = args.FriendlyName;

                SourceIcon = new BitmapImage();
                SourceIcon.SetSource(args.Icon);    
            });
        }

        private async Task<PlayToSource> CreateSourceAsync()
        {
            switch (CurrentFile.MediaType)
            {
                case MediaType.Picture:

                    var imageStream = await CurrentFile.File.OpenReadAsync();

                    var imageSource = new BitmapImage();

                    await imageSource.SetSourceAsync(imageStream);

                    var image = new Image
                                {
                                    Source = imageSource
                                };

                    return image.PlayToSource;
                case MediaType.Video:

                    var videoStream = await CurrentFile.File.OpenReadAsync();

                    var mediaElement = new MediaElement();

                    mediaElement.SetSource(videoStream, CurrentFile.File.ContentType);

                    return mediaElement.PlayToSource;

                default:
                    return null;
            }
        }

        public void Handle(MediaSelectedEventArgs message)
        {
            CurrentFile = message.StorageFile;<commit_msg>Sharing a video doesn't play audio in the background<commit_after>﻿using System;
using System.Threading.Tasks;
using Windows.Media.PlayTo;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Caliburn.Micro;
using PropertyChanged;
using XboxMediaRemote.App.Events;
using XboxMediaRemote.App.Resources;

namespace XboxMediaRemote.App.ViewModels
{
    [ImplementPropertyChanged]
    public class PlayToViewModel : ViewModelBase, IHandle<MediaSelectedEventArgs>
    {
        private readonly IEventAggregator eventAggregator;
        private PlayToManager playToManager;
        private PlayToConnection currentConnection;

        public PlayToViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            playToManager = PlayToManager.GetForCurrentView();

            playToManager.SourceRequested += OnSourceRequsted;
            playToManager.SourceSelected += OnSourceSelected;

            eventAggregator.Subscribe(this);
        }

        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);

            playToManager.SourceRequested -= OnSourceRequsted;
            playToManager.SourceSelected -= OnSourceSelected;

            eventAggregator.Unsubscribe(this);
        }

        private void OnSourceRequsted(PlayToManager sender, PlayToSourceRequestedEventArgs args)
        {
            var deferral = args.SourceRequest.GetDeferral();

            Execute.OnUIThread(async () =>
                {
                    if (CurrentFile != null)
                    {
                        var source = await CreateSourceAsync();

                        if (source == null)
                            args.SourceRequest.DisplayErrorString(Strings.PlayToInvalidMediaType);
                        else
                        {
                            args.SourceRequest.SetSource(source);

                            CurrentConnection = source.Connection;
                        }
                    }
                    else
                    {
                        args.SourceRequest.DisplayErrorString(Strings.PlayToNoFile);
                    }

                    deferral.Complete();
                });
        }

        private void OnSourceSelected(PlayToManager sender, PlayToSourceSelectedEventArgs args)
        {
            Execute.OnUIThread(() =>
            {
                SourceName = args.FriendlyName;

                SourceIcon = new BitmapImage();
                SourceIcon.SetSource(args.Icon);    
            });
        }

        private async Task<PlayToSource> CreateSourceAsync()
        {
            switch (CurrentFile.MediaType)
            {
                case MediaType.Picture:

                    var imageStream = await CurrentFile.File.OpenReadAsync();

                    var imageSource = new BitmapImage();

                    await imageSource.SetSourceAsync(imageStream);

                    var image = new Image
                                {
                                    Source = imageSource
                                };

                    return image.PlayToSource;
                case MediaType.Video:

                    var videoStream = await CurrentFile.File.OpenReadAsync();

                    var mediaElement = new MediaElement
                    {
                        AutoPlay = false
                    };

                    mediaElement.SetSource(videoStream, CurrentFile.File.ContentType);

                    return mediaElement.PlayToSource;

                default:
                    return null;
            }
        }

        public void Handle(MediaSelectedEventArgs message)
        {
            CurrentFile = message.StorageFile;
{'fraction_non_alphanumeric': 0.06479091378420238, 'fraction_numerical': 0.0, 'mean_word_length': 1.4804737516005122, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5100807', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1666, 'n_words': 321}