diff --git a/SmartImage.Lib/SearchClient.cs b/SmartImage.Lib/SearchClient.cs index aba10dd2..01182f09 100644 --- a/SmartImage.Lib/SearchClient.cs +++ b/SmartImage.Lib/SearchClient.cs @@ -212,30 +212,12 @@ public List MaximizeResults(Func property) public ImageResult FindDirectResult() => FindDirectResults().FirstOrDefault(); - public ImageResult[] FindDirectResults(int lim = 5) + public ImageResult[] FindDirectResults() { + int lim = 15; var best = FindBestResults().ToList(); - //const int FRAG_SIZE = 10; - - //var frag = best.Chunk(FRAG_SIZE).ToList(); - - //var tasks = new List(); - - //for (int i = 0; i < frag.Count; i++) { - // int iCopy = i; - - // tasks.Add(Task.Factory.StartNew(() => - // { - // foreach (var result in frag[iCopy]) { - // result.FindDirectImages(); - // } - // })); - //} - - - //Task.WaitAll(tasks.ToArray()); var cts = new CancellationTokenSource(); @@ -246,19 +228,19 @@ public ImageResult[] FindDirectResults(int lim = 5) CancellationToken = cts.Token }; + Debug.WriteLine($"{best.Count}"); - var rx = new ConcurrentBag(); + + var images = new ConcurrentBag(); Parallel.For(0, best.Count, options, i => { - - if (options.CancellationToken.IsCancellationRequested || rx.Count >= lim) { - Debug.WriteLine("stop"); - + if (options.CancellationToken.IsCancellationRequested || images.Count >= lim) { return; } + var item = best[i]; item.FindDirectImages(); @@ -269,7 +251,7 @@ public ImageResult[] FindDirectResults(int lim = 5) if (ImageHelper.IsDirect(item.Direct.ToString(), DirectImageType.Binary)) { Debug.WriteLine($"Adding {item.Direct}"); - rx.Add(item); + images.Add(item); } }); @@ -278,16 +260,15 @@ public ImageResult[] FindDirectResults(int lim = 5) Task.Factory.StartNew(() => { //SpinWait.SpinUntil(() => rx.Count >= lim); - while (!(rx.Count >= lim)) { } - - Debug.WriteLine($"Cancel"); + while (!(images.Count >= lim)) { } + cts.Cancel(); }); - Debug.WriteLine($"Found {rx.Count} direct results"); + Debug.WriteLine($"Found {images.Count} direct results"); - return rx.OrderByDescending(r => r.Similarity) + return images.OrderByDescending(r => r.Similarity) .ToArray(); } diff --git a/SmartImage.Lib/SmartImage.Lib.csproj b/SmartImage.Lib/SmartImage.Lib.csproj index a867d69c..2bd206a3 100644 --- a/SmartImage.Lib/SmartImage.Lib.csproj +++ b/SmartImage.Lib/SmartImage.Lib.csproj @@ -8,14 +8,14 @@ - - - - + + + + - + diff --git a/SmartImage.Lib/Utilities/ImageHelper.cs b/SmartImage.Lib/Utilities/ImageHelper.cs index bd69244d..9c498b66 100644 --- a/SmartImage.Lib/Utilities/ImageHelper.cs +++ b/SmartImage.Lib/Utilities/ImageHelper.cs @@ -173,12 +173,6 @@ public static Image GetImage(string s) public static List FindDirectImages(string url, DirectImageType directType = DirectImageType.Regex, int count = 5, double pingTimeSec = 1) { - /* - * TODO - * - * This function creates an insane memory leak. - * Disposing neither the images nor the streams does anything (?) - */ var images = new List(); @@ -209,6 +203,7 @@ public static List FindDirectImages(string url, DirectImageType directTy string str = standardOutput.ReadLine() .Split('|') .First(); + if (!string.IsNullOrWhiteSpace(str)) { images.Add(str); @@ -335,7 +330,6 @@ public static List FindDirectImages(string url, DirectImageType directTy ret: - return images; } diff --git a/SmartImage/Program.cs b/SmartImage/Program.cs index f0f8da57..0ba31f78 100644 --- a/SmartImage/Program.cs +++ b/SmartImage/Program.cs @@ -28,6 +28,7 @@ using System.Text.Unicode; using System.Threading; using System.Threading.Tasks; +using Windows.ApplicationModel.Background; using Windows.UI.Notifications; using Microsoft.Toolkit.Uwp.Notifications; using Novus.Win32; @@ -39,6 +40,7 @@ using SmartImage.Lib.Searching; using SmartImage.Lib.Utilities; using SmartImage.Utilities; +using SmartImage.UX; // ReSharper disable CognitiveComplexity @@ -65,7 +67,6 @@ public static class Program Description = AppInterface.Description }; - #endregion /// @@ -80,20 +81,18 @@ private static async Task Main(string[] args) } - - #endif /* * Setup * Check compatibility */ - + ToastNotificationManagerCompat.OnActivated += AppToast.OnActivated; Native.SetConsoleOutputCP(Native.CP_IBM437); - - + + Console.Title = $"{AppInfo.NAME}"; NConsole.Init(); @@ -125,37 +124,44 @@ private static async Task Main(string[] args) * Handle CLI args */ - var argEnumerator = args.GetEnumerator(); + try { + + var argEnumerator = args.GetEnumerator(); + + while (argEnumerator.MoveNext()) { + object? arg = argEnumerator.Current; - while (argEnumerator.MoveNext()) { - object? arg = argEnumerator.Current; + switch (arg) { + case CMD_FIND_DIRECT: + argEnumerator.MoveNext(); - switch (arg) { - case CMD_FIND_DIRECT: - argEnumerator.MoveNext(); + var directImages = ImageHelper.FindDirectImages((string) argEnumerator.Current); - var directImages = ImageHelper.FindDirectImages((string) argEnumerator.Current); - - var imageResults = directImages.Select(ImageResult.FromDirectImage); - var directOptions = AppInterface.CreateResultOptions(imageResults, "Image"); + var imageResults = directImages.Select(ImageResult.FromDirectImage); + var directOptions = AppInterface.CreateResultOptions(imageResults, "Image"); - NConsole.ReadOptions(new NConsoleDialog - { - Options = directOptions, - Description = AppInterface.Description - }); + NConsole.ReadOptions(new NConsoleDialog + { + Options = directOptions, + Description = AppInterface.Description + }); - return; - case CMD_SEARCH: - argEnumerator.MoveNext(); - Config.Query = (string) argEnumerator.Current; - break; - default: - Config.Query = args.First(); - break; + return; + case CMD_SEARCH: + argEnumerator.MoveNext(); + Config.Query = (string) argEnumerator.Current; + break; + default: + Config.Query = args.First(); + break; + } } } + catch (Exception e) { + Console.WriteLine(e); + Console.ReadLine(); + } } try { @@ -197,6 +203,7 @@ private static async Task Main(string[] args) } } + private static void OnSearchCompleted(object? sender, EventArgs eventArgs, CancellationTokenSource cts) { AppInterface.FlashConsoleWindow(); @@ -205,7 +212,7 @@ private static void OnSearchCompleted(object? sender, EventArgs eventArgs, Cance cts.Dispose(); if (Config.Notification) { - AppInterface.ShowToast(); + AppToast.Show(); } else { SystemSounds.Exclamation.Play(); diff --git a/SmartImage/SmartImage.csproj b/SmartImage/SmartImage.csproj index 946b6261..f6e13348 100644 --- a/SmartImage/SmartImage.csproj +++ b/SmartImage/SmartImage.csproj @@ -38,7 +38,7 @@ - + diff --git a/SmartImage/Core/AppInterface.cs b/SmartImage/UX/AppInterface.cs similarity index 92% rename from SmartImage/Core/AppInterface.cs rename to SmartImage/UX/AppInterface.cs index adc1656d..3ae2fb77 100644 --- a/SmartImage/Core/AppInterface.cs +++ b/SmartImage/UX/AppInterface.cs @@ -15,6 +15,9 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using Windows.ApplicationModel.Background; +using Windows.Foundation; +using Windows.UI.Notifications; using JetBrains.Annotations; using Microsoft.Toolkit.Uwp.Notifications; using Novus.Memory; @@ -422,84 +425,6 @@ private static void UpdateConfig() AppConfig.SaveConfigFile(); } - public static void ShowToast() - { - var button = new ToastButton(); - - button.SetContent("Open") - .AddArgument("action", "open"); - - var button2 = new ToastButton(); - - button2.SetContent("Dismiss") - .AddArgument("action", "dismiss"); - - var builder = new ToastContentBuilder(); - - var bestResult = Client.FindBestResult(); - - builder.AddButton(button) - .AddButton(button2) - .AddText("Search complete") - .AddText($"{bestResult}") - .AddText($"Results: {Client.Results.Count}"); - - if (Config.NotificationImage) { - - var direct = Client.FindDirectResult(); - - Debug.WriteLine(direct); - Debug.WriteLine(direct.Direct.ToString()); - - - if (direct is {Direct: { }}) { - - - var tmp = Path.GetTempPath(); - - string filename = Path.GetFileName(direct.Direct.AbsolutePath); - - var file = Path.Combine(tmp, filename); - - new WebClient().DownloadFile(direct.Direct, file); - - //string file = WebUtilities.Download(abs.ToString(), s); - - Debug.WriteLine($"Downloaded {file}"); - - builder.AddHeroImage(new Uri(file)); - - AppDomain.CurrentDomain.ProcessExit += (sender, args) => - { - File.Delete(file); - }; - } - } - - - ToastNotificationManagerCompat.OnActivated += compat => - { - // Obtain the arguments from the notification - var args = ToastArguments.Parse(compat.Argument); - - foreach (var argument in args) { - Debug.WriteLine($">>> {argument}"); - - if (argument.Key == "action" && argument.Value == "open") { - //Client.Results.Sort(); - - - if (bestResult is {Url: { }}) { - WebUtilities.OpenUrl(bestResult.Url.ToString()); - } - } - } - }; - - builder.Show(); - - //ToastNotificationManager.CreateToastNotifier(); - } #region Native diff --git a/SmartImage/UX/AppToast.cs b/SmartImage/UX/AppToast.cs new file mode 100644 index 00000000..bd687ea2 --- /dev/null +++ b/SmartImage/UX/AppToast.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Toolkit.Uwp.Notifications; +using SimpleCore.Net; + +namespace SmartImage.UX +{ + public static class AppToast + { + private const string ARG_KEY_ACTION = "action"; + + private const string ARG_VALUE_DISMISS = "dismiss"; + + public static void Show() + { + var bestResult = Program.Client.FindBestResult(); + + + var builder = new ToastContentBuilder(); + + var button = new ToastButton(); + + + var button2 = new ToastButton(); + + + button2.SetContent("Dismiss") + .AddArgument(ARG_KEY_ACTION, ARG_VALUE_DISMISS); + + + button.SetContent("Open") + .AddArgument(ARG_KEY_ACTION, $"{bestResult.Url}"); + + + builder.AddButton(button) + .AddButton(button2) + .AddText("Search complete") + .AddText($"{bestResult}") + .AddText($"Results: {Program.Client.Results.Count}"); + + if (Program.Config.NotificationImage) { + + var direct = Program.Client.FindDirectResult(); + + Debug.WriteLine(direct); + + Debug.WriteLine(direct.Direct.ToString()); + + + string filename = Path.GetFileName(direct.Direct.AbsolutePath); + + var file = Path.Combine(Path.GetTempPath(), filename); + + using var wc = new WebClient(); + + wc.DownloadFile(direct.Direct, file); + + Debug.WriteLine($"Downloaded {file}"); + + builder.AddHeroImage(new Uri(file)); + + AppDomain.CurrentDomain.ProcessExit += (sender, args) => + { + File.Delete(file); + }; + } + + builder.SetBackgroundActivation(); + + //... + + + builder.Show(); + + //ToastNotificationManager.CreateToastNotifier(); + } + + + [DoesNotReturn] + public static void OnActivated(ToastNotificationActivatedEventArgsCompat compat) + { + // Obtain the arguments from the notification + + var arguments = ToastArguments.Parse(compat.Argument); + + foreach (var argument in arguments) { + Debug.WriteLine($">>> {argument}"); + + if (argument.Key == ARG_KEY_ACTION) { + + if (argument.Value == ARG_VALUE_DISMISS) { + break; + } + + + WebUtilities.OpenUrl(argument.Value); + } + } + + if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated()) { + // + Environment.Exit(0); + } + } + } +} \ No newline at end of file diff --git a/UnitTest/UnitTest.csproj b/UnitTest/UnitTest.csproj index 92e8b17f..ed44be47 100644 --- a/UnitTest/UnitTest.csproj +++ b/UnitTest/UnitTest.csproj @@ -12,9 +12,9 @@ CA1416 - - - + + +