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

DYN-6857 homepage settings refactor #15223

Merged
merged 1 commit into from
May 17, 2024
Merged
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
18 changes: 4 additions & 14 deletions src/DynamoCore/Configuration/PreferenceSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,19 +397,8 @@ public bool IsBackgroundPreviewActive

/// <summary>
/// Persistence for Dynamo HomePage
/// </summary>
[XmlIgnore]
internal Dictionary<string, object> HomePageSettings { get; set; }

/// <summary>
/// A helper intermediary string to allow the serialization of the HomePageSettings dictionary
/// </summary>
public string HomePageSettingsSerialized
{
get => Newtonsoft.Json.JsonConvert.SerializeObject(HomePageSettings);
set => HomePageSettings = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(value);
}

/// </summary>
public List<string> HomePageSettings { get; set; }
#endregion

#region Dynamo application settings
Expand Down Expand Up @@ -997,6 +986,7 @@ public PreferenceSettings()
backupLocation = string.Empty;
GraphChecksumItemsList = new List<GraphChecksumItem>();
isMLAutocompleteTOUApproved = true;
HomePageSettings = new List<string>();
}

/// <summary>
Expand Down Expand Up @@ -1114,7 +1104,7 @@ public static PreferenceSettings LoadContent(string content)
return new PreferenceSettings() { isCreatedFromValidFile = false };
}
}

settings.CustomPackageFolders = settings.CustomPackageFolders.Distinct().ToList();
settings.GroupStyleItemsList = settings.GroupStyleItemsList.GroupBy(entry => entry.Name).Select(result => result.First()).ToList();
MigrateStdLibTokenToBuiltInToken(settings);
Expand Down
4 changes: 2 additions & 2 deletions src/DynamoCore/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ Dynamo.Configuration.PreferenceSettings.HideAutocompleteMethodOptions.get -> boo
Dynamo.Configuration.PreferenceSettings.HideAutocompleteMethodOptions.set -> void
Dynamo.Configuration.PreferenceSettings.HideNodesBelowSpecificConfidenceLevel.get -> bool
Dynamo.Configuration.PreferenceSettings.HideNodesBelowSpecificConfidenceLevel.set -> void
Dynamo.Configuration.PreferenceSettings.HomePageSettingsSerialized.get -> string
Dynamo.Configuration.PreferenceSettings.HomePageSettingsSerialized.set -> void
Dynamo.Configuration.PreferenceSettings.HomePageSettings.get -> System.Collections.Generic.List<string>
Dynamo.Configuration.PreferenceSettings.HomePageSettings.set -> void
Dynamo.Configuration.PreferenceSettings.IronPythonResolveTargetVersion.get -> string
Dynamo.Configuration.PreferenceSettings.IronPythonResolveTargetVersion.set -> void
Dynamo.Configuration.PreferenceSettings.IsADPAnalyticsReportingApproved.get -> bool
Expand Down
53 changes: 43 additions & 10 deletions src/DynamoCoreWpf/Views/HomePage/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ private async void SendRecentGraphsData()
{
if (startPage.DynamoViewModel.PreferenceSettings.HomePageSettings != null)
{
var settingsJson = Newtonsoft.Json.JsonConvert.SerializeObject(startPage.DynamoViewModel.PreferenceSettings.HomePageSettings);
var dict = GetDictFromListStrings(startPage.DynamoViewModel.PreferenceSettings.HomePageSettings);
var settingsJson = Newtonsoft.Json.JsonConvert.SerializeObject(dict);
settingsJson = System.Web.HttpUtility.JavaScriptStringEncode(settingsJson);

await dynWebView.CoreWebView2.ExecuteScriptAsync(@$"window.setHomePageSettings('{settingsJson}');");
Expand Down Expand Up @@ -442,30 +443,62 @@ internal void SaveSettings(string settingsJson)
{
if (!string.IsNullOrEmpty(settingsJson) && this.startPage != null)
{
var settingsDict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(settingsJson);
var settingsDict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(settingsJson);

// If the HomePageSettings have not been previously created, initialize it now
if (startPage.DynamoViewModel.PreferenceSettings.HomePageSettings == null)
{
startPage.DynamoViewModel.PreferenceSettings.HomePageSettings = new Dictionary<string, object>();
}
startPage.DynamoViewModel.PreferenceSettings.HomePageSettings = new List<string>();
}

var dict = GetDictFromListStrings(startPage.DynamoViewModel.PreferenceSettings.HomePageSettings);

// Change existing values, or add new ones
foreach (var setting in settingsDict)
{
if (startPage.DynamoViewModel.PreferenceSettings.HomePageSettings.ContainsKey(setting.Key)
&& startPage.DynamoViewModel.PreferenceSettings.HomePageSettings[setting.Key] != setting.Value)
if (dict.ContainsKey(setting.Key))
{
startPage.DynamoViewModel.PreferenceSettings.HomePageSettings[setting.Key] = setting.Value;
if (dict[setting.Key] == setting.Value) continue;
dict[setting.Key] = setting.Value;
}
else
{
startPage.DynamoViewModel.PreferenceSettings.HomePageSettings.Add(setting.Key, setting.Value);
}
dict.Add(setting.Key, setting.Value);
}
}

SetListStringsFromDict(dict, startPage.DynamoViewModel.PreferenceSettings.HomePageSettings);
}
}

private void SetListStringsFromDict(Dictionary<string, string> dict, List<string> homePageSettings)
{
homePageSettings.Clear();
foreach (var kvp in dict)
{
homePageSettings.Add($"{kvp.Key}:{kvp.Value}");
}
}

private Dictionary<string, string> GetDictFromListStrings(List<string> homePageSettings)
{
var dict = new Dictionary<string, string>();
if (!homePageSettings.Any()) return dict;

try
{
dict = homePageSettings.Select(item => item.Split(':'))
.Where(parts => parts.Length == 2)
.ToDictionary(parts => parts[0], parts => parts[1]);
}
catch(Exception ex)
{
this.startPage.DynamoViewModel.Model.Logger.Log("Failed to load HomePageSettings: " + ex.Message);
}

return dict;
}

internal void NewWorkspace()
{
this.startPage?.DynamoViewModel?.NewHomeWorkspaceCommand.Execute(null);
Expand Down
7 changes: 3 additions & 4 deletions test/DynamoCoreTests/Configuration/PreferenceSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,15 @@ public void TestSerializingHomePageSettings()
PreferenceSettings settings = new PreferenceSettings();

// Assert defaults
Assert.AreEqual(settings.HomePageSettings, null);
Assert.AreEqual(settings.HomePageSettingsSerialized, "null");
Assert.IsEmpty(settings.HomePageSettings);

settings.HomePageSettings = new Dictionary<string, object> { { "greeting", "Hello World" } };
settings.HomePageSettings = new List<string> { { String.Concat("greeting", "Hello World") } };

// Save
settings.Save(tempPath);
settings = PreferenceSettings.Load(tempPath);

Assert.AreEqual(settings.HomePageSettings["greeting"], "Hello World");
Assert.IsTrue(settings.HomePageSettings.Contains(String.Concat("greeting", "Hello World")));
}
}
}
2 changes: 1 addition & 1 deletion test/DynamoCoreWpfTests/HomePageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void HomePage_NewSettingsAreAddedCorrectly()

homePage.DataContext = startPage;

Assert.IsNull(preferences.HomePageSettings);
Assert.IsEmpty(preferences.HomePageSettings);

// Act
var pair1 = @"{""Name"": ""Alice""}";
Expand Down
5 changes: 4 additions & 1 deletion test/settings/DynamoSettings-NewSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
<WindowW>1936</WindowW>
<WindowH>1056</WindowH>
<UseHardwareAcceleration>false</UseHardwareAcceleration>
<HomePageSettingsSerialized>{"recentPageViewMode":"grid","samplesViewMode":"list"}</HomePageSettingsSerialized>
<HomePageSettings>
<string>recentPageViewMode:list</string>
<string>samplesViewMode:list</string>
</HomePageSettings>
<NumberFormat>f4</NumberFormat>
<MaxNumRecentFiles>12</MaxNumRecentFiles>
<RecentFiles>
Expand Down
Loading