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:
rbalsleyMSFT
2025-06-17 15:07:18 -07:00
parent 0bcedadc5c
commit 61ec7509ad
2 changed files with 87 additions and 83 deletions
-83
View File
@@ -169,89 +169,6 @@ $window.Add_Loaded({
$script:uiState.Controls.spModelFilterSection.Visibility = 'Collapsed'
$script:uiState.Controls.lstDriverModels.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({
param($sourceObject, $textChangedEventArgs)
Search-DriverModels -filterText $script:uiState.Controls.txtModelFilter.Text -State $script:uiState