diff --git a/FoliCon/FoliCon.csproj b/FoliCon/FoliCon.csproj index bd5244d..a20cc39 100644 --- a/FoliCon/FoliCon.csproj +++ b/FoliCon/FoliCon.csproj @@ -33,6 +33,7 @@ dineshsolanki.github.io/folicon/ + diff --git a/FoliCon/Modules/Extension/Extensions.cs b/FoliCon/Modules/Extension/Extensions.cs index ae47e43..28cf8a2 100644 --- a/FoliCon/Modules/Extension/Extensions.cs +++ b/FoliCon/Modules/Extension/Extensions.cs @@ -36,15 +36,13 @@ public static ObservableCollection ToObservableCollection(this IEnumerable { return new ObservableCollection(col); } - public static Task GetBitmap(this HttpResponseMessage responseMessage) + public static async Task GetBitmap(this HttpResponseMessage responseMessage) { Logger.Trace("GetBitmap from HttpResponseMessage"); - return responseMessage.Content.ReadAsStreamAsync().ContinueWith(t => - { - Bitmap bitmap = new(t.Result); - Logger.Trace("GetBitmap from HttpResponseMessage - done"); - return bitmap; - }); + var stream = await responseMessage.Content.ReadAsStreamAsync(); + var bitmap = new Bitmap(stream); + Logger.Trace("GetBitmap from HttpResponseMessage - done"); + return bitmap; } public static void SentryConfig(this LoggingConfiguration config, bool enableSentry) diff --git a/FoliCon/ViewModels/SearchResultViewModel.cs b/FoliCon/ViewModels/SearchResultViewModel.cs index 425af71..9e359dc 100644 --- a/FoliCon/ViewModels/SearchResultViewModel.cs +++ b/FoliCon/ViewModels/SearchResultViewModel.cs @@ -389,51 +389,58 @@ private async Task GetGameTrailer(string itemId) private async Task GetMovieTrailer(string itemId) { - await _tmdbObject.GetClient().GetMovieVideosAsync(itemId.ConvertToInt()).ContinueWith(item => + try { - if (item.Result == null) return; - var i = ChooseTrailer(item.Result.Results); - if (i != null) - { - SetTrailer(i.Key); - } - else + var result = await _tmdbObject.GetClient().GetMovieVideosAsync(itemId.ConvertToInt()); + + if (result != null) { - Logger.Warn("No trailer found for {Title}", ResultListViewData.SelectedItem.Title); + var trailer = ChooseTrailer(result.Results); + if (trailer != null) + { + SetTrailer(trailer.Key); + } + else + { + Logger.Warn("No trailer found for {Title}", ResultListViewData.SelectedItem.Title); + } } - }); + } + catch (Exception ex) + { + Logger.Error(ex, "Error fetching movie trailer for {Title}", ResultListViewData.SelectedItem.Title); + } } private async Task GetTvTrailer(string itemId) { - await _tmdbObject.GetClient().GetTvShowVideosAsync(itemId.ConvertToInt()).ContinueWith(item => + var result = await _tmdbObject.GetClient().GetTvShowVideosAsync(itemId.ConvertToInt()); + + if (result == null) return; + + var i = ChooseTrailer(result.Results); + + if (i != null) { - if (item.Result == null) return; - var i = ChooseTrailer(item.Result.Results); - if (i != null) - { - SetTrailer(i.Key); - } - else - { - Logger.Warn("No trailer found for {Title}", ResultListViewData.SelectedItem.Title); - } - }); + SetTrailer(i.Key); + } + else + { + Logger.Warn("No trailer found for {Title}", ResultListViewData.SelectedItem.Title); + } } private dynamic? ChooseTrailer(IReadOnlyCollection results) { - if (!results.Any()) return null; - return results.Any(i => i.Type == "Trailer" && i.Site == "YouTube") - ? results.First(i => i.Type == "Trailer") - : results.First(); + var trailerYouTube = results.FirstOrDefault(item => item?.Type == "Trailer" && item?.Site == "YouTube"); + return trailerYouTube != null ? trailerYouTube : results.FirstOrDefault(); } private void SetTrailer(string trailerKey) { ResultListViewData.SelectedItem.TrailerKey = trailerKey; ResultListViewData.SelectedItem.Trailer = - new Uri("https://www.youtube.com/embed/" + trailerKey); + new Uri($"https://www.youtube.com/embed/{trailerKey}"); Logger.Debug("Trailer for {Title} is {Trailer}", ResultListViewData.SelectedItem.Title, ResultListViewData.SelectedItem.Trailer); } diff --git a/FoliCon/Views/HtmlBox.xaml b/FoliCon/Views/HtmlBox.xaml index 6363cdb..afc8438 100644 --- a/FoliCon/Views/HtmlBox.xaml +++ b/FoliCon/Views/HtmlBox.xaml @@ -3,6 +3,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf" mc:Ignorable="d" Background="{DynamicResource RegionBrush}"> - + diff --git a/FoliCon/Views/HtmlBox.xaml.cs b/FoliCon/Views/HtmlBox.xaml.cs index 3ab7532..dd5cd65 100644 --- a/FoliCon/Views/HtmlBox.xaml.cs +++ b/FoliCon/Views/HtmlBox.xaml.cs @@ -5,7 +5,7 @@ namespace FoliCon.Views; /// /// Interaction logic for HtmlBox.xaml /// -public partial class HtmlBox : UserControl +public partial class HtmlBox { private const string VideoUnavailable = "Video not available!"; private readonly string _backgroundColor; @@ -17,12 +17,12 @@ public static readonly DependencyProperty HtmlTextProperty typeof(HtmlBox), new PropertyMetadata(default(string), OnHtmlTextPropertyChanged)); - public bool IsVideoAvailable => !string.IsNullOrEmpty(HtmlText) && !HtmlText.Contains(VideoUnavailable); + public bool IsVideoAvailable { get; private set; } public HtmlBox() { InitializeComponent(); - _backgroundColor = ThemeManager.Current.ApplicationTheme == ApplicationTheme.Dark ? "#000000" : "#FFFFFF"; + _backgroundColor = ThemeManager.Current.ApplicationTheme == ApplicationTheme.Dark ? "#1C1C1C" : "#FFFFFF"; } public string HtmlText @@ -31,74 +31,87 @@ public string HtmlText set => SetValue(HtmlTextProperty, value); } - private void ProcessBrowse() + private async Task ProcessBrowse() { if (Browser is not {IsLoaded: true}) return; + + var content = !IsVideoAvailable + ? $"""""" + : GenerateHtmlContent(); - if (!IsVideoAvailable) - { - Browser.NavigateToString($""""""); - return; - } - var content = GenerateHtmlContent(); - Browser.NavigateToString(content); + await InitializeAsync(content); } private string GenerateHtmlContent() { - return $$""" - - - - - - - - - - - - - - """; + return string.Format(HtmlTemplate, LangProvider.Culture.TwoLetterISOLanguageName, _backgroundColor, HtmlText); } - private static void OnHtmlTextPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) + private static async void OnHtmlTextPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) { - var control = source as HtmlBox; - if (control!.CheckAccess()) + var htmlText = e.NewValue as string; + if (source is not HtmlBox control) + { + return; + } + control.IsVideoAvailable = !string.IsNullOrEmpty(htmlText) && !htmlText.Contains(VideoUnavailable); + if (control.CheckAccess()) { - control.ProcessBrowse(); + await control.ProcessBrowse(); } else { - control.Dispatcher.Invoke(control.ProcessBrowse); + await control.Dispatcher.InvokeAsync(control.ProcessBrowse); } } + + private async Task InitializeAsync(string content) + { + await Browser.EnsureCoreWebView2Async(null); + Browser.DefaultBackgroundColor = ColorTranslator.FromHtml(_backgroundColor); + Browser.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false; + Browser.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false; + Browser.CoreWebView2.NavigateToString(content); + } + + private const string HtmlTemplate = """ + + + + + + + + + + + + + """; } \ No newline at end of file diff --git a/Folicon.sln.DotSettings b/Folicon.sln.DotSettings index 2de1ac6..0d04349 100644 --- a/Folicon.sln.DotSettings +++ b/Folicon.sln.DotSettings @@ -1,5 +1,6 @@  True + True True True True \ No newline at end of file