-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPayloadExchangePage.xaml.cs
More file actions
117 lines (101 loc) · 4.01 KB
/
PayloadExchangePage.xaml.cs
File metadata and controls
117 lines (101 loc) · 4.01 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
using EntglDb.Sample.Shared;
using System.Text.Json;
namespace EntglDb.Test.Maui;
public partial class PayloadExchangePage : ContentPage
{
private readonly SampleDbContext _db;
public PayloadExchangePage(SampleDbContext db)
{
InitializeComponent();
_db = db;
}
protected override void OnAppearing()
{
base.OnAppearing();
GenerateRandomPayload();
}
private void GenerateRandomPayload()
{
var random = new Random();
var itemCount = random.Next(50, 100); // 50 to 100 items to make it substantial
var items = new List<object>();
for (int i = 0; i < itemCount; i++)
{
items.Add(new
{
id = Guid.NewGuid().ToString(),
name = $"Item-{i}-{Guid.NewGuid().ToString().Substring(0, 8)}",
value = random.NextDouble() * 1000,
category = random.Next(0, 2) == 0 ? "A" : "B",
tags = new[] { "tag1", "tag2", $"random-{random.Next(100)}" },
details = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + Guid.NewGuid()
});
}
var payload = new
{
id = Guid.NewGuid().ToString(),
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
type = "LargeDataset",
description = "Generated payload for compression testing",
flags = new { active = true, verified = random.Next(0, 2) == 1 },
items = items,
metadata = new
{
source = "MAUI Test App",
version = "1.0",
hash = Guid.NewGuid().ToString() // Ensure high entropy
}
};
var options = new JsonSerializerOptions { WriteIndented = true };
JsonEditor.Text = JsonSerializer.Serialize(payload, options);
}
private async void OnSaveClicked(object sender, EventArgs e)
{
var collection = CollectionEntry.Text?.Trim();
var jsonText = JsonEditor.Text;
if (string.IsNullOrWhiteSpace(collection))
{
await DisplayAlert("Error", "Please enter a collection name.", "OK");
return;
}
if (string.IsNullOrWhiteSpace(jsonText))
{
await DisplayAlert("Error", "Please enter a JSON payload.", "OK");
return;
}
try
{
// Validate JSON
var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonText);
// Extract ID if present, otherwise generate one?
// For now, let's assume the user puts an ID or we generate a random one if missing?
// Actually Document constructor usually takes an ID.
// Let's try to find an 'id' property.
string id = Guid.NewGuid().ToString();
if (jsonElement.TryGetProperty("id", out var idProp))
{
id = idProp.ToString();
}
else if (jsonElement.TryGetProperty("Id", out idProp))
{
id = idProp.ToString();
}
// Insert as a User for sync testing purposes
var user = new User { Id = id, Name = "payload-test", Age = 0, Address = new Address { City = "PayloadTest" } };
await _db.Users.InsertAsync(user);
await _db.SaveChangesAsync();
StatusLabel.Text = $"Saved document {id} to '{collection}' at {DateTime.Now.ToLongTimeString()}";
StatusLabel.TextColor = Colors.Green;
}
catch (JsonException jex)
{
await DisplayAlert("JSON Error", $"Invalid JSON: {jex.Message}", "OK");
}
catch (Exception ex)
{
await DisplayAlert("Error", $"Failed to save: {ex.Message}", "OK");
StatusLabel.Text = $"Error: {ex.Message}";
StatusLabel.TextColor = Colors.Red;
}
}
}