Skip to content

Commit d883603

Browse files
committed
Code Quality: Add null checks to MultitaskingControl event handler
Improves stability by adding null checks for event arguments and nested properties in MultitaskingControl_CurrentInstanceChanged. Ensures safe access and prevents potential null reference exceptions.
1 parent d52a657 commit d883603

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/Files.App/Views/MainPage.xaml.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,31 @@ public async void TabItemContent_ContentChanged(object? sender, TabBarItemParame
134134
AppLifecycleHelper.SaveSessionTabs();
135135
}
136136

137+
137138
public async void MultitaskingControl_CurrentInstanceChanged(object? sender, CurrentInstanceChangedEventArgs e)
138139
{
139-
if (SidebarAdaptiveViewModel.PaneHolder is not null)
140+
// Add null check for the event args and CurrentInstance
141+
if (e?.CurrentInstance == null)
142+
return;
143+
144+
// Safely unsubscribe from previous instance
145+
if (SidebarAdaptiveViewModel?.PaneHolder is not null)
140146
SidebarAdaptiveViewModel.PaneHolder.PropertyChanged -= PaneHolder_PropertyChanged;
141147

142148
var navArgs = e.CurrentInstance.TabBarItemParameter?.NavigationParameter;
143-
if (e.CurrentInstance is IShellPanesPage currentInstance)
149+
150+
if (e.CurrentInstance is IShellPanesPage currentInstance && SidebarAdaptiveViewModel != null)
144151
{
145152
SidebarAdaptiveViewModel.PaneHolder = currentInstance;
146153
SidebarAdaptiveViewModel.PaneHolder.PropertyChanged += PaneHolder_PropertyChanged;
147154
}
148-
SidebarAdaptiveViewModel.NotifyInstanceRelatedPropertiesChanged((navArgs as PaneNavigationArguments)?.LeftPaneNavPathParam);
149155

150-
if (SidebarAdaptiveViewModel.PaneHolder?.ActivePaneOrColumn.SlimContentPage?.StatusBarViewModel is not null)
151-
SidebarAdaptiveViewModel.PaneHolder.ActivePaneOrColumn.SlimContentPage.StatusBarViewModel.ShowLocals = true;
156+
SidebarAdaptiveViewModel?.NotifyInstanceRelatedPropertiesChanged((navArgs as PaneNavigationArguments)?.LeftPaneNavPathParam);
157+
158+
// Safely access nested properties with null checks
159+
var statusBarViewModel = SidebarAdaptiveViewModel?.PaneHolder?.ActivePaneOrColumn?.SlimContentPage?.StatusBarViewModel;
160+
if (statusBarViewModel is not null)
161+
statusBarViewModel.ShowLocals = true;
152162

153163
UpdateStatusBarProperties();
154164
UpdateNavToolbarProperties();
@@ -161,7 +171,8 @@ public async void MultitaskingControl_CurrentInstanceChanged(object? sender, Cur
161171

162172
// Focus the content of the selected tab item (this also avoids an issue where the Omnibar sometimes steals the focus)
163173
await Task.Delay(100);
164-
ContentPageContext.ShellPage!.PaneHolder.FocusActivePane();
174+
if (ContentPageContext?.ShellPage?.PaneHolder != null)
175+
ContentPageContext.ShellPage.PaneHolder.FocusActivePane();
165176
}
166177

167178
private void PaneHolder_PropertyChanged(object? sender, PropertyChangedEventArgs e)

0 commit comments

Comments
 (0)