Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
State bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed May 23, 2024
1 parent f7c2849 commit eee849a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public string NameOnPage
}
}

public override void InitializeState()
public override async Task InitializeStateAsync()
{
await Task.Delay(1000);
State.Text = $"Counter State initialized on: {renderingEnvironment}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ protected virtual async Task InitializeStateAsync()
statefulViewModel.OnStateInitializedAsync?.Invoke(ViewModel);

StateHasChanged();
ViewModel.ComponentStateContainer?.NotifyStateChanged();
}

protected virtual async Task RestoreStateAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public partial class ComponentStateContainer : ComponentBase
[Parameter]
public string StateElementKey { get; set; } = "state";

protected override void OnInitialized()
{
base.OnInitialized();
ViewModel.ComponentStateContainer = this;
}

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
var stateElement = BuildStateElement();
Expand All @@ -35,4 +41,9 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)

return $"<script id=\"{StateElementKey}\" type=\"text/template\">{stateEncoded}</script>";
}

public void NotifyStateChanged()
{
StateHasChanged();
}
}
3 changes: 3 additions & 0 deletions src/BitzArt.Blazor.MVVM/Factory/ViewModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public ViewModel Create(IServiceProvider serviceProvider, Type viewModelType, Co
var nestedSignature = new ComponentSignature(parent: signature);
var injectedViewModel = Create(serviceProvider, injection.DependencyType, nestedSignature, parent: viewModel, affectedViewModels: affectedViewModels);
injection.Property.SetValue(viewModel, injectedViewModel);

viewModel.OnComponentStateContainerWasSet += (container)
=> injectedViewModel.ComponentStateContainer = container;
}

else if (injection.IsParentViewModelInjection)
Expand Down
14 changes: 14 additions & 0 deletions src/BitzArt.Blazor.MVVM/Models/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ public abstract class ViewModel

internal ComponentSignature Signature { get; set; } = null!;

private ComponentStateContainer? _componentStateContainer;
internal ComponentStateContainer? ComponentStateContainer
{
get => _componentStateContainer;
set
{
_componentStateContainer = value;
OnComponentStateContainerWasSet?.Invoke(value!);
}
}

internal delegate void ComponentStateContainerWasSetHandler(ComponentStateContainer sender);
internal ComponentStateContainerWasSetHandler? OnComponentStateContainerWasSet { get; set; }

protected TViewModel CreateNestedViewModel<TViewModel>()
where TViewModel : ViewModel
{
Expand Down
12 changes: 11 additions & 1 deletion src/BitzArt.Blazor.MVVM/Services/BlazorViewModelStateManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace BitzArt.Blazor.MVVM;

Expand Down Expand Up @@ -27,8 +29,16 @@ internal class BlazorViewModelStateManager(IViewModelFactory viewModelFactory)

if (viewModel is IStatefulViewModel statefulViewModel && statefulViewModel.State.IsInitialized)
{
foreach (var property in statefulViewModel.StateType.GetProperties())
var nodeJson = (JsonObject)JsonSerializer.SerializeToNode(statefulViewModel.State, statefulViewModel.State.GetType(), StateJsonOptionsProvider.Options)!;
foreach (var node in nodeJson) state.Add(node.Key, node.Value);

//var nodeState = JsonSerializer.Deserialize<IDictionary<string, object>>(json, StateJsonOptionsProvider.Options)!;
//foreach (var item in nodeState) state.Add(item.Key, item.Value);

/*foreach (var property in statefulViewModel.StateType.GetProperties())
{
state.Add(property.Name, property.GetValue(statefulViewModel.State));
}*/
}

foreach (var injection in injectionMap.Injections.Where(x => x.IsNestedViewModelInjection))
Expand Down

0 comments on commit eee849a

Please sign in to comment.