-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleApp.cs
More file actions
122 lines (102 loc) · 3.31 KB
/
SampleApp.cs
File metadata and controls
122 lines (102 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Reactive.Linq;
using System.Reactive.Subjects;
public class TodoItem
{
public string Text { get; }
public bool Done { get; }
public TodoItem(string text, bool done)
{
Text = text;
Done = done;
}
}
public class AppState
{
public string TodoText { get; }
public List<TodoItem> TodoItems { get; }
public AppState(string todoText, List<TodoItem> todoItems)
{
TodoText = todoText;
TodoItems = new List<TodoItem>(todoItems);
}
}
public static class AppStateManager
{
public static BehaviorSubject<AppState> SampleAppState { get; } =
new BehaviorSubject<AppState>(new AppState("", new List<TodoItem>()));
public static void OnClick()
{
var newTodo = new TodoItem("New Todo", false);
var currentState = SampleAppState.Value;
var newState = new AppState(
currentState.TodoText,
new List<TodoItem>(currentState.TodoItems) { newTodo }
);
SampleAppState.OnNext(newState);
}
}
public class App : BaseComponent, IDisposable
{
private IDisposable _appStateSubscription;
private bool _disposed;
public WidgetStyle TextStyle { get; }
public WidgetStyle ButtonStyle { get; }
public App()
{
TextStyle = new WidgetStyle(
style: new WidgetStyleDef(
styleRules: new StyleRules(
font: new FontDef("roboto-regular", 32)
)
)
);
ButtonStyle = new WidgetStyle(
style: new WidgetStyleDef(
styleRules: new StyleRules(
font: new FontDef("roboto-regular", 32)
),
layout: new YogaStyle(
width: "50%",
padding: new Dictionary<Edge, float> { { Edge.Vertical, 10 } },
margin: new Dictionary<Edge, float> { { Edge.Left, 140 } }
)
)
);
_appStateSubscription = AppStateManager.SampleAppState.Subscribe(latestAppState =>
{
// Ensure that we are updating the Props value with the correct dictionary
props.OnNext(new Dictionary<string, object>
{
{ "todoText", latestAppState.TodoText },
{ "todoItems", latestAppState.TodoItems }
});
});
}
public override IRenderable Render()
{
var children = new List<IRenderable>
{
WidgetNodeFactory.Button("Add todo", AppStateManager.OnClick, ButtonStyle)
};
// Safe access to "todoItems" from Props.Value
if (props.Value.TryGetValue("todoItems", out var todoItemsObj) && todoItemsObj is List<TodoItem> todoItems)
{
foreach (var todoItem in todoItems)
{
string text = $"{todoItem.Text} ({(todoItem.Done ? "done" : "to do")}).";
children.Add(WidgetNodeFactory.UnformattedText(text, TextStyle));
}
}
return WidgetNodeFactory.Node(children);
}
public override void Dispose()
{
_appStateSubscription?.Dispose();
GC.SuppressFinalize(this);
base.Dispose();
}
}
public class Root : BaseComponent
{
public override IRenderable Render() => WidgetNodeFactory.RootNode(new List<IRenderable> { new App() });
}