Skip to content

Commit

Permalink
Run Code Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
marticliment committed Jun 11, 2024
1 parent b1e61d0 commit 7b3f499
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 105 deletions.
7 changes: 1 addition & 6 deletions src/Widgets/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Widgets_for_UniGetUI
{
Expand Down
9 changes: 3 additions & 6 deletions src/Widgets/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// Program.cs

using System.Runtime.InteropServices;
using ComTypes = System.Runtime.InteropServices.ComTypes;
using Microsoft.Windows.Widgets;
using WingetUIWidgetProvider;
using COM;
using System;
using System.Runtime.InteropServices;
using Widgets_for_UniGetUI;
using WingetUIWidgetProvider;

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
Expand Down Expand Up @@ -40,7 +37,7 @@ static extern int CoRegisterClassObject(
else
{
// Wait until the manager has disposed of the last widget provider.
using (var emptyWidgetListEvent = WidgetProvider.GetEmptyWidgetListEvent())
using (ManualResetEvent emptyWidgetListEvent = WidgetProvider.GetEmptyWidgetListEvent())
{
emptyWidgetListEvent.WaitOne();
}
Expand Down
20 changes: 1 addition & 19 deletions src/Widgets/Templates.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
using ABI.System;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Graphics.Printing.PrintSupport;
using Windows.UI.Text;
using WingetUIWidgetProvider;
using static System.Net.Mime.MediaTypeNames;

namespace WingetUIWidgetProvider
namespace WingetUIWidgetProvider
{

public static class Verbs
Expand Down
61 changes: 25 additions & 36 deletions src/Widgets/WidgetProvider.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
using Microsoft.Windows.Widgets;
using Microsoft.Windows.Widgets.Providers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Widgets_for_UniGetUI;
using Windows.ApplicationModel.Chat;
using Windows.Management.Deployment;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Pickers;

namespace WingetUIWidgetProvider
{

internal class WidgetProvider : IWidgetProvider
{
public static Dictionary<string, GenericWidget> RunningWidgets = new Dictionary<string, GenericWidget>();
public static Dictionary<string, GenericWidget> RunningWidgets = new();

WingetUIConnector UniGetUI;

Expand All @@ -27,16 +16,16 @@ public WidgetProvider()
UniGetUI = new WingetUIConnector();
UniGetUI.UpdateCheckFinished += UniGetUI_UpdateCheckFinished;

var runningWidgets = WidgetManager.GetDefault().GetWidgetInfos();
WidgetInfo[] runningWidgets = WidgetManager.GetDefault().GetWidgetInfos();

foreach (var widgetInfo in runningWidgets)
foreach (WidgetInfo? widgetInfo in runningWidgets)
{
var widgetContext = widgetInfo.WidgetContext;
var widgetId = widgetContext.Id;
var widgetName = widgetContext.DefinitionId;
WidgetContext widgetContext = widgetInfo.WidgetContext;
string widgetId = widgetContext.Id;
string widgetName = widgetContext.DefinitionId;
if (!RunningWidgets.ContainsKey(widgetId))
{
GenericWidget runningWidgetInfo = new GenericWidget(widgetId, widgetName);
GenericWidget runningWidgetInfo = new(widgetId, widgetName);
try
{
runningWidgetInfo.isActive = true;
Expand All @@ -54,7 +43,7 @@ public WidgetProvider()

private void StartLoadingRoutine(GenericWidget widget)
{
WidgetUpdateRequestOptions updateOptions = new WidgetUpdateRequestOptions(widget.Id);
WidgetUpdateRequestOptions updateOptions = new(widget.Id);
updateOptions.Data = "{ \"IsLoading\": true }";
Logger.Log("Calling to UniGetUI.GetAvailableUpdates(widget) from widget");
updateOptions.Template = Templates.BaseTemplate;
Expand All @@ -64,7 +53,7 @@ private void StartLoadingRoutine(GenericWidget widget)

private void UniGetUI_UpdateCheckFinished(object? sender, UpdatesCheckFinishedEventArgs e)
{
WidgetUpdateRequestOptions updateOptions = new WidgetUpdateRequestOptions(e.widget.Id);
WidgetUpdateRequestOptions updateOptions = new(e.widget.Id);

updateOptions.Template = Templates.BaseTemplate;
if (!e.Succeeded)
Expand All @@ -88,7 +77,7 @@ private void UniGetUI_UpdateCheckFinished(object? sender, UpdatesCheckFinishedEv

e.widget.AvailableUpdates = e.Updates;
Logger.Log("Showing available updates...");
List<Package> upgradablePackages = new List<Package>();
List<Package> upgradablePackages = new();
for (int i = 0; i < e.widget.AvailableUpdates.Length; i++)
{
if (e.widget.AvailableUpdates[i].Name != String.Empty)
Expand Down Expand Up @@ -119,9 +108,9 @@ private void UniGetUI_UpdateCheckFinished(object? sender, UpdatesCheckFinishedEv

public void CreateWidget(WidgetContext widgetContext)
{
var widgetId = widgetContext.Id;
var widgetName = widgetContext.DefinitionId;
GenericWidget runningWidgetInfo = new GenericWidget(widgetId, widgetName);
string widgetId = widgetContext.Id;
string widgetName = widgetContext.DefinitionId;
GenericWidget runningWidgetInfo = new(widgetId, widgetName);
RunningWidgets[widgetId] = runningWidgetInfo;
StartLoadingRoutine(runningWidgetInfo);
}
Expand All @@ -136,7 +125,7 @@ public void DeleteWidget(string widgetId, string customState)
}
}

static ManualResetEvent emptyWidgetListEvent = new ManualResetEvent(false);
static ManualResetEvent emptyWidgetListEvent = new(false);

public static ManualResetEvent GetEmptyWidgetListEvent()
{
Expand All @@ -145,13 +134,13 @@ public static ManualResetEvent GetEmptyWidgetListEvent()

public void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs)
{
var widgetId = actionInvokedArgs.WidgetContext.Id;
var data = actionInvokedArgs.Data;
WidgetUpdateRequestOptions updateOptions = new WidgetUpdateRequestOptions(widgetId);
string widgetId = actionInvokedArgs.WidgetContext.Id;
string data = actionInvokedArgs.Data;
WidgetUpdateRequestOptions updateOptions = new(widgetId);
if (RunningWidgets.ContainsKey(widgetId))
{
GenericWidget widget = RunningWidgets[widgetId];
var verb = actionInvokedArgs.Verb;
string verb = actionInvokedArgs.Verb;

switch (verb)
{
Expand Down Expand Up @@ -201,12 +190,12 @@ public void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs)

public void OnWidgetContextChanged(WidgetContextChangedArgs contextChangedArgs)
{
var widgetContext = contextChangedArgs.WidgetContext;
var widgetId = widgetContext.Id;
var widgetSize = widgetContext.Size;
WidgetContext widgetContext = contextChangedArgs.WidgetContext;
string widgetId = widgetContext.Id;
WidgetSize widgetSize = widgetContext.Size;
if (RunningWidgets.ContainsKey(widgetId))
{
var widget = RunningWidgets[widgetId];
GenericWidget widget = RunningWidgets[widgetId];
widget.size = widgetContext.Size;
UniGetUI.GetAvailableUpdates(widget);

Expand All @@ -215,11 +204,11 @@ public void OnWidgetContextChanged(WidgetContextChangedArgs contextChangedArgs)

public void Activate(WidgetContext widgetContext)
{
var widgetId = widgetContext.Id;
string widgetId = widgetContext.Id;

if (RunningWidgets.ContainsKey(widgetId))
{
var widget = RunningWidgets[widgetId];
GenericWidget widget = RunningWidgets[widgetId];
widget.isActive = true;
widget.size = widgetContext.Size;
UniGetUI.GetAvailableUpdates(widget);
Expand All @@ -229,7 +218,7 @@ public void Deactivate(string widgetId)
{
if (RunningWidgets.ContainsKey(widgetId))
{
var widget = RunningWidgets[widgetId];
GenericWidget widget = RunningWidgets[widgetId];
widget.isActive = false;
}
}
Expand Down
59 changes: 21 additions & 38 deletions src/Widgets/WingetUIConnector.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
using Microsoft.Windows.ApplicationModel.DynamicDependency;
using Microsoft.Windows.Widgets.Providers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Linq;
using Microsoft.Windows.Widgets.Providers;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Widgets_for_UniGetUI;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.UserDataTasks;
using Windows.Devices.Printers;
using Windows.Management.Deployment;
using Windows.Media.Protection.PlayReady;
using static System.Net.Mime.MediaTypeNames;

namespace WingetUIWidgetProvider
{
Expand All @@ -34,9 +17,9 @@ internal class WingetUIConnector
private bool update_cache_is_valid = false;
private Package[] cached_updates = new Package[0];

private System.Timers.Timer CacheExpirationTimer = new System.Timers.Timer();
private System.Timers.Timer CacheExpirationTimer = new();

public Dictionary<string, string> WidgetSourceReference = new Dictionary<string, string>()
public Dictionary<string, string> WidgetSourceReference = new()
{
{Widgets.All, ""},
{Widgets.Winget, "Winget"},
Expand Down Expand Up @@ -77,7 +60,7 @@ public void OnCacheExpire(object? source, ElapsedEventArgs? e)
async public void GetAvailableUpdates(GenericWidget Widget, bool DeepCheck = false)
{
Logger.Log("BEGIN GetAvailableUpdates(). Widget.Name=" + Widget.Name + ", DeepCheck=" + DeepCheck.ToString());
UpdatesCheckFinishedEventArgs result = new UpdatesCheckFinishedEventArgs(Widget);
UpdatesCheckFinishedEventArgs result = new(Widget);
string AllowedSource = WidgetSourceReference[Widget.Name];
Package[] found_updates;

Expand All @@ -88,13 +71,13 @@ async public void GetAvailableUpdates(GenericWidget Widget, bool DeepCheck = fal
if (!is_connected_to_host)
{
Logger.Log("GetAvailableUpdates: BEGIN connection to the host");
WidgetUpdateRequestOptions updateOptions = new WidgetUpdateRequestOptions(Widget.Id);
WidgetUpdateRequestOptions updateOptions = new(Widget.Id);
updateOptions.Template = Templates.BaseTemplate;
updateOptions.Data = Templates.GetData_IsLoading();
WidgetManager.GetDefault().UpdateWidget(updateOptions);

var old_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".wingetui", "CurrentSessionToken");
var new_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UniGetUI", "CurrentSessionToken");
string old_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".wingetui", "CurrentSessionToken");
string new_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UniGetUI", "CurrentSessionToken");

string SessionTokenFile;
if(!File.Exists(new_path))
Expand All @@ -103,18 +86,18 @@ async public void GetAvailableUpdates(GenericWidget Widget, bool DeepCheck = fal
SessionTokenFile = new_path;
else
{
FileInfo old_path_data = new FileInfo(old_path);
var old_created = old_path_data.LastWriteTimeUtc; //File Creation
FileInfo new_path_data = new FileInfo(new_path);
var new_created = new_path_data.LastWriteTimeUtc; //File Creation
FileInfo old_path_data = new(old_path);
DateTime old_created = old_path_data.LastWriteTimeUtc; //File Creation
FileInfo new_path_data = new(new_path);
DateTime new_created = new_path_data.LastWriteTimeUtc; //File Creation
SessionTokenFile = old_created > new_created ? old_path : new_path;
}

StreamReader reader = new StreamReader(SessionTokenFile);
StreamReader reader = new(SessionTokenFile);
SessionToken = reader.ReadToEnd().ToString().Replace("\n", "").Trim();
reader.Close();

HttpClient client = new HttpClient();
HttpClient client = new();
client.BaseAddress = new Uri("http://localhost:7058//");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand Down Expand Up @@ -180,13 +163,13 @@ async public void GetAvailableUpdates(GenericWidget Widget, bool DeepCheck = fal
try
{
Logger.Log("GetAvailableUpdates: BEGIN retrieving updates from the host");
WidgetUpdateRequestOptions updateOptions = new WidgetUpdateRequestOptions(Widget.Id);
WidgetUpdateRequestOptions updateOptions = new(Widget.Id);
updateOptions.Template = Templates.BaseTemplate;
updateOptions.Data = Templates.GetData_IsLoading();
WidgetManager.GetDefault().UpdateWidget(updateOptions);

Logger.Log("Fetching updates from server");
HttpClient client = new HttpClient();
HttpClient client = new();
client.BaseAddress = new Uri("http://localhost:7058//");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand All @@ -210,7 +193,7 @@ async public void GetAvailableUpdates(GenericWidget Widget, bool DeepCheck = fal
cached_updates = new Package[updateCount];
for (int i = 0; i < updateCount; i++)
{
Package package = new Package(packageStrings[i]);
Package package = new(packageStrings[i]);
cached_updates[i] = package;
}
update_cache_is_valid = true;
Expand Down Expand Up @@ -284,7 +267,7 @@ async public void OpenWingetUI()
{
try
{
HttpClient client = new HttpClient();
HttpClient client = new();
client.BaseAddress = new Uri("http://localhost:7058//");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand All @@ -302,7 +285,7 @@ async public void ViewOnWingetUI()
{
try
{
HttpClient client = new HttpClient();
HttpClient client = new();
client.BaseAddress = new Uri("http://localhost:7058//");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand All @@ -320,7 +303,7 @@ async public void UpdatePackage(Package package)
{
try
{
HttpClient client = new HttpClient();
HttpClient client = new();
client.BaseAddress = new Uri("http://localhost:7058//");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand All @@ -339,7 +322,7 @@ async public void UpdateAllPackages()
{
try
{
HttpClient client = new HttpClient();
HttpClient client = new();
client.BaseAddress = new Uri("http://localhost:7058//");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand All @@ -357,7 +340,7 @@ async public void UpdateAllPackagesForSource(string source)
{
try
{
HttpClient client = new HttpClient();
HttpClient client = new();
client.BaseAddress = new Uri("http://localhost:7058//");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand Down

0 comments on commit 7b3f499

Please sign in to comment.