Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 28 additions & 3 deletions src/CommunityToolkit.Maui.UnitTests/Layouts/StateContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public StateContainerTests()

controller = new StateContainerController(layout)
{
StateViews = StateContainer.GetStateViews(layout)
StateViews = [..StateContainer.GetStateViews(layout)]
};

gridController = new StateContainerController(grid)
{
StateViews = StateContainer.GetStateViews(grid)
StateViews = [..StateContainer.GetStateViews(grid)]
};
}

Expand Down Expand Up @@ -594,12 +594,37 @@ public void EnsureDefaults()
var stackLayout = new StackLayout();

// Act Assert
Assert.Equal(StateContainerDefaults.StateViews, StateContainer.GetStateViews(stackLayout));
Assert.Equal([], StateContainer.GetStateViews(stackLayout));
Assert.Empty(StateContainer.GetStateViews(stackLayout));
Assert.Equal(StateContainerDefaults.CurrentState, StateContainer.GetCurrentState(stackLayout));
Assert.Equal(StateContainerDefaults.CanStateChange, StateContainer.GetCanStateChange(stackLayout));
Assert.Equal(StateViewDefaults.StateKey, StateView.GetStateKey(stackLayout));
}

[Fact]
public void EnsureLayoutControllerIsUniquePerLayout()
{
// Arrange
var grid1 = new Grid();
var grid2 = new Grid();

// Act

StateContainer.SetStateViews(grid1, stateViews);
StateContainer.SetStateViews(grid2,
[
new Label() { Text = "Test" },
]);

var grid1StateViews = StateContainer.GetStateViews(grid1);
var grid2StateViews = StateContainer.GetStateViews(grid2);

// Assert
Assert.NotEqual(grid1StateViews, grid2StateViews);
Assert.Equal(stateViews.Count, grid1StateViews.Count);
Assert.Single(StateContainer.GetStateViews(grid2));
Comment thread
TheCodeTraveler marked this conversation as resolved.
Outdated
}

sealed class ViewModel : INotifyPropertyChanged
{
public bool CanChangeState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static StateContainerController ContainerControllerCreator(BindableObject bindab

return new StateContainerController(layoutView)
{
StateViews = GetStateViews(layoutView)
StateViews = [..GetStateViews(layoutView)]
Comment thread
TheCodeTraveler marked this conversation as resolved.
Outdated
};
}

Expand All @@ -192,7 +192,7 @@ static void ValidateCanStateChange(in BindableObject bindable)
}
}

static IList<View> CreateDefaultStateViewsProperty(BindableObject bindable) => StateContainerDefaults.StateViews;
static IList<View> CreateDefaultStateViewsProperty(BindableObject bindable) => [];
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
/// <summary>
/// StateContainer Controller
/// </summary>
sealed class StateContainerController
/// <remarks>
/// Initialize <see cref="StateContainerController"/> with a <see cref="Layout"/>
/// </remarks>
/// <param name="layout"></param>
Comment thread
TheCodeTraveler marked this conversation as resolved.
Outdated
sealed class StateContainerController(Layout layout)
{
readonly WeakReference<Layout> layoutWeakReference;
readonly WeakReference<Layout> layoutWeakReference = new(layout);

string? previousState;
List<IView> originalContent = Enumerable.Empty<IView>().ToList();

/// <summary>
/// Initialize <see cref="StateContainerController"/> with a <see cref="Layout"/>
/// </summary>
/// <param name="layout"></param>
public StateContainerController(Layout layout) => layoutWeakReference = new WeakReference<Layout>(layout);

/// <summary>
/// The StateViews defined in the StateContainer.
/// </summary>
public required IList<View> StateViews { get; set; }
public required IReadOnlyList<View> StateViews { get; init; }
Comment thread
TheCodeTraveler marked this conversation as resolved.
Outdated

/// <summary>
/// Display the default content.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ static class StateContainerDefaults
{
public const string? CurrentState = null;
public const bool CanStateChange = true;
public static IList<View> StateViews { get; } = [];
}
Loading