Skip to content

Commit 2eb8266

Browse files
committed
Client improvements; misc
1 parent 6bd0ec0 commit 2eb8266

File tree

9 files changed

+67
-65
lines changed

9 files changed

+67
-65
lines changed

SmartImage.Lib/Engines/Model/BaseSearchEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public async Task<SearchResult> GetResultAsync(ImageQuery query)
5757

5858
var task = Task.Run(delegate
5959
{
60-
Debug.WriteLine($"{Name}: getting result async", C_INFO);
60+
Debug.WriteLine($"{Name}: getting result async", C_VERBOSE);
6161

6262
var res = GetResult(query);
6363

@@ -72,7 +72,7 @@ public async Task<SearchResult> GetResultAsync(ImageQuery query)
7272
protected virtual Uri GetRaw(ImageQuery query)
7373
{
7474
//
75-
return new Uri(BaseUrl + query.UploadUri);
75+
return new(BaseUrl + query.UploadUri);
7676

7777
}
7878

SmartImage.Lib/SearchClient.cs

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public async Task RunSearchAsync()
156156
}
157157

158158
// Call event
159-
ResultCompleted?.Invoke(null, new SearchResultEventArgs(value)
159+
ResultCompleted?.Invoke(null, new ResultCompletedEventArgs(value)
160160
{
161161
IsFiltered = isFiltered,
162162
IsPriority = isPriority,
@@ -172,35 +172,30 @@ public async Task RunSearchAsync()
172172

173173
Trace.WriteLine($"{nameof(SearchClient)}: {d.TotalSeconds}");
174174

175-
SearchCompleted?.Invoke(null, Results);
176-
177-
if (Config.Notification) {
178-
179-
180-
var args = new ExtraResultEventArgs()
175+
var args2 = new SearchCompletedEventArgs()
176+
{
177+
Results = Results,
178+
Best = new Lazy<ImageResult>(FindBestResult),
179+
Direct = new Lazy<ImageResult>(() =>
181180
{
182-
Results = this.Results,
183-
Best = FindBestResult(),
184-
};
185-
186-
if (Config.NotificationImage) {
181+
if (Config.Notification && Config.NotificationImage) {
187182

188-
Debug.WriteLine($"Finding direct result");
183+
Debug.WriteLine($"Finding direct result");
184+
var direct = FindDirectResult();
189185

190-
var direct = FindDirectResult();
191-
192-
if (direct?.Direct != null) {
193-
Debug.WriteLine(direct);
194-
Debug.WriteLine(direct.Direct.ToString());
195-
args.Direct = direct;
186+
if (direct?.Direct != null) {
187+
Debug.WriteLine(direct);
188+
Debug.WriteLine(direct.Direct.ToString());
189+
}
196190

191+
return direct;
197192
}
198-
}
199-
200-
ExtraResults?.Invoke(null, args);
201193

202-
}
194+
return null;
195+
})
196+
};
203197

198+
SearchCompleted?.Invoke(null, args2);
204199
}
205200

206201
#endregion
@@ -337,36 +332,33 @@ public static BaseSearchEngine[] GetAllSearchEngines()
337332
/// <summary>
338333
/// An event that fires whenever a result is returned (<see cref="RunSearchAsync"/>).
339334
/// </summary>
340-
public event EventHandler<SearchResultEventArgs> ResultCompleted;
335+
public event EventHandler<ResultCompletedEventArgs> ResultCompleted;
341336

342337
/// <summary>
343338
/// An event that fires when a search is complete (<see cref="RunSearchAsync"/>).
344339
/// </summary>
345-
public event EventHandler<List<SearchResult>> SearchCompleted;
346-
347-
/// <summary>
348-
/// An event that fires when a search is complete, with extra information
349-
/// (<see cref="SearchConfig.Notification"/>; <see cref="SearchConfig.NotificationImage"/>)
350-
/// </summary>
351-
public event EventHandler<ExtraResultEventArgs> ExtraResults;
340+
public event EventHandler<SearchCompletedEventArgs> SearchCompleted;
352341

353342

354343
private const string ERR_SEARCH_NOT_COMPLETE = "Search must be completed";
355344

356345
private const string ERR_NO_BEST_RESULT = "Could not find best result";
357346
}
358347

359-
public sealed class ExtraResultEventArgs : EventArgs
348+
349+
public sealed class SearchCompletedEventArgs : EventArgs
360350
{
361351
public List<SearchResult> Results { get; init; }
362352

363353
[CanBeNull]
364-
public ImageResult Direct { get; internal set; }
354+
public Lazy<ImageResult> Direct { get; internal set; }
355+
365356

366-
public ImageResult Best { get; internal set; }
357+
[CanBeNull]
358+
public Lazy<ImageResult> Best { get; internal set; }
367359
}
368360

369-
public sealed class SearchResultEventArgs : EventArgs
361+
public sealed class ResultCompletedEventArgs : EventArgs
370362
{
371363
/// <summary>
372364
/// Search result
@@ -389,7 +381,7 @@ public sealed class SearchResultEventArgs : EventArgs
389381
public bool IsPriority { get; init; }
390382

391383

392-
public SearchResultEventArgs(SearchResult result)
384+
public ResultCompletedEventArgs(SearchResult result)
393385
{
394386
Result = result;
395387
}

SmartImage.Lib/SearchConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public sealed class SearchConfig
4040
public bool Filtering { get; set; } = true;
4141

4242
/// <summary>
43-
/// <see cref="SearchClient.ExtraResults"/>
43+
/// <see cref="SearchClient.SearchCompleted"/>
4444
/// </summary>
4545
public bool Notification { get; set; } = true;
4646

4747
/// <summary>
48-
/// <see cref="SearchClient.ExtraResults"/>
48+
/// <see cref="SearchClient.SearchCompleted"/>
4949
/// </summary>
5050
public bool NotificationImage { get; set; } = false;
5151

SmartImage.Lib/Searching/ImageQuery.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public sealed class ImageQuery
4343
/// </summary>
4444
public BaseUploadEngine UploadEngine { get; }
4545

46-
public Stream Stream { get; }
46+
public Stream Stream { get; }
4747

48+
public TimeSpan UploadTime { get; }
4849

4950

5051
public ImageQuery([NotNull] string value, [CanBeNull] BaseUploadEngine engine = null)
@@ -69,15 +70,14 @@ public ImageQuery([NotNull] string value, [CanBeNull] BaseUploadEngine engine =
6970

7071

7172
UploadEngine = engine ?? new LitterboxEngine(); //note: default upload engine
72-
73-
73+
7474

7575
UploadUri = IsUri ? new Uri(Value) : UploadEngine.Upload(Value);
7676

7777

78-
Stream = IsFile ? File.OpenRead(value) : WebUtilities.GetStream(value);
78+
Stream = IsFile ? File.OpenRead(value) : WebUtilities.GetStream(value);
7979

80-
m_upload = TimeSpan.FromTicks(Stopwatch.GetTimestamp() - now);
80+
UploadTime = TimeSpan.FromTicks(Stopwatch.GetTimestamp() - now);
8181

8282
Trace.WriteLine($"{nameof(ImageQuery)}: {UploadUri}", C_SUCCESS);
8383
}
@@ -87,8 +87,6 @@ public ImageQuery([NotNull] string value, [CanBeNull] BaseUploadEngine engine =
8787

8888
public static implicit operator ImageQuery(string value) => new(value);
8989

90-
private TimeSpan m_upload;
91-
9290
public static (bool IsUri, bool IsFile) IsUriOrFile(string x)
9391
{
9492
return (ImageHelper.IsDirect(x, DirectImageType.Binary), File.Exists(x));
@@ -107,14 +105,12 @@ public ImageResult GetImageResult()
107105
result.OtherMetadata.Add("Upload engine", UploadEngine.Name);
108106
result.OtherMetadata.Add("Input type", IsUri ? "URI" : "File");
109107
result.OtherMetadata.Add("Input value", Value);
110-
result.OtherMetadata.Add("Time", $"{m_upload.TotalSeconds:F3} uploading");
108+
result.OtherMetadata.Add("Time", $"{UploadTime.TotalSeconds:F3} uploading");
111109

112110

113111
result.UpdateImageData();
114112

115113
return result;
116-
117-
118114
}
119115

120116

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,14 @@ private static List<FieldInfo> GetDetailFields()
150150

151151
private static readonly List<FieldInfo> DetailFields = GetDetailFields();
152152

153+
/// <summary>
154+
/// Score representing the number of fields that are populated (i.e., non-<c>null</c> or <c>default</c>);
155+
/// used as a heuristic for determining image result quality
156+
/// </summary>
153157
public int DetailScore
154158
{
155159
get
156160
{
157-
/*
158-
* The number of non-null fields
159-
*/
160-
161161
int s = DetailFields.Select(f => f.GetValue(this))
162162
.Count(v => v != null);
163163

SmartImage.UI/Form.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void Init()
7373
m_cl.ResultCompleted += HandleResult;
7474
}
7575

76-
private void HandleResult(object o, SearchResultEventArgs args)
76+
private void HandleResult(object o, ResultCompletedEventArgs args)
7777
{
7878
var searchResult = args.Result;
7979

SmartImage/Program.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,19 @@ public static class Program
5858
{
5959
#region Core fields
6060

61+
/// <summary>
62+
/// User search config
63+
/// </summary>
6164
public static readonly SearchConfig Config = new();
6265

66+
/// <summary>
67+
/// Search client
68+
/// </summary>
6369
public static readonly SearchClient Client = new(Config);
6470

71+
/// <summary>
72+
/// Console UI for search results
73+
/// </summary>
6574
public static readonly NConsoleDialog ResultDialog = new()
6675
{
6776
Options = new List<NConsoleOption>(),
@@ -83,6 +92,7 @@ private static async Task Main(string[] args)
8392

8493
ToastNotificationManagerCompat.OnActivated += AppInterface.OnToastActivated;
8594

95+
//...
8696
Native.SetConsoleOutputCP(Native.CP_IBM437);
8797

8898
Console.Title = $"{AppInfo.NAME}";
@@ -126,12 +136,17 @@ private static async Task Main(string[] args)
126136

127137
CancellationTokenSource cts = new();
128138

129-
130139
// Run search
131140

132141
Client.ResultCompleted += OnResultCompleted;
133-
Client.SearchCompleted += (obj, eventArgs) => OnSearchCompleted(obj, eventArgs, cts);
134-
Client.ExtraResults += AppInterface.ShowToast;
142+
Client.SearchCompleted += (obj, eventArgs) =>
143+
{
144+
OnSearchCompleted(obj, eventArgs, cts);
145+
146+
if (Config.Notification) {
147+
AppInterface.ShowToast(obj, eventArgs);
148+
}
149+
};
135150

136151
NConsoleProgress.Queue(cts);
137152

@@ -232,7 +247,6 @@ private static bool HandleArguments()
232247
}
233248
};
234249

235-
236250
handler.Run(args);
237251

238252
Client.Reload();
@@ -249,7 +263,7 @@ private static bool HandleArguments()
249263

250264
#region Event handlers
251265

252-
private static void OnSearchCompleted(object? sender, List<SearchResult> eventArgs, CancellationTokenSource cts)
266+
private static void OnSearchCompleted(object? sender, SearchCompletedEventArgs eventArgs, CancellationTokenSource cts)
253267
{
254268
Native.FlashConsoleWindow();
255269

@@ -260,7 +274,7 @@ private static void OnSearchCompleted(object? sender, List<SearchResult> eventAr
260274

261275
}
262276

263-
private static void OnResultCompleted(object? sender, SearchResultEventArgs eventArgs)
277+
private static void OnResultCompleted(object? sender, ResultCompletedEventArgs eventArgs)
264278
{
265279
var result = eventArgs.Result;
266280

SmartImage/UI/AppInterface.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ internal static bool ApplicationIsActivated()
182182
private const string ARG_KEY_ACTION = "action";
183183
private const string ARG_VALUE_DISMISS = "dismiss";
184184

185-
public static void ShowToast(object sender, ExtraResultEventArgs args)
185+
public static void ShowToast(object sender, SearchCompletedEventArgs args)
186186
{
187187
var bestResult = args.Best;
188188

@@ -199,7 +199,7 @@ public static void ShowToast(object sender, ExtraResultEventArgs args)
199199

200200

201201
button.SetContent("Open")
202-
.AddArgument(ARG_KEY_ACTION, $"{bestResult.Url}");
202+
.AddArgument(ARG_KEY_ACTION, $"{bestResult.Value.Url}");
203203

204204

205205
builder.AddButton(button)
@@ -209,7 +209,7 @@ public static void ShowToast(object sender, ExtraResultEventArgs args)
209209
.AddText($"Results: {Program.Client.Results.Count}");
210210

211211

212-
var direct = args.Direct?.Direct;
212+
var direct = args.Direct?.Value.Direct;
213213

214214
if (direct != null) {
215215
var path = Path.GetTempPath();

Test/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace Test
3232

3333
public static class Program
3434
{
35-
public static void OnResult(object _, SearchResultEventArgs e)
35+
public static void OnResult(object _, ResultCompletedEventArgs e)
3636
{
3737

3838
if (e.Result.IsSuccessful) {

0 commit comments

Comments
 (0)