Skip to content

Commit ddf3b2a

Browse files
authored
Code Quality: Fixed null warnings in MainPage.xaml.cs (#14145)
1 parent ef896ad commit ddf3b2a

File tree

10 files changed

+38
-22
lines changed

10 files changed

+38
-22
lines changed

src/Files.App/Helpers/Navigation/NavigationHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private static async Task UpdateTabInfoAsync(TabBarItem tabItem, object navigati
211211
return (tabLocationHeader, iconSource, toolTipText);
212212
}
213213

214-
public static async Task UpdateInstancePropertiesAsync(object navigationArg)
214+
public static async Task UpdateInstancePropertiesAsync(object? navigationArg)
215215
{
216216
await SafetyExtensions.IgnoreExceptions(async () =>
217217
{

src/Files.App/UserControls/AddressToolbar.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public bool ShowSearchBox
4646
// Using a DependencyProperty as the backing store for ViewModel. This enables animation, styling, binding, etc...
4747
public static readonly DependencyProperty ViewModelProperty =
4848
DependencyProperty.Register(nameof(ViewModel), typeof(ToolbarViewModel), typeof(AddressToolbar), new PropertyMetadata(null));
49-
public ToolbarViewModel ViewModel
49+
public ToolbarViewModel? ViewModel
5050
{
5151
get => (ToolbarViewModel)GetValue(ViewModelProperty);
5252
set => SetValue(ViewModelProperty, value);

src/Files.App/UserControls/InnerNavigationToolbar.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public InnerNavigationToolbar()
2727

2828
public AppModel AppModel => App.AppModel;
2929

30-
public ToolbarViewModel ViewModel
30+
public ToolbarViewModel? ViewModel
3131
{
3232
get => (ToolbarViewModel)GetValue(ViewModelProperty);
3333
set => SetValue(ViewModelProperty, value);

src/Files.App/UserControls/StatusBarControl.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public DirectoryPropertiesViewModel? DirectoryPropertiesViewModel
2121
public static readonly DependencyProperty DirectoryPropertiesViewModelProperty =
2222
DependencyProperty.Register(nameof(DirectoryPropertiesViewModel), typeof(DirectoryPropertiesViewModel), typeof(StatusBarControl), new PropertyMetadata(null));
2323

24-
public SelectedItemsPropertiesViewModel SelectedItemsPropertiesViewModel
24+
public SelectedItemsPropertiesViewModel? SelectedItemsPropertiesViewModel
2525
{
2626
get => (SelectedItemsPropertiesViewModel)GetValue(SelectedItemsPropertiesViewModelProperty);
2727
set => SetValue(SelectedItemsPropertiesViewModelProperty, value);

src/Files.App/Utils/RecycleBin/RecycleBinHelpers.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,14 @@ public static async Task RestoreRecycleBinAsync()
117117

118118
public static async Task RestoreSelectionRecycleBinAsync(IShellPage associatedInstance)
119119
{
120+
var items = associatedInstance.SlimContentPage.SelectedItems;
121+
if (items == null)
122+
return;
120123
var ConfirmEmptyBinDialog = new ContentDialog()
121124
{
122125
Title = "ConfirmRestoreSelectionBinDialogTitle".GetLocalizedResource(),
123-
Content = string.Format("ConfirmRestoreSelectionBinDialogContent".GetLocalizedResource(), associatedInstance.SlimContentPage.SelectedItems.Count),
126+
127+
Content = string.Format("ConfirmRestoreSelectionBinDialogContent".GetLocalizedResource(), items.Count),
124128
PrimaryButtonText = "Yes".GetLocalizedResource(),
125129
SecondaryButtonText = "Cancel".GetLocalizedResource(),
126130
DefaultButton = ContentDialogButton.Primary
@@ -152,7 +156,10 @@ public static bool RecycleBinHasItems()
152156

153157
public static async Task RestoreItemAsync(IShellPage associatedInstance)
154158
{
155-
var items = associatedInstance.SlimContentPage.SelectedItems.ToList().Where(x => x is RecycleBinItem).Select((item) => new
159+
var selected = associatedInstance.SlimContentPage.SelectedItems;
160+
if (selected == null)
161+
return;
162+
var items = selected.ToList().Where(x => x is RecycleBinItem).Select((item) => new
156163
{
157164
Source = StorageHelpers.FromPathAndType(
158165
item.ItemPath,
@@ -164,10 +171,13 @@ public static async Task RestoreItemAsync(IShellPage associatedInstance)
164171

165172
public static async Task DeleteItemAsync(IShellPage associatedInstance)
166173
{
167-
var items = associatedInstance.SlimContentPage.SelectedItems.ToList().Select((item) => StorageHelpers.FromPathAndType(
174+
var selected = associatedInstance.SlimContentPage.SelectedItems;
175+
if (selected == null)
176+
return;
177+
var items = selected.ToList().Select((item) => StorageHelpers.FromPathAndType(
168178
item.ItemPath,
169179
item.PrimaryItemAttribute == StorageItemTypes.File ? FilesystemItemType.File : FilesystemItemType.Directory));
170180
await associatedInstance.FilesystemHelpers.DeleteItemsAsync(items, userSettingsService.FoldersSettingsService.DeleteConfirmationPolicy, false, true);
171181
}
172182
}
173-
}
183+
}

src/Files.App/Utils/Storage/History/StorageHistoryWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class StorageHistoryWrapper : IDisposable
1414

1515
public IStorageHistory GetCurrentHistory() => histories[index];
1616

17-
public void AddHistory(IStorageHistory history)
17+
public void AddHistory(IStorageHistory? history)
1818
{
1919
if (history is not null)
2020
{

src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed class FilesystemHelpers : IFilesystemHelpers
2525
private readonly IJumpListService jumpListService;
2626
private IFilesystemOperations filesystemOperations;
2727

28-
private ItemManipulationModel itemManipulationModel => associatedInstance.SlimContentPage?.ItemManipulationModel;
28+
private ItemManipulationModel? itemManipulationModel => associatedInstance.SlimContentPage?.ItemManipulationModel;
2929

3030
private readonly CancellationToken cancellationToken;
3131
private static char[] RestrictedCharacters
@@ -58,7 +58,7 @@ public FilesystemHelpers(IShellPage associatedInstance, CancellationToken cancel
5858
jumpListService = Ioc.Default.GetRequiredService<IJumpListService>();
5959
filesystemOperations = new ShellFilesystemOperations(this.associatedInstance);
6060
}
61-
public async Task<(ReturnResult, IStorageItem)> CreateAsync(IStorageItemWithPath source, bool registerHistory)
61+
public async Task<(ReturnResult, IStorageItem?)> CreateAsync(IStorageItemWithPath source, bool registerHistory)
6262
{
6363
var returnStatus = ReturnResult.InProgress;
6464
var progress = new Progress<StatusCenterItemProgressModel>();
@@ -363,7 +363,7 @@ public async Task<ReturnResult> CopyItemsFromClipboard(DataPackageView packageVi
363363
ReturnResult returnStatus = ReturnResult.InProgress;
364364

365365
var destinations = new List<string>();
366-
List<ShellFileItem> binItems = null;
366+
List<ShellFileItem>? binItems = null;
367367
foreach (var item in source)
368368
{
369369
if (RecycleBinHelpers.IsPathUnderRecycleBin(item.Path))
@@ -511,7 +511,7 @@ public async Task<ReturnResult> MoveItemsFromClipboard(DataPackageView packageVi
511511
ReturnResult returnStatus = ReturnResult.InProgress;
512512

513513
var destinations = new List<string>();
514-
List<ShellFileItem> binItems = null;
514+
List<ShellFileItem>? binItems = null;
515515
foreach (var item in source)
516516
{
517517
if (RecycleBinHelpers.IsPathUnderRecycleBin(item.Path))
@@ -551,7 +551,7 @@ await DialogDisplayHelper.ShowDialogAsync(
551551
return ReturnResult.Failed;
552552
}
553553

554-
IStorageHistory history = null;
554+
IStorageHistory? history = null;
555555

556556
switch (source.ItemType)
557557
{
@@ -658,7 +658,8 @@ public static bool IsValidForFilename(string name)
658658
{
659659
var itemPathOrName = string.IsNullOrEmpty(item.src.Path) ? item.src.Item.Name : item.src.Path;
660660
incomingItems.Add(new FileSystemDialogConflictItemViewModel() { ConflictResolveOption = FileNameConflictResolveOptionType.None, SourcePath = itemPathOrName, DestinationPath = item.dest, DestinationDisplayName = Path.GetFileName(item.dest) });
661-
if (collisions.ContainsKey(incomingItems.ElementAt(item.index).SourcePath))
661+
var path = incomingItems.ElementAt(item.index).SourcePath;
662+
if (path is not null && collisions.ContainsKey(path))
662663
{
663664
// Something strange happened, log
664665
App.Logger.LogWarning($"Duplicate key when resolving conflicts: {incomingItems.ElementAt(item.index).SourcePath}, {item.src.Name}\n" +
@@ -870,6 +871,8 @@ public void Dispose()
870871
{
871872
filesystemOperations?.Dispose();
872873

874+
// SUPPRESS: Cannot convert null literal to non-nullable reference type.
875+
#pragma warning disable CS8625
873876
associatedInstance = null;
874877
filesystemOperations = null;
875878
}

src/Files.App/Utils/Storage/Operations/IFilesystemHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface IFilesystemHelpers : IDisposable
1414
/// <param name="source">FullPath to the item</param>
1515
/// <param name="registerHistory">Determines whether <see cref="IStorageHistory"/> is saved</param>
1616
/// <returns><see cref="ReturnResult"/> of performed operation</returns>
17-
Task<(ReturnResult, IStorageItem)> CreateAsync(IStorageItemWithPath source, bool registerHistory);
17+
Task<(ReturnResult, IStorageItem?)> CreateAsync(IStorageItemWithPath source, bool registerHistory);
1818

1919
#region Delete
2020

src/Files.App/ViewModels/UserControls/SidebarViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ public SidebarDisplayMode SidebarDisplayMode
8888
public bool IsSidebarCompactSize
8989
=> SidebarDisplayMode == SidebarDisplayMode.Compact || SidebarDisplayMode == SidebarDisplayMode.Minimal;
9090

91-
public void NotifyInstanceRelatedPropertiesChanged(string arg)
91+
public void NotifyInstanceRelatedPropertiesChanged(string? arg)
9292
{
9393
UpdateSidebarSelectedItemFromArgs(arg);
9494

9595
OnPropertyChanged(nameof(SidebarSelectedItem));
9696
}
9797

98-
public void UpdateSidebarSelectedItemFromArgs(string arg)
98+
public void UpdateSidebarSelectedItemFromArgs(string? arg)
9999
{
100100
var value = arg;
101101

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public async void TabItemContent_ContentChanged(object? sender, CustomTabViewIte
161161

162162
var paneArgs = e.NavigationParameter as PaneNavigationArguments;
163163
SidebarAdaptiveViewModel.UpdateSidebarSelectedItemFromArgs(SidebarAdaptiveViewModel.PaneHolder.IsLeftPaneActive ?
164-
paneArgs.LeftPaneNavPathParam : paneArgs.RightPaneNavPathParam);
164+
paneArgs?.LeftPaneNavPathParam : paneArgs?.RightPaneNavPathParam);
165165

166166
UpdateStatusBarProperties();
167167
LoadPaneChanged();
@@ -175,9 +175,12 @@ public void MultitaskingControl_CurrentInstanceChanged(object? sender, CurrentIn
175175
SidebarAdaptiveViewModel.PaneHolder.PropertyChanged -= PaneHolder_PropertyChanged;
176176

177177
var navArgs = e.CurrentInstance.TabItemParameter?.NavigationParameter;
178-
SidebarAdaptiveViewModel.PaneHolder = e.CurrentInstance as IPaneHolder;
179-
SidebarAdaptiveViewModel.PaneHolder.PropertyChanged += PaneHolder_PropertyChanged;
180-
SidebarAdaptiveViewModel.NotifyInstanceRelatedPropertiesChanged((navArgs as PaneNavigationArguments).LeftPaneNavPathParam);
178+
if (e.CurrentInstance is IPaneHolder currentInstance)
179+
{
180+
SidebarAdaptiveViewModel.PaneHolder = currentInstance;
181+
SidebarAdaptiveViewModel.PaneHolder.PropertyChanged += PaneHolder_PropertyChanged;
182+
}
183+
SidebarAdaptiveViewModel.NotifyInstanceRelatedPropertiesChanged((navArgs as PaneNavigationArguments)?.LeftPaneNavPathParam);
181184

182185
if (SidebarAdaptiveViewModel.PaneHolder?.ActivePaneOrColumn.SlimContentPage?.DirectoryPropertiesViewModel is not null)
183186
SidebarAdaptiveViewModel.PaneHolder.ActivePaneOrColumn.SlimContentPage.DirectoryPropertiesViewModel.ShowLocals = true;

0 commit comments

Comments
 (0)