mirror of
https://github.com/rbalsleyMSFT/FFU.git
synced 2026-06-14 02:09:35 -06:00
Refactor: Move driver download logic to core module
Relocates the click event handler for the driver download button from the main UI script to the `FFUUI.Core` module. This change centralizes UI logic within the core module, improving code organization and maintainability. The handler is updated to use the local window state object instead of global script variables.
This commit is contained in:
@@ -169,100 +169,6 @@ $window.Add_Loaded({
|
|||||||
$script:uiState.Controls.spModelFilterSection.Visibility = 'Collapsed'
|
$script:uiState.Controls.spModelFilterSection.Visibility = 'Collapsed'
|
||||||
$script:uiState.Controls.lstDriverModels.Visibility = 'Collapsed'
|
$script:uiState.Controls.lstDriverModels.Visibility = 'Collapsed'
|
||||||
$script:uiState.Controls.spDriverActionButtons.Visibility = 'Collapsed'
|
$script:uiState.Controls.spDriverActionButtons.Visibility = 'Collapsed'
|
||||||
$script:uiState.Controls.btnDownloadSelectedDrivers.Add_Click({
|
|
||||||
param($buttonSender, $clickEventArgs)
|
|
||||||
|
|
||||||
$selectedDrivers = @($script:uiState.Controls.lstDriverModels.Items | Where-Object { $_.IsSelected })
|
|
||||||
if (-not $selectedDrivers) {
|
|
||||||
[System.Windows.MessageBox]::Show("No drivers selected to download.", "Download Drivers", "OK", "Information")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
$buttonSender.IsEnabled = $false
|
|
||||||
$script:uiState.Controls.pbOverallProgress.Visibility = 'Visible'
|
|
||||||
$script:uiState.Controls.pbOverallProgress.Value = 0
|
|
||||||
$script:uiState.Controls.txtStatus.Text = "Preparing driver downloads..."
|
|
||||||
|
|
||||||
# Define common necessary task-specific variables locally
|
|
||||||
# Ensure required selections are made
|
|
||||||
if ($null -eq $script:uiState.Controls.cmbWindowsRelease.SelectedItem) {
|
|
||||||
[System.Windows.MessageBox]::Show("Please select a Windows Release.", "Missing Information", "OK", "Warning")
|
|
||||||
$buttonSender.IsEnabled = $true
|
|
||||||
$script:uiState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
|
||||||
$script:uiState.Controls.txtStatus.Text = "Driver download cancelled."
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if ($null -eq $script:uiState.Controls.cmbWindowsArch.SelectedItem) {
|
|
||||||
[System.Windows.MessageBox]::Show("Please select a Windows Architecture.", "Missing Information", "OK", "Warning")
|
|
||||||
$buttonSender.IsEnabled = $true
|
|
||||||
$script:uiState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
|
||||||
$script:uiState.Controls.txtStatus.Text = "Driver download cancelled."
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (($selectedDrivers | Where-Object { $_.Make -eq 'HP' }) -and $null -ne $script:uiState.Controls.cmbWindowsVersion -and $null -eq $script:uiState.Controls.cmbWindowsVersion.SelectedItem) {
|
|
||||||
[System.Windows.MessageBox]::Show("HP drivers are selected. Please select a Windows Version.", "Missing Information", "OK", "Warning")
|
|
||||||
$buttonSender.IsEnabled = $true
|
|
||||||
$script:uiState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
|
||||||
$script:uiState.Controls.txtStatus.Text = "Driver download cancelled."
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
$localDriversFolder = $script:uiState.Controls.txtDriversFolder.Text
|
|
||||||
$localWindowsRelease = $script:uiState.Controls.cmbWindowsRelease.SelectedItem.Value
|
|
||||||
$localWindowsArch = $script:uiState.Controls.cmbWindowsArch.SelectedItem
|
|
||||||
$localWindowsVersion = if ($null -ne $script:uiState.Controls.cmbWindowsVersion -and $null -ne $script:uiState.Controls.cmbWindowsVersion.SelectedItem) { $script:uiState.Controls.cmbWindowsVersion.SelectedItem } else { $null }
|
|
||||||
$coreStaticVars = Get-CoreStaticVariables
|
|
||||||
$localHeaders = $coreStaticVars.Headers
|
|
||||||
$localUserAgent = $coreStaticVars.UserAgent
|
|
||||||
$compressDrivers = $script:uiState.Controls.chkCompressDriversToWIM.IsChecked
|
|
||||||
|
|
||||||
$script:uiState.Controls.txtStatus.Text = "Processing all selected drivers..."
|
|
||||||
WriteLog "Processing all selected drivers: $($selectedDrivers.Model -join ', ')"
|
|
||||||
|
|
||||||
$taskArguments = @{
|
|
||||||
DriversFolder = $localDriversFolder
|
|
||||||
WindowsRelease = $localWindowsRelease
|
|
||||||
WindowsArch = $localWindowsArch
|
|
||||||
WindowsVersion = $localWindowsVersion
|
|
||||||
Headers = $localHeaders
|
|
||||||
UserAgent = $localUserAgent
|
|
||||||
CompressToWim = $compressDrivers
|
|
||||||
}
|
|
||||||
|
|
||||||
Invoke-ParallelProcessing -ItemsToProcess $selectedDrivers `
|
|
||||||
-ListViewControl $script:uiState.Controls.lstDriverModels `
|
|
||||||
-IdentifierProperty 'Model' `
|
|
||||||
-StatusProperty 'DownloadStatus' `
|
|
||||||
-TaskType 'DownloadDriverByMake' `
|
|
||||||
-TaskArguments $taskArguments `
|
|
||||||
-CompletedStatusText 'Completed' `
|
|
||||||
-ErrorStatusPrefix 'Error: ' `
|
|
||||||
-WindowObject $window `
|
|
||||||
-MainThreadLogPath $script:uiState.LogFilePath
|
|
||||||
|
|
||||||
$overallSuccess = $true
|
|
||||||
# Check if any item has an error status after processing
|
|
||||||
# We iterate over $script:lstDriverModels.Items because their DownloadStatus property was updated by Invoke-ParallelProcessing
|
|
||||||
foreach ($item in ($script:uiState.Controls.lstDriverModels.Items | Where-Object { $_.IsSelected })) {
|
|
||||||
# Check only originally selected items
|
|
||||||
if ($item.DownloadStatus -like 'Error:*') {
|
|
||||||
$overallSuccess = $false
|
|
||||||
WriteLog "Error detected for model $($item.Model) (Make: $($item.Make)): $($item.DownloadStatus)"
|
|
||||||
# No break here, log all errors
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$script:uiState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
|
||||||
$buttonSender.IsEnabled = $true
|
|
||||||
if ($overallSuccess) {
|
|
||||||
$script:uiState.Controls.txtStatus.Text = "All selected driver downloads processed."
|
|
||||||
[System.Windows.MessageBox]::Show("All selected driver downloads processed. Check status column for details.", "Download Process Finished", "OK", "Information")
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$script:uiState.Controls.txtStatus.Text = "Driver downloads processed with some errors. Check status column and log."
|
|
||||||
[System.Windows.MessageBox]::Show("Driver downloads processed, but some errors occurred. Please check the status column for each driver and the log file for details.", "Download Process Finished with Errors", "OK", "Warning")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
$script:uiState.Controls.btnClearDriverList.Add_Click({
|
$script:uiState.Controls.btnClearDriverList.Add_Click({
|
||||||
$script:uiState.Controls.lstDriverModels.ItemsSource = $null
|
$script:uiState.Controls.lstDriverModels.ItemsSource = $null
|
||||||
$script:uiState.Data.allDriverModels = @()
|
$script:uiState.Data.allDriverModels = @()
|
||||||
|
|||||||
@@ -583,6 +583,104 @@ function Register-EventHandlers {
|
|||||||
$localState = $window.Tag
|
$localState = $window.Tag
|
||||||
Search-DriverModels -filterText $localState.Controls.txtModelFilter.Text -State $localState
|
Search-DriverModels -filterText $localState.Controls.txtModelFilter.Text -State $localState
|
||||||
})
|
})
|
||||||
|
|
||||||
|
$State.Controls.btnDownloadSelectedDrivers.Add_Click({
|
||||||
|
param($buttonSender, $clickEventArgs)
|
||||||
|
|
||||||
|
$window = [System.Windows.Window]::GetWindow($buttonSender)
|
||||||
|
$localState = $window.Tag
|
||||||
|
|
||||||
|
$selectedDrivers = @($localState.Controls.lstDriverModels.Items | Where-Object { $_.IsSelected })
|
||||||
|
if (-not $selectedDrivers) {
|
||||||
|
[System.Windows.MessageBox]::Show("No drivers selected to download.", "Download Drivers", "OK", "Information")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
$buttonSender.IsEnabled = $false
|
||||||
|
$localState.Controls.pbOverallProgress.Visibility = 'Visible'
|
||||||
|
$localState.Controls.pbOverallProgress.Value = 0
|
||||||
|
$localState.Controls.txtStatus.Text = "Preparing driver downloads..."
|
||||||
|
|
||||||
|
# Define common necessary task-specific variables locally
|
||||||
|
# Ensure required selections are made
|
||||||
|
if ($null -eq $localState.Controls.cmbWindowsRelease.SelectedItem) {
|
||||||
|
[System.Windows.MessageBox]::Show("Please select a Windows Release.", "Missing Information", "OK", "Warning")
|
||||||
|
$buttonSender.IsEnabled = $true
|
||||||
|
$localState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||||
|
$localState.Controls.txtStatus.Text = "Driver download cancelled."
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ($null -eq $localState.Controls.cmbWindowsArch.SelectedItem) {
|
||||||
|
[System.Windows.MessageBox]::Show("Please select a Windows Architecture.", "Missing Information", "OK", "Warning")
|
||||||
|
$buttonSender.IsEnabled = $true
|
||||||
|
$localState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||||
|
$localState.Controls.txtStatus.Text = "Driver download cancelled."
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (($selectedDrivers | Where-Object { $_.Make -eq 'HP' }) -and $null -ne $localState.Controls.cmbWindowsVersion -and $null -eq $localState.Controls.cmbWindowsVersion.SelectedItem) {
|
||||||
|
[System.Windows.MessageBox]::Show("HP drivers are selected. Please select a Windows Version.", "Missing Information", "OK", "Warning")
|
||||||
|
$buttonSender.IsEnabled = $true
|
||||||
|
$localState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||||
|
$localState.Controls.txtStatus.Text = "Driver download cancelled."
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
$localDriversFolder = $localState.Controls.txtDriversFolder.Text
|
||||||
|
$localWindowsRelease = $localState.Controls.cmbWindowsRelease.SelectedItem.Value
|
||||||
|
$localWindowsArch = $localState.Controls.cmbWindowsArch.SelectedItem
|
||||||
|
$localWindowsVersion = if ($null -ne $localState.Controls.cmbWindowsVersion -and $null -ne $localState.Controls.cmbWindowsVersion.SelectedItem) { $localState.Controls.cmbWindowsVersion.SelectedItem } else { $null }
|
||||||
|
$coreStaticVars = Get-CoreStaticVariables
|
||||||
|
$localHeaders = $coreStaticVars.Headers
|
||||||
|
$localUserAgent = $coreStaticVars.UserAgent
|
||||||
|
$compressDrivers = $localState.Controls.chkCompressDriversToWIM.IsChecked
|
||||||
|
|
||||||
|
$localState.Controls.txtStatus.Text = "Processing all selected drivers..."
|
||||||
|
WriteLog "Processing all selected drivers: $($selectedDrivers.Model -join ', ')"
|
||||||
|
|
||||||
|
$taskArguments = @{
|
||||||
|
DriversFolder = $localDriversFolder
|
||||||
|
WindowsRelease = $localWindowsRelease
|
||||||
|
WindowsArch = $localWindowsArch
|
||||||
|
WindowsVersion = $localWindowsVersion
|
||||||
|
Headers = $localHeaders
|
||||||
|
UserAgent = $localUserAgent
|
||||||
|
CompressToWim = $compressDrivers
|
||||||
|
}
|
||||||
|
|
||||||
|
Invoke-ParallelProcessing -ItemsToProcess $selectedDrivers `
|
||||||
|
-ListViewControl $localState.Controls.lstDriverModels `
|
||||||
|
-IdentifierProperty 'Model' `
|
||||||
|
-StatusProperty 'DownloadStatus' `
|
||||||
|
-TaskType 'DownloadDriverByMake' `
|
||||||
|
-TaskArguments $taskArguments `
|
||||||
|
-CompletedStatusText 'Completed' `
|
||||||
|
-ErrorStatusPrefix 'Error: ' `
|
||||||
|
-WindowObject $window `
|
||||||
|
-MainThreadLogPath $localState.LogFilePath
|
||||||
|
|
||||||
|
$overallSuccess = $true
|
||||||
|
# Check if any item has an error status after processing
|
||||||
|
# We iterate over $localState.Controls.lstDriverModels.Items because their DownloadStatus property was updated by Invoke-ParallelProcessing
|
||||||
|
foreach ($item in ($localState.Controls.lstDriverModels.Items | Where-Object { $_.IsSelected })) {
|
||||||
|
# Check only originally selected items
|
||||||
|
if ($item.DownloadStatus -like 'Error:*') {
|
||||||
|
$overallSuccess = $false
|
||||||
|
WriteLog "Error detected for model $($item.Model) (Make: $($item.Make)): $($item.DownloadStatus)"
|
||||||
|
# No break here, log all errors
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$localState.Controls.pbOverallProgress.Visibility = 'Collapsed'
|
||||||
|
$buttonSender.IsEnabled = $true
|
||||||
|
if ($overallSuccess) {
|
||||||
|
$localState.Controls.txtStatus.Text = "All selected driver downloads processed."
|
||||||
|
[System.Windows.MessageBox]::Show("All selected driver downloads processed. Check status column for details.", "Download Process Finished", "OK", "Information")
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$localState.Controls.txtStatus.Text = "Driver downloads processed with some errors. Check status column and log."
|
||||||
|
[System.Windows.MessageBox]::Show("Driver downloads processed, but some errors occurred. Please check the status column for each driver and the log file for details.", "Download Process Finished with Errors", "OK", "Warning")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
Export-ModuleMember -Function *
|
Export-ModuleMember -Function *
|
||||||
|
|||||||
Reference in New Issue
Block a user