Skip to content

Commit 5077a54

Browse files
committed
fixes
1 parent 772758c commit 5077a54

File tree

6 files changed

+68
-49
lines changed

6 files changed

+68
-49
lines changed

SmartImage.Lib/DirectImage.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,17 @@ public override int GetHashCode()
3434
return HashCode.Combine(Stream, Response, Url);
3535
}
3636

37-
public static bool operator ==(DirectImage left, DirectImage right) => left.Equals(right);
37+
public static bool operator ==(DirectImage left, DirectImage right)
38+
{
39+
return !ReferenceEquals(left, null) && left.Equals(right);
40+
41+
}
3842

39-
public static bool operator !=(DirectImage left, DirectImage right) => !left.Equals(right);
43+
public static bool operator !=(DirectImage left, DirectImage right)
44+
{
45+
return !ReferenceEquals(left, null) && !left.Equals(right);
46+
47+
}
4048

4149
public void Dispose()
4250
{

SmartImage.Lib/SearchClient.cs

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public SearchClient(SearchConfig config)
8484
/// </summary>
8585
public int Pending { get; private set; }
8686

87+
public List<SearchResult> AllResults => Results.Union(FilteredResults).Distinct().ToList();
88+
8789
/// <summary>
8890
/// Reloads <see cref="Config" /> and <see cref="Engines" /> accordingly.
8991
/// </summary>
@@ -190,6 +192,7 @@ public async Task RunSearchAsync()
190192
});
191193

192194
IsComplete = !tasks.Any();
195+
193196
}
194197

195198
Trace.WriteLine($"{nameof(SearchClient)}: Search complete", C_SUCCESS);
@@ -210,28 +213,7 @@ public async Task RunSearchAsync()
210213
SearchCompleted?.Invoke(null, args);
211214
}
212215

213-
public async void FindDirectResults(SearchResult result)
214-
{
215-
Debug.WriteLine($"searching within {result.Engine.Name}");
216-
217-
foreach (ImageResult ir in result.AllResults) {
218-
var b = await ir.TryScanForDirectImages();
219-
220-
if (b && !DirectResults.Contains(ir)) {
221-
222-
Debug.WriteLine($"{nameof(SearchClient)}: Found direct result {ir.Direct.Url}");
223-
DirectResults.Add(ir);
224-
result.PrimaryResult.Direct.Url ??= ir.Direct.Url;
225-
226-
DirectFound?.Invoke(null, new DirectResultsFoundEventArgs
227-
{
228-
DirectResultsSubset = new() { ir },
229-
});
230-
231-
ResultUpdated?.Invoke(null, EventArgs.Empty);
232-
}
233-
}
234-
}
216+
235217

236218

237219
private void FindDirectResults(object state, SearchResult value, int take2 = 5)
@@ -247,14 +229,21 @@ private void FindDirectResults(object state, SearchResult value, int take2 = 5)
247229
Debug.WriteLine($"*{nameof(SearchClient)}: Found {images.Count} direct results", C_DEBUG);
248230
DirectResults.AddRange(images);
249231

250-
DirectFound?.Invoke(null, new DirectResultsFoundEventArgs
251-
{
252-
DirectResultsSubset = images,
253-
});
254232
}
255233
else {
256-
var t = Task.Factory.StartNew(() => FindDirectResults(value));
234+
var t = Task.Factory.StartNew(async () =>
235+
{
236+
if (value.Scanned) {
237+
return;
238+
}
239+
var d = await value.FindDirectResults();
240+
Debug.WriteLine($"adding {d.Count} to {DirectResults.Count}");
241+
DirectResults.AddRange(d);
242+
value.Scanned = true;
257243

244+
ResultUpdated?.Invoke(null, EventArgs.Empty);
245+
});
246+
258247
}
259248
}
260249

@@ -374,11 +363,7 @@ public static BaseSearchEngine[] GetAllSearchEngines()
374363
/// Fires when a search is complete (<see cref="RunSearchAsync" />).
375364
/// </summary>
376365
public event EventHandler<SearchCompletedEventArgs> SearchCompleted;
377-
378-
/// <summary>
379-
/// Fires when a direct image result is found (<see cref="FindDirectResults"/>)
380-
/// </summary>
381-
public event EventHandler<DirectResultsFoundEventArgs> DirectFound;
366+
382367

383368
private static readonly Predicate<SearchResult> DetailPredicate = r => r.IsNonPrimitive;
384369

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ public async Task<bool> TryScanForDirectImages()
223223

224224
if (direct != null) {
225225
Direct = direct;
226-
Direct.Url = ((Direct.Url));
227226
ReloadImageData();
228227
return true;
229228
}
@@ -247,7 +246,12 @@ public bool IsAlreadyDirect()
247246
var b = ImageHelper.IsImage(s, out var di);
248247

249248
if (b) {
250-
Image = Image.FromStream(di.Stream);
249+
250+
try {
251+
Image = Image.FromStream(di.Stream);
252+
}
253+
catch (Exception e) {
254+
}
251255

252256
Direct.Url = Url;
253257
}

SmartImage.Lib/Searching/SearchResult.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
using SmartImage.Lib.Utilities;
44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.Drawing;
78
using System.Linq;
89
using System.Reflection;
910
using System.Runtime.CompilerServices;
1011
using System.Text;
12+
using System.Threading.Tasks;
1113
using Kantan.Model;
1214
using Kantan.Text;
1315
using Kantan.Utilities;
@@ -137,6 +139,34 @@ public SearchResult(BaseSearchEngine engine)
137139
/// </summary>
138140
public TimeSpan? RetrievalTime { get; internal set; }
139141

142+
143+
public bool Scanned { get; internal set; }
144+
145+
public async Task<List<ImageResult>> FindDirectResults()
146+
{
147+
148+
Debug.WriteLine($"searching within {Engine.Name}");
149+
150+
var directResults = new List<ImageResult>();
151+
152+
foreach (ImageResult ir in AllResults) {
153+
var b = await ir.TryScanForDirectImages();
154+
155+
if (b && !directResults.Contains(ir)) {
156+
157+
Debug.WriteLine($"{nameof(SearchClient)}: Found direct result {ir.Direct.Url}");
158+
directResults.Add(ir);
159+
PrimaryResult.Direct.Url ??= ir.Direct.Url;
160+
}
161+
}
162+
163+
Scanned = true;
164+
165+
return directResults;
166+
167+
}
168+
169+
140170
public void Consolidate()
141171
{
142172
PrimaryResult = ReflectionHelper.Consolidate(PrimaryResult, OtherResults);

SmartImage/Program.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,25 +222,15 @@ private static async Task Main(string[] args)
222222
}
223223
};
224224

225-
Client.DirectFound += (sender, eventArgs) =>
226-
{
227-
// ...
228-
};
225+
229226

230227
Client.ResultUpdated += (sender, result) =>
231228
{
232-
/*var option = ResultDialog.Options.First(x => x.Name == result.Engine.Name);
233-
var i = ResultDialog.Options.IndexOf(option);
234-
ResultDialog.Options[i] = ConsoleUIFactory.CreateResultOption(result);
235229
ResultDialog.Refresh();
236-
237-
Debug.WriteLine($"{i} update");*/
238-
ResultDialog.Refresh();
239-
240230
};
241231

242232

243-
ConsoleProgressIndicator.Start(_cancellationToken);
233+
CPI.Start(_cancellationToken);
244234

245235

246236
// Show results

SmartImage/UI/ConsoleUIFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ internal static ConsoleOption CreateResultOption(SearchResult result)
169169
CPI.Start(cts);
170170
}
171171

172-
Program.Client.FindDirectResults(result);
172+
// Program.Client.FindDirectResults(result);
173+
174+
result.FindDirectResults();
173175

174176
cts.Cancel();
175177
cts.Dispose();

0 commit comments

Comments
 (0)