Skip to content

Commit

Permalink
Merge pull request #731 from IvanJosipovic/dev
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
IvanJosipovic authored Sep 25, 2024
2 parents a27331b + 208cc18 commit 7dbfac2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/KubeUI/Client/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public sealed partial class Cluster : ObservableObject, ICluster
private IGenerator _generator;

[ObservableProperty]
private bool _isExpanded = true;
private bool _isExpanded;

[ObservableProperty]
private ConcurrentObservableDictionary<NamespacedName, V1Namespace> _namespaces = [];
Expand Down
6 changes: 6 additions & 0 deletions src/KubeUI/ViewModels/NavigationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public async void TreeView_SelectionChanged(object? item)
if (item is Cluster cluster)
{
_ = Task.Run(cluster.Connect);
cluster.IsExpanded = !cluster.IsExpanded;
}
else if (item is ResourceNavigationLink resourceNavLink)
{
Expand All @@ -30,6 +31,11 @@ public async void TreeView_SelectionChanged(object? item)
{
await SelectNavigationLink(navLink);
}

if (item is NavigationItem nav)
{
nav.IsExpanded = !nav.IsExpanded;
}
}

private void SelectResourceNavigationLink(ResourceNavigationLink link)
Expand Down
51 changes: 43 additions & 8 deletions src/KubeUI/ViewModels/ResourceListViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Specialized;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using Avalonia.Data.Converters;
using Avalonia.Styling;
Expand Down Expand Up @@ -1323,11 +1325,10 @@ private void SelectedNamespaces_CollectionChanged(object? sender, NotifyCollecti
{
var exp = JsonPathLINQ.JsonPathLINQ.GetExpression<T, Enum>(item.JsonPath, true);

var colDef = new ResourceListViewDefinitionColumn<T, Enum>()
var colDef = new ResourceListViewDefinitionColumn<T, string>()
{
Name = item.Name,
Display = TransformToFuncOfString<T>(exp.Body, exp.Parameters).Compile(),
Field = exp.Compile(),
Field = TransformToFuncOfString<T>(exp.Body, exp.Parameters).Compile(),
//Width = "*"
};

Expand All @@ -1346,7 +1347,7 @@ private void SelectedNamespaces_CollectionChanged(object? sender, NotifyCollecti
{
var typeString = match.Groups[1].Value;
typeString = typeString.TrimStart("System.Nullable`1[").TrimEnd("]");
var type = Type.GetType(match.Groups[1].Value);
var type = Type.GetType(typeString);

if (type == null)
{
Expand Down Expand Up @@ -1432,11 +1433,45 @@ private void SelectedNamespaces_CollectionChanged(object? sender, NotifyCollecti

private static Expression<Func<T, string>> TransformToFuncOfString<T>(Expression expression, ReadOnlyCollection<ParameterExpression> parameters)

Check warning on line 1434 in src/KubeUI/ViewModels/ResourceListViewModel.cs

View workflow job for this annotation

GitHub Actions / Build & Test

Type parameter 'T' has the same name as the type parameter from outer type 'ResourceListViewModel<T>'

Check warning on line 1434 in src/KubeUI/ViewModels/ResourceListViewModel.cs

View workflow job for this annotation

GitHub Actions / Publish Installers (linux-arm64, KubeUI.Desktop, ubuntu-latest)

Type parameter 'T' has the same name as the type parameter from outer type 'ResourceListViewModel<T>'

Check warning on line 1434 in src/KubeUI/ViewModels/ResourceListViewModel.cs

View workflow job for this annotation

GitHub Actions / Publish Installers (osx-x64, KubeUI.Desktop, macos-latest, --icon ../KubeUI/Assets/icon.icns --pl...

Type parameter 'T' has the same name as the type parameter from outer type 'ResourceListViewModel<T>'

Check warning on line 1434 in src/KubeUI/ViewModels/ResourceListViewModel.cs

View workflow job for this annotation

GitHub Actions / Publish Installers (win-x64, KubeUI.Desktop.exe, windows-latest, --icon ../KubeUI/Assets/icon.ico)

Type parameter 'T' has the same name as the type parameter from outer type 'ResourceListViewModel<T>'

Check warning on line 1434 in src/KubeUI/ViewModels/ResourceListViewModel.cs

View workflow job for this annotation

GitHub Actions / Publish Installers (win-arm64, KubeUI.Desktop.exe, windows-latest, --icon ../KubeUI/Assets/icon.ico)

Type parameter 'T' has the same name as the type parameter from outer type 'ResourceListViewModel<T>'
{
// Convert the body of the original expression to return a string
var bodyAsString = Expression.Call(expression, nameof(string.ToString), Type.EmptyTypes);
// Check if the expression type is an enum
if (expression.Type == typeof(Enum))
{
// Create a method to get the enum member name from the JsonStringEnumMemberNameAttribute
var getEnumMemberNameMethod = typeof(ResourceListViewModel<V1Pod>).GetMethod(nameof(GetEnumMemberName), BindingFlags.NonPublic | BindingFlags.Static)
.MakeGenericMethod(expression.Type);

// Call the method to get the enum member name
var bodyAsString = Expression.Call(getEnumMemberNameMethod, expression);

// Create a new lambda expression
return Expression.Lambda<Func<T, string>>(bodyAsString, parameters);
}
else
{
// Convert the body of the original expression to return a string
var bodyAsString = Expression.Condition(
Expression.Equal(expression, Expression.Constant(null, expression.Type)),
Expression.Constant(string.Empty),
Expression.Call(expression, nameof(object.ToString), Type.EmptyTypes)
);

// Create a new lambda expression
return Expression.Lambda<Func<T, string>>(bodyAsString, parameters);
}
}

// Create a new lambda expression
return Expression.Lambda<Func<T, string>>(bodyAsString, parameters);
private static string GetEnumMemberName<TEnum>(TEnum enumValue) where TEnum : Enum
{
var memberInfo = typeof(TEnum).GetMember(enumValue.ToString()).FirstOrDefault();
if (memberInfo != null)
{
var attribute = memberInfo.GetCustomAttribute<JsonStringEnumMemberNameAttribute>();
if (attribute != null)
{
return attribute.Name;
}
}
return enumValue.ToString();
}

#region Actions
Expand Down

0 comments on commit 7dbfac2

Please sign in to comment.