-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCollectionPage.xaml.cs
More file actions
97 lines (88 loc) · 3.08 KB
/
CollectionPage.xaml.cs
File metadata and controls
97 lines (88 loc) · 3.08 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
using EntglDb.Sample.Shared;
using System.Collections.ObjectModel;
using System.Text.Json;
using System.Windows.Input;
namespace EntglDb.Test.Maui;
public partial class CollectionPage : ContentPage
{
private readonly SampleDbContext _db;
public string CollectionName { get; }
public ObservableCollection<DocumentViewModel> Documents { get; } = new();
public ICommand RefreshCommand { get; }
private int _documentCount;
public int DocumentCount
{
get => _documentCount;
set
{
_documentCount = value;
OnPropertyChanged();
}
}
public CollectionPage(SampleDbContext db, string collectionName)
{
InitializeComponent();
_db = db;
CollectionName = collectionName;
Title = collectionName;
RefreshCommand = new Command(async () => await LoadDocuments());
BindingContext = this;
}
protected override async void OnAppearing()
{
base.OnAppearing();
await LoadDocuments();
}
private async Task LoadDocuments()
{
try
{
Documents.Clear();
if (CollectionName == "Users")
{
var allItems = await _db.Users.FindAllAsync().ToListAsync();
DocumentCount = allItems.Count;
foreach (var item in allItems.Take(100))
{
var json = JsonSerializer.Serialize(item);
var shortText = json.Length > 50 ? json[..50] + "..." : json;
Documents.Add(new DocumentViewModel { Key = item.Id, Timestamp = "-", Payload = json, ShortPayload = shortText });
}
}
else if (CollectionName == "TodoLists")
{
var allItems = await _db.TodoLists.FindAllAsync().ToListAsync();
DocumentCount = allItems.Count;
foreach (var item in allItems.Take(100))
{
var json = JsonSerializer.Serialize(item);
var shortText = json.Length > 50 ? json[..50] + "..." : json;
Documents.Add(new DocumentViewModel { Key = item.Id, Timestamp = "-", Payload = json, ShortPayload = shortText });
}
}
}
catch (Exception ex)
{
_ = DisplayAlert("Error", $"Failed to load documents: {ex.Message}", "OK");
}
finally
{
DocsRefreshView.IsRefreshing = false;
}
}
private async void OnDocumentSelected(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection.FirstOrDefault() is DocumentViewModel doc)
{
DocsCollectionView.SelectedItem = null;
await Navigation.PushAsync(new DocumentDetailPage(doc));
}
}
}
public class DocumentViewModel
{
public string Key { get; set; }
public string Timestamp { get; set; }
public string Payload { get; set; }
public string ShortPayload { get; set; }
}