Skip to content

Commit

Permalink
Merge pull request #1780 from unoplatform/mergify/bp/legacy/2x/pr-1756
Browse files Browse the repository at this point in the history
fix: FeedView not initializing properly if not visible on first load (backport #1756)
  • Loading branch information
dr1rrb authored Aug 15, 2023
2 parents de0154a + 3baf2b7 commit b14af00
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
23 changes: 23 additions & 0 deletions src/Uno.Extensions.Reactive.UI.Tests/Given_FeedView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.Extensions.Reactive.Core;
using Uno.Extensions.Reactive.Testing;
Expand Down Expand Up @@ -41,6 +42,28 @@ public async Task When_Loading()
await UIHelper.WaitFor(() => isLoadingValues.Count > 0, CT);
}

[TestMethod]
public async Task When_NotVisible_Then_DoesNotSubscribeToSource()
{
// This tests will also ensure that is the FeedView will not try to GoToState while it does not have any template yet.

var isLoaded = false;
var src = Feed.Async(async ct => isLoaded = true);
var sut = new FeedView { Source = src };
var root = new Grid { Visibility = Visibility.Collapsed, Children = { sut } };

await UIHelper.Load(root, CT);

isLoaded.Should().BeFalse("The FeedView should not have subscribed to the source while it is not visible.");

root.Visibility = Visibility.Visible;

await UIHelper.WaitFor(() => isLoaded, CT);

isLoaded.Should().BeTrue("The FeedView should have subscribed to the source when it became visible.");
}


[TestMethod]
public async Task When_GetSource_Then_ContextContainsDispatcher()
{
Expand Down
31 changes: 26 additions & 5 deletions src/Uno.Extensions.Reactive.UI/View/FeedView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,18 @@ public FeedViewRefreshState RefreshingState
}
#endregion

private bool _isReady;
private ControlState _state;
private Subscription? _subscription;

[Flags]
private enum ControlState
{
IsLoaded = 1 << 0,
HasTemplate = 1 << 1,

IsReady = IsLoaded | HasTemplate,
}

/// <summary>
/// Gets a command which request to refresh the source when executed.
/// </summary>
Expand Down Expand Up @@ -236,7 +245,7 @@ private static void Enable(object snd, RoutedEventArgs _)
{
if (snd is FeedView that)
{
that._isReady = true;
that._state |= ControlState.IsLoaded;
if (that.Source is ISignal<IMessage> feed)
{
that.Subscribe(feed);
Expand All @@ -248,16 +257,28 @@ private static void Disable(object snd, RoutedEventArgs _)
{
if (snd is FeedView that)
{
that._isReady = false;
that._state &= ~ControlState.IsLoaded;
that._subscription?.Dispose();
}
}

/// <inheritdoc />
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

_state |= ControlState.HasTemplate;
if (Source is ISignal<IMessage> feed)
{
Subscribe(feed);
}
}

private void Subscribe(ISignal<IMessage>? feed)
{
if (feed is null || !_isReady)
if (feed is null || _state is not ControlState.IsReady)
{
SetIsLoading(!_isReady); // If we set the Source to null while we are already ready, we clear the loading flag.
SetIsLoading(_state is not ControlState.IsReady); // If we set the Source to null while we are already ready, we clear the loading flag.
_subscription?.Dispose();

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
{
#if NET6_0_OR_GREATER && WINDOWS && !HAS_UNO
_window = new Window();
_window.Activate();
#else
_window = Microsoft.UI.Xaml.Window.Current;
#endif

ForceAssemblyLoading();
_window.Content ??= new Uno.UI.RuntimeTests.UnitTestsControl();
_window.Activate();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"RuntimeTests.Windows (Package)": {
"commandName": "MsixPackage"
},
"RuntimeTests.Windows (Unpackaged)": {
"commandName": "Project"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<EnableMsixTooling>true</EnableMsixTooling>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
<DefineConstants>$(DefineConstants);WINDOWS_WINUI;WINUI</DefineConstants>

<!-- Comment this out if you want to handle distributing the WinAppSdk -->
<WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>
<!-- Uncomment for to package dotnet in addition to WinAppSdk -->
<!-- <SelfContained>true</SelfContained>-->
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit b14af00

Please sign in to comment.