Skip to content

Commit 6e79141

Browse files
committed
v2.1.5; bug fixes; update packages
1 parent e030c6e commit 6e79141

File tree

8 files changed

+175
-155
lines changed

8 files changed

+175
-155
lines changed

SmartImage.Lib/SearchClient.cs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -212,30 +212,12 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
212212
public ImageResult FindDirectResult() => FindDirectResults().FirstOrDefault();
213213

214214

215-
public ImageResult[] FindDirectResults(int lim = 5)
215+
public ImageResult[] FindDirectResults()
216216
{
217+
int lim = 15;
217218

218219
var best = FindBestResults().ToList();
219220

220-
//const int FRAG_SIZE = 10;
221-
222-
//var frag = best.Chunk(FRAG_SIZE).ToList();
223-
224-
//var tasks = new List<Task>();
225-
226-
//for (int i = 0; i < frag.Count; i++) {
227-
// int iCopy = i;
228-
229-
// tasks.Add(Task.Factory.StartNew(() =>
230-
// {
231-
// foreach (var result in frag[iCopy]) {
232-
// result.FindDirectImages();
233-
// }
234-
// }));
235-
//}
236-
237-
238-
//Task.WaitAll(tasks.ToArray());
239221

240222
var cts = new CancellationTokenSource();
241223

@@ -246,19 +228,19 @@ public ImageResult[] FindDirectResults(int lim = 5)
246228
CancellationToken = cts.Token
247229
};
248230

231+
Debug.WriteLine($"{best.Count}");
249232

250-
var rx = new ConcurrentBag<ImageResult>();
233+
234+
var images = new ConcurrentBag<ImageResult>();
251235

252236

253237
Parallel.For(0, best.Count, options, i =>
254238
{
255-
256-
if (options.CancellationToken.IsCancellationRequested || rx.Count >= lim) {
257-
Debug.WriteLine("stop");
258-
239+
if (options.CancellationToken.IsCancellationRequested || images.Count >= lim) {
259240
return;
260241
}
261242

243+
262244
var item = best[i];
263245

264246
item.FindDirectImages();
@@ -269,7 +251,7 @@ public ImageResult[] FindDirectResults(int lim = 5)
269251

270252
if (ImageHelper.IsDirect(item.Direct.ToString(), DirectImageType.Binary)) {
271253
Debug.WriteLine($"Adding {item.Direct}");
272-
rx.Add(item);
254+
images.Add(item);
273255
}
274256

275257
});
@@ -278,16 +260,15 @@ public ImageResult[] FindDirectResults(int lim = 5)
278260
Task.Factory.StartNew(() =>
279261
{
280262
//SpinWait.SpinUntil(() => rx.Count >= lim);
281-
while (!(rx.Count >= lim)) { }
282-
283-
Debug.WriteLine($"Cancel");
263+
while (!(images.Count >= lim)) { }
264+
284265
cts.Cancel();
285266
});
286267

287268

288-
Debug.WriteLine($"Found {rx.Count} direct results");
269+
Debug.WriteLine($"Found {images.Count} direct results");
289270

290-
return rx.OrderByDescending(r => r.Similarity)
271+
return images.OrderByDescending(r => r.Similarity)
291272
.ToArray();
292273
}
293274

SmartImage.Lib/SmartImage.Lib.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="AngleSharp" Version="0.15.0" />
12-
<PackageReference Include="AngleSharp.Css" Version="0.15.0" />
13-
<PackageReference Include="AngleSharp.Io" Version="0.15.0" />
14-
<PackageReference Include="AngleSharp.Js" Version="0.14.0" />
11+
<PackageReference Include="AngleSharp" Version="0.16.0" />
12+
<PackageReference Include="AngleSharp.Css" Version="0.16.0" />
13+
<PackageReference Include="AngleSharp.Io" Version="0.16.0" />
14+
<PackageReference Include="AngleSharp.Js" Version="0.15.0" />
1515
<PackageReference Include="AngleSharp.XPath" Version="1.1.7" />
1616
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
1717
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
18-
<PackageReference Include="RestSharp" Version="106.11.7" />
18+
<PackageReference Include="RestSharp" Version="106.12.0" />
1919
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
2020
<PackageReference Include="System.Json" Version="4.7.1" />
2121
<PackageReference Include="System.Windows.Extensions" Version="5.0.0" />

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,6 @@ public static Image GetImage(string s)
173173
public static List<string> FindDirectImages(string url, DirectImageType directType = DirectImageType.Regex,
174174
int count = 5, double pingTimeSec = 1)
175175
{
176-
/*
177-
* TODO
178-
*
179-
* This function creates an insane memory leak.
180-
* Disposing neither the images nor the streams does anything (?)
181-
*/
182176

183177
var images = new List<string>();
184178

@@ -209,6 +203,7 @@ public static List<string> FindDirectImages(string url, DirectImageType directTy
209203
string str = standardOutput.ReadLine()
210204
.Split('|')
211205
.First();
206+
212207
if (!string.IsNullOrWhiteSpace(str)) {
213208
images.Add(str);
214209

@@ -335,7 +330,6 @@ public static List<string> FindDirectImages(string url, DirectImageType directTy
335330

336331
ret:
337332

338-
339333
return images;
340334

341335
}

SmartImage/Program.cs

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using System.Text.Unicode;
2929
using System.Threading;
3030
using System.Threading.Tasks;
31+
using Windows.ApplicationModel.Background;
3132
using Windows.UI.Notifications;
3233
using Microsoft.Toolkit.Uwp.Notifications;
3334
using Novus.Win32;
@@ -39,6 +40,7 @@
3940
using SmartImage.Lib.Searching;
4041
using SmartImage.Lib.Utilities;
4142
using SmartImage.Utilities;
43+
using SmartImage.UX;
4244

4345
// ReSharper disable CognitiveComplexity
4446

@@ -65,7 +67,6 @@ public static class Program
6567
Description = AppInterface.Description
6668
};
6769

68-
6970
#endregion
7071

7172
/// <summary>
@@ -80,20 +81,18 @@ private static async Task Main(string[] args)
8081
}
8182

8283

83-
84-
8584
#endif
8685

8786
/*
8887
* Setup
8988
* Check compatibility
9089
*/
91-
9290

91+
ToastNotificationManagerCompat.OnActivated += AppToast.OnActivated;
9392

9493
Native.SetConsoleOutputCP(Native.CP_IBM437);
95-
96-
94+
95+
9796
Console.Title = $"{AppInfo.NAME}";
9897

9998
NConsole.Init();
@@ -125,37 +124,44 @@ private static async Task Main(string[] args)
125124
* Handle CLI args
126125
*/
127126

128-
var argEnumerator = args.GetEnumerator();
127+
try {
128+
129+
var argEnumerator = args.GetEnumerator();
130+
131+
while (argEnumerator.MoveNext()) {
132+
object? arg = argEnumerator.Current;
129133

130-
while (argEnumerator.MoveNext()) {
131-
object? arg = argEnumerator.Current;
134+
switch (arg) {
135+
case CMD_FIND_DIRECT:
136+
argEnumerator.MoveNext();
132137

133-
switch (arg) {
134-
case CMD_FIND_DIRECT:
135-
argEnumerator.MoveNext();
138+
var directImages = ImageHelper.FindDirectImages((string) argEnumerator.Current);
136139

137-
var directImages = ImageHelper.FindDirectImages((string) argEnumerator.Current);
138-
139-
var imageResults = directImages.Select(ImageResult.FromDirectImage);
140-
var directOptions = AppInterface.CreateResultOptions(imageResults, "Image");
140+
var imageResults = directImages.Select(ImageResult.FromDirectImage);
141+
var directOptions = AppInterface.CreateResultOptions(imageResults, "Image");
141142

142143

143-
NConsole.ReadOptions(new NConsoleDialog
144-
{
145-
Options = directOptions,
146-
Description = AppInterface.Description
147-
});
144+
NConsole.ReadOptions(new NConsoleDialog
145+
{
146+
Options = directOptions,
147+
Description = AppInterface.Description
148+
});
148149

149-
return;
150-
case CMD_SEARCH:
151-
argEnumerator.MoveNext();
152-
Config.Query = (string) argEnumerator.Current;
153-
break;
154-
default:
155-
Config.Query = args.First();
156-
break;
150+
return;
151+
case CMD_SEARCH:
152+
argEnumerator.MoveNext();
153+
Config.Query = (string) argEnumerator.Current;
154+
break;
155+
default:
156+
Config.Query = args.First();
157+
break;
158+
}
157159
}
158160
}
161+
catch (Exception e) {
162+
Console.WriteLine(e);
163+
Console.ReadLine();
164+
}
159165
}
160166

161167
try {
@@ -197,6 +203,7 @@ private static async Task Main(string[] args)
197203
}
198204
}
199205

206+
200207
private static void OnSearchCompleted(object? sender, EventArgs eventArgs, CancellationTokenSource cts)
201208
{
202209
AppInterface.FlashConsoleWindow();
@@ -205,7 +212,7 @@ private static void OnSearchCompleted(object? sender, EventArgs eventArgs, Cance
205212
cts.Dispose();
206213

207214
if (Config.Notification) {
208-
AppInterface.ShowToast();
215+
AppToast.Show();
209216
}
210217
else {
211218
SystemSounds.Exclamation.Play();

SmartImage/SmartImage.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.0.2" />
3939
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
4040
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
41-
<PackageReference Include="RestSharp" Version="106.11.7" />
41+
<PackageReference Include="RestSharp" Version="106.12.0" />
4242
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
4343
<PackageReference Include="System.Json" Version="4.7.1" />
4444
<PackageReference Include="System.Windows.Extensions" Version="5.0.0" />

SmartImage/Core/AppInterface.cs renamed to SmartImage/UX/AppInterface.cs

Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
using System.Runtime.CompilerServices;
1616
using System.Runtime.InteropServices;
1717
using System.Threading;
18+
using Windows.ApplicationModel.Background;
19+
using Windows.Foundation;
20+
using Windows.UI.Notifications;
1821
using JetBrains.Annotations;
1922
using Microsoft.Toolkit.Uwp.Notifications;
2023
using Novus.Memory;
@@ -422,84 +425,6 @@ private static void UpdateConfig()
422425
AppConfig.SaveConfigFile();
423426
}
424427

425-
public static void ShowToast()
426-
{
427-
var button = new ToastButton();
428-
429-
button.SetContent("Open")
430-
.AddArgument("action", "open");
431-
432-
var button2 = new ToastButton();
433-
434-
button2.SetContent("Dismiss")
435-
.AddArgument("action", "dismiss");
436-
437-
var builder = new ToastContentBuilder();
438-
439-
var bestResult = Client.FindBestResult();
440-
441-
builder.AddButton(button)
442-
.AddButton(button2)
443-
.AddText("Search complete")
444-
.AddText($"{bestResult}")
445-
.AddText($"Results: {Client.Results.Count}");
446-
447-
if (Config.NotificationImage) {
448-
449-
var direct = Client.FindDirectResult();
450-
451-
Debug.WriteLine(direct);
452-
Debug.WriteLine(direct.Direct.ToString());
453-
454-
455-
if (direct is {Direct: { }}) {
456-
457-
458-
var tmp = Path.GetTempPath();
459-
460-
string filename = Path.GetFileName(direct.Direct.AbsolutePath);
461-
462-
var file = Path.Combine(tmp, filename);
463-
464-
new WebClient().DownloadFile(direct.Direct, file);
465-
466-
//string file = WebUtilities.Download(abs.ToString(), s);
467-
468-
Debug.WriteLine($"Downloaded {file}");
469-
470-
builder.AddHeroImage(new Uri(file));
471-
472-
AppDomain.CurrentDomain.ProcessExit += (sender, args) =>
473-
{
474-
File.Delete(file);
475-
};
476-
}
477-
}
478-
479-
480-
ToastNotificationManagerCompat.OnActivated += compat =>
481-
{
482-
// Obtain the arguments from the notification
483-
var args = ToastArguments.Parse(compat.Argument);
484-
485-
foreach (var argument in args) {
486-
Debug.WriteLine($">>> {argument}");
487-
488-
if (argument.Key == "action" && argument.Value == "open") {
489-
//Client.Results.Sort();
490-
491-
492-
if (bestResult is {Url: { }}) {
493-
WebUtilities.OpenUrl(bestResult.Url.ToString());
494-
}
495-
}
496-
}
497-
};
498-
499-
builder.Show();
500-
501-
//ToastNotificationManager.CreateToastNotifier();
502-
}
503428

504429
#region Native
505430

0 commit comments

Comments
 (0)