Skip to content

Commit

Permalink
Feature: Add option to View all available videos for a media in poste…
Browse files Browse the repository at this point in the history
…r mode instead of default one earlier (#216)

* dummy: Add Video selector to HtmlBox control

Introduced a new Video model and integrated a ComboBox for video selection in the HtmlBox control. Updated the HtmlBox.cs to handle video selection and loaded videos dynamically.

* feature: Add support for displaying multiple videos

Introduced a new Videos property in ListItem to handle a collection of videos. Updated SearchResultViewModel to set this property and modified SearchResult and HtmlBox views to bind and display these videos.

* fix: Rename and refactor Video model to MediaVideo

Renamed `Video` to `MediaVideo` and updated its properties. Adjusted references throughout the project, ensuring consistent naming and functionality improvements. Removed redundant trailer selection logic and optimized video processing in the SearchResultViewModel and HtmlBox components.

* refactor: Remove unused method from SearchResultViewModel

Removed the SetTrailer method from SearchResultViewModel as it was not being used. Also cleaned up unnecessary using statement in HtmlBox.xaml.cs.
  • Loading branch information
DineshSolanki authored Aug 2, 2024
1 parent d019959 commit daa93ad
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 38 deletions.
9 changes: 8 additions & 1 deletion FoliCon/Models/Data/ListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class ListItem : BindableBase
private Uri _trailerLink;
private string _id;
private MediaType _mediaType = MediaType.Unknown;

private ICollection<MediaVideo> _videos;

private string _initialPoster;
private bool _isInitialSet;
Expand Down Expand Up @@ -79,7 +81,12 @@ public string TrailerKey
get => _trailerKey;
set => SetProperty(ref _trailerKey, value);
}


public ICollection<MediaVideo> Videos
{
get => _videos;
set => SetProperty(ref _videos, value);
}
public string Id
{
get => _id;
Expand Down
3 changes: 3 additions & 0 deletions FoliCon/Models/Data/MediaVideo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace FoliCon.Models.Data;

public record MediaVideo(string Name, string Url);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
{
if (value is string text)
{
return text == "Video not available!" ? Visibility.Visible : Visibility.Hidden;
return text is "" or "Video not available!" ? Visibility.Visible : Visibility.Hidden;
}

return Visibility.Hidden;
Expand Down
2 changes: 1 addition & 1 deletion FoliCon/Modules/Convertor/TextToVisibilityConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
{
if (value is string text)
{
return text == "Video not available!" ? Visibility.Hidden : Visibility.Visible;
return text is "" or "Video not available!" ? Visibility.Hidden : Visibility.Visible;
}

return Visibility.Hidden;
Expand Down
72 changes: 42 additions & 30 deletions FoliCon/ViewModels/SearchResultViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace FoliCon.ViewModels;
public class SearchResultViewModel : BindableBase, IDialogAware
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private const string YoutubeEmbedUrl = "https://www.youtube.com/embed/{0}";

#region Variables

private string _title = Lang.SearchResult;
Expand Down Expand Up @@ -434,10 +436,9 @@ private async Task GetGameTrailer(string itemId)
{
return;
}

var key = r.First(v => v.Name == "Trailer");
SetTrailer(key.VideoId);
SetVideos(r);
}


private async Task GetMovieTrailer(string itemId)
{
Expand All @@ -447,15 +448,7 @@ private async Task GetMovieTrailer(string itemId)

if (result != null)
{
var trailer = ChooseTrailer(result.Results);
if (trailer != null)
{
SetTrailer(trailer.Key);
}
else
{
Logger.Warn("No trailer found for {Title}", ResultListViewData.SelectedItem.Title);
}
SetVideos(result.Results);
}
}
catch (Exception ex)
Expand All @@ -472,32 +465,51 @@ private async Task GetTvTrailer(string itemId)
{
return;
}
SetVideos(result.Results);
}

var i = ChooseTrailer(result.Results);

if (i != null)
private static Video ChooseTrailer(IReadOnlyCollection<Video> results)
{
var trailerYouTube = results.FirstOrDefault(item => item is { Type: "Trailer", Site: "YouTube" });
return trailerYouTube ?? results.FirstOrDefault();
}

private void SetVideos(List<Video> videos)
{
if (videos == null || videos.Count == 0)
{
SetTrailer(i.Key);
return;
}
else

var trailer = ChooseTrailer(videos);
var videoList = new List<MediaVideo>
{
Logger.Warn("No trailer found for {Title}", ResultListViewData.SelectedItem.Title);
}
}
new(trailer.Name, YoutubeEmbedUrl.Format(trailer.Key))
};

private static dynamic ChooseTrailer(IReadOnlyCollection<dynamic> results)
{
var trailerYouTube = results.FirstOrDefault(item => item?.Type == "Trailer" && item.Site == "YouTube");
return trailerYouTube != null ? trailerYouTube : results.FirstOrDefault();
videoList.AddRange(
videos.Where(video => video != trailer)
.Select(video => new MediaVideo(video.Name, YoutubeEmbedUrl.Format(video.Key)))
);

ResultListViewData.SelectedItem.Videos = videoList;
}

private void SetTrailer(string trailerKey)
private void SetVideos(GameVideo[] videos)
{
ResultListViewData.SelectedItem.TrailerKey = trailerKey;
ResultListViewData.SelectedItem.Trailer =
new Uri($"https://www.youtube.com/embed/{trailerKey}");
Logger.Debug("Trailer for {Title} is {Trailer}", ResultListViewData.SelectedItem.Title,
ResultListViewData.SelectedItem.Trailer);
var trailer = videos.First(v => v.Name == "Trailer");

var videoList = new List<MediaVideo>
{
new(trailer.Name, YoutubeEmbedUrl.Format(trailer.VideoId))
};

videoList.AddRange(
videos.Where(video => video != trailer)
.Select(video => new MediaVideo(video.Name, YoutubeEmbedUrl.Format(video.VideoId)))
);

ResultListViewData.SelectedItem.Videos = videoList;
}
private void ResetPoster()
{
Expand Down
12 changes: 11 additions & 1 deletion FoliCon/Views/HtmlBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
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"
xmlns:hc="https://handyorg.github.io/handycontrol"
mc:Ignorable="d" Background="{DynamicResource RegionBrush}">
<wv2:WebView2 x:Name="Browser"/>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<hc:ComboBox Grid.Row="0" x:Name="VideoSelector" Margin="10" SelectionChanged="VideoSelector_OnSelectionChanged"
SelectedIndex="0">
</hc:ComboBox>
<wv2:WebView2 Grid.Row="1" x:Name="Browser"/>
</Grid>
</UserControl>
51 changes: 48 additions & 3 deletions FoliCon/Views/HtmlBox.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,33 @@ public static readonly DependencyProperty HtmlTextProperty
typeof(HtmlBox),
new PropertyMetadata(default(string), OnHtmlTextPropertyChanged));

public static readonly DependencyProperty VideosProperty
= DependencyProperty.Register(
nameof(Videos),
typeof(ICollection<MediaVideo>),
typeof(HtmlBox),
new PropertyMetadata(default(ICollection<MediaVideo>), OnVideosPropertyChanged));

private static async void OnVideosPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not HtmlBox control)
{
return;
}
await control.Dispatcher.InvokeAsync(() =>
{
control.VideoSelector.ItemsSource = e.NewValue as ICollection<MediaVideo>;
control.VideoSelector.SelectedIndex = 0;
});
}

private bool IsVideoAvailable { get; set; }

public HtmlBox()
{
InitializeComponent();
_backgroundColor = ThemeManager.Current.ApplicationTheme == ApplicationTheme.Dark ? DarkGray : White;
InitializeVideoSelector();
}

public string HtmlText
Expand All @@ -35,20 +56,30 @@ public string HtmlText
set => SetValue(HtmlTextProperty, value);
}

public ICollection<MediaVideo> Videos
{
get => (ICollection<MediaVideo>)GetValue(VideosProperty);
set => SetValue(VideosProperty, value);
}
private async Task ProcessBrowse()
{
if (Browser is not {IsLoaded: true})
{
return;
}

var content = !IsVideoAvailable
? $"""<html><body style="background-color: {_backgroundColor}"></body></html>"""
: GenerateHtmlContent();
var content = GenerateContentString();

await InitializeAsync(content);
}

private string GenerateContentString()
{
return !IsVideoAvailable
? $"""<html><body style="background-color: {_backgroundColor}"></body></html>"""
: GenerateHtmlContent();
}

private string GenerateHtmlContent()
{
return string.Format(HtmlTemplate, LangProvider.Culture.TwoLetterISOLanguageName, _backgroundColor, HtmlText);
Expand Down Expand Up @@ -122,4 +153,18 @@ private async Task InitializeAsync(string content)
</body>
</html>
""";

private async void VideoSelector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
HtmlText = VideoSelector.SelectedValue?.ToString() ?? string.Empty;
await ProcessBrowse();
}

private void InitializeVideoSelector()
{
VideoSelector.ItemsSource = Videos;
VideoSelector.SelectedValuePath = "Url";
VideoSelector.DisplayMemberPath = "Name";
VideoSelector.SelectedIndex = 0;
}
}
2 changes: 1 addition & 1 deletion FoliCon/Views/SearchResult.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
</hc:EventTrigger>
</hc:Interaction.Triggers>-->
</ListBox>
<views:HtmlBox x:Name="WebBox" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"
<views:HtmlBox x:Name="WebBox" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Videos="{Binding Path=SelectedItem.(data:ListItem.Videos), ElementName=ListViewResult}"
HtmlText="{Binding Path=SelectedItem.(data:ListItem.Trailer), ElementName=ListViewResult, TargetNullValue='Video not available!'}"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
Visibility="{Binding HtmlText, Converter={StaticResource TextToVisibilityConverter}, RelativeSource={RelativeSource Self}}"
Expand Down

0 comments on commit daa93ad

Please sign in to comment.