Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Quality: Improved MainPageViewModel #13552

Closed
Closed
Show file tree
Hide file tree
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
7 changes: 2 additions & 5 deletions src/Files.App/Actions/Navigation/DuplicateCurrentTabAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ internal class DuplicateCurrentTabAction : IAction
{
private readonly IMultitaskingContext context;

private readonly MainPageViewModel mainPageViewModel;

public string Label
=> "DuplicateTab".GetLocalizedResource();

Expand All @@ -18,16 +16,15 @@ public string Description
public DuplicateCurrentTabAction()
{
context = Ioc.Default.GetRequiredService<IMultitaskingContext>();
mainPageViewModel = Ioc.Default.GetRequiredService<MainPageViewModel>();
}

public async Task ExecuteAsync()
{
var arguments = context.CurrentTabItem.NavigationParameter;
if (arguments is null)
await mainPageViewModel.AddNewTabByPathAsync(typeof(PaneHolderPage), "Home");
await MultitaskingTabsHelpers.AddNewTabWithPathAsync(typeof(PaneHolderPage), "Home");
else
await mainPageViewModel.AddNewTabByParamAsync(arguments.InitialPageType, arguments.NavigationParameter, context.CurrentTabIndex + 1);
await MultitaskingTabsHelpers.AddNewTabWithParameterAsync(arguments.InitialPageType, arguments.NavigationParameter, context.CurrentTabIndex + 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ internal class DuplicateSelectedTabAction : IAction
{
private readonly IMultitaskingContext context;

private readonly MainPageViewModel mainPageViewModel;

public string Label
=> "DuplicateTab".GetLocalizedResource();

Expand All @@ -21,16 +19,15 @@ public HotKey HotKey
public DuplicateSelectedTabAction()
{
context = Ioc.Default.GetRequiredService<IMultitaskingContext>();
mainPageViewModel = Ioc.Default.GetRequiredService<MainPageViewModel>();
}

public async Task ExecuteAsync()
{
var arguments = context.SelectedTabItem.NavigationParameter;
if (arguments is null)
await mainPageViewModel.AddNewTabByPathAsync(typeof(PaneHolderPage), "Home");
await MultitaskingTabsHelpers.AddNewTabWithPathAsync(typeof(PaneHolderPage), "Home");
else
await mainPageViewModel.AddNewTabByParamAsync(arguments.InitialPageType, arguments.NavigationParameter, context.SelectedTabIndex + 1);
await MultitaskingTabsHelpers.AddNewTabWithParameterAsync(arguments.InitialPageType, arguments.NavigationParameter, context.SelectedTabIndex + 1);
}
}
}
5 changes: 1 addition & 4 deletions src/Files.App/Actions/Navigation/NewTabAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace Files.App.Actions
{
internal class NewTabAction : IAction
{
private readonly MainPageViewModel mainPageViewModel;

public string Label
=> "NewTab".GetLocalizedResource();

Expand All @@ -18,12 +16,11 @@ public HotKey HotKey

public NewTabAction()
{
mainPageViewModel = Ioc.Default.GetRequiredService<MainPageViewModel>();
}

public Task ExecuteAsync()
{
return mainPageViewModel.AddNewTabAsync();
return MultitaskingTabsHelpers.AddNewTabAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task ExecuteAsync()
{
await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () =>
{
await _mainPageViewModel.AddNewTabByPathAsync(
await MultitaskingTabsHelpers.AddNewTabWithPathAsync(
typeof(PaneHolderPage),
(listedItem as ShortcutItem)?.TargetPath ?? listedItem.ItemPath);
},
Expand Down
8 changes: 4 additions & 4 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ private async void Window_ClosedAsync(object sender, WindowEventArgs args)

// Save and close all tabs
SaveSessionTabs();
MainPageViewModel.AppInstances.ForEach(tabItem => tabItem.Unload());
MainPageViewModel.AppInstances.Clear();
MainPageViewModel.CurrentInstanceTabBarItems.ForEach(tabItem => tabItem.Unload());
MainPageViewModel.CurrentInstanceTabBarItems.Clear();

// Wait for all properties windows to close
await FilePropertiesHelpers.WaitClosingAll();
Expand Down Expand Up @@ -352,7 +352,7 @@ private async void Window_ClosedAsync(object sender, WindowEventArgs args)
{
await SafetyExtensions.IgnoreExceptions(async () =>
{
var instance = MainPageViewModel.AppInstances.FirstOrDefault(x => x.TabItemContent.IsCurrentInstance);
var instance = MainPageViewModel.CurrentInstanceTabBarItems.FirstOrDefault(x => x.TabItemContent.IsCurrentInstance);
if (instance is null)
return;

Expand Down Expand Up @@ -392,7 +392,7 @@ public static void SaveSessionTabs()
{
IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();

userSettingsService.GeneralSettingsService.LastSessionTabList = MainPageViewModel.AppInstances.DefaultIfEmpty().Select(tab =>
userSettingsService.GeneralSettingsService.LastSessionTabList = MainPageViewModel.CurrentInstanceTabBarItems.DefaultIfEmpty().Select(tab =>
{
if (tab is not null && tab.NavigationParameter is not null)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Files.App/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ namespace Files.App
{
public static class Constants
{
public static class App
{
public const string AppName = "Files";

public const string AppLaunchAlias = "files-uwp:";

public const string AppStorePackageId = "49306atecsolution.FilesUWP";
}

public static class AdaptiveLayout
{
public const float ExtraLargeThreshold = 85.0f;
Expand Down
12 changes: 6 additions & 6 deletions src/Files.App/Data/Contexts/Multitasking/MultitaskingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ internal class MultitaskingContext : ObservableObject, IMultitaskingContext
private ushort tabCount = 0;
public ushort TabCount => tabCount;

public TabBarItem CurrentTabItem => MainPageViewModel.AppInstances[currentTabIndex];
public TabBarItem SelectedTabItem => MainPageViewModel.AppInstances[selectedTabIndex];
public TabBarItem CurrentTabItem => MainPageViewModel.CurrentInstanceTabBarItems[currentTabIndex];
public TabBarItem SelectedTabItem => MainPageViewModel.CurrentInstanceTabBarItems[selectedTabIndex];

private ushort currentTabIndex = 0;
public ushort CurrentTabIndex => currentTabIndex;
Expand All @@ -28,7 +28,7 @@ internal class MultitaskingContext : ObservableObject, IMultitaskingContext

public MultitaskingContext()
{
MainPageViewModel.AppInstances.CollectionChanged += AppInstances_CollectionChanged;
MainPageViewModel.CurrentInstanceTabBarItems.CollectionChanged += AppInstances_CollectionChanged;
App.AppModel.PropertyChanged += AppModel_PropertyChanged;
BaseTabBar.OnLoaded += BaseMultitaskingControl_OnLoaded;
TabBar.SelectedTabItemChanged += HorizontalMultitaskingControl_SelectedTabItemChanged;
Expand All @@ -54,7 +54,7 @@ private void BaseMultitaskingControl_OnLoaded(object? sender, ITabBar control)
private void HorizontalMultitaskingControl_SelectedTabItemChanged(object? sender, TabBarItem? e)
{
isPopupOpen = e is not null;
int newSelectedIndex = e is null ? currentTabIndex : MainPageViewModel.AppInstances.IndexOf(e);
int newSelectedIndex = e is null ? currentTabIndex : MainPageViewModel.CurrentInstanceTabBarItems.IndexOf(e);
UpdateSelectedTabIndex(newSelectedIndex);
}
private void FocusManager_GotFocus(object? sender, FocusManagerGotFocusEventArgs e)
Expand All @@ -64,7 +64,7 @@ private void FocusManager_GotFocus(object? sender, FocusManagerGotFocusEventArgs

if (e.NewFocusedElement is FrameworkElement element && element.DataContext is TabBarItem tabItem)
{
int newSelectedIndex = MainPageViewModel.AppInstances.IndexOf(tabItem);
int newSelectedIndex = MainPageViewModel.CurrentInstanceTabBarItems.IndexOf(tabItem);
UpdateSelectedTabIndex(newSelectedIndex);
}
}
Expand All @@ -81,7 +81,7 @@ private void FocusManager_LosingFocus(object? sender, LosingFocusEventArgs e)

private void UpdateTabCount()
{
SetProperty(ref tabCount, (ushort)MainPageViewModel.AppInstances.Count, nameof(TabCount));
SetProperty(ref tabCount, (ushort)MainPageViewModel.CurrentInstanceTabBarItems.Count, nameof(TabCount));
}
private void UpdateCurrentTabIndex()
{
Expand Down
7 changes: 3 additions & 4 deletions src/Files.App/Data/Models/AppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ public int TabStripSelectedIndex
{
SetProperty(ref tabStripSelectedIndex, value);

if (value >= 0 && value < MainPageViewModel.AppInstances.Count)
if (value >= 0 && value < MainPageViewModel.CurrentInstanceTabBarItems.Count)
{
Frame rootFrame = (Frame)MainWindow.Instance.Content;
var mainView = (MainPage)rootFrame.Content;
mainView.ViewModel.SelectedTabItem = MainPageViewModel.AppInstances[value];
var mainPageViewModel = Ioc.Default.GetRequiredService<MainPageViewModel>();
mainPageViewModel.SelectedTabBarItem = MainPageViewModel.CurrentInstanceTabBarItems[value];
}
}
}
Expand Down
Loading
Loading