<commit_before>﻿using Jbe.NewsReader.Applications.ViewModels;
using Jbe.NewsReader.Applications.Views;
using System;
using System.Composition;
using System.Threading.Tasks;
using System.Waf.Applications;
using System.Windows.Input;
using Windows.ApplicationModel.DataTransfer;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace Jbe.NewsReader.Presentation.Views
{
    [Export(typeof(IFeedListView)), Shared]
    public sealed partial class FeedListView : UserControl, IFeedListView
    {
        private readonly Lazy<FeedListViewModel> viewModel;
        private readonly AsyncDelegateCommand pasteCommand;


        public FeedListView()
        {
            InitializeComponent();
            addFeedGrid.MaxWidth = Window.Current.CoreWindow.Bounds.Width - 24;  // Minus the margin defined by the Flyout
            viewModel = new Lazy<FeedListViewModel>(() => (FeedListViewModel)DataContext);
            pasteCommand = new AsyncDelegateCommand(PasteUriAsync, CanPasteUri);
            Clipboard.ContentChanged += ClipboardContentChanged;
        }

        
        public FeedListViewModel ViewModel => viewModel.Value;

        public ICommand PasteCommand => pasteCommand;


        public void FeedAdded()
        {
            addFeedButton.Flyout.Hide();
        }

        private void AddFeedFlyoutOpened(object sender, object e)
        {
            ViewModel.LoadErrorMessage = null;
            feedBox.Select(feedBox.Text.Length, 0);
            feedBox.Focus(FocusState.Programmatic);
        }

        private void AddNewFeedUriBoxKeyUp(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == VirtualKey.Enter)
            {
                ViewModel.AddNewFeedCommand.Execute(null);
            }
        }

        private void AddFeedFlyoutCloseAction(object sender, RoutedEventArgs e)
        {
            addFeedButton.Flyout.Hide();
        }

        private bool CanPasteUri()
        {
            return Clipboard.GetContent().Contains(StandardDataFormats.Text);
        }

        private async Task PasteUriAsync()
        {
            ViewModel.AddNewFeedUri = await Clipboard.GetContent().GetTextAsync();
        }

        private void ClipboardContentChanged(object sender, object e)
        {
            pasteCommand.RaiseCanExecuteChanged();
        }
    }
}<commit_msg>Fix crash because of a Clipboard issue when the debugger is not running<commit_after>﻿using Jbe.NewsReader.Applications.ViewModels;
using Jbe.NewsReader.Applications.Views;
using System;
using System.Composition;
using System.Threading.Tasks;
using System.Waf.Applications;
using System.Windows.Input;
using Windows.ApplicationModel.DataTransfer;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace Jbe.NewsReader.Presentation.Views
{
    [Export(typeof(IFeedListView)), Shared]
    public sealed partial class FeedListView : UserControl, IFeedListView
    {
        private readonly Lazy<FeedListViewModel> viewModel;
        private readonly AsyncDelegateCommand pasteCommand;


        public FeedListView()
        {
            InitializeComponent();
            addFeedGrid.MaxWidth = Window.Current.CoreWindow.Bounds.Width - 24;  // Minus the margin defined by the Flyout
            viewModel = new Lazy<FeedListViewModel>(() => (FeedListViewModel)DataContext);
            pasteCommand = new AsyncDelegateCommand(PasteUriAsync, CanPasteUri);
            Clipboard.ContentChanged += ClipboardContentChanged;
        }

        
        public FeedListViewModel ViewModel => viewModel.Value;

        public ICommand PasteCommand => pasteCommand;


        public void FeedAdded()
        {
            addFeedButton.Flyout.Hide();
        }

        private void AddFeedFlyoutOpened(object sender, object e)
        {
            ViewModel.LoadErrorMessage = null;
            feedBox.Select(feedBox.Text.Length, 0);
            feedBox.Focus(FocusState.Programmatic);
            pasteCommand.RaiseCanExecuteChanged();  // Manual update necessary because of the CanPasteUri workaround.
        }

        private void AddNewFeedUriBoxKeyUp(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == VirtualKey.Enter)
            {
                ViewModel.AddNewFeedCommand.Execute(null);
            }
        }

        private void AddFeedFlyoutCloseAction(object sender, RoutedEventArgs e)
        {
            addFeedButton.Flyout.Hide();
        }

        private bool CanPasteUri()
        {
            try
            {
                return Clipboard.GetContent().Contains(StandardDataFormats.Text);
            }
            catch (Exception ex)
            {
                // Workaround: The first call comes too early. An UnauthorizedAccessException is thrown but only when the debugger is not running.
                return false;
            }
        }

        private async Task PasteUriAsync()
        {
            ViewModel.AddNewFeedUri = await Clipboard.GetContent().GetTextAsync();
        }

        private void ClipboardContentChanged(object sender, object e)
        {
            pasteCommand.RaiseCanExecuteChanged();
        }
    }
}
{'fraction_non_alphanumeric': 0.07819634703196347, 'fraction_numerical': 0.001141552511415525, 'mean_word_length': 2.3741976893453147, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '3435967', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1322, 'n_words': 296}