Skip to content

Commit 74a1c22

Browse files
committed
refactor(demo): trim inputs and extract RenderPairList
1 parent ef04560 commit 74a1c22

11 files changed

Lines changed: 110 additions & 113 deletions

examples/demo/Assets/Resources/Theme.uss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@
106106
color: var(--os-text-primary);
107107
}
108108

109+
.hint-text {
110+
font-size: 12px;
111+
color: var(--os-text-hint);
112+
-unity-text-align: middle-center;
113+
margin-top: 4px;
114+
white-space: normal;
115+
}
116+
109117
.text-warning-link {
110118
font-size: 12px;
111119
-unity-font-style: bold;

examples/demo/Assets/Scripts/UI/Dialogs/LoginDialog.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ protected override void BuildContent(VisualElement container)
4949

5050
private void ValidateInput()
5151
{
52-
_confirmButton?.SetEnabled(!string.IsNullOrEmpty(_externalIdField?.value));
52+
_confirmButton?.SetEnabled(!string.IsNullOrWhiteSpace(_externalIdField?.value));
5353
}
5454

5555
private void OnConfirm()
5656
{
57-
var value = _externalIdField?.value;
57+
var value = _externalIdField?.value?.Trim();
5858
if (!string.IsNullOrEmpty(value))
5959
{
6060
_externalIdField?.Blur();

examples/demo/Assets/Scripts/UI/Dialogs/MultiPairInputDialog.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private void ValidateAll()
166166
bool allValid = _rows.Count > 0;
167167
foreach (var (key, value, _) in _rows)
168168
{
169-
if (string.IsNullOrEmpty(key.value) || string.IsNullOrEmpty(value.value))
169+
if (string.IsNullOrWhiteSpace(key.value) || string.IsNullOrWhiteSpace(value.value))
170170
{
171171
allValid = false;
172172
break;
@@ -183,8 +183,10 @@ private void OnConfirm()
183183
var dict = new Dictionary<string, string>();
184184
foreach (var (key, value, _) in _rows)
185185
{
186-
if (!string.IsNullOrEmpty(key.value) && !string.IsNullOrEmpty(value.value))
187-
dict[key.value] = value.value;
186+
var trimmedKey = key.value?.Trim();
187+
var trimmedValue = value.value?.Trim();
188+
if (!string.IsNullOrEmpty(trimmedKey) && !string.IsNullOrEmpty(trimmedValue))
189+
dict[trimmedKey] = trimmedValue;
188190
}
189191

190192
if (dict.Count == 0)

examples/demo/Assets/Scripts/UI/Dialogs/PairInputDialog.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ protected override void BuildContent(VisualElement container)
9090
private void ValidateInput()
9191
{
9292
bool valid =
93-
!string.IsNullOrEmpty(_keyField?.value)
94-
&& !string.IsNullOrEmpty(_valueField?.value);
93+
!string.IsNullOrWhiteSpace(_keyField?.value)
94+
&& !string.IsNullOrWhiteSpace(_valueField?.value);
9595
_confirmButton?.SetEnabled(valid);
9696
}
9797

@@ -103,8 +103,8 @@ private void OnConfirm()
103103
if (_submitted)
104104
return;
105105

106-
var key = _keyField?.value;
107-
var value = _valueField?.value;
106+
var key = _keyField?.value?.Trim();
107+
var value = _valueField?.value?.Trim();
108108
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
109109
return;
110110

examples/demo/Assets/Scripts/UI/Dialogs/SingleInputDialog.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ protected override void BuildContent(VisualElement container)
6161

6262
private void ValidateInput()
6363
{
64-
_confirmButton?.SetEnabled(!string.IsNullOrEmpty(_inputField?.value));
64+
_confirmButton?.SetEnabled(!string.IsNullOrWhiteSpace(_inputField?.value));
6565
}
6666

6767
private void OnConfirm()
6868
{
69-
var value = _inputField?.value;
69+
var value = _inputField?.value?.Trim();
7070
if (!string.IsNullOrEmpty(value))
7171
{
7272
_onConfirm?.Invoke(value);

examples/demo/Assets/Scripts/UI/Sections/AliasesSectionController.cs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using OneSignalDemo.ViewModels;
43
using UnityEngine.UIElements;
54

@@ -60,28 +59,13 @@ private VisualElement BuildSection()
6059

6160
private void RefreshList()
6261
{
63-
_listContainer.Clear();
64-
var aliases = _viewModel.Aliases;
65-
66-
if (aliases.Count == 0)
67-
{
68-
_listContainer.Add(
69-
_viewModel.IsLoading
70-
? SectionBuilder.CreateLoadingState("aliases")
71-
: SectionBuilder.CreateEmptyState("No Aliases Added", "aliases")
72-
);
73-
return;
74-
}
75-
76-
for (int i = 0; i < aliases.Count; i++)
77-
{
78-
if (i > 0)
79-
_listContainer.Add(SectionBuilder.CreateDivider(tight: true));
80-
var kvp = aliases[i];
81-
_listContainer.Add(
82-
SectionBuilder.CreateKeyValueItem(kvp.Key, kvp.Value, "aliases", kvp.Key)
83-
);
84-
}
62+
SectionBuilder.RenderPairList(
63+
_listContainer,
64+
_viewModel.Aliases,
65+
"No Aliases Added",
66+
"aliases",
67+
loading: _viewModel.IsLoading
68+
);
8569
}
8670
}
8771
}

examples/demo/Assets/Scripts/UI/Sections/LiveActivitiesSectionController.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class LiveActivitiesSectionController
1313
private Button _startButton;
1414
private Button _updateButton;
1515
private Button _endButton;
16+
private Label _apiKeyHint;
1617

1718
public Action OnInfoTap;
1819

@@ -76,6 +77,11 @@ private VisualElement BuildSection()
7677
);
7778
section.Add(_endButton);
7879

80+
_apiKeyHint = new Label("Set ONESIGNAL_API_KEY in .env to enable update & end");
81+
_apiKeyHint.name = "live_activity_api_key_hint";
82+
_apiKeyHint.AddToClassList("hint-text");
83+
section.Add(_apiKeyHint);
84+
7985
RefreshButtonStates();
8086
return section;
8187
}
@@ -90,14 +96,18 @@ private void RefreshButtonStates()
9096
bool hasActivityId = !string.IsNullOrEmpty(_activityIdField?.value);
9197
bool hasApiKey = _viewModel.HasApiKey;
9298

93-
_startButton?.SetEnabled(hasActivityId && !_viewModel.IsLiveActivityUpdating);
99+
_startButton?.SetEnabled(hasActivityId);
94100

95-
bool canUpdate = hasActivityId && hasApiKey && !_viewModel.IsLiveActivityUpdating;
96-
_updateButton?.SetEnabled(canUpdate);
101+
_updateButton?.SetEnabled(
102+
hasActivityId && hasApiKey && !_viewModel.IsLiveActivityUpdating
103+
);
97104
if (_updateButton != null)
98105
_updateButton.text = $"UPDATE \u2192 {_viewModel.NextStatusLabel}";
99106

100-
_endButton?.SetEnabled(hasActivityId && hasApiKey && !_viewModel.IsLiveActivityUpdating);
107+
_endButton?.SetEnabled(hasActivityId && hasApiKey);
108+
109+
if (_apiKeyHint != null)
110+
_apiKeyHint.style.display = hasApiKey ? DisplayStyle.None : DisplayStyle.Flex;
101111
}
102112

103113
private void OnStartTap()

examples/demo/Assets/Scripts/UI/Sections/SectionBuilder.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using OneSignalDemo.Services;
34
using OneSignalDemo.UI;
45
using UnityEngine.UIElements;
@@ -246,6 +247,47 @@ public static Label CreateLoadingState(string sectionKey)
246247
return label;
247248
}
248249

250+
/// <summary>
251+
/// Mirrors the Capacitor demo's PairList: rebuilds <paramref name="container"/>
252+
/// with a loading state, an empty state, or a divider-separated list of
253+
/// key/value rows. Section-specific button visibility is owned by callers.
254+
/// </summary>
255+
public static void RenderPairList(
256+
VisualElement container,
257+
IReadOnlyList<KeyValuePair<string, string>> items,
258+
string emptyText,
259+
string sectionKey,
260+
bool loading = false,
261+
Action<string> onRemove = null
262+
)
263+
{
264+
container.Clear();
265+
266+
if (items.Count == 0)
267+
{
268+
container.Add(
269+
loading
270+
? CreateLoadingState(sectionKey)
271+
: CreateEmptyState(emptyText, sectionKey)
272+
);
273+
return;
274+
}
275+
276+
for (int i = 0; i < items.Count; i++)
277+
{
278+
if (i > 0)
279+
container.Add(CreateDivider(tight: true));
280+
var kvp = items[i];
281+
var capturedKey = kvp.Key;
282+
Action delete = null;
283+
if (onRemove != null)
284+
delete = () => onRemove(capturedKey);
285+
container.Add(
286+
CreateKeyValueItem(capturedKey, kvp.Value, sectionKey, capturedKey, delete)
287+
);
288+
}
289+
}
290+
249291
private static string SectionKeyFromName(string name) =>
250292
name != null && name.EndsWith("_section")
251293
? name.Substring(0, name.Length - "_section".Length)

examples/demo/Assets/Scripts/UI/Sections/TagsSectionController.cs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Linq;
32
using OneSignalDemo.ViewModels;
43
using UnityEngine.UIElements;
54

@@ -69,37 +68,17 @@ private VisualElement BuildSection()
6968

7069
private void RefreshList()
7170
{
72-
_listContainer.Clear();
73-
var tags = _viewModel.Tags;
74-
75-
if (tags.Count == 0)
76-
{
77-
_listContainer.Add(
78-
_viewModel.IsLoading
79-
? SectionBuilder.CreateLoadingState("tags")
80-
: SectionBuilder.CreateEmptyState("No Tags Added", "tags")
81-
);
82-
_removeSelectedButton.style.display = DisplayStyle.None;
83-
return;
84-
}
85-
86-
_removeSelectedButton.style.display = DisplayStyle.Flex;
71+
SectionBuilder.RenderPairList(
72+
_listContainer,
73+
_viewModel.Tags,
74+
"No Tags Added",
75+
"tags",
76+
loading: _viewModel.IsLoading,
77+
onRemove: key => _viewModel.RemoveTag(key)
78+
);
8779

88-
for (int i = 0; i < tags.Count; i++)
89-
{
90-
if (i > 0)
91-
_listContainer.Add(SectionBuilder.CreateDivider(tight: true));
92-
var kvp = tags[i];
93-
_listContainer.Add(
94-
SectionBuilder.CreateKeyValueItem(
95-
kvp.Key,
96-
kvp.Value,
97-
"tags",
98-
kvp.Key,
99-
() => _viewModel.RemoveTag(kvp.Key)
100-
)
101-
);
102-
}
80+
_removeSelectedButton.style.display =
81+
_viewModel.Tags.Count > 0 ? DisplayStyle.Flex : DisplayStyle.None;
10382
}
10483
}
10584
}

examples/demo/Assets/Scripts/UI/Sections/TriggersSectionController.cs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
2-
using System.Linq;
3-
using OneSignalDemo.Services;
42
using OneSignalDemo.ViewModels;
5-
using UnityEngine;
63
using UnityEngine.UIElements;
74

85
namespace OneSignalDemo.UI.Sections
@@ -101,36 +98,18 @@ private VisualElement BuildSection()
10198

10299
private void RefreshList()
103100
{
104-
_listContainer.Clear();
105-
var triggers = _viewModel.Triggers;
101+
SectionBuilder.RenderPairList(
102+
_listContainer,
103+
_viewModel.Triggers,
104+
"No triggers added",
105+
"triggers",
106+
onRemove: key => _viewModel.RemoveTrigger(key)
107+
);
106108

107-
bool hasTriggers = triggers.Count > 0;
108-
_removeSelectedButton.style.display = hasTriggers
109-
? DisplayStyle.Flex
110-
: DisplayStyle.None;
109+
bool hasTriggers = _viewModel.Triggers.Count > 0;
110+
_removeSelectedButton.style.display =
111+
hasTriggers ? DisplayStyle.Flex : DisplayStyle.None;
111112
_clearAllButton.style.display = hasTriggers ? DisplayStyle.Flex : DisplayStyle.None;
112-
113-
if (!hasTriggers)
114-
{
115-
_listContainer.Add(SectionBuilder.CreateEmptyState("No triggers added", "triggers"));
116-
return;
117-
}
118-
119-
for (int i = 0; i < triggers.Count; i++)
120-
{
121-
if (i > 0)
122-
_listContainer.Add(SectionBuilder.CreateDivider(tight: true));
123-
var kvp = triggers[i];
124-
_listContainer.Add(
125-
SectionBuilder.CreateKeyValueItem(
126-
kvp.Key,
127-
kvp.Value,
128-
"triggers",
129-
kvp.Key,
130-
() => _viewModel.RemoveTrigger(kvp.Key)
131-
)
132-
);
133-
}
134113
}
135114
}
136115
}

0 commit comments

Comments
 (0)