Repository navigation
Expand file tree
/
Copy pathOutcomeDialog.cs
More file actions
139 lines (117 loc) · 4.74 KB
/
Copy pathOutcomeDialog.cs
File metadata and controls
139 lines (117 loc) · 4.74 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using UnityEngine.UIElements;
namespace OneSignalDemo.UI.Dialogs
{
public enum OutcomeType
{
Normal,
Unique,
WithValue,
}
public class OutcomeDialog : DialogBase
{
private readonly Action<OutcomeType, string, float> _onConfirm;
private OutcomeType _selectedType = OutcomeType.Normal;
private TextField _nameField;
private TextField _valueField;
private VisualElement _valueContainer;
private Button _confirmButton;
private RadioButton _normalRadio;
private RadioButton _uniqueRadio;
private RadioButton _withValueRadio;
public OutcomeDialog(Action<OutcomeType, string, float> onConfirm)
{
_onConfirm = onConfirm;
}
protected override void BuildContent(VisualElement container)
{
var title = new Label("Send Outcome");
title.AddToClassList("dialog-title");
title.AddToClassList("text-dialog-title");
container.Add(title);
var radioGroup = new VisualElement();
_normalRadio = CreateRadio("Normal Outcome", "outcome_type_normal_radio", true);
_uniqueRadio = CreateRadio("Unique Outcome", "outcome_type_unique_radio", false);
_withValueRadio = CreateRadio("Outcome with Value", "outcome_type_value_radio", false);
_normalRadio.RegisterValueChangedCallback(e =>
{
if (e.newValue)
SelectType(OutcomeType.Normal);
});
_uniqueRadio.RegisterValueChangedCallback(e =>
{
if (e.newValue)
SelectType(OutcomeType.Unique);
});
_withValueRadio.RegisterValueChangedCallback(e =>
{
if (e.newValue)
SelectType(OutcomeType.WithValue);
});
radioGroup.Add(_normalRadio);
radioGroup.Add(_uniqueRadio);
radioGroup.Add(_withValueRadio);
container.Add(radioGroup);
_nameField = new TextField();
_nameField.name = "outcome_name_input";
_nameField.AddToClassList("input-field");
SetPlaceholder(_nameField, "Outcome Name");
_nameField.RegisterValueChangedCallback(_ => ValidateInput());
container.Add(_nameField);
_valueContainer = new VisualElement();
_valueContainer.style.display = DisplayStyle.None;
_valueField = new TextField();
_valueField.name = "outcome_value_input";
_valueField.AddToClassList("input-field");
SetPlaceholder(_valueField, "Value");
_valueField.RegisterValueChangedCallback(_ => ValidateInput());
_valueContainer.Add(_valueField);
container.Add(_valueContainer);
var actions = new VisualElement();
actions.AddToClassList("dialog-actions");
actions.Add(CreateCancelButton());
_confirmButton = CreateConfirmButton("Send", OnConfirm);
_confirmButton.name = "outcome_send_button";
_confirmButton.SetEnabled(false);
actions.Add(_confirmButton);
container.Add(actions);
}
private RadioButton CreateRadio(string label, string name, bool selected)
{
var radio = new RadioButton(label);
radio.name = name;
radio.value = selected;
radio.AddToClassList("radio-row");
radio.AddToClassList("text-body-large");
return radio;
}
private void SelectType(OutcomeType type)
{
_selectedType = type;
_normalRadio.SetValueWithoutNotify(type == OutcomeType.Normal);
_uniqueRadio.SetValueWithoutNotify(type == OutcomeType.Unique);
_withValueRadio.SetValueWithoutNotify(type == OutcomeType.WithValue);
_valueContainer.style.display =
type == OutcomeType.WithValue ? DisplayStyle.Flex : DisplayStyle.None;
ValidateInput();
}
private void ValidateInput()
{
bool valid = !string.IsNullOrEmpty(_nameField?.value);
if (_selectedType == OutcomeType.WithValue)
valid = valid && float.TryParse(_valueField?.value, out _);
_confirmButton?.SetEnabled(valid);
}
private void OnConfirm()
{
var name = _nameField?.value;
if (string.IsNullOrEmpty(name))
return;
float value = 0;
if (_selectedType == OutcomeType.WithValue)
float.TryParse(_valueField?.value, out value);
_onConfirm?.Invoke(_selectedType, name, value);
Dismiss();
}
}
}