adospace / reactorui-maui

MauiReactor is a MVU UI framework built on top of .NET MAUI
MIT License
588 stars 49 forks source link

ListView Regression in 2.0.7-beta #187

Closed Code-DJ closed 9 months ago

Code-DJ commented 9 months ago

ListView works fine when you have a single ItemTemplate but for multiple ItemTemplates it renders the wrong cached rows.

iOS 2.0.7-beta

Run the following with .UseMauiReactorApp<ListViewPage>():

public class ListViewPage : Component
{
    private readonly IEnumerable<Monkey> _allMonkeys = Monkey.GetList();

    public override VisualNode Render() => ContentPage([
        new ListView(MauiControls.ListViewCachingStrategy.RecycleElementAndDataTemplate)
            .HasUnevenRows(true)
            .ItemsSource(_allMonkeys, RenderMonkeyTemplate)
    ]);

    private ViewCell RenderMonkeyTemplate(Monkey monkey) => monkey.Name switch
    {
        "Capuchin Monkey" or "Howler Monkey" or "Red-shanked Douc" => RenderSpecialMonkeyTemplate(monkey),
        _ => RenderFullMonkeyTemplate(monkey)
    };

    private ViewCell RenderSpecialMonkeyTemplate(Monkey monkey) => ViewCell([
        HorizontalStackLayout([
            Label(monkey.Name)
                .FontSize(12.0)
                .Margin(5)
        ])
        .BackgroundColor(Colors.AliceBlue)
    ]);

    private ViewCell RenderFullMonkeyTemplate(Monkey monkey) => ViewCell([
        HorizontalStackLayout([
            Image()
                .Source(new Uri(monkey.ImageUrl))
                .Margin(4),

            StackLayout([
                Label(monkey.Name)
                    .FontSize(12.0)
                    .Margin(5),

                Label(monkey.Location)
                    .FontSize(12.0)
                    .Margin(5)
            ])
        ])
        .Padding(10)
    ]);
}

public class Monkey
{
    public required string Name { get; set; }
    public required string Location { get; set; }
    public required string Details { get; set; }
    public required string ImageUrl { get; set; }

    public static IReadOnlyList<Monkey> GetList() => new List<Monkey>
    {
        new Monkey
        {
            Name = "Baboon",
            Location = "Africa & Asia",
            Details = "Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg"
        },

        new Monkey
        {
            Name = "Capuchin Monkey",
            Location = "Central & South America",
            Details = "The capuchin monkeys are New World monkeys of the subfamily Cebinae. Prior to 2011, the subfamily contained only a single genus, Cebus.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Capuchin_Costa_Rica.jpg/200px-Capuchin_Costa_Rica.jpg"
        },

        new Monkey
        {
            Name = "Blue Monkey",
            Location = "Central and East Africa",
            Details = "The blue monkey or diademed monkey is a species of Old World monkey native to Central and East Africa, ranging from the upper Congo River basin east to the East African Rift and south to northern Angola and Zambia",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/BlueMonkey.jpg/220px-BlueMonkey.jpg"
        },

        new Monkey
        {
            Name = "Squirrel Monkey",
            Location = "Central & South America",
            Details = "The squirrel monkeys are the New World monkeys of the genus Saimiri. They are the only genus in the subfamily Saimirinae. The name of the genus Saimiri is of Tupi origin, and was also used as an English name by early researchers.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Saimiri_sciureus-1_Luc_Viatour.jpg/220px-Saimiri_sciureus-1_Luc_Viatour.jpg"
        },

        new Monkey
        {
            Name = "Golden Lion Tamarin",
            Location = "Brazil",
            Details = "The golden lion tamarin also known as the golden marmoset, is a small New World monkey of the family Callitrichidae.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Golden_lion_tamarin_portrait3.jpg/220px-Golden_lion_tamarin_portrait3.jpg"
        },

        new Monkey
        {
            Name = "Howler Monkey",
            Location = "South America",
            Details = "Howler monkeys are among the largest of the New World monkeys. Fifteen species are currently recognised. Previously classified in the family Cebidae, they are now placed in the family Atelidae.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Alouatta_guariba.jpg/200px-Alouatta_guariba.jpg"
        },

        new Monkey
        {
            Name = "Japanese Macaque",
            Location = "Japan",
            Details = "The Japanese macaque, is a terrestrial Old World monkey species native to Japan. They are also sometimes known as the snow monkey because they live in areas where snow covers the ground for months each",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Macaca_fuscata_fuscata1.jpg/220px-Macaca_fuscata_fuscata1.jpg"
        },

        new Monkey
        {
            Name = "Mandrill",
            Location = "Southern Cameroon, Gabon, Equatorial Guinea, and Congo",
            Details = "The mandrill is a primate of the Old World monkey family, closely related to the baboons and even more closely to the drill. It is found in southern Cameroon, Gabon, Equatorial Guinea, and Congo.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/75/Mandrill_at_san_francisco_zoo.jpg/220px-Mandrill_at_san_francisco_zoo.jpg"
        },

        new Monkey
        {
            Name = "Proboscis Monkey",
            Location = "Borneo",
            Details = "The proboscis monkey or long-nosed monkey, known as the bekantan in Malay, is a reddish-brown arboreal Old World monkey that is endemic to the south-east Asian island of Borneo.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Proboscis_Monkey_in_Borneo.jpg/250px-Proboscis_Monkey_in_Borneo.jpg"
        },

        new Monkey
        {
            Name = "Red-shanked Douc",
            Location = "Vietnam, Laos",
            Details = "The red-shanked douc is a species of Old World monkey, among the most colourful of all primates. This monkey is sometimes called the \"costumed ape\" for its extravagant appearance. From its knees to its ankles it sports maroon-red \"stockings\", and it appears to wear white forearm length gloves. Its attire is finished with black hands and feet. The golden face is framed by a white ruff, which is considerably fluffier in males. The eyelids are a soft powder blue. The tail is white with a triangle of white hair at the base. Males of all ages have a white spot on both sides of the corners of the rump patch, and red and white genitals.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/Portrait_of_a_Douc.jpg/159px-Portrait_of_a_Douc.jpg"
        },

        new Monkey
        {
            Name = "Gray-shanked Douc",
            Location = "Vietnam",
            Details = "The gray-shanked douc langur is a douc species native to the Vietnamese provinces of Quảng Nam, Quảng Ngãi, Bình Định, Kon Tum, and Gia Lai. The total population is estimated at 550 to 700 individuals. In 2016, Dr Benjamin Rawson, Country Director of Fauna & Flora International - Vietnam Programme, announced a discovery of an additional population of more than 500 individuals found in Central Vietnam, bringing the total population up to approximately 1000 individuals.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Cuc.Phuong.Primate.Rehab.center.jpg/320px-Cuc.Phuong.Primate.Rehab.center.jpg"
        },

        new Monkey
        {
            Name = "Golden Snub-nosed Monkey",
            Location = "China",
            Details = "The golden snub-nosed monkey is an Old World monkey in the Colobinae subfamily. It is endemic to a small area in temperate, mountainous forests of central and Southwest China. They inhabit these mountainous forests of Southwestern China at elevations of 1,500-3,400 m above sea level. The Chinese name is Sichuan golden hair monkey. It is also widely referred to as the Sichuan snub-nosed monkey. Of the three species of snub-nosed monkeys in China, the golden snub-nosed monkey is the most widely distributed throughout China.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Golden_Snub-nosed_Monkeys%2C_Qinling_Mountains_-_China.jpg/165px-Golden_Snub-nosed_Monkeys%2C_Qinling_Mountains_-_China.jpg"
        },

        new Monkey
        {
            Name = "Black Snub-nosed Monkey",
            Location = "China",
            Details = "The black snub-nosed monkey, also known as the Yunnan snub-nosed monkey, is an endangered species of primate in the family Cercopithecidae. It is endemic to China, where it is known to the locals as the Yunnan golden hair monkey and the black golden hair monkey. It is threatened by habitat loss. It was named after Bishop Félix Biet.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/RhinopitecusBieti.jpg/320px-RhinopitecusBieti.jpg"
        },

        new Monkey
        {
            Name = "Tonkin Snub-nosed Monkey",
            Location = "Vietnam",
            Details = "The Tonkin snub-nosed monkey or Dollman's snub-nosed monkey is a slender-bodied arboreal Old World monkey, endemic to northern Vietnam. It is a black and white monkey with a pink nose and lips and blue patches round the eyes. It is found at altitudes of 200 to 1,200 m (700 to 3,900 ft) on fragmentary patches of forest on craggy limestone areas. First described in 1912, the monkey was rediscovered in 1990 but is exceedingly rare. In 2008, fewer than 250 individuals were thought to exist, and the species was the subject of intense conservation effort. The main threats faced by these monkeys is habitat loss and hunting, and the International Union for Conservation of Nature has rated the species as \"critically endangered\".",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9c/Tonkin_snub-nosed_monkeys_%28Rhinopithecus_avunculus%29.jpg/320px-Tonkin_snub-nosed_monkeys_%28Rhinopithecus_avunculus%29.jpg"
        },

        new Monkey
        {
            Name = "Thomas's Langur",
            Location = "Indonesia",
            Details = "Thomas's langur is a species of primate in the family Cercopithecidae. It is endemic to North Sumatra, Indonesia. Its natural habitat is subtropical or tropical dry forests. It is threatened by habitat loss. Its native names are reungkah in Acehnese and kedih in Alas.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Thomas%27s_langur_Presbytis_thomasi.jpg/142px-Thomas%27s_langur_Presbytis_thomasi.jpg"
        },

        new Monkey
        {
            Name = "Purple-faced Langur",
            Location = "Sri Lanka",
            Details = "The purple-faced langur, also known as the purple-faced leaf monkey, is a species of Old World monkey that is endemic to Sri Lanka. The animal is a long-tailed arboreal species, identified by a mostly brown appearance, dark face (with paler lower face) and a very shy nature. The species was once highly prevalent, found in suburban Colombo and the \"wet zone\" villages (areas with high temperatures and high humidity throughout the year, whilst rain deluges occur during the monsoon seasons), but rapid urbanization has led to a significant decrease in the population level of the monkeys.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Semnopithèque_blanchâtre_mâle.JPG/192px-Semnopithèque_blanchâtre_mâle.JPG"
        },

        new Monkey
        {
            Name = "Gelada",
            Location = "Ethiopia",
            Details = "The gelada, sometimes called the bleeding-heart monkey or the gelada baboon, is a species of Old World monkey found only in the Ethiopian Highlands, with large populations in the Semien Mountains. Theropithecus is derived from the Greek root words for \"beast-ape.\" Like its close relatives the baboons, it is largely terrestrial, spending much of its time foraging in grasslands.",
            ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/13/Gelada-Pavian.jpg/320px-Gelada-Pavian.jpg"
        }
    };
}
Code-DJ commented 9 months ago

Hi the ListView issue works now but there seems to be something else that isn't right. Let me know if I should create a new issue.

I get the following error on 2.0.8-beta (works fine on 2.0.5-beta) - see code below:

Operation is not valid due to the current state of the object.
    at at MauiReactor.Internals.Validate.EnsureNotNull[Label](Label value)
    at at MauiReactor.VisualNode`1[[Microsoft.Maui.Controls.Label, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnAnimate()
    at at MauiReactor.VisualElement`1[[Microsoft.Maui.Controls.Label, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnAnimate()
    at at MauiReactor.View`1[[Microsoft.Maui.Controls.Label, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnAnimate()
    at at MauiReactor.Label`1[[Microsoft.Maui.Controls.Label, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnAnimate()
    at at MauiReactor.VisualNode.Animate()

I have a variation of your MudEntry. Run the following with .UseMauiReactorApp<ListViewPage>():

In the simulator make sure you have Connect Hardware Keyboard so you can type fast. As soon as you open the Modal dialog, click in the Editor and start typing fast. The app crashes. If you wait, type one character, wait - then start typing fast, it works.

public class StartPage : Component
{
    public override VisualNode Render() => new NavigationPage()
    {
        new ContentPage()
        {
            new Button("Open Child Page")
                .OnClicked(async () => await Navigation!.PushModalAsync<ChildPage>())
        }
    };
}

public class ChildPageState
{
    public string? SearchText { get; set; }
}

public class ChildPage : Component<ChildPageState>
{
    public override VisualNode Render() => new NavigationPage()
    {
        new ContentPage
        {
            new Grid
            {
                new MudEditor()
                    .Label("Test")
            }
        }
        .Title("Child Page")
        .BackgroundColor(Colors.Aquamarine)
    }
    .OniOS(page => page.Set(MauiControls.PlatformConfiguration.iOSSpecific.Page.ModalPresentationStyleProperty, MauiControls.PlatformConfiguration.iOSSpecific.UIModalPresentationStyle.FormSheet));
}

class MudEditorState
{
    public string? Text { get; set; }
    public bool Focused { get; set; }

    public bool IsEmpty { get; set; } = true;
    public bool Invalidate { get; set; }
}

class MudEditor : Component<MudEditorState>
{
    private MauiControls.Editor? _editorRef;
    private Action<string>? _textChangedAction;
    private string? _label;

    public MudEditor OnTextChanged(Action<string> textChangedAction)
    {
        _textChangedAction = textChangedAction;
        return this;
    }

    public MudEditor Label(string label)
    {
        _label = label;
        return this;
    }

    public override VisualNode Render()
    {
        return new Grid("Auto", "*")
        {
            new Editor(editorRef => _editorRef = editorRef)
                .Text(State.Text ?? "")
                // .OnAfterTextChanged(OnTextChanged)
                .OnTextChanged((s, e) =>
                {
                    SetState(state => state.Invalidate = (State.Text?.Length == 0 && e.NewTextValue.Length > 0) || (State.Text?.Length > 0 && e.NewTextValue.Length == 0));
                    SetState(state => state.Text = e.NewTextValue, State.Invalidate);
                    _textChangedAction?.Invoke(e.NewTextValue);
                })
                .HFill()
                .VFill()
                .OnFocused(() => SetState(s => s.Focused = true))
                .OnUnfocused(() => SetState(s => s.Focused = false)),

            new Label(_label)
                .OnTapped(() => _editorRef?.Focus())
                .Margin(5,0)
                .HStart()
                .VCenter()
                .TranslationY(State.Focused || !State.IsEmpty ? -20 : 0)
                .ScaleX(State.Focused || !State.IsEmpty ? 0.8 : 1.0)
                .AnchorX(0)
                .TextColor(!State.Focused || State.IsEmpty ? Colors.Gray : Colors.Red)
                .WithAnimation(duration: 200),
        }
        .HFill()
        .VFill();
    }
}

side note: not sure why the entire editor is sliding out of view when hardware keyboard is connected. It looks like a MAUI thing though.

adospace commented 9 months ago

Hi, looking at the issue right now, I'm sorry for the problems caused, I'm trying to make it work with both patterns but it seems more complicated than I thought initially. Your help is much appreciated!

adospace commented 9 months ago

out of curiosity do you see (or have measured) any performance gains in adopting the new format for your list views?

Code-DJ commented 9 months ago

Hi @adospace I have mostly done testing on iOS simulators and haven't seen a difference. I think it will be apparent on older devices. Also, I had to revert it with 2.0.8 because for Flyout Page, the pages (master and detail) were not displaying correctly (it was like the page was getting displayed in 100x100 area instead of taking the entire space. Thanks!

adospace commented 9 months ago

Hi, thanks, latest version 2.0.9 contains a new component called FrameRateIndicator: put it anywhere on top of other controls and it will show the realtime FPS.

You need to enable it for the application:

 var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiReactorApp<HomePage>(app =>
                {
                    app.AddResource("Resources/Styles/DefaultTheme.xaml");

                    app.SetWindowsSpecificAssetsDirectory("Assets");
                })
#if DEBUG
                .EnableMauiReactorHotReload()
                //This will enable the FrameRateIndicator widget
                //Disable before publishing the app
                .EnableFrameRateIndicator()
#endif                
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                })
                .UseMauiCommunityToolkit();

            builder.Services.AddSingleton<Services.IncrementService>();

            return builder.Build();
    public override VisualNode Render() 
        => ContentPage([
            Grid("*", "*", [
                new ListView(MauiControls.ListViewCachingStrategy.RecycleElementAndDataTemplate)
                    .HasUnevenRows(true)
                    .ItemsSource(_allMonkeys, RenderMonkeyTemplate),

                new Internals.FrameRateIndicator()
                    .VStart()
                    .HEnd()
                    .BackgroundColor(Colors.White)
            ]),
        ]);

just in case you want to measure page performance drops

Code-DJ commented 9 months ago

@adospace another reason I reverted was because it sounded like the new format was an experiment (as in it may go away). If this is the new way, I will use the new way and report any issues. Thanks!

adospace commented 9 months ago

The new way is an experiment in the sense that until I don't feel it's secure enough for production I won't publish version 2 as stable. The new and classic way to declare components will be part of the stable version 2 and both will be kept in the future as I guess any developer has different preferences (for example, those coming from Flutter should like the new way).

Said that anyone can use one or the other or both in the same app/component. The new way has some pros and cons, in particular, it should have low memory pressure spending a bit more on CPU cycles, but again as you pointed out it could be visible only to lower-end devices or on really complex pages.