Repository navigation
chore: [SDK-5531] add sticky log view to demo app #905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c6ebd90
chore: [SDK-5531] add sticky log view to demo app
fadi-george 79f6bbe
chore: [SDK-5531] defer log view updates off the SDK listener
fadi-george dd356b7
chore: [SDK-5531] keep app bar shadow above the log view
fadi-george 8a8d6e4
chore: [SDK-5531] remove app bar shadow from demo
fadi-george File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using OneSignalSDK; | ||
| using UnityEngine; | ||
|
|
||
| namespace OneSignalDemo.Services | ||
| { | ||
| public readonly struct LogEntry | ||
| { | ||
| public readonly string Time; | ||
| public readonly string Message; | ||
|
|
||
| public LogEntry(string time, string message) | ||
| { | ||
| Time = time; | ||
| Message = message; | ||
| } | ||
| } | ||
|
|
||
| public static class LogService | ||
| { | ||
| private const int MaxEntries = 100; | ||
|
|
||
| // In memory only, so each launch starts empty and the Appium suite can assert per-launch counts. | ||
| private static readonly List<LogEntry> _entries = new(); | ||
|
|
||
| public static IReadOnlyList<LogEntry> Entries => _entries; | ||
|
|
||
| public static event Action Changed; | ||
|
|
||
| public static void Info(string message) => Debug.Log($"[OneSignal] {message}"); | ||
|
|
||
| public static void Show(string message) | ||
| { | ||
| Info(message); | ||
| AddToView(message); | ||
| } | ||
|
|
||
| public static void Error(string message) | ||
| { | ||
| Debug.LogError($"[OneSignal] {message}"); | ||
| AddToView(message); | ||
| } | ||
|
|
||
| public static void Clear() => | ||
| UnityMainThreadDispatch.Post(_ => | ||
| { | ||
| _entries.Clear(); | ||
| Changed?.Invoke(); | ||
| }); | ||
|
|
||
| // Deferred so a view rebuild can never throw into the SDK listener that logged. | ||
| private static void AddToView(string message) | ||
| { | ||
| var entry = new LogEntry( | ||
| DateTime.Now.ToString("HH:mm:ss", CultureInfo.InvariantCulture), | ||
| message | ||
| ); | ||
| UnityMainThreadDispatch.Post(_ => | ||
| { | ||
| _entries.Add(entry); | ||
| if (_entries.Count > MaxEntries) | ||
| _entries.RemoveRange(0, _entries.Count - MaxEntries); | ||
| Changed?.Invoke(); | ||
| }); | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| using OneSignalDemo.Services; | ||
| using UnityEngine; | ||
| using UnityEngine.UIElements; | ||
|
|
||
| namespace OneSignalDemo.UI | ||
| { | ||
| public class LogView | ||
| { | ||
| public VisualElement Root { get; } | ||
|
|
||
| private readonly Label _headerLabel; | ||
| private readonly Button _toggleButton; | ||
| private readonly ScrollView _body; | ||
| private bool _collapsed; | ||
|
|
||
| public LogView() | ||
| { | ||
| Root = new VisualElement(); | ||
| Root.name = "log_view"; | ||
| Root.AddToClassList("log-view"); | ||
|
|
||
| var header = new VisualElement(); | ||
| header.AddToClassList("log-header"); | ||
|
|
||
| _headerLabel = new Label(); | ||
| _headerLabel.AddToClassList("log-header-label"); | ||
| header.Add(_headerLabel); | ||
|
|
||
| var clearButton = new Button(LogService.Clear); | ||
| clearButton.name = "log_clear_button"; | ||
| clearButton.text = MaterialIcons.Delete; | ||
| clearButton.AddToClassList("log-header-button"); | ||
| clearButton.AddToClassList("log-clear-button"); | ||
| AccessibilityBridge.RegisterE2ETapTarget(clearButton, () => true, LogService.Clear); | ||
| header.Add(clearButton); | ||
|
|
||
| _toggleButton = new Button(ToggleCollapsed); | ||
| _toggleButton.name = "log_toggle_button"; | ||
| _toggleButton.AddToClassList("log-header-button"); | ||
| _toggleButton.AddToClassList("log-toggle-button"); | ||
| AccessibilityBridge.RegisterE2ETapTarget(_toggleButton, () => true, ToggleCollapsed); | ||
| header.Add(_toggleButton); | ||
|
|
||
| Root.Add(header); | ||
|
|
||
| _body = new ScrollView(ScrollViewMode.Vertical); | ||
| _body.AddToClassList("log-body"); | ||
| _body.contentContainer.AddToClassList("log-body-content"); | ||
| _body.verticalScrollerVisibility = ScrollerVisibility.Hidden; | ||
| _body.horizontalScrollerVisibility = ScrollerVisibility.Hidden; | ||
| _body.touchScrollBehavior = ScrollView.TouchScrollBehavior.Clamped; | ||
| _body.contentContainer.RegisterCallback<GeometryChangedEvent>(_ => ScrollToEnd()); | ||
| Root.Add(_body); | ||
|
|
||
| // Entries is static, so only stay subscribed while attached or rebuilt screens leak. | ||
| Root.RegisterCallback<AttachToPanelEvent>(_ => | ||
| { | ||
| LogService.Changed += Render; | ||
| Render(); | ||
| }); | ||
| Root.RegisterCallback<DetachFromPanelEvent>(_ => LogService.Changed -= Render); | ||
|
|
||
| ApplyCollapsed(); | ||
| } | ||
|
|
||
| private void ToggleCollapsed() | ||
| { | ||
| _collapsed = !_collapsed; | ||
| ApplyCollapsed(); | ||
| AccessibilityBridge.RequestResync(); | ||
| } | ||
|
|
||
| private void ApplyCollapsed() | ||
| { | ||
| _body.style.display = _collapsed ? DisplayStyle.None : DisplayStyle.Flex; | ||
| _toggleButton.text = _collapsed ? MaterialIcons.ExpandMore : MaterialIcons.ExpandLess; | ||
| if (!_collapsed) | ||
| ScrollToEnd(); | ||
| } | ||
|
|
||
| // Rebuilt in full so `log_entry_{index}` stays 0-based after the oldest entries are dropped. | ||
| private void Render() | ||
| { | ||
| var entries = LogService.Entries; | ||
| _headerLabel.text = $"LOGS ({entries.Count})"; | ||
| _body.Clear(); | ||
|
|
||
| if (entries.Count == 0) | ||
| { | ||
| var empty = new Label("No logs yet"); | ||
| empty.AddToClassList("log-empty"); | ||
| _body.Add(empty); | ||
| } | ||
|
|
||
| for (var i = 0; i < entries.Count; i++) | ||
| { | ||
| var row = new VisualElement(); | ||
| row.AddToClassList("log-entry"); | ||
|
|
||
| var time = new Label(entries[i].Time); | ||
| time.AddToClassList("log-entry-time"); | ||
| row.Add(time); | ||
|
|
||
| // Plain text (no rich-text spans) so the a11y bridge reports the message verbatim. | ||
| var message = new Label(entries[i].Message); | ||
| message.name = $"log_entry_{i}"; | ||
| message.enableRichText = false; | ||
| message.AddToClassList("log-entry-message"); | ||
| row.Add(message); | ||
|
|
||
| _body.Add(row); | ||
| } | ||
|
|
||
| AccessibilityBridge.RequestResync(); | ||
| } | ||
|
|
||
| // Deferred a tick so the ScrollView has picked up the new content height. | ||
| private void ScrollToEnd() | ||
| { | ||
| if (_collapsed) | ||
| return; | ||
| _body.schedule.Execute(() => | ||
| { | ||
| var overflow = | ||
| _body.contentContainer.layout.height - _body.contentViewport.layout.height; | ||
| if (overflow > 0) | ||
| _body.scrollOffset = new Vector2(0, overflow); | ||
| }); | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.