Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SohamDas2021 committed Apr 8, 2024
1 parent 0967d46 commit c243131
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static IServiceCollection AddWindowsCustomization(this IServiceCollection
services.AddSingleton<DeveloperFileExplorerViewModel>();
services.AddTransient<DeveloperFileExplorerPage>();

services.AddSingleton<OptimizeDevDriveDialogViewModelFactory>(sp => (cacheLocation, environmentVariable, exampleDevDriveLocation) => ActivatorUtilities.CreateInstance<OptimizeDevDriveDialogViewModel>(sp, cacheLocation, environmentVariable, exampleDevDriveLocation));
services.AddSingleton<OptimizeDevDriveDialogViewModelFactory>(sp => (cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters) => ActivatorUtilities.CreateInstance<OptimizeDevDriveDialogViewModel>(sp, cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters));
services.AddSingleton<DevDriveInsightsViewModel>();
services.AddTransient<DevDriveInsightsPage>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@
<value>Enable end task in taskbar by right click</value>
<comment>The description for the end task on task bar settings card</comment>
</data>
<data name="ExampleText" xml:space="preserve">
<value>Example: </value>
<comment>Example string</comment>
</data>
<data name="EndTaskOnTaskBar.Header" xml:space="preserve">
<value>End Task</value>
<comment>The header for the end task on task bar settings card</comment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
Expand All @@ -18,6 +19,8 @@ public partial class DevDriveOptimizerCardViewModel : ObservableObject
{
public OptimizeDevDriveDialogViewModelFactory OptimizeDevDriveDialogViewModelFactory { get; set; }

public List<string> ExistingDevDriveLetters { get; set; }

public string CacheToBeMoved { get; set; }

public string DevDriveOptimizationSuggestion { get; set; }
Expand All @@ -41,17 +44,28 @@ private async Task OptimizeDevDriveAsync(object sender)
var settingsCard = sender as Button;
if (settingsCard != null)
{
var optimizeDevDriveViewModel = OptimizeDevDriveDialogViewModelFactory(ExistingCacheLocation, EnvironmentVariableToBeSet, ExampleLocationOnDevDrive);
var optimizeDevDriveViewModel = OptimizeDevDriveDialogViewModelFactory(
ExistingCacheLocation,
EnvironmentVariableToBeSet,
ExampleLocationOnDevDrive,
ExistingDevDriveLetters);
var optimizeDevDriveDialog = new OptimizeDevDriveDialog(optimizeDevDriveViewModel);
optimizeDevDriveDialog.XamlRoot = settingsCard.XamlRoot;
optimizeDevDriveDialog.RequestedTheme = settingsCard.ActualTheme;
await optimizeDevDriveDialog.ShowAsync();
}
}

public DevDriveOptimizerCardViewModel(OptimizeDevDriveDialogViewModelFactory optimizeDevDriveDialogViewModelFactory, string cacheToBeMoved, string existingCacheLocation, string exampleLocationOnDevDrive, string environmentVariableToBeSet)
public DevDriveOptimizerCardViewModel(
OptimizeDevDriveDialogViewModelFactory optimizeDevDriveDialogViewModelFactory,
string cacheToBeMoved,
string existingCacheLocation,
string exampleLocationOnDevDrive,
string environmentVariableToBeSet,
List<string> existingDevDriveLetters)
{
OptimizeDevDriveDialogViewModelFactory = optimizeDevDriveDialogViewModelFactory;
ExistingDevDriveLetters = existingDevDriveLetters;
CacheToBeMoved = cacheToBeMoved;
ExistingCacheLocation = existingCacheLocation;
ExampleLocationOnDevDrive = exampleLocationOnDevDrive;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
Expand All @@ -20,6 +21,9 @@ namespace DevHome.Customization.ViewModels.DevDriveInsights;
/// </summary>
public partial class OptimizeDevDriveDialogViewModel : ObservableObject
{
[ObservableProperty]
private List<string> _existingDevDriveLetters;

[ObservableProperty]
private string _exampleDevDriveLocation;

Expand All @@ -41,11 +45,16 @@ public partial class OptimizeDevDriveDialogViewModel : ObservableObject
[ObservableProperty]
private string _directoryPathTextBox;

public OptimizeDevDriveDialogViewModel(string existingCacheLocation, string environmentVariableToBeSet, string exampleDevDriveLocation)
public OptimizeDevDriveDialogViewModel(
string existingCacheLocation,
string environmentVariableToBeSet,
string exampleDevDriveLocation,
List<string> existingDevDriveLetters)
{
DirectoryPathTextBox = string.Empty;
var stringResource = new StringResource("DevHome.Customization.pri", "DevHome.Customization/Resources");
ExampleDevDriveLocation = exampleDevDriveLocation;
ExistingDevDriveLetters = existingDevDriveLetters;
ExampleDevDriveLocation = stringResource.GetLocalized("ExampleText") + exampleDevDriveLocation;
ChooseDirectoryPromptText = stringResource.GetLocalized("ChooseDirectoryPromptText");
MakeChangesText = stringResource.GetLocalized("MakeChangesText");
ExistingCacheLocation = existingCacheLocation;
Expand Down Expand Up @@ -130,6 +139,19 @@ private void SetEnvironmentVariable(string variableName, string value)
}
}

private bool ChosenDirectoryInDevDrive(string directoryPath)
{
foreach (var devDriveLetter in ExistingDevDriveLetters)
{
if (directoryPath.StartsWith(devDriveLetter + ":", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
}

[RelayCommand]
private void DirectoryInputConfirmed()
{
Expand All @@ -138,11 +160,18 @@ private void DirectoryInputConfirmed()
if (!string.IsNullOrEmpty(directoryPath))
{
// Handle the selected folder
// TODO: If chosen folder not a dev drive location, currently we no-op. Instead we should display the error.
MoveDirectory(ExistingCacheLocation, directoryPath);
SetEnvironmentVariable(EnvironmentVariableToBeSet, directoryPath);
// TODO: If chosen folder not a dev drive location, currently we no-op and log the error. Instead we should display the error.
if (ChosenDirectoryInDevDrive(directoryPath))
{
MoveDirectory(ExistingCacheLocation, directoryPath);
SetEnvironmentVariable(EnvironmentVariableToBeSet, directoryPath);

Log.Debug($"Moved cache from {ExistingCacheLocation} to {directoryPath}");
Log.Debug($"Moved cache from {ExistingCacheLocation} to {directoryPath}");
}
else
{
Log.Error($"Chosen directory {directoryPath} not on a dev drive.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,19 @@ public void UpdateOptimizerListViewModelList()
continue;
}

List<string> existingDevDriveLetters = new List<string>();
foreach (var existingDevDrive in ExistingDevDrives)
{
existingDevDriveLetters.Add(existingDevDrive.DriveLetter.ToString());
}

var card = new DevDriveOptimizerCardViewModel(
_optimizeDevDriveDialogViewModelFactory,
cache.CacheName!,
existingCacheLocation,
cache.ExampleDirectory!, // example location on dev drive to move cache to
cache.EnvironmentVariable!); // environmentVariableToBeSet
cache.EnvironmentVariable!, // environmentVariableToBeSet
existingDevDriveLetters);
DevDriveOptimizerCardCollection.Add(card);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Generic;
using DevHome.Customization.ViewModels.DevDriveInsights;
using Microsoft.UI.Xaml.Controls;

namespace DevHome.Customization.Views;

public delegate OptimizeDevDriveDialogViewModel OptimizeDevDriveDialogViewModelFactory(string existingCacheLocation, string environmentVariableToBeSet, string exampleDevDriveLocation);
public delegate OptimizeDevDriveDialogViewModel OptimizeDevDriveDialogViewModelFactory(
string existingCacheLocation,
string environmentVariableToBeSet,
string exampleDevDriveLocation,
List<string> existingDevDriveLetters);

public sealed partial class OptimizeDevDriveDialog : ContentDialog
{
Expand Down

0 comments on commit c243131

Please sign in to comment.