mirror of
https://github.com/rbalsleyMSFT/FFU.git
synced 2026-06-14 02:09:35 -06:00
Refactor: Move model fetching logic to core UI module
Centralizes the `btnGetModels` click event handler from the main UI script into the `FFUUI.Core` module to improve code organization and separation of concerns. The handler is updated to use the local state object instead of a global script variable. The logic for updating the model list is also improved to preserve the collection's object reference, ensuring data binding stability.
This commit is contained in:
@@ -169,89 +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.btnGetModels.Add_Click({
|
|
||||||
$selectedMake = $script:uiState.Controls.cmbMake.SelectedItem
|
|
||||||
$script:uiState.Controls.txtStatus.Text = "Getting models for $selectedMake..."
|
|
||||||
$window.Cursor = [System.Windows.Input.Cursors]::Wait
|
|
||||||
$this.IsEnabled = $false
|
|
||||||
try {
|
|
||||||
# Get previously selected models from the master list ($script:uiState.Data.allDriverModels)
|
|
||||||
# This ensures all selected items are captured, regardless of any active filter.
|
|
||||||
$previouslySelectedModels = @($script:uiState.Data.allDriverModels | Where-Object { $_.IsSelected })
|
|
||||||
|
|
||||||
# Get newly fetched models for the current make (already standardized)
|
|
||||||
$newlyFetchedStandardizedModels = Get-ModelsForMake -SelectedMake $selectedMake -State $script:uiState
|
|
||||||
|
|
||||||
$combinedModelsList = [System.Collections.Generic.List[PSCustomObject]]::new()
|
|
||||||
$modelIdentifiersInCombinedList = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
|
|
||||||
|
|
||||||
# Add previously selected models first to preserve their selection state and order (if any)
|
|
||||||
foreach ($item in $previouslySelectedModels) {
|
|
||||||
$combinedModelsList.Add($item)
|
|
||||||
# Use a composite key of Make and Model for uniqueness tracking
|
|
||||||
$modelIdentifiersInCombinedList.Add("$($item.Make)::$($item.Model)") | Out-Null
|
|
||||||
}
|
|
||||||
|
|
||||||
# Add newly fetched models if they are not already in the combined list (based on Make::Model identifier)
|
|
||||||
$addedNewCount = 0
|
|
||||||
foreach ($item in $newlyFetchedStandardizedModels) {
|
|
||||||
if (-not $modelIdentifiersInCombinedList.Contains("$($item.Make)::$($item.Model)")) {
|
|
||||||
$combinedModelsList.Add($item)
|
|
||||||
# Add to HashSet to prevent duplicates if the new list itself has them (though Get-ModelsForMake should try to avoid this)
|
|
||||||
$modelIdentifiersInCombinedList.Add("$($item.Make)::$($item.Model)") | Out-Null
|
|
||||||
$addedNewCount++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$script:uiState.Data.allDriverModels = $combinedModelsList.ToArray() | Sort-Object @{Expression = { $_.IsSelected }; Descending = $true }, Make, Model # Sort by selection status, then Make, then Model
|
|
||||||
$script:uiState.Controls.lstDriverModels.ItemsSource = $script:uiState.Data.allDriverModels
|
|
||||||
$script:uiState.Controls.txtModelFilter.Text = "" # Clear any existing filter
|
|
||||||
|
|
||||||
if ($script:uiState.Data.allDriverModels.Count -gt 0) {
|
|
||||||
$script:uiState.Controls.spModelFilterSection.Visibility = 'Visible'
|
|
||||||
$script:uiState.Controls.lstDriverModels.Visibility = 'Visible'
|
|
||||||
$script:uiState.Controls.spDriverActionButtons.Visibility = 'Visible'
|
|
||||||
$statusText = "Displaying $($script:uiState.Data.allDriverModels.Count) models."
|
|
||||||
if ($newlyFetchedStandardizedModels.Count -gt 0 -and $addedNewCount -eq 0 -and $previouslySelectedModels.Count -gt 0) {
|
|
||||||
# This case means new models were fetched, but all were already present in the selected list.
|
|
||||||
$statusText = "Fetched $($newlyFetchedStandardizedModels.Count) models for $selectedMake; all were already in the selected list. Displaying $($script:uiState.Data.allDriverModels.Count) total selected models."
|
|
||||||
}
|
|
||||||
elseif ($addedNewCount -gt 0) {
|
|
||||||
$statusText = "Added $addedNewCount new models for $selectedMake. Displaying $($script:uiState.Data.allDriverModels.Count) total models."
|
|
||||||
}
|
|
||||||
elseif ($newlyFetchedStandardizedModels.Count -eq 0 -and $selectedMake -eq 'Lenovo' ) {
|
|
||||||
# Handled Lenovo specific no new models found message inside Get-ModelsForMake or if user cancelled prompt
|
|
||||||
$statusText = if ($previouslySelectedModels.Count -gt 0) { "No new models found for $selectedMake. Displaying $($previouslySelectedModels.Count) previously selected models." } else { "No models found for $selectedMake." }
|
|
||||||
}
|
|
||||||
elseif ($newlyFetchedStandardizedModels.Count -eq 0) {
|
|
||||||
$statusText = "No new models found for $selectedMake. Displaying $($script:uiState.Data.allDriverModels.Count) previously selected models."
|
|
||||||
}
|
|
||||||
$script:uiState.Controls.txtStatus.Text = $statusText
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$script:uiState.Controls.spModelFilterSection.Visibility = 'Collapsed'
|
|
||||||
$script:uiState.Controls.lstDriverModels.Visibility = 'Collapsed'
|
|
||||||
$script:uiState.Controls.spDriverActionButtons.Visibility = 'Collapsed'
|
|
||||||
$script:uiState.Controls.txtStatus.Text = "No models to display for $selectedMake."
|
|
||||||
}
|
|
||||||
} # End Try
|
|
||||||
catch {
|
|
||||||
$script:uiState.Controls.txtStatus.Text = "Error getting models: $($_.Exception.Message)"
|
|
||||||
[System.Windows.MessageBox]::Show("Error getting models: $($_.Exception.Message)", "Error", "OK", "Error")
|
|
||||||
# Minimal UI reset on error, keep previously selected if any
|
|
||||||
if ($null -eq $script:uiState.Data.allDriverModels -or $script:uiState.Data.allDriverModels.Count -eq 0) {
|
|
||||||
$script:uiState.Controls.spModelFilterSection.Visibility = 'Collapsed'
|
|
||||||
$script:uiState.Controls.lstDriverModels.Visibility = 'Collapsed'
|
|
||||||
$script:uiState.Controls.spDriverActionButtons.Visibility = 'Collapsed'
|
|
||||||
$script:uiState.Controls.lstDriverModels.ItemsSource = $null
|
|
||||||
$script:uiState.Controls.txtModelFilter.Text = ""
|
|
||||||
}
|
|
||||||
} # End Catch
|
|
||||||
finally {
|
|
||||||
$window.Cursor = $null
|
|
||||||
$this.IsEnabled = $true # Re-enable the button
|
|
||||||
} # End Finally
|
|
||||||
})
|
|
||||||
$script:uiState.Controls.txtModelFilter.Add_TextChanged({
|
$script:uiState.Controls.txtModelFilter.Add_TextChanged({
|
||||||
param($sourceObject, $textChangedEventArgs)
|
param($sourceObject, $textChangedEventArgs)
|
||||||
Search-DriverModels -filterText $script:uiState.Controls.txtModelFilter.Text -State $script:uiState
|
Search-DriverModels -filterText $script:uiState.Controls.txtModelFilter.Text -State $script:uiState
|
||||||
|
|||||||
@@ -490,6 +490,93 @@ function Register-EventHandlers {
|
|||||||
$localState.Data.allDriverModels.Clear()
|
$localState.Data.allDriverModels.Clear()
|
||||||
$localState.Controls.txtModelFilter.Text = ""
|
$localState.Controls.txtModelFilter.Text = ""
|
||||||
})
|
})
|
||||||
|
|
||||||
|
$State.Controls.btnGetModels.Add_Click({
|
||||||
|
param($eventSource, $routedEventArgs)
|
||||||
|
$window = [System.Windows.Window]::GetWindow($eventSource)
|
||||||
|
$localState = $window.Tag
|
||||||
|
|
||||||
|
$selectedMake = $localState.Controls.cmbMake.SelectedItem
|
||||||
|
$localState.Controls.txtStatus.Text = "Getting models for $selectedMake..."
|
||||||
|
$window.Cursor = [System.Windows.Input.Cursors]::Wait
|
||||||
|
$eventSource.IsEnabled = $false
|
||||||
|
try {
|
||||||
|
# Get previously selected models from the master list ($localState.Data.allDriverModels)
|
||||||
|
$previouslySelectedModels = @($localState.Data.allDriverModels | Where-Object { $_.IsSelected })
|
||||||
|
|
||||||
|
# Get newly fetched models for the current make
|
||||||
|
$newlyFetchedStandardizedModels = Get-ModelsForMake -SelectedMake $selectedMake -State $localState
|
||||||
|
|
||||||
|
$combinedModelsList = [System.Collections.Generic.List[PSCustomObject]]::new()
|
||||||
|
$modelIdentifiersInCombinedList = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
|
||||||
|
|
||||||
|
# Add previously selected models first
|
||||||
|
foreach ($item in $previouslySelectedModels) {
|
||||||
|
$combinedModelsList.Add($item)
|
||||||
|
$modelIdentifiersInCombinedList.Add("$($item.Make)::$($item.Model)") | Out-Null
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add newly fetched models if they are not already in the list
|
||||||
|
$addedNewCount = 0
|
||||||
|
foreach ($item in $newlyFetchedStandardizedModels) {
|
||||||
|
if (-not $modelIdentifiersInCombinedList.Contains("$($item.Make)::$($item.Model)")) {
|
||||||
|
$combinedModelsList.Add($item)
|
||||||
|
$modelIdentifiersInCombinedList.Add("$($item.Make)::$($item.Model)") | Out-Null
|
||||||
|
$addedNewCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Sort the combined list and update the master list while preserving its List<> type
|
||||||
|
$sortedModels = $combinedModelsList | Sort-Object @{Expression = { $_.IsSelected }; Descending = $true }, Make, Model
|
||||||
|
$localState.Data.allDriverModels.Clear()
|
||||||
|
$sortedModels.ForEach({ $localState.Data.allDriverModels.Add($_) })
|
||||||
|
|
||||||
|
# Update the UI
|
||||||
|
$localState.Controls.lstDriverModels.ItemsSource = $localState.Data.allDriverModels
|
||||||
|
$localState.Controls.txtModelFilter.Text = ""
|
||||||
|
|
||||||
|
if ($localState.Data.allDriverModels.Count -gt 0) {
|
||||||
|
$localState.Controls.spModelFilterSection.Visibility = 'Visible'
|
||||||
|
$localState.Controls.lstDriverModels.Visibility = 'Visible'
|
||||||
|
$localState.Controls.spDriverActionButtons.Visibility = 'Visible'
|
||||||
|
$statusText = "Displaying $($localState.Data.allDriverModels.Count) models."
|
||||||
|
if ($newlyFetchedStandardizedModels.Count -gt 0 -and $addedNewCount -eq 0 -and $previouslySelectedModels.Count -gt 0) {
|
||||||
|
$statusText = "Fetched $($newlyFetchedStandardizedModels.Count) models for $selectedMake; all were already in the selected list. Displaying $($localState.Data.allDriverModels.Count) total selected models."
|
||||||
|
}
|
||||||
|
elseif ($addedNewCount -gt 0) {
|
||||||
|
$statusText = "Added $addedNewCount new models for $selectedMake. Displaying $($localState.Data.allDriverModels.Count) total models."
|
||||||
|
}
|
||||||
|
elseif ($newlyFetchedStandardizedModels.Count -eq 0 -and $selectedMake -eq 'Lenovo' ) {
|
||||||
|
$statusText = if ($previouslySelectedModels.Count -gt 0) { "No new models found for $selectedMake. Displaying $($previouslySelectedModels.Count) previously selected models." } else { "No models found for $selectedMake." }
|
||||||
|
}
|
||||||
|
elseif ($newlyFetchedStandardizedModels.Count -eq 0) {
|
||||||
|
$statusText = "No new models found for $selectedMake. Displaying $($localState.Data.allDriverModels.Count) previously selected models."
|
||||||
|
}
|
||||||
|
$localState.Controls.txtStatus.Text = $statusText
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$localState.Controls.spModelFilterSection.Visibility = 'Collapsed'
|
||||||
|
$localState.Controls.lstDriverModels.Visibility = 'Collapsed'
|
||||||
|
$localState.Controls.spDriverActionButtons.Visibility = 'Collapsed'
|
||||||
|
$localState.Controls.txtStatus.Text = "No models to display for $selectedMake."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$localState.Controls.txtStatus.Text = "Error getting models: $($_.Exception.Message)"
|
||||||
|
[System.Windows.MessageBox]::Show("Error getting models: $($_.Exception.Message)", "Error", "OK", "Error")
|
||||||
|
if ($null -eq $localState.Data.allDriverModels -or $localState.Data.allDriverModels.Count -eq 0) {
|
||||||
|
$localState.Controls.spModelFilterSection.Visibility = 'Collapsed'
|
||||||
|
$localState.Controls.lstDriverModels.Visibility = 'Collapsed'
|
||||||
|
$localState.Controls.spDriverActionButtons.Visibility = 'Collapsed'
|
||||||
|
$localState.Controls.lstDriverModels.ItemsSource = $null
|
||||||
|
$localState.Controls.txtModelFilter.Text = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
$window.Cursor = $null
|
||||||
|
$eventSource.IsEnabled = $true
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
Export-ModuleMember -Function *
|
Export-ModuleMember -Function *
|
||||||
|
|||||||
Reference in New Issue
Block a user