Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public partial class ListViewModel : PageViewModel, IDisposable
private Task? _initializeItemsTask;
private CancellationTokenSource? _cancellationTokenSource;

private ListItemViewModel? _lastSelectedItem;

public override bool IsInitialized
{
get => base.IsInitialized; protected set
Expand Down Expand Up @@ -330,6 +332,11 @@ private void InvokeSecondaryCommand(ListItemViewModel? item)
[RelayCommand]
private void UpdateSelectedItem(ListItemViewModel? item)
{
if (_lastSelectedItem != null)
{
_lastSelectedItem.PropertyChanged -= SelectedItemPropertyChanged;
}

if (item != null)
{
SetSelectedItem(item);
Expand Down Expand Up @@ -367,6 +374,43 @@ private void SetSelectedItem(ListItemViewModel item)

TextToSuggest = item.TextToSuggest;
});

_lastSelectedItem = item;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these two lines move to "UpdateSelectedItem", so that the lifecycle of _lastSelectItem and its PropertyChanged will only control by 1 method, which is cleaner.

_lastSelectedItem.PropertyChanged += SelectedItemPropertyChanged;
}

private void SelectedItemPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
var item = _lastSelectedItem;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should here be using the "sender" instead of assume it must be the "lastSelectedItem"?
if (sender is not ListItemViewModel item)
{
return;
}

if (item == null)
{
return;
}

// already on the UI thread here
switch (e.PropertyName)
{
case nameof(item.Command):
case nameof(item.SecondaryCommand):
case nameof(item.AllCommands):
case nameof(item.Name):
WeakReferenceMessenger.Default.Send<UpdateCommandBarMessage>(new(item));
break;
case nameof(item.Details):
if (ShowDetails && item.HasDetails)
{
WeakReferenceMessenger.Default.Send<ShowDetailsMessage>(new(item.Details));
}
else
{
WeakReferenceMessenger.Default.Send<HideDetailsMessage>();
}

break;
case nameof(item.TextToSuggest):
TextToSuggest = item.TextToSuggest;
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want a default case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah, these are the only properties that the List Page VM cares about on the selected item. Everything else doesn't bubble up through the page

}
}

private void ClearSelectedItem()
Expand Down
Loading