Skip to content
This repository was archived by the owner on Feb 16, 2024. It is now read-only.
This repository was archived by the owner on Feb 16, 2024. It is now read-only.

Model-View-(Reactive)Presenter Pattern Example need update #496

@wodndb

Description

@wodndb

Hi,

I want to study about MV(R)P Pattern based UniRx. So I test MV(R)P Example in readme.

ToReactiveProperty method return IReadOnlyReactiveProperty<T>. However IsDead property in Enemy class is ReactiveProperty<T>. So source code in example is not working.

I change a type of IsDead to IReadOnlyReactiveProperty<bool> and the code works well without error.

Modified source code.

using UniRx;
using UnityEngine;
using UnityEngine.UI;

// Presenter for scene(canvas) root.
public class ReactivePresenter : MonoBehaviour
{
    // Presenter is aware of its View (binded in the inspector)
    public Button MyButton;
    public Toggle MyToggle;
    public Text MyText;

    Enemy enemy = new Enemy(1000);

    // Start is called before the first frame update
    void Start()
    {
        // Rx supplies user events from Views and Models in a reactive manner 
        MyButton.OnClickAsObservable().Subscribe(_ => enemy.CurrentHp.Value -= 99);
        MyToggle.OnValueChangedAsObservable().SubscribeToInteractable(MyButton);

        // Models notify Presenters via Rx, and Presenters update their views
        enemy.CurrentHp.SubscribeToText(MyText);
        enemy.IsDead.Where(isDead => isDead == true)
            .Subscribe(_ =>
            {
                MyToggle.interactable = MyButton.interactable = false;
            });
    }
}

// The Model. All property notify when their values change
public class Enemy
{
    public ReactiveProperty<long> CurrentHp { get; private set; }
    public IReadOnlyReactiveProperty<bool> IsDead { get; private set; }

    public Enemy(int initialHp)
    {
        // Declarative Property
        CurrentHp = new ReactiveProperty<long>(initialHp);
        IsDead = CurrentHp.Select(x => x <= 0).ToReactiveProperty();
    }
}

Actually I can't be sure that this source code is right...

Could anybody feedback this source code and update the example?

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions