Skip to content

Commit

Permalink
Replace ReactiveUI with CommunityToolkit to make it even more trimmable
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkatz6 committed Dec 21, 2024
1 parent 53ad27c commit 56a1f5f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 35 deletions.
8 changes: 4 additions & 4 deletions samples/TreeDataGridDemo/Models/DragDropItem.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
using ReactiveUI;
using CommunityToolkit.Mvvm.ComponentModel;

namespace TreeDataGridDemo.Models
{
public class DragDropItem : ReactiveObject
public class DragDropItem : ObservableObject
{
private static Random _random = new Random(0);
private ObservableCollection<DragDropItem>? _children;
Expand All @@ -18,13 +18,13 @@ public class DragDropItem : ReactiveObject
public bool AllowDrag
{
get => _allowDrag;
set => this.RaiseAndSetIfChanged(ref _allowDrag, value);
set => SetProperty(ref _allowDrag, value);
}

public bool AllowDrop
{
get => _allowDrop;
set => this.RaiseAndSetIfChanged(ref _allowDrop, value);
set => SetProperty(ref _allowDrop, value);
}

public ObservableCollection<DragDropItem> Children => _children ??= CreateRandomItems();
Expand Down
16 changes: 8 additions & 8 deletions samples/TreeDataGridDemo/Models/FileTreeNodeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using System.ComponentModel;
using System.IO;
using Avalonia.Threading;
using ReactiveUI;
using CommunityToolkit.Mvvm.ComponentModel;

namespace TreeDataGridDemo.Models
{
public class FileTreeNodeModel : ReactiveObject, IEditableObject
public class FileTreeNodeModel : ObservableObject, IEditableObject
{
private string _path;
private string _name;
Expand Down Expand Up @@ -42,37 +42,37 @@ public FileTreeNodeModel(
public string Path
{
get => _path;
private set => this.RaiseAndSetIfChanged(ref _path, value);
private set => SetProperty(ref _path, value);
}

public string Name
{
get => _name;
private set => this.RaiseAndSetIfChanged(ref _name, value);
private set => SetProperty(ref _name, value);
}

public long? Size
{
get => _size;
private set => this.RaiseAndSetIfChanged(ref _size, value);
private set => SetProperty(ref _size, value);
}

public DateTimeOffset? Modified
{
get => _modified;
private set => this.RaiseAndSetIfChanged(ref _modified, value);
private set => SetProperty(ref _modified, value);
}

public bool HasChildren
{
get => _hasChildren;
private set => this.RaiseAndSetIfChanged(ref _hasChildren, value);
private set => SetProperty(ref _hasChildren, value);
}

public bool IsExpanded
{
get => _isExpanded;
set => this.RaiseAndSetIfChanged(ref _isExpanded, value);
set => SetProperty(ref _isExpanded, value);
}

public bool IsChecked { get; set; }
Expand Down
2 changes: 0 additions & 2 deletions samples/TreeDataGridDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Diagnostics;
using Avalonia;
using Avalonia.ReactiveUI;

namespace TreeDataGridDemo
{
Expand All @@ -21,7 +20,6 @@ public static void Main(string[] args)
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.UseReactiveUI()
.LogToTrace();
}
}
2 changes: 1 addition & 1 deletion samples/TreeDataGridDemo/TreeDataGridDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageReference Include="Avalonia.Diagnostics" Version="$(AvaloniaSamplesVersion)" Condition="'$(Configuration)' != 'Release'" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="$(AvaloniaSamplesVersion)" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="$(AvaloniaSamplesVersion)" />
<PackageReference Include="Avalonia.ReactiveUI" Version="$(AvaloniaSamplesVersion)" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Bogus" Version="34.0.2" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Selection;
using ReactiveUI;
using CommunityToolkit.Mvvm.ComponentModel;
using TreeDataGridDemo.Models;

namespace TreeDataGridDemo.ViewModels
{
internal class CountriesPageViewModel : ReactiveObject
internal class CountriesPageViewModel : ObservableObject
{
private readonly ObservableCollection<Country> _data;
private bool _cellSelection;
Expand Down Expand Up @@ -50,7 +50,7 @@ public bool CellSelection
Source.Selection = new TreeDataGridCellSelectionModel<Country>(Source) { SingleSelect = false };
else
Source.Selection = new TreeDataGridRowSelectionModel<Country>(Source) { SingleSelect = false };
this.RaisePropertyChanged();
OnPropertyChanged();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions samples/TreeDataGridDemo/ViewModels/DragDropPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Collections.ObjectModel;
using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using ReactiveUI;
using CommunityToolkit.Mvvm.ComponentModel;
using TreeDataGridDemo.Models;

namespace TreeDataGridDemo.ViewModels
{
internal class DragDropPageViewModel : ReactiveObject
internal class DragDropPageViewModel : ObservableObject
{
private ObservableCollection<DragDropItem> _data;

Expand Down
37 changes: 22 additions & 15 deletions samples/TreeDataGridDemo/ViewModels/FilesPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Selection;
using Avalonia.Data.Converters;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using ReactiveUI;
using CommunityToolkit.Mvvm.ComponentModel;
using TreeDataGridDemo.Models;

namespace TreeDataGridDemo.ViewModels
{
public class FilesPageViewModel : ReactiveObject
public class FilesPageViewModel : ObservableObject
{
private static IconConverter? s_iconConverter;
private readonly HierarchicalTreeDataGridSource<FileTreeNodeModel>? _treeSource;
Expand All @@ -42,16 +41,24 @@ public FilesPageViewModel()

_source = _treeSource = CreateTreeSource();

this.WhenAnyValue(x => x.SelectedDrive)
.Subscribe(x =>
UpdateRoot();
PropertyChanged += (sender, args) =>
{
if (args.PropertyName == nameof(SelectedDrive))
{
_root = new FileTreeNodeModel(_selectedDrive, isDirectory: true, isRoot: true);
UpdateRoot();
}
};

if (_treeSource is not null)
_treeSource.Items = new[] { _root };
else if (_flatSource is not null)
_flatSource.Items = _root.Children;
});
void UpdateRoot()
{
_root = new FileTreeNodeModel(SelectedDrive, isDirectory: true, isRoot: true);

if (_treeSource is not null)
_treeSource.Items = new[] { _root };
else if (_flatSource is not null)
_flatSource.Items = _root.Children;
}
}

public bool CellSelection
Expand All @@ -66,7 +73,7 @@ public bool CellSelection
Source.Selection = new TreeDataGridCellSelectionModel<FileTreeNodeModel>(Source) { SingleSelect = false };
else
Source.Selection = new TreeDataGridRowSelectionModel<FileTreeNodeModel>(Source) { SingleSelect = false };
this.RaisePropertyChanged();
OnPropertyChanged();
}
}
}
Expand All @@ -86,7 +93,7 @@ public bool FlatList
public string SelectedDrive
{
get => _selectedDrive;
set => this.RaiseAndSetIfChanged(ref _selectedDrive, value);
set => SetProperty(ref _selectedDrive, value);
}

public string? SelectedPath
Expand All @@ -98,7 +105,7 @@ public string? SelectedPath
public ITreeDataGridSource<FileTreeNodeModel> Source
{
get => _source;
private set => this.RaiseAndSetIfChanged(ref _source, value);
private set => SetProperty(ref _source, value);
}

public static IMultiValueConverter FileIconConverter
Expand Down Expand Up @@ -292,7 +299,7 @@ private ITreeDataGridRowSelectionModel<FileTreeNodeModel> GetRowSelection(ITreeD
private void SelectionChanged(object? sender, TreeSelectionModelSelectionChangedEventArgs<FileTreeNodeModel> e)
{
var selectedPath = GetRowSelection(Source).SelectedItem?.Path;
this.RaiseAndSetIfChanged(ref _selectedPath, selectedPath, nameof(SelectedPath));
SetProperty(ref _selectedPath, selectedPath, nameof(SelectedPath));

foreach (var i in e.DeselectedItems)
System.Diagnostics.Trace.WriteLine($"Deselected '{i?.Path}'");
Expand Down

0 comments on commit 56a1f5f

Please sign in to comment.