Skip to content

Commit 90ee926

Browse files
committed
chore(demo): add empty state to log view
1 parent fbaa888 commit 90ee926

3 files changed

Lines changed: 31 additions & 20 deletions

File tree

examples/demo/Controls/LogView.xaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,19 @@
8686
BackgroundColor="{StaticResource OsLogBackground}"
8787
AutomationId="log_view_list"
8888
>
89-
<VerticalStackLayout x:Name="LogList" Spacing="0" Padding="12,0" />
89+
<Grid>
90+
<Label
91+
x:Name="EmptyLabel"
92+
Text="No logs yet"
93+
TextColor="{StaticResource OsGrey500}"
94+
FontSize="11"
95+
FontFamily="DroidSansMono"
96+
HorizontalOptions="Center"
97+
VerticalOptions="Center"
98+
AutomationId="log_view_empty"
99+
/>
100+
<VerticalStackLayout x:Name="LogList" Spacing="0" Padding="12,0" />
101+
</Grid>
90102
</ScrollView>
91103
</Grid>
92104
</ContentView>

examples/demo/Controls/LogView.xaml.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ public LogView()
1818

1919
private void OnLogAdded(object? sender, EventArgs e)
2020
{
21-
MainThread.BeginInvokeOnMainThread(() =>
22-
{
23-
RebuildLogList();
24-
_ = ScrollToBottomAsync();
25-
});
21+
MainThread.BeginInvokeOnMainThread(RebuildLogList);
2622
}
2723

2824
private void RebuildLogList()
@@ -31,8 +27,9 @@ private void RebuildLogList()
3127
LogList.Children.Clear();
3228
_logRows.Clear();
3329

34-
for (int i = 0; i < logs.Count; i++)
30+
for (int i = logs.Count - 1; i >= 0; i--)
3531
{
32+
var displayIndex = logs.Count - 1 - i;
3633
var entry = logs[i];
3734
var row = new HorizontalStackLayout { Spacing = 4, Padding = new Thickness(0, 1) };
3835

@@ -43,7 +40,7 @@ private void RebuildLogList()
4340
FontSize = 11,
4441
FontFamily = "DroidSansMono",
4542
VerticalOptions = LayoutOptions.Center,
46-
AutomationId = $"log_entry_{i}_timestamp",
43+
AutomationId = $"log_entry_{displayIndex}_timestamp",
4744
};
4845
var level = new Label
4946
{
@@ -54,7 +51,7 @@ private void RebuildLogList()
5451
FontAttributes = FontAttributes.Bold,
5552
VerticalOptions = LayoutOptions.Center,
5653
Margin = new Thickness(4, 0),
57-
AutomationId = $"log_entry_{i}_level",
54+
AutomationId = $"log_entry_{displayIndex}_level",
5855
};
5956
var msg = new Label
6057
{
@@ -63,13 +60,13 @@ private void RebuildLogList()
6360
FontSize = 11,
6461
FontFamily = "DroidSansMono",
6562
VerticalOptions = LayoutOptions.Center,
66-
AutomationId = $"log_entry_{i}_message",
63+
AutomationId = $"log_entry_{displayIndex}_message",
6764
};
6865

6966
row.Children.Add(ts);
7067
row.Children.Add(level);
7168
row.Children.Add(msg);
72-
row.AutomationId = $"log_entry_{i}";
69+
row.AutomationId = $"log_entry_{displayIndex}";
7370

7471
LogList.Children.Add(row);
7572
_logRows.Add(row);
@@ -83,12 +80,7 @@ private void UpdateCount()
8380
var count = LogManager.Instance.Logs.Count;
8481
LogCountLabel.Text = $"({count})";
8582
ClearIcon.IsVisible = count > 0;
86-
}
87-
88-
private async Task ScrollToBottomAsync()
89-
{
90-
await Task.Delay(50);
91-
await LogScrollView.ScrollToAsync(0, LogScrollView.ContentSize.Height, false);
83+
EmptyLabel.IsVisible = count == 0;
9284
}
9385

9486
private void OnHeaderTapped(object? sender, TappedEventArgs e)

examples/demo/Services/LogManager.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.ObjectModel;
22
using System.ComponentModel;
3-
using System.Diagnostics;
43

54
namespace OneSignalDemo.Services;
65

@@ -47,14 +46,22 @@ private LogManager() { }
4746

4847
public void E(string tag, string message) => AddLog("E", tag, message);
4948

49+
private const int MaxEntries = 100;
50+
5051
private void AddLog(string level, string tag, string message)
5152
{
5253
var entry = new LogEntry(level, $"[{tag}] {message}");
53-
Debug.WriteLine($"{level} {entry.Timestamp} [{tag}] {message}");
54+
var line = $"[{level}][{tag}] {message}";
55+
if (level is "W" or "E")
56+
Console.Error.WriteLine(line);
57+
else
58+
Console.WriteLine(line);
5459

5560
MainThread.BeginInvokeOnMainThread(() =>
5661
{
57-
Logs.Add(entry);
62+
Logs.Insert(0, entry);
63+
if (Logs.Count > MaxEntries)
64+
Logs.RemoveAt(Logs.Count - 1);
5865
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Logs)));
5966
LogAdded?.Invoke(this, EventArgs.Empty);
6067
});

0 commit comments

Comments
 (0)