Skip to content
Permalink
Browse files

Fix Unrecognized Files Search

  • Loading branch information...
da3dsoul committed May 5, 2019
1 parent db94183 commit 312bba41a38e223514f3cc0b0435b97ffc681924
@@ -34,7 +34,7 @@ public partial class UnrecognisedVideos : UserControl
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();

private readonly List<CancellationTokenSource> runningTasks = new List<CancellationTokenSource>();
private CancellationTokenSource runningTask;

public ICollectionView ViewFiles { get; set; }
public ObservableCollection<VM_VideoLocal> UnrecognisedFiles { get; set; }
@@ -969,42 +969,62 @@ public void RefreshSeries(bool all=false)

public async void SearchAnime(object argument)
{
lock (runningTasks)
try
{
if (runningTasks.Count > 0)
{
foreach (CancellationTokenSource runningTask in runningTasks)
runningTask.Cancel();
}
runningTask?.Cancel();
}
catch {}
CancellationTokenSource tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
lock (runningTasks)
runningTask = tokenSource;

//use listbox with dummy instead
VM_AnimeSeries_User dummy = new VM_AnimeSeries_User();
dummy.SeriesNameOverride = "Loading...";
AllSeries.Add(dummy);
btnAddSeries.IsEnabled = false;
cboEpisodes.IsEnabled = false;
cboMultiType.IsEnabled = false;
txtSeriesSearch.IsEnabled = false;
lbSeries.IsEnabled = false;
List<VM_AnimeSeries_User> series = null;

try
{
series = await Task.Run(() => SearchAnime(tokenSource, argument));
}
catch (TaskCanceledException)
{
runningTasks.Add(tokenSource);
// ignored
}

Progress<List<VM_AnimeSeries_User>> progress = new Progress<List<VM_AnimeSeries_User>>(ReportProgress);
bool cancelled = runningTask.IsCancellationRequested;
runningTask = null;

if (cancelled) return;

await Task.Run(() =>
// update
if (series != null)
{
SearchAnime(token, argument, progress);
lock (runningTasks)
{
runningTasks.Remove(tokenSource);
}
}, token);
AllSeries.Clear();
series.ForEach(a => AllSeries.Add(a));

if (AllSeries.Count >= 1)
lbSeries.SelectedIndex = 0;
}

txtSeriesSearch.IsEnabled = true;
btnAddSeries.IsEnabled = true;
cboEpisodes.IsEnabled = true;
cboMultiType.IsEnabled = true;
lbSeries.IsEnabled = true;
}

private void SearchAnime(CancellationToken token, object argument, IProgress<List<VM_AnimeSeries_User>> progress)
private List<VM_AnimeSeries_User> SearchAnime(CancellationTokenSource token, object argument)
{
if (token.IsCancellationRequested) return null;
List<VM_AnimeSeries_User> tempAnime = new List<VM_AnimeSeries_User>();
if (token.IsCancellationRequested) return;
progress.Report(tempAnime);
if (token.IsCancellationRequested) return;
SearchAnime(token, argument, tempAnime);
if (token.IsCancellationRequested) return;
progress.Report(tempAnime);
SearchAnime(token.Token, argument, tempAnime);
return token.IsCancellationRequested ? null : tempAnime;
}

private void SearchAnime(CancellationToken token, object argument, List<VM_AnimeSeries_User> tempAnime)
@@ -1083,31 +1103,6 @@ private void SearchAnime(CancellationToken token, object argument, List<VM_Anime
}
}

public void ReportProgress(List<VM_AnimeSeries_User> series)
{
if (series == null || series.Count <= 0)
{
//dont mess with user input
txtSeriesSearch.IsEnabled = false;
lbSeries.IsEnabled = false;
//use listbox with dummy instead
VM_AnimeSeries_User dummy = new VM_AnimeSeries_User();
dummy.SeriesNameOverride = "Loading...";
AllSeries.Add(dummy);
}
else
{
AllSeries.Clear();
series.ForEach(a => AllSeries.Add(a));

if (AllSeries.Count >= 1)
lbSeries.SelectedIndex = 0;

txtSeriesSearch.IsEnabled = true;
lbSeries.IsEnabled = true;
}
}

private bool SeriesSearchFilter(object obj)
{
VM_AnimeSeries_User servm = obj as VM_AnimeSeries_User;
@@ -221,19 +221,17 @@ public string SeriesName_with_aID
{
get
{
if (AniDBAnime != null)
{
if (!string.IsNullOrEmpty(SeriesNameOverride))
return SeriesNameOverride + " [" + AniDBAnime.AniDBAnime.AnimeID + "]";
if (VM_ShokoServer.Instance.SeriesNameSource == DataSourceType.AniDB)
return AniDBAnime.AniDBAnime.FormattedTitle + " [" + AniDBAnime.AniDBAnime.AnimeID + "]";
if (TvDB_Series != null && TvDB_Series.Count > 0 && !string.IsNullOrEmpty(TvDB_Series[0].SeriesName) &&
!TvDB_Series[0].SeriesName.ToUpper().Contains("**DUPLICATE"))
return TvDB_Series[0].SeriesName + " [" + AniDBAnime.AniDBAnime.AnimeID + "]";
if (AniDBAnime == null) return !string.IsNullOrEmpty(SeriesNameOverride) ? SeriesNameOverride : "";

if (!string.IsNullOrEmpty(SeriesNameOverride))
return SeriesNameOverride + " [" + AniDBAnime.AniDBAnime.AnimeID + "]";
if (VM_ShokoServer.Instance.SeriesNameSource == DataSourceType.AniDB)
return AniDBAnime.AniDBAnime.FormattedTitle + " [" + AniDBAnime.AniDBAnime.AnimeID + "]";
}
if (TvDB_Series != null && TvDB_Series.Count > 0 && !string.IsNullOrEmpty(TvDB_Series[0].SeriesName) &&
!TvDB_Series[0].SeriesName.ToUpper().Contains("**DUPLICATE"))
return TvDB_Series[0].SeriesName + " [" + AniDBAnime.AniDBAnime.AnimeID + "]";
return AniDBAnime.AniDBAnime.FormattedTitle + " [" + AniDBAnime.AniDBAnime.AnimeID + "]";

return "";
}
}

0 comments on commit 312bba4

Please sign in to comment.
You can’t perform that action at this time.