Skip to content

Commit

Permalink
â�refactor: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
K12f committed Aug 27, 2024
1 parent 4f6d28d commit dec845b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
27 changes: 15 additions & 12 deletions src/BlueCatKoKo.Ui/ViewModels/Pages/HomeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ [ObservableProperty] [Required(ErrorMessage = "缺少分享链接")]
// 是否已经下载
[ObservableProperty] private string _isDownload;

// 解析按钮状态,正在解析/下载时,禁止点击
[ObservableProperty] private bool _parsingBtnStatus;

// 是否下载音频,默认false
[ObservableProperty] private bool _isDownloadAudio;

Expand All @@ -51,22 +54,20 @@ [ObservableProperty] [Required(ErrorMessage = "缺少分享链接")]
// 视频是否已解析
[ObservableProperty] private string _isParsed;

// 视频是否已解析
[ObservableProperty] private bool _isParsing;


public HomeViewModel(IMessenger messenger, ILogger logger, DouyinDownloaderService douyinDownloaderService,
IOptions<AppConfig> appConfig)
{
Messenger = messenger;
IsActive = true;

IsParsed = "Hidden";
IsParsing = false;

IsDownloadAudio = false;
IsDownloadVideo = true;

IsDownload = "Hidden";
ParsingBtnStatus = true;


_logger = logger;
_douyinDownloaderService = douyinDownloaderService;
Expand All @@ -75,6 +76,7 @@ public HomeViewModel(IMessenger messenger, ILogger logger, DouyinDownloaderServi

LibVlc = new LibVLC();
MediaPlayer = new MediaPlayer(LibVlc);
MediaPlayer.EnableMouseInput = true;
//通过设置宽高比为窗体宽高可达到视频铺满全屏的效果
MediaPlayer.AspectRatio = MediaPlayerWidth + ":" + MediaPlayerHeight;
}
Expand All @@ -88,8 +90,7 @@ public HomeViewModel(IMessenger messenger, ILogger logger, DouyinDownloaderServi
private async Task Parse()
{
ValidateAllProperties();
IsParsing = true;

ParsingBtnStatus = false;
string message = "解析成功~";
DownloaderEnum type = DownloaderEnum.Success;

Expand Down Expand Up @@ -142,9 +143,6 @@ private async Task Parse()
{
MediaPlayer.Play(media);
}

IsParsed = "Visible";
IsParsing = false;
}
catch (Exception ex)
{
Expand All @@ -153,6 +151,8 @@ private async Task Parse()
}
finally
{
ParsingBtnStatus = true;
IsParsed = "Visible";
DownloaderMessage downloadMessage = new(type, message, DownloadUrlText);
Messenger.Send(new ValueChangedMessage<DownloaderMessage>(downloadMessage));
}
Expand All @@ -162,6 +162,8 @@ private async Task Parse()
private async Task DownloadAll()
{
IsDownload = "Visible";
ParsingBtnStatus = false;

string message = "下载中...";
DownloaderEnum type = DownloaderEnum.Success;
try
Expand All @@ -185,18 +187,19 @@ await _douyinDownloaderService.Download(Data.VideoUrl, _appConfig.Value.Download
}, (sender, e) =>
{
DownloadProcess = 100;
IsDownload = "hidden";
message = filename + "下载成功~";
_logger.Error($"Download completed! Status: {e.Error}");
});
}
catch (Exception ex)
{
type = DownloaderEnum.Warning;
message = ex.Message;
_logger.Error($"DownloadException: {message}");
}
finally
{
IsDownload = "hidden";
ParsingBtnStatus = true;
DownloaderMessage downloadMessage = new(type, message, DownloadUrlText);
Messenger.Send(new ValueChangedMessage<DownloaderMessage>(downloadMessage));
}
Expand Down
18 changes: 11 additions & 7 deletions src/BlueCatKoKo.Ui/Views/Pages/HomePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,29 @@
Style="{StaticResource TextBoxExtend}" />
</hc:Col>
<hc:Col Span="4">
<ToggleButton Style="{StaticResource ToggleButtonLoadingPrimary}" IsChecked="{Binding ViewModel.IsParsing,Mode=TwoWay}"
Content="解析链接" Command="{Binding ViewModel.ParseCommand}" Margin="5"/>
<Button Style="{StaticResource ButtonPrimary}" Content="解析链接" Command="{Binding ViewModel.ParseCommand}"
IsEnabled="{Binding ViewModel.ParsingBtnStatus}" Margin="5" />
</hc:Col>
</hc:Row>
<hc:Row>
<hc:Col Span="20">
<hc:UniformSpacingPanel Orientation="Vertical">
<ProgressBar Style="{StaticResource ProgressBarInfo}" Visibility="{Binding ViewModel.IsDownload}" Value="{Binding ViewModel.DownloadProcess}" Maximum="100"/>
<hc:UniformSpacingPanel Orientation="Vertical">
<ProgressBar Style="{StaticResource ProgressBarInfo}" Visibility="{Binding ViewModel.IsDownload}"
Value="{Binding ViewModel.DownloadProcess}" Maximum="100" />
</hc:UniformSpacingPanel>
</hc:Col>
</hc:Row>
<hc:Row Margin="0,20,0,0" Visibility="{Binding ViewModel.IsParsed}">
<hc:Col Span="10" Margin="8">
<hc:UniformSpacingPanel Orientation="Vertical">
<vlc:VideoView MediaPlayer="{Binding ViewModel.MediaPlayer, Mode=TwoWay}"
Width="{Binding ViewModel.MediaPlayerWidth}" Height="{Binding ViewModel.MediaPlayerHeight}" Background="Gainsboro" />
<vlc:VideoView MediaPlayer="{Binding ViewModel.MediaPlayer, Mode=OneWay}"
IsEnabled="False"
Width="{Binding ViewModel.MediaPlayerWidth}" Height="{Binding ViewModel.MediaPlayerHeight}"
Background="Gainsboro" />
<!-- <CheckBox Margin="0,16,0,0" Content="视频" IsChecked="{Binding ViewModel.IsDownloadVideo}"/> -->
<!-- <CheckBox Margin="0,16,0,0" Content="音频" IsChecked="{Binding ViewModel.IsDownloadAudio}"/> -->
<Button Content="一键下载" Command="{Binding ViewModel.DownloadAllCommand}" Style="{StaticResource ButtonSuccess}" />
<Button Content="一键下载" Command="{Binding ViewModel.DownloadAllCommand}"
Style="{StaticResource ButtonSuccess}" />
</hc:UniformSpacingPanel>
</hc:Col>
<hc:Col Span="10" Margin="8">
Expand Down
1 change: 1 addition & 0 deletions src/BlueCatKoKo.Ui/Views/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows.Controls;
using System.Windows.Input;

using BlueCatKoKo.Ui.ViewModels.Pages;

Expand Down

0 comments on commit dec845b

Please sign in to comment.