Refactor Winget functions into a dedicated module

Extracts Winget-related functionalities, including search, list management, installation, and download tasks, from `BuildFFUVM_UI.ps1` and `FFUUI.Core.psm1`.
Consolidates these features into a new module, `FFUUI.Core.Winget.psm1`, to improve code organization and maintainability.
This commit is contained in:
rbalsleyMSFT
2025-06-13 10:34:46 -07:00
parent 7babad8262
commit d1835c5c06
4 changed files with 695 additions and 680 deletions
-138
View File
@@ -1763,144 +1763,6 @@ $window.Add_Loaded({
})
# Function to search for Winget apps
function Search-WingetApps {
param(
[Parameter(Mandatory = $true)]
[psobject]$State
)
try {
$searchQuery = $State.Controls.txtWingetSearch.Text
if ([string]::IsNullOrWhiteSpace($searchQuery)) { return }
# Get current items from the ListView
$currentItemsInListView = @()
if ($null -ne $State.Controls.lstWingetResults.ItemsSource) {
$currentItemsInListView = @($State.Controls.lstWingetResults.ItemsSource)
}
elseif ($State.Controls.lstWingetResults.HasItems) {
$currentItemsInListView = @($State.Controls.lstWingetResults.Items)
}
# Store selected apps from the current view
$selectedAppsFromView = @($currentItemsInListView | Where-Object { $_.IsSelected })
# Search for new apps
$searchedAppResults = Search-WingetPackagesPublic -Query $searchQuery | ForEach-Object {
[PSCustomObject]@{
IsSelected = $false # New items are not selected by default
Name = $_.Name
Id = $_.Id
Version = $_.Version
Source = $_.Source
DownloadStatus = ""
}
}
$finalAppList = [System.Collections.Generic.List[object]]::new()
$addedAppIds = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
# Add previously selected apps first
foreach ($app in $selectedAppsFromView) {
$finalAppList.Add($app)
$addedAppIds.Add($app.Id) | Out-Null
}
# Add new search results, avoiding duplicates of already added (selected) apps
foreach ($result in $searchedAppResults) {
if (-not $addedAppIds.Contains($result.Id)) {
$finalAppList.Add($result)
$addedAppIds.Add($result.Id) | Out-Null # Track added IDs to prevent duplicates from search results themselves
}
}
# Update the ListView's ItemsSource
$script:uiState.Controls.lstWingetResults.ItemsSource = $finalAppList.ToArray()
}
catch {
[System.Windows.MessageBox]::Show("Error searching for apps: $_", "Error", "OK", "Error")
}
}
# Function to save selected apps to JSON
function Save-WingetList {
param(
[Parameter(Mandatory = $true)]
[psobject]$State
)
try {
$selectedApps = $State.Controls.lstWingetResults.Items | Where-Object { $_.IsSelected }
if (-not $selectedApps) {
[System.Windows.MessageBox]::Show("No apps selected to save.", "Warning", "OK", "Warning")
return
}
$appList = @{
apps = @($selectedApps | ForEach-Object {
[ordered]@{
name = $_.Name
id = $_.Id
source = $_.Source.ToLower()
}
})
}
$sfd = New-Object System.Windows.Forms.SaveFileDialog
$sfd.Filter = "JSON files (*.json)|*.json"
$sfd.Title = "Save App List"
$sfd.InitialDirectory = $AppsPath
$sfd.FileName = "AppList.json"
if ($sfd.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$appList | ConvertTo-Json -Depth 10 | Set-Content $sfd.FileName -Encoding UTF8
[System.Windows.MessageBox]::Show("App list saved successfully.", "Success", "OK", "Information")
}
}
catch {
[System.Windows.MessageBox]::Show("Error saving app list: $_", "Error", "OK", "Error")
}
}
# Function to import app list from JSON
function Import-WingetList {
param(
[Parameter(Mandatory = $true)]
[psobject]$State
)
try {
$ofd = New-Object System.Windows.Forms.OpenFileDialog
$ofd.Filter = "JSON files (*.json)|*.json"
$ofd.Title = "Import App List"
$ofd.InitialDirectory = $AppsPath
if ($ofd.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$importedAppsData = Get-Content $ofd.FileName -Raw | ConvertFrom-Json
$newAppListForItemsSource = [System.Collections.Generic.List[object]]::new()
if ($null -ne $importedAppsData.apps) {
foreach ($appInfo in $importedAppsData.apps) {
$newAppListForItemsSource.Add([PSCustomObject]@{
IsSelected = $true # Imported apps are marked as selected
Name = $appInfo.name
Id = $appInfo.id
Version = "" # Will be populated when searching or if data exists
Source = $appInfo.source
DownloadStatus = ""
})
}
}
$State.Controls.lstWingetResults.ItemsSource = $newAppListForItemsSource.ToArray()
[System.Windows.MessageBox]::Show("App list imported successfully.", "Success", "OK", "Information")
}
}
catch {
[System.Windows.MessageBox]::Show("Error importing app list: $_", "Error", "OK", "Error")
}
}
# Function to remove application and reorder priorities
function Remove-Application {
param(