Giter VIP home page Giter VIP logo

unimob.ui's Introduction

UniMob.UI Github license Unity 2019.3 GitHub package.json version openupm

A declarative library for building reactive user interface. Built over UniMob.

Getting Started

1. Create view

View are regular MonoBehaviour that should be attached to gameObject.

using UniMob.UI;

public class CounterView : View<ICounterState>
{
    public UnityEngine.UI.Text counterText;
    public UnityEngine.UI.Button incrementButton;

    // initial setup
    protected override void Awake()
    {
        base.Awake();

        incrementButton.Click(() => State.Increment);
    }

    // update view with data provided by State
    // Render() is called automatically when data changes
    // so view always be synchronized with state
    protected override void Render()
    {
        counterText.text = "Conter: " + State.Counter;
    }
}

// describes the data required for the view 
// and the actions performed by the view 
public interface IConterState : IViewState {
  int Counter { get; }
  
  void Increment();
}

2. Create widget

A widget is an immutable description of an interface element. May contain additional data if necessary.

using UniMob.UI;

public class CounterWidget : StatefulWidget
{
    public int IncrementStep { get; set; }

    public override State CreateState() => new CounterState();
}

3. Create state

The State provides data for the View and optionally contains mutable state of this interface part.

using UniMob;
using UniMob.UI;

public class CounterState : ViewState<CounterWidget>, ICounterState
{
    // where to load the view from? 
    public override WidgetViewReference View {
        // supports direct prefab link, Resources and Addressables
        get => WidgetViewReference.Resource("Prefabs/Counter View");
    }
    
    [Atom] public int Counter { get; private set; }

    public void Increment()
    {
        // atom modification will automatically update UI
        Counter += Widget.IncrementStep;
    }
}

3. Run app

using UniMob;
using UniMob.UI;

public class CounterApp : UniMobUIApp
{
    protected override Widget Build(BuildContext context)
    {
        return new ConterWidget() {
            IncrementStep = 1
        };
    }
}

Widget Composition

Widgets are the building blocks of a app’s user interface. Widgets form a hierarchy based on composition.

Composition of widgets allows you to build complex custom interfaces that will automatically update when needed.

private Widget Build(BuildContext context) {
    return new ScrollGridFlow {
        CrossAxisAlignment = CrossAxisAlignment.Center,
        MaxCrossAxisExtent = 750.0f,
        Children = {
            this.BuildDailyOffers(),
            
            this.BuildGemsHeader(),
            this.BuildGemsSlots(),

            this.BuildGoldHeader(),
            this.BuildGoldSlots(),
        }
    };
}

private IEnumerable<StoreItem> BuildDailyOffers() {
    return this.DailyOffersModel
        .GetAllOffers()
        .Where(offer => offer.Visible) // subscribe to 'Visible' atom
        .Select(offer => this.BuildDailyOffer(offer.Id);
}

private Widget BuildDailyOffer(string offerId) {
    return new DailyOfferWidget(offerId) {
        // keys must be set for list elements
        Key = Key.Of($"store_item_{offerId}")
    };
}

// ...

More code samples are located in UniMob.UI Samples repository.

Built-in widgets

UniMob.UI comes with a suite of powerful basic widgets:

Row, Column
These widgets let you create layouts in both the horizontal (Row) and vertical (Column) directions.

ZStack
A Stack widget lets you place widgets on top of each other in paint order.

Container
The Container widget lets you create a rectangular visual element that has background color and custom size.

Empty
Widget that displays nothing.

Scroll Grid Flow
The ScrollGridFlow widget lets you create a virtualized scrollable grid of elements.

Grid Flow
A widget lets you create grid of elements.

Vertical Split Box
Column with two elements. Unlike a column allows to specify a stretched size for elements.

Tabs
Widget that displays horizontally scrollable and draggable list of tabs.

Navigator
A widget that manages a set of child widgets.

Composite Transition
Animates the opacity, position, rotation and scale of a widget.

Animated Cross Fade
A widget that cross-fades between two given children and animates itself between their sizes.

Animated Switcher
A widget that by default does a cross-fade between a new widget and the widget previously set on the AnimatedSwitcher as a child.

Dismissible Dialog
A widget with swipe-down-to-dismiss and swipe-up-to-expand callbacks.

Padding Box
Widget that add extra padding for inner element.

UniMob Button
Widget that detects clicks.

UniMob Text
Display and style text.

How to Install

Minimal Unity Version is 2019.3.

Library distributed as git package (How to install package from git URL)
Git URL (UniMob.UI): https://github.com/codewriter-packages/UniMob.UI.git
Git URL (UniMob): https://github.com/codewriter-packages/UniMob.git

License

UniMob.UI is MIT licensed.

Credits

UniMob.UI inspired by Flutter.

unimob.ui's People

Contributors

girildo avatar vanifatovvlad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unimob.ui's Issues

Using `AnimateTo` on TabController with Duration set to 0 never updates `Value`

Describe the bug
A TabController a Duration of 0f and will not update TabController#Value if AnimateTo(int) is used.

Expected behavior
Even if the transition is istantaneous, Value kept in sync with Index

With Duration set to 0 the AnimationTicker is not added and therefore the assignment value is never reassigned.

Desktop: Windows 10
Unity version: 2021.
UniMob version: 2.3.0
UniMob.UI version: 0.5.2
Other plugins installed: UniTask, DoTween

ScrollGridFlow does not always recalculate _visibilityIndices

Describe the bug
While reusing the ScrollGridFlow components, there are some edge cases that break the _visibilityIndices that is not recomputed if the children change.
I think this happens when the normalizedVerticalPosition of the underlying Unity ScrollRect is already 0, thus not invalidating the _visibilityIndices, and one of the two ScrollGridFlowState has 0 children.

Below some code that reproduces the issue with two tabs with a ScrollGridFlow as child, one with 0 children, one with 10 children.

Remark: the TabController in the repro example has a duration of 0.

There are several weird behaviours:

  • Upon scrolling the issue is fixed (this is clear, as this triggers a recompute of _visibilityIndices
  • If the _visibilityIndices moves away from (0, -1), becoming (0, n) [n > 0], the behaviour stays consistent.
  • If the _visibilityIndices becomes (m, n) [m > 0, n > 0], swapping state with an empty ScrollGridFlowState will again break things.
  • Interestingly, dragging the table works as intended. The behaviour only manifests itself if the lateral tab behavior is disabled (duration = 0) and tabs are swapped programmatically.

I could workaround the issue by invalidating the _visibilityIndices atom in Activate() but I am not sure that's a solid fix.
I suspect the problem might be in the Tabs widget, perhaps related to #16 .

Expected behavior
The tabs are always rendered correctly.

Here some code to reproduce the issue.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UniMob.UI;
using UniMob.UI.Widgets;
using UnityEngine;

public class MainApp : UniMob.UI.UniMobUIApp
{
    TabController controller;
    protected override void Initialize()
    {
        controller = new TabController(this.Lifetime, 2, 0); //Important: duration = 0!!
    }

    protected override Widget Build(BuildContext context)
    {
        return new Column()
        {
            CrossAxisSize = AxisSize.Max,
            Children =
            {
                new UniMobButton()
                {
                    OnClick = () => controller.AnimateTo((controller.Index + 1) % 2),
                    Child = new Container()
                    {
                        BackgroundColor = UnityEngine.Color.white,
                        Child = new UniMobText(WidgetSize.Fixed(300, 60))
                        {
                            Value = "Swap tab"
                        }
                    }
                },
                new Tabs(controller)
                {
                    Children =
                    {
                        new ScrollGridFlow()
                        {
                            MaxCrossAxisCount = 1,
                            CrossAxisAlignment = CrossAxisAlignment.End,
                            Children =
                            {
                                Enumerable
                                    .Range(0, 0)
                                    .Select(
                                        i =>
                                            new Container()
                                            {
                                                BackgroundColor = Color.black,
                                                Size = WidgetSize.FixedHeight(
                                                    Mathf.Lerp(200, 200, UnityEngine.Random.value)
                                                ),
                                                Child = new PaddingBox(new RectPadding(1, 1, 1, 1))
                                                {
                                                    Child = new Container()
                                                    {
                                                        BackgroundColor = Color.Lerp(
                                                            Color.red,
                                                            Color.green,
                                                            0
                                                        ),
                                                        Child = new UniMobText(WidgetSize.Stretched)
                                                        {
                                                            Value = i.ToString(),
                                                            MainAxisAlignment =
                                                                MainAxisAlignment.Center,
                                                            CrossAxisAlignment =
                                                                CrossAxisAlignment.Center,
                                                            FontSize = 36
                                                        }
                                                    }
                                                }
                                            }
                                    )
                            },
                        },
                        new ScrollGridFlow()
                        {
                            MaxCrossAxisCount = 1,
                            CrossAxisAlignment = CrossAxisAlignment.End,
                            Sticky = Key.Of(0),
                            Children =
                            {
                                Enumerable
                                    .Range(0, 10)
                                    .Select(
                                        i =>
                                            new Container()
                                            {
                                                BackgroundColor = Color.black,
                                                Size = WidgetSize.FixedHeight(
                                                    Mathf.Lerp(300, 300, UnityEngine.Random.value)
                                                ),
                                                Child = new PaddingBox(new RectPadding(1, 1, 1, 1))
                                                {
                                                    Child = new Container()
                                                    {
                                                        BackgroundColor = Color.Lerp(
                                                            Color.red,
                                                            Color.green,
                                                            (float)i / 10
                                                        ),
                                                        Child = new UniMobText(WidgetSize.Stretched)
                                                        {
                                                            Value = i.ToString(),
                                                            MainAxisAlignment =
                                                                MainAxisAlignment.Center,
                                                            CrossAxisAlignment =
                                                                CrossAxisAlignment.Center,
                                                            FontSize = 36
                                                        }
                                                    }
                                                }
                                            }
                                    )
                            }
                        }
                    }
                }
            }
        };
    }
}

Compilation error on NavigatorView

Hi, I'm at the latest commit on the develop branch and it throws this error:

UniMob.UI\Runtime\Widgets\NavigatorView.cs(25,41): error CS7036: There is no argument given that corresponds to the required formal parameter 'reaction' of 'ReactionAtom.ReactionAtom(Lifetime, string, Action, Action<Exception>)'

It's strange that 1 hour ago there is no issue with my work machine. I've just come back home and open the project on my personal machine.

ScrollGridFlow does not work with children that have an infinite width.

Describe the bug
If all children of a ScrollGridFlow have infinite width, the rendering results in NaN position.

Expected behavior
I would expect that the horizontal layout respects the InfiniteWidth of the children.
In particular, with MaxCrossAxisCount = 1, the ScrollGridFlow should behave like a virtualized ScrollList.

Desktop: Windows 10
Unity version: 2021.
UniMob version: 2.3.0
UniMob.UI version: 0.5.2
Other plugins installed: None (as in it is reproducible with no plugins)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.