Skip to content

Commit

Permalink
* Fixed Bug with the CopyDownload Feature
Browse files Browse the repository at this point in the history
 * It's now stopping properly
 * Already existing files are ignored and logged
  • Loading branch information
JustForFunDeveloper committed Oct 17, 2021
1 parent 25b8f5d commit 5a20b30
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 24 deletions.
7 changes: 7 additions & 0 deletions HS-Feed-Manager/Control/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Controller : IHomeView, ISettingsView
public event EventHandler DownloadFeed;
public event EventHandler SearchLocalFolder;
public event EventHandler CopyFromDownload;
public event EventHandler StopCopyFromDownload;
public event EventHandler<List<object>> StartDownloadEpisodes;
public event EventHandler<object> PlayEpisode;
public event EventHandler<object> DeleteEpisode;
Expand All @@ -38,6 +39,7 @@ public Controller()
Mediator.Register(MediatorGlobal.SearchLocalFolder, OnSearchLocalFolder);
Mediator.Register(MediatorGlobal.StartDownloadEpisodes, OnStartDownloadEpisodes);
Mediator.Register(MediatorGlobal.CopyFromDownload, OnCopyFromDownload);
Mediator.Register(MediatorGlobal.StopCopyFromDownload, OnStopCopyFromDownload);
Mediator.Register(MediatorGlobal.PlayEpisode, OnPlayEpisode);
Mediator.Register(MediatorGlobal.DeleteEpisode, OnDeleteEpisode);
Mediator.Register(MediatorGlobal.DeleteTvShow, OnDeleteTvShow);
Expand Down Expand Up @@ -93,6 +95,11 @@ protected virtual void OnCopyFromDownload(object e)
CopyFromDownload?.Invoke(this, null);
}

protected virtual void OnStopCopyFromDownload(object e)
{
StopCopyFromDownload?.Invoke(this, null);
}

protected virtual void OnPlayEpisode(object e)
{
PlayEpisode?.Invoke(this, e);
Expand Down
15 changes: 12 additions & 3 deletions HS-Feed-Manager/Core/Handler/FileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public short OverwriteFile(string fileName, string text, string path = "")
/// <summary>
/// This method gets all shows from the download folder.
/// </summary>
/// <param name="cancellationToken"></param>
/// <param name="ct"></param>
/// <returns>A list of <see cref="TvShow"/></returns>
public List<TvShow> GetAndCopyDownloadedShows(CancellationToken ct)
{
Expand Down Expand Up @@ -266,11 +266,20 @@ public List<TvShow> GetAndCopyDownloadedShows(CancellationToken ct)
}

var destPath = Path.Combine(directoryPath, fileInfo.Name);
// If an old file exists ignore it.
var oldFile = new FileInfo(destPath);
if (oldFile.Exists)
{
Log.Information($"File {oldFile.FullName} does already exist. File will be ignored.");
counter++;
Mediator.NotifyColleagues(MediatorGlobal.ProgressMessage, $"Finished {counter} of {fileInfos.Count}");
continue;
}

fileInfo.MoveTo(destPath);
episode.LocalPath = destPath;
counter++;
var progressMessage = $"Finished {counter} of {fileInfos.Count}";
Mediator.NotifyColleagues(MediatorGlobal.ProgressMessage, progressMessage);
Mediator.NotifyColleagues(MediatorGlobal.ProgressMessage, $"Finished {counter} of {fileInfos.Count}");
}

return tvShows;
Expand Down
46 changes: 28 additions & 18 deletions HS-Feed-Manager/Core/Logic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public Logic()
_controller.SearchLocalFolder += OnSearchLocalFolder;
_controller.StartDownloadEpisodes += OnStartDownloadEpisodes;
_controller.CopyFromDownload += OnCopyFromDownload;
_controller.StopCopyFromDownload += OnStopCopyFromDownload;
_controller.PlayEpisode += OnPlayEpisode;
_controller.DeleteEpisode += OnDeleteEpisode;
_controller.DeleteTvShow += OnDeleteTvShow;
Expand Down Expand Up @@ -181,36 +182,45 @@ private void OnStartDownloadEpisodes(object sender, List<object> e)
}
}

private void OnStopCopyFromDownload(object sender, EventArgs e)
{
if (_copyDownloadTask == null) return;
Log.Information("Stop Copy Downloaded Shows");
_copyDownloadTokenSource.Cancel();
_copyDownloadTask = null;
}

private void OnCopyFromDownload(object sender, EventArgs e)
{
try
{
if (_copyDownloadTask != null && !_copyDownloadTask.IsCompleted)
{
_copyDownloadTokenSource.Cancel();
_copyDownloadTask = null;
return;
}

_copyDownloadTokenSource = new CancellationTokenSource();
CancellationToken ct = _copyDownloadTokenSource.Token;
_copyDownloadTask = Task.Run(() =>
{
// Get and copy shows from DownloadFolder to the CopyToFolder
var tvShows = _fileHandler.GetAndCopyDownloadedShows(ct);

// Sync to database
if (tvShows != null)
try
{
_dbHandler.SyncLocalTvShows(tvShows);
var sum = tvShows?.Sum(tv => tv.Episodes.Count);
Log.Information($"Finished Copy {sum} Downloaded Shows");
// Get and copy shows from DownloadFolder to the CopyToFolder
var tvShows = _fileHandler.GetAndCopyDownloadedShows(ct);

// Sync to database
if (tvShows != null)
{
_dbHandler.SyncLocalTvShows(tvShows);
var sum = tvShows?.Sum(tv => tv.Episodes.Count);
Log.Information($"Finished Copy {sum} Downloaded Shows");
}
else
{
Log.Information("Nothing to copy. Folder is empty.");
}
Mediator.NotifyColleagues(MediatorGlobal.FinishedCopyDownload, null);
}
else
catch (Exception exception)
{
Log.Information("Nothing to copy. Folder is empty.");
Log.Error(exception,"Task OnCopyFromDownload Error!");
Mediator.NotifyColleagues(MediatorGlobal.FinishedCopyDownload, null);
}
Mediator.NotifyColleagues(MediatorGlobal.FinishedCopyDownload, null);
}, ct);
}
catch (Exception ex)
Expand Down
4 changes: 2 additions & 2 deletions HS-Feed-Manager/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]
[assembly: NeutralResourcesLanguage("en")]

1 change: 1 addition & 0 deletions HS-Feed-Manager/ViewModels/Handler/MediatorGlobal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class MediatorGlobal
public static readonly string SearchLocalFolder = "SearchLocalFolder";
public static readonly string StartDownloadEpisodes = "StartDownloadEpisodes";
public static readonly string CopyFromDownload = "CopyFromDownload";
public static readonly string StopCopyFromDownload = "StopCopyFromDownload";
public static readonly string PlayEpisode = "PlayEpisode";
public static readonly string DeleteEpisode = "DeleteEpisode";
public static readonly string DeleteTvShow = "DeleteTvShow";
Expand Down
31 changes: 31 additions & 0 deletions HS-Feed-Manager/ViewModels/HomeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class HomeViewModel : INotifyPropertyChanged
private ICommand _downloadFeed;
private ICommand _searchLocalFolder;
private ICommand _copyFromDownload;
private ICommand _stopcopyFromDownload;

private ICommand _copyTvShowName;
private ICommand _textBoxButtonCmd;
Expand Down Expand Up @@ -352,6 +353,36 @@ private void CopyFromDownloadCommand()
Log.Error(ex,"CopyFromDownloadCommand Error!");
}
}

public ICommand StopCopyFromDownload
{
get
{
if (_stopcopyFromDownload == null)
_stopcopyFromDownload = new RelayCommand(
param => StopCopyFromDownloadCommand(),
param => CanStopCopyFromDownload()
);
return _stopcopyFromDownload;
}
}

private bool CanStopCopyFromDownload()
{
return true;
}

private void StopCopyFromDownloadCommand()
{
try
{
Mediator.NotifyColleagues(MediatorGlobal.StopCopyFromDownload, null);
}
catch (Exception ex)
{
Log.Error(ex,"StopCopyFromDownloadCommand Error!");
}
}

private void OnProgressMessage(object obj)
{
Expand Down
2 changes: 1 addition & 1 deletion HS-Feed-Manager/Views/HomeView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
Visibility="{Binding ShowCopyFromDownloadStart, Converter={StaticResource BooleanToVisibilityConverter}}"
ToolTip="Copy From Download" />
<Button
Command="{Binding CopyFromDownload}"
Command="{Binding StopCopyFromDownload}"
Visibility="{Binding ShowCopyFromDownloadStop, Converter={StaticResource BooleanToVisibilityConverter}}"
Style="{DynamicResource StopCopyFromDownload}"
ToolTip="Stop Copy From Download" />
Expand Down

0 comments on commit 5a20b30

Please sign in to comment.