Skip to content

Commit

Permalink
Test wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
papafe committed May 7, 2024
1 parent 93bbd4b commit 16e4ee8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
82 changes: 64 additions & 18 deletions examples/QuickJournal/ViewModels/EntriesViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using CommunityToolkit.Maui.Alerts;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
Expand All @@ -13,18 +16,12 @@ public partial class EntriesViewModel : ObservableObject
private readonly Realm realm;

[ObservableProperty]
private IQueryable<JournalEntry>? entries;
private IEnumerable<JournalEntryViewModel>? entries;

public EntriesViewModel()
{
realm = Realm.GetInstance();
Entries = realm.All<JournalEntry>();

// We are using a WeakReferenceManager here to get notified when JournalEntriesDetailPage is closed.
// This could have been implemeted hooking up on the back button behaviour
// (with Shell.BackButtonBehaviour), but there is a current bug in MAUI
// that would make the application crash (https://github.com/dotnet/maui/pull/11438)
WeakReferenceMessenger.Default.Register<EntryModifiedMessage>(this, EntryModifiedHandler);
Entries = new WrapperCollection<JournalEntry, JournalEntryViewModel>(realm.All<JournalEntry>(), i => new JournalEntryViewModel(i));
}

[RelayCommand]
Expand All @@ -45,17 +42,17 @@ public async Task AddEntry()
}

[RelayCommand]
public async Task EditEntry(JournalEntry entry)
public async Task EditEntry(JournalEntryViewModel entry)
{
await GoToEntry(entry);
await GoToEntry(entry.Entry);
}

[RelayCommand]
public async Task DeleteEntry(JournalEntry entry)
public async Task DeleteEntry(JournalEntryViewModel entry)
{
await realm.WriteAsync(() =>
{
realm.Remove(entry);
realm.Remove(entry.Entry);
});
}

Expand All @@ -67,14 +64,63 @@ private async Task GoToEntry(JournalEntry entry)
};
await Shell.Current.GoToAsync($"entryDetail", navigationParameter);
}
}

public class WrapperCollection<T, TViewModel> : INotifyCollectionChanged, IEnumerable<TViewModel>
where T : IRealmObject
where TViewModel : class
{
private IRealmCollection<T> _results;
private Func<T, TViewModel> _viewModelFactory;

public int Count => _results.Count;

public TViewModel this[int index] => _viewModelFactory(_results[index]);

public event NotifyCollectionChangedEventHandler? CollectionChanged
{
add { _results.CollectionChanged += value; }
remove { _results.CollectionChanged -= value; }
}

public WrapperCollection(IQueryable<T> query, Func<T, TViewModel> viewModelFactory)
{
_results = query.AsRealmCollection();
_viewModelFactory = viewModelFactory;
}

public IEnumerator<TViewModel> GetEnumerator()
{
foreach (var item in _results)
{
yield return _viewModelFactory(item);
}
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public class JournalEntryViewModel : INotifyPropertyChanged
{
private JournalEntry _entry;

public event PropertyChangedEventHandler? PropertyChanged;

public JournalEntry Entry => _entry;

public string Summary => _entry.Title + _entry.Body;

public JournalEntryViewModel(JournalEntry entry)
{
_entry = entry;
_entry.PropertyChanged += Inner_PropertyChanged;
}

private async void EntryModifiedHandler(object recipient, EntryModifiedMessage message)
private void Inner_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
var newEntry = message.Value;
if (string.IsNullOrEmpty(newEntry.Body + newEntry.Title))
if (e.PropertyName == nameof(JournalEntry.Title) || e.PropertyName == nameof(JournalEntry.Body))
{
await DeleteEntry(newEntry);
await Toast.Make("Empty note discarded").Show();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Summary)));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/QuickJournal/Views/EntriesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Title}"
Detail="{Binding Metadata.CreatedDate, StringFormat='{0:dddd, MMMM d yyyy}'}">
<TextCell Text="{Binding Summary}"
Detail="{Binding Entry.Metadata.CreatedDate, StringFormat='{0:dddd, MMMM d yyyy}'}">
<TextCell.ContextActions>
<MenuItem Text="Delete" IsDestructive="true"
Command="{Binding Path=BindingContext.DeleteEntryCommand, Source={x:Reference entriesPage}}"
Expand Down

0 comments on commit 16e4ee8

Please sign in to comment.