1010using System . Linq ;
1111using System . Reactive ;
1212using System . Reactive . Linq ;
13+ using System . Threading ;
14+ using System . Threading . Tasks ;
1315using YoutubeDl . Wpf . Models ;
1416using YoutubeDl . Wpf . Utils ;
1517
@@ -34,19 +36,26 @@ public class HomeViewModel : ReactiveValidationObject
3436
3537 public ObservableCollection < Preset > Presets { get ; } = new ( ) ;
3638
39+ /// <summary>
40+ /// Gets the output template history.
41+ /// This collection was first constructed from <see cref="ObservableSettings.OutputTemplateHistory"/> in reverse order.
42+ /// So the newest template is always the first element.
43+ /// </summary>
44+ public ObservableCollection < HistoryItemViewModel > OutputTemplateHistory { get ; }
45+
3746 /// <summary>
3847 /// Gets the download path history.
3948 /// This collection was first constructed from <see cref="ObservableSettings.DownloadPathHistory"/> in reverse order.
4049 /// So the newest path is always the first element.
4150 /// </summary>
42- public ObservableCollection < HistoryItemViewModel > DownloadPathHistory { get ; } = new ( ) ;
51+ public ObservableCollection < HistoryItemViewModel > DownloadPathHistory { get ; }
4352
4453 /// <summary>
4554 /// Gets the collection of view models of the arguments area.
4655 /// A view model in this collection must be of either
4756 /// <see cref="ArgumentChipViewModel"/> or <see cref="AddArgumentViewModel"/> type.
4857 /// </summary>
49- public ObservableCollection < object > DownloadArguments { get ; } = new ( ) ;
58+ public ObservableCollection < object > DownloadArguments { get ; }
5059
5160 [ Reactive ]
5261 public string Link { get ; set ; } = "" ;
@@ -113,10 +122,12 @@ public HomeViewModel(ObservableSettings settings, BackendService backendService,
113122 Presets . AddRange ( Preset . PredefinedPresets . Where ( x => ( x . SupportedBackends & SharedSettings . Backend ) == SharedSettings . Backend ) ) ;
114123 } ) ;
115124
116- DownloadPathHistory . AddRange ( SharedSettings . DownloadPathHistory . Select ( x => new HistoryItemViewModel ( x , DeleteDownloadPathItem ) ) . Reverse ( ) ) ;
117-
118- DownloadArguments . AddRange ( SharedSettings . BackendDownloadArguments . Select ( x => new ArgumentChipViewModel ( x , true , DeleteArgumentChip ) ) ) ;
119- DownloadArguments . Add ( new AddArgumentViewModel ( AddArgument ) ) ;
125+ OutputTemplateHistory = new ( SharedSettings . OutputTemplateHistory . Select ( x => new HistoryItemViewModel ( x , DeleteOutputTemplateItem ) ) . Reverse ( ) ) ;
126+ DownloadPathHistory = new ( SharedSettings . DownloadPathHistory . Select ( x => new HistoryItemViewModel ( x , DeleteDownloadPathItem ) ) . Reverse ( ) ) ;
127+ DownloadArguments = new ( SharedSettings . BackendDownloadArguments . Select ( x => new ArgumentChipViewModel ( x , true , DeleteArgumentChip ) ) )
128+ {
129+ new AddArgumentViewModel ( AddArgument ) ,
130+ } ;
120131
121132 var gdaA = this . WhenAnyValue (
122133 x => x . SharedSettings . Backend ,
@@ -164,15 +175,18 @@ public HomeViewModel(ObservableSettings settings, BackendService backendService,
164175
165176 var canRun = this . WhenAnyValue (
166177 x => x . Link ,
178+ x => x . BackendInstance . IsRunning ,
179+ x => x . SharedSettings . UseCustomOutputTemplate ,
167180 x => x . SharedSettings . UseCustomPath ,
181+ x => x . SharedSettings . CustomOutputTemplate ,
168182 x => x . SharedSettings . DownloadPath ,
169183 x => x . SharedSettings . BackendPath ,
170- x => x . BackendInstance . IsRunning ,
171- ( link , useCustomPath , downloadPath , dlBinaryPath , isRunning ) =>
184+ ( link , isRunning , useCustomOutputTemplate , useCustomPath , outputTemplate , downloadPath , backendPath ) =>
172185 ! string . IsNullOrEmpty ( link ) &&
186+ ! isRunning &&
187+ ( ! useCustomOutputTemplate || ! string . IsNullOrEmpty ( outputTemplate ) ) &&
173188 ( ! useCustomPath || Directory . Exists ( downloadPath ) ) &&
174- ! string . IsNullOrEmpty ( dlBinaryPath ) &&
175- ! isRunning ) ;
189+ ! string . IsNullOrEmpty ( backendPath ) ) ;
176190
177191 var canAbort = this . WhenAnyValue ( x => x . BackendInstance . IsRunning ) ;
178192
@@ -187,7 +201,7 @@ public HomeViewModel(ObservableSettings settings, BackendService backendService,
187201 ResetCustomFilenameTemplateCommand = ReactiveCommand . Create ( ResetCustomFilenameTemplate , canResetCustomFilenameTemplate ) ;
188202 BrowseDownloadFolderCommand = ReactiveCommand . Create ( BrowseDownloadFolder , canBrowseDownloadFolder ) ;
189203 OpenDownloadFolderCommand = ReactiveCommand . Create ( OpenDownloadFolder , canOpenDownloadFolder ) ;
190- StartDownloadCommand = ReactiveCommand . CreateFromTask < string > ( BackendInstance . StartDownloadAsync , canRun ) ;
204+ StartDownloadCommand = ReactiveCommand . CreateFromTask < string > ( StartDownloadAsync , canRun ) ;
191205 ListFormatsCommand = ReactiveCommand . CreateFromTask < string > ( BackendInstance . ListFormatsAsync , canRun ) ;
192206 AbortCommand = ReactiveCommand . CreateFromTask ( BackendInstance . AbortAsync , canAbort ) ;
193207
@@ -237,17 +251,29 @@ private void DeleteCustomPreset()
237251 SharedSettings . SelectedPreset = Presets . First ( ) ;
238252 }
239253
254+ private void DeleteOutputTemplateItem ( HistoryItemViewModel item )
255+ {
256+ SharedSettings . OutputTemplateHistory . Remove ( item . Text ) ;
257+ OutputTemplateHistory . Remove ( item ) ;
258+ }
259+
240260 private void DeleteDownloadPathItem ( HistoryItemViewModel item )
241261 {
242262 SharedSettings . DownloadPathHistory . Remove ( item . Text ) ;
243263 DownloadPathHistory . Remove ( item ) ;
244264 }
245265
266+ private void UpdateOutputTemplateHistory ( )
267+ {
268+ if ( ! SharedSettings . OutputTemplateHistory . Contains ( SharedSettings . CustomOutputTemplate ) )
269+ {
270+ SharedSettings . OutputTemplateHistory . Add ( SharedSettings . CustomOutputTemplate ) ;
271+ OutputTemplateHistory . Insert ( 0 , new ( SharedSettings . CustomOutputTemplate , DeleteOutputTemplateItem ) ) ;
272+ }
273+ }
274+
246275 private void UpdateDownloadPathHistory ( )
247276 {
248- // No need to check if path is null or empty.
249- // Because this code path can only be reached
250- // when custom path is toggled and a valid path is supplied.
251277 if ( ! SharedSettings . DownloadPathHistory . Contains ( SharedSettings . DownloadPath ) )
252278 {
253279 SharedSettings . DownloadPathHistory . Add ( SharedSettings . DownloadPath ) ;
@@ -337,11 +363,6 @@ private void GenerateDownloadArguments()
337363
338364 BackendInstance . GenerateDownloadArguments ( ) ;
339365
340- if ( SharedSettings . UseCustomPath )
341- {
342- UpdateDownloadPathHistory ( ) ;
343- }
344-
345366 var pos = _globalArgCount ;
346367
347368 foreach ( var arg in BackendInstance . GeneratedDownloadArguments )
@@ -350,5 +371,16 @@ private void GenerateDownloadArguments()
350371 pos ++ ;
351372 }
352373 }
374+
375+ private Task StartDownloadAsync ( string link , CancellationToken cancellationToken = default )
376+ {
377+ if ( SharedSettings . UseCustomOutputTemplate )
378+ UpdateOutputTemplateHistory ( ) ;
379+
380+ if ( SharedSettings . UseCustomPath )
381+ UpdateDownloadPathHistory ( ) ;
382+
383+ return BackendInstance . StartDownloadAsync ( link , cancellationToken ) ;
384+ }
353385 }
354386}
0 commit comments