Skip to content

Commit

Permalink
You can now open a video in your browser by clicking the thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
schneidermanuel committed Apr 3, 2024
1 parent 02a6ded commit 63caac6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
53 changes: 38 additions & 15 deletions TwitchLeecher/TwitchLeecher.Gui/ViewModels/SearchResultViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using TwitchLeecher.Core.Events;
using TwitchLeecher.Core.Models;
using TwitchLeecher.Gui.Interfaces;
Expand All @@ -12,7 +13,7 @@

namespace TwitchLeecher.Gui.ViewModels
{
public class SearchResultViewModel : ViewModelBase, INavigationState
public partial class SearchResultViewModel : ViewModelBase, INavigationState
{
#region Fields

Expand Down Expand Up @@ -71,10 +72,7 @@ public SearchResultViewModel(

public ObservableCollection<TwitchVideo> Videos
{
get
{
return _searchService.Videos;
}
get { return _searchService.Videos; }
}

public ICommand ViewCommand
Expand Down Expand Up @@ -187,31 +185,41 @@ private void DownloadVideo(string id)
{
if (_isAuthenticatedSubOnly)
{
_dialogService.ShowMessageBox("This video is sub-only but you are not subscribed to the channel!", "Download", MessageBoxButton.OK, MessageBoxImage.Exclamation);
_dialogService.ShowMessageBox(
"This video is sub-only but you are not subscribed to the channel!", "Download",
MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
else
{
_dialogService.ShowMessageBox("This video is sub-only! You need to enable sub-only video download support first!", "Download", MessageBoxButton.OK, MessageBoxImage.Exclamation);
_dialogService.ShowMessageBox(
"This video is sub-only! You need to enable sub-only video download support first!",
"Download", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}

return;
}

Dictionary<TwitchVideoQuality, string> playlistInfo = _apiService.GetPlaylistInfo(id, vodAuthInfo);
Dictionary<TwitchVideoQuality, string> playlistInfo =
_apiService.GetPlaylistInfo(id, vodAuthInfo);
List<TwitchVideoQuality> qualities = playlistInfo.Keys.OrderBy(q => q).ToList();

Preferences currentPrefs = _preferencesService.CurrentPreferences.Clone();

TwitchVideoQuality selectedQuality = GetSelectedQuality(qualities, currentPrefs.DownloadDefaultQuality);
TwitchVideoQuality selectedQuality =
GetSelectedQuality(qualities, currentPrefs.DownloadDefaultQuality);

string folder = currentPrefs.DownloadSubfoldersForFav && _preferencesService.IsChannelInFavourites(video.Channel)
string folder = currentPrefs.DownloadSubfoldersForFav &&
_preferencesService.IsChannelInFavourites(video.Channel)
? Path.Combine(currentPrefs.DownloadFolder, video.Channel)
: currentPrefs.DownloadFolder;

string filename = _filenameService.SubstituteWildcards(currentPrefs.DownloadFileName, video, selectedQuality);
filename = _filenameService.EnsureExtension(filename, currentPrefs.DownloadDisableConversion);
string filename = _filenameService.SubstituteWildcards(currentPrefs.DownloadFileName, video,
selectedQuality);
filename = _filenameService.EnsureExtension(filename,
currentPrefs.DownloadDisableConversion);

DownloadParameters downloadParams = new DownloadParameters(video, qualities, selectedQuality, folder, filename, currentPrefs.DownloadDisableConversion);
DownloadParameters downloadParams = new DownloadParameters(video, qualities,
selectedQuality, folder, filename, currentPrefs.DownloadDisableConversion);

if (video.StartTime.HasValue)
{
Expand All @@ -229,6 +237,17 @@ private void DownloadVideo(string id)
}
}

[RelayCommand]
private void OpenInBrowser(Uri url)
{
var psi = new ProcessStartInfo($"https://twitch.tv" + url.AbsolutePath)
{
UseShellExecute = true,
Verb = "open"
};
Process.Start(psi);
}

private TwitchVideoQuality GetSelectedQuality(List<TwitchVideoQuality> qualities, DefaultQuality defaultQuality)
{
TwitchVideoQuality sourceQuality = qualities.Find(q => q.IsSource);
Expand Down Expand Up @@ -265,7 +284,9 @@ private TwitchVideoQuality GetSelectedQuality(List<TwitchVideoQuality> qualities

foreach (TwitchVideoQuality quality in visualQualities)
{
if (quality.VerticalResolution <= defaultRes && (selectedQuality == null || selectedQuality.VerticalResolution < quality.VerticalResolution))
if (quality.VerticalResolution <= defaultRes && (selectedQuality == null ||
selectedQuality.VerticalResolution <
quality.VerticalResolution))
{
selectedQuality = quality;
}
Expand All @@ -278,7 +299,9 @@ private TwitchVideoQuality GetSelectedQuality(List<TwitchVideoQuality> qualities

foreach (TwitchVideoQuality quality in visualQualities)
{
if (quality.VerticalResolution >= defaultRes && (selectedQuality == null || selectedQuality.VerticalResolution > quality.VerticalResolution))
if (quality.VerticalResolution >= defaultRes && (selectedQuality == null ||
selectedQuality.VerticalResolution >
quality.VerticalResolution))
{
selectedQuality = quality;
}
Expand Down
8 changes: 6 additions & 2 deletions TwitchLeecher/TwitchLeecher.Gui/Views/SearchResultView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@
</Grid>
<Grid Cursor="Hand">
<Canvas>
<Image Source="{Binding Thumbnail^}"
Stretch="Fill" />
<Button
Command="{Binding $parent[UserControl].((viewModels:SearchResultViewModel)DataContext).OpenInBrowserCommand}"
CommandParameter="{Binding Url}">
<Image Source="{Binding Thumbnail^}"
Stretch="Fill" />
</Button>
</Canvas>
<Border IsVisible="{Binding Live}"
Background="#FFEB0400" HorizontalAlignment="Left" VerticalAlignment="Top"
Expand Down

0 comments on commit 63caac6

Please sign in to comment.