ユニティ - 異なるモジュールReduxのにUIWidgets 7. Reduxのアクセス(ⅱ)データ

参考QF.UIWidgetsは
ユニティ公式の例を参照する- ConnectAppCNを

私はたびにリターンを減速機、問題を理解するために考えられた、前に言ったnew State()私が解決するにはどのような方法を期待していなかった、それは偉大な廃棄物が発生します。
そして、これらの例を見つけ、全く新しい状態を作成するたびに存在しませんが、直接そのそんなに簡単に......対応する値を変更します。異なるデータモジュールにReduxの構造は、より多くの管理します。

ここでは、簡単なデモを行うにはスクラッチ2つのページで構成されています。「ホームページ」、メッセージ、「NEWPAGE」、ディスプレイのカウントを表示します

エントランス

// Main.cs
using System.Collections.Generic;
using Unity.UIWidgets;
using Unity.UIWidgets.engine;
using Unity.UIWidgets.material;
using Unity.UIWidgets.Redux;
using Unity.UIWidgets.widgets;
using UnityEngine;
using Redux.State;
using Redux.Reducer;
using Redux.View;

namespace Redux
{
    public class Main : UIWidgetsPanel
    {
        protected override void OnEnable()
        {
            base.OnEnable();            
        }

        protected override Widget createWidget()
        {
            var widget = new MaterialApp(
                initialRoute: "/",
                routes: new Dictionary<string, WidgetBuilder>()
                {
                    { "/", context => new HomePage() },
                    { "new", context => new NewPage() },
                }
                );

            var store = new Store<AppState>(
                reducer: AppReducer.Reduce,
                initialState: new AppState()
                );

            return new StoreProvider<AppState>(
                child: widget,
                store: store
                );
        }
    }
}

表示セクション

// View/HomePage.cs
// 因为这里不是重点,NewPage略
using System;
using System.Collections.Generic;
using Unity.UIWidgets.materal;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.Redux;
using Unity.UIWidgets.widgets;
using UnityEngine;

namespace Redux.View
{
    public class HomePage : StatelessWidget
    {
        var widget = new Scaffold(
            appBar: new appBar(
                title: new Text("Home")
                ),
            body: new Center(
                children: new List<Widget>
                {
                    new StoreConnector<AppState, string>(
                        converter: (state) => state.globalState.message,
                        builder: (context1, text, dispatcher) => new Text(text)
                        ),
                    new StoreConnector<AppState, object>(
                        converter: (state) => null,
                        builder: (context2, _, dispatcher) => new RaisedButton(
                            child: new Text("当前时间"),
                            onPressed: () => {
                                dispatcher.dispatch(new Action.Global.SetMessageAction
                                {
                                    message = DateTime.Now.ToString()
                                });
                            }
                            )
                        ),
                    new RaisedButton(
                        child: new Text("打开新页面"),
                        onPressed: () => {
                            Navigator.pushNamed(context, "new");
                        }
                        )
                }
                )
            );

        return widget;
    }
}

状態セクション


// State/AppState.cs
namespace Redux.State
{
    public class AppState
    {
        public BagState bagState;
        public GlobalState globalState;

        public static AppState GetState()
        {
            AppState = new AppState
            {
                bagState = new BagState
                {
                    itemCount = 0;
                },
                globalState = new GlobalState
                {
                    message = "hello",
                },
            }
        }
    }
}

// State/BagState.cs
namespace Redux.State
{
    public class BagState
    {
        public int itemCount;
    }
}

// State/GlobalState.cs
namespace Redux.State
{
    public class GlobalState
    {
        public string message;
    }
}

アクションセクション

// Action/BagAction.cs
namespace Redux.Action
{
    public interface IBagAction {}
}

namespace Redux.Action.Bag
{
    public class AddCountAction : IBagAction
    {
        public int count;
    }

    public class RemoveCountAction : IBagAction
    {
        public int count;
    }
}

// Action/GlobalAction.cs
namespace Redux.Action
{
    public interface IGlobalAction {}
}

namespace Redux.Action.Global
{
    public class SetMessageAction : IGlobalAction
    {
        public string message;
    }
}

減速部


// Reducer/AppReducer.cs
using Redux.Action;
using Redux.State;

namespace Redux.Reducer
{
    public static class AppReducer
    {
        public static AppState Reduce(AppState state, object action)
        {
            switch (action)
            {
                case IBagAction bagAction:
                    return BagReducer.Reduce(state, bagAction);
                case IGlobalAction globalAction:
                    return GlobalReducer.Reduce(state, globalAction);
                default:
                    return state;
            }
        }
    }
}

// Reducer/BagReducer.cs
using Redux.Action;
using Redux.Action.Bag;
using Redux.State;

namespace Redux.Reducer
{
    public static class BagReducer
    {
        public static AppState Reduce(AppState state, IBagAction action)
        {
            switch (action)
            {
                case AddCountAction addCountAction:
                    return OnAddCountAction(state, addCountAction);
                case RemoveCountAction removeCountAction:
                    return OnRemoveCountAction(state, removeCountAction);
                default:
                    return state;
            }
        }

        private static AppState OnAddCountAction(AppState state, AddCountAction action)
        {
            state.bagState.itemCount += action.count;
            return state;
        }

        private static AppState OnRemoveCountAction(AppState state, RemoveCountAction action)
        {
            state.bagState.itemCount -= action.count;
            return state;
        }
    }
}

// Reducer/GlobalReducer.cs
using Redux.Action;
using Redux.Action.Global;
using Redux.State;

namespace Redux.Reducer
{
    public static class GlobalReducer
    {
        public static AppState Reduce(AppState state, IGlobalAction action)
        {
            switch (action)
            {
                case SetMessageAction setMessageAction:
                    return OnSetMessageAction(state, setMessageAction);
                default:
                    return state;
            }
        }

        private static AppState OnSetMessageAction(AppState state, SetMessageAction action)
        {
            state.globalState.message = action.message;
            return state;
        }
    }
}

そのようなモードでは、Reduxの部分(この部分のみ)は、コード生成ツールを記述するために適切と考えることができます

おすすめ

転載: www.cnblogs.com/lunoctis/p/12372670.html