Add tmp progress ring to show applying changes progress

Ignore not needed json fields
This commit is contained in:
Stefan Markovic
2023-09-18 15:11:15 +02:00
parent 4d6cb99a98
commit fd1cfef3f8
5 changed files with 60 additions and 12 deletions

View File

@@ -66,6 +66,7 @@
<KeyboardAccelerator Key="N" Modifiers="Control" />
</Button.KeyboardAccelerators>
</Button>
<ProgressRing IsActive="{x:Bind ViewModel.ApplyingChanges, Mode=TwoWay}" />
</Grid>
<Grid Grid.Row="1" ColumnSpacing="32">
<Grid.ColumnDefinitions>

View File

@@ -6,6 +6,7 @@ using System;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using EnvironmentVariables.Helpers;
using EnvironmentVariables.ViewModels;
using ManagedCommon;
namespace EnvironmentVariables.Models
@@ -18,16 +19,19 @@ namespace EnvironmentVariables.Models
public ProfileVariablesSet()
: base()
{
Type = VariablesSetType.Profile;
IconPath = ProfileIconPath;
}
public ProfileVariablesSet(Guid id, string name)
: base(id, name, VariablesSetType.Profile)
{
IconPath = ProfileIconPath;
}
public void Apply()
public Task Apply()
{
Task.Run(() =>
return Task.Run(() =>
{
foreach (var variable in Variables)
{
@@ -51,13 +55,17 @@ namespace EnvironmentVariables.Models
Logger.LogError("Failed to set profile variable.");
}
}
// viewModel.ApplyingChanges = false;
});
}
public void UnApply()
public Task UnApply()
{
Task.Run(() =>
return Task.Run(() =>
{
var viewModel = App.GetService<MainViewModel>();
viewModel.ApplyingChanges = true;
foreach (var variable in Variables)
{
// Unset the variable
@@ -87,6 +95,8 @@ namespace EnvironmentVariables.Models
}
}
}
viewModel.ApplyingChanges = false;
});
}
}

View File

@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using CommunityToolkit.Mvvm.ComponentModel;
using EnvironmentVariables.Helpers;
@@ -17,8 +18,16 @@ namespace EnvironmentVariables.Models
[ObservableProperty]
private string _values;
public bool Editable { get; private set; }
[JsonIgnore]
public bool Editable
{
get
{
return ParentType != VariablesSetType.System || App.GetService<IElevationHelper>().IsElevated;
}
}
[JsonIgnore]
public VariablesSetType ParentType { get; set; }
public List<string> ValuesList { get; set; }
@@ -32,7 +41,6 @@ namespace EnvironmentVariables.Models
Name = name;
Values = values;
ParentType = parentType;
Editable = ParentType == VariablesSetType.User || (ParentType == VariablesSetType.System && App.GetService<IElevationHelper>().IsElevated);
var splitValues = Values.Split(';');
if (splitValues.Length > 0)

View File

@@ -3,8 +3,8 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.Json.Serialization;
using CommunityToolkit.Mvvm.ComponentModel;
namespace EnvironmentVariables.Models
@@ -16,16 +16,18 @@ namespace EnvironmentVariables.Models
private static readonly string UserIconPath = "/Assets/EnvironmentVariables/UserIcon.png";
private static readonly string SystemIconPath = "/Assets/EnvironmentVariables/SystemIcon.png";
private static readonly string ProfileIconPath = "/Assets/EnvironmentVariables/ProfileIcon.png";
protected static readonly string ProfileIconPath = "/Assets/EnvironmentVariables/ProfileIcon.png";
public Guid Id { get; }
[ObservableProperty]
private string _name;
public VariablesSetType Type { get; }
[JsonIgnore]
public VariablesSetType Type { get; set; }
public string IconPath { get; }
[JsonIgnore]
public string IconPath { get; set; }
[ObservableProperty]
private ObservableCollection<Variable> _variables;

View File

@@ -13,6 +13,7 @@ using CommunityToolkit.Mvvm.Input;
using EnvironmentVariables.Helpers;
using EnvironmentVariables.Models;
using ManagedCommon;
using Microsoft.UI.Dispatching;
namespace EnvironmentVariables.ViewModels
{
@@ -20,6 +21,8 @@ namespace EnvironmentVariables.ViewModels
{
private readonly IEnvironmentVariablesService _environmentVariablesService;
private readonly DispatcherQueue _dispatcherQueue = DispatcherQueue.GetForCurrentThread();
public DefaultVariablesSet UserDefaultSet { get; private set; } = new DefaultVariablesSet(VariablesSet.UserGuid, ResourceLoaderInstance.ResourceLoader.GetString("User"), VariablesSetType.User);
public DefaultVariablesSet SystemDefaultSet { get; private set; } = new DefaultVariablesSet(VariablesSet.SystemGuid, ResourceLoaderInstance.ResourceLoader.GetString("System"), VariablesSetType.System);
@@ -35,6 +38,9 @@ namespace EnvironmentVariables.ViewModels
[ObservableProperty]
private bool _isElevated;
[ObservableProperty]
private bool _applyingChanges;
public ProfileVariablesSet AppliedProfile { get; set; }
public MainViewModel(IEnvironmentVariablesService environmentVariablesService)
@@ -71,6 +77,11 @@ namespace EnvironmentVariables.ViewModels
foreach (var profile in profiles)
{
profile.PropertyChanged += Profile_PropertyChanged;
foreach (var variable in profile.Variables)
{
variable.ParentType = VariablesSetType.Profile;
}
}
var applied = profiles.Where(x => x.IsEnabled).ToList();
@@ -157,7 +168,15 @@ namespace EnvironmentVariables.ViewModels
private void SetAppliedProfile(ProfileVariablesSet profile)
{
profile.Apply();
ApplyingChanges = true;
var task = profile.Apply();
task.ContinueWith((a) =>
{
_dispatcherQueue.TryEnqueue(() =>
{
ApplyingChanges = false;
});
});
AppliedProfile = profile;
PopulateAppliedVariables();
}
@@ -168,7 +187,15 @@ namespace EnvironmentVariables.ViewModels
{
var appliedProfile = AppliedProfile;
appliedProfile.PropertyChanged -= Profile_PropertyChanged;
AppliedProfile.UnApply();
ApplyingChanges = true;
var task = AppliedProfile.UnApply();
task.ContinueWith((a) =>
{
_dispatcherQueue.TryEnqueue(() =>
{
ApplyingChanges = false;
});
});
AppliedProfile.IsEnabled = false;
AppliedProfile = null;
appliedProfile.PropertyChanged += Profile_PropertyChanged;