Skip to content

Commit 56a1f5f

Browse files
committed
Replace ReactiveUI with CommunityToolkit to make it even more trimmable
1 parent 53ad27c commit 56a1f5f

File tree

7 files changed

+40
-35
lines changed

7 files changed

+40
-35
lines changed

samples/TreeDataGridDemo/Models/DragDropItem.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using System.Collections.ObjectModel;
33
using System.Linq;
4-
using ReactiveUI;
4+
using CommunityToolkit.Mvvm.ComponentModel;
55

66
namespace TreeDataGridDemo.Models
77
{
8-
public class DragDropItem : ReactiveObject
8+
public class DragDropItem : ObservableObject
99
{
1010
private static Random _random = new Random(0);
1111
private ObservableCollection<DragDropItem>? _children;
@@ -18,13 +18,13 @@ public class DragDropItem : ReactiveObject
1818
public bool AllowDrag
1919
{
2020
get => _allowDrag;
21-
set => this.RaiseAndSetIfChanged(ref _allowDrag, value);
21+
set => SetProperty(ref _allowDrag, value);
2222
}
2323

2424
public bool AllowDrop
2525
{
2626
get => _allowDrop;
27-
set => this.RaiseAndSetIfChanged(ref _allowDrop, value);
27+
set => SetProperty(ref _allowDrop, value);
2828
}
2929

3030
public ObservableCollection<DragDropItem> Children => _children ??= CreateRandomItems();

samples/TreeDataGridDemo/Models/FileTreeNodeModel.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
using System.ComponentModel;
55
using System.IO;
66
using Avalonia.Threading;
7-
using ReactiveUI;
7+
using CommunityToolkit.Mvvm.ComponentModel;
88

99
namespace TreeDataGridDemo.Models
1010
{
11-
public class FileTreeNodeModel : ReactiveObject, IEditableObject
11+
public class FileTreeNodeModel : ObservableObject, IEditableObject
1212
{
1313
private string _path;
1414
private string _name;
@@ -42,37 +42,37 @@ public FileTreeNodeModel(
4242
public string Path
4343
{
4444
get => _path;
45-
private set => this.RaiseAndSetIfChanged(ref _path, value);
45+
private set => SetProperty(ref _path, value);
4646
}
4747

4848
public string Name
4949
{
5050
get => _name;
51-
private set => this.RaiseAndSetIfChanged(ref _name, value);
51+
private set => SetProperty(ref _name, value);
5252
}
5353

5454
public long? Size
5555
{
5656
get => _size;
57-
private set => this.RaiseAndSetIfChanged(ref _size, value);
57+
private set => SetProperty(ref _size, value);
5858
}
5959

6060
public DateTimeOffset? Modified
6161
{
6262
get => _modified;
63-
private set => this.RaiseAndSetIfChanged(ref _modified, value);
63+
private set => SetProperty(ref _modified, value);
6464
}
6565

6666
public bool HasChildren
6767
{
6868
get => _hasChildren;
69-
private set => this.RaiseAndSetIfChanged(ref _hasChildren, value);
69+
private set => SetProperty(ref _hasChildren, value);
7070
}
7171

7272
public bool IsExpanded
7373
{
7474
get => _isExpanded;
75-
set => this.RaiseAndSetIfChanged(ref _isExpanded, value);
75+
set => SetProperty(ref _isExpanded, value);
7676
}
7777

7878
public bool IsChecked { get; set; }

samples/TreeDataGridDemo/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Diagnostics;
22
using Avalonia;
3-
using Avalonia.ReactiveUI;
43

54
namespace TreeDataGridDemo
65
{
@@ -21,7 +20,6 @@ public static void Main(string[] args)
2120
public static AppBuilder BuildAvaloniaApp()
2221
=> AppBuilder.Configure<App>()
2322
.UsePlatformDetect()
24-
.UseReactiveUI()
2523
.LogToTrace();
2624
}
2725
}

samples/TreeDataGridDemo/TreeDataGridDemo.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<PackageReference Include="Avalonia.Diagnostics" Version="$(AvaloniaSamplesVersion)" Condition="'$(Configuration)' != 'Release'" />
1919
<PackageReference Include="Avalonia.Themes.Fluent" Version="$(AvaloniaSamplesVersion)" />
2020
<PackageReference Include="Avalonia.Fonts.Inter" Version="$(AvaloniaSamplesVersion)" />
21-
<PackageReference Include="Avalonia.ReactiveUI" Version="$(AvaloniaSamplesVersion)" />
21+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
2222
<PackageReference Include="Bogus" Version="34.0.2" />
2323
</ItemGroup>
2424
<ItemGroup>

samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
using Avalonia.Controls;
44
using Avalonia.Controls.Models.TreeDataGrid;
55
using Avalonia.Controls.Selection;
6-
using ReactiveUI;
6+
using CommunityToolkit.Mvvm.ComponentModel;
77
using TreeDataGridDemo.Models;
88

99
namespace TreeDataGridDemo.ViewModels
1010
{
11-
internal class CountriesPageViewModel : ReactiveObject
11+
internal class CountriesPageViewModel : ObservableObject
1212
{
1313
private readonly ObservableCollection<Country> _data;
1414
private bool _cellSelection;
@@ -50,7 +50,7 @@ public bool CellSelection
5050
Source.Selection = new TreeDataGridCellSelectionModel<Country>(Source) { SingleSelect = false };
5151
else
5252
Source.Selection = new TreeDataGridRowSelectionModel<Country>(Source) { SingleSelect = false };
53-
this.RaisePropertyChanged();
53+
OnPropertyChanged();
5454
}
5555
}
5656
}

samples/TreeDataGridDemo/ViewModels/DragDropPageViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System.Collections.ObjectModel;
22
using Avalonia.Controls;
33
using Avalonia.Controls.Models.TreeDataGrid;
4-
using ReactiveUI;
4+
using CommunityToolkit.Mvvm.ComponentModel;
55
using TreeDataGridDemo.Models;
66

77
namespace TreeDataGridDemo.ViewModels
88
{
9-
internal class DragDropPageViewModel : ReactiveObject
9+
internal class DragDropPageViewModel : ObservableObject
1010
{
1111
private ObservableCollection<DragDropItem> _data;
1212

samples/TreeDataGridDemo/ViewModels/FilesPageViewModel.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
using System.Globalization;
44
using System.IO;
55
using System.Linq;
6-
using System.Reactive.Linq;
76
using System.Runtime.InteropServices;
87
using Avalonia.Controls;
98
using Avalonia.Controls.Models.TreeDataGrid;
109
using Avalonia.Controls.Selection;
1110
using Avalonia.Data.Converters;
1211
using Avalonia.Media.Imaging;
1312
using Avalonia.Platform;
14-
using ReactiveUI;
13+
using CommunityToolkit.Mvvm.ComponentModel;
1514
using TreeDataGridDemo.Models;
1615

1716
namespace TreeDataGridDemo.ViewModels
1817
{
19-
public class FilesPageViewModel : ReactiveObject
18+
public class FilesPageViewModel : ObservableObject
2019
{
2120
private static IconConverter? s_iconConverter;
2221
private readonly HierarchicalTreeDataGridSource<FileTreeNodeModel>? _treeSource;
@@ -42,16 +41,24 @@ public FilesPageViewModel()
4241

4342
_source = _treeSource = CreateTreeSource();
4443

45-
this.WhenAnyValue(x => x.SelectedDrive)
46-
.Subscribe(x =>
44+
UpdateRoot();
45+
PropertyChanged += (sender, args) =>
46+
{
47+
if (args.PropertyName == nameof(SelectedDrive))
4748
{
48-
_root = new FileTreeNodeModel(_selectedDrive, isDirectory: true, isRoot: true);
49+
UpdateRoot();
50+
}
51+
};
4952

50-
if (_treeSource is not null)
51-
_treeSource.Items = new[] { _root };
52-
else if (_flatSource is not null)
53-
_flatSource.Items = _root.Children;
54-
});
53+
void UpdateRoot()
54+
{
55+
_root = new FileTreeNodeModel(SelectedDrive, isDirectory: true, isRoot: true);
56+
57+
if (_treeSource is not null)
58+
_treeSource.Items = new[] { _root };
59+
else if (_flatSource is not null)
60+
_flatSource.Items = _root.Children;
61+
}
5562
}
5663

5764
public bool CellSelection
@@ -66,7 +73,7 @@ public bool CellSelection
6673
Source.Selection = new TreeDataGridCellSelectionModel<FileTreeNodeModel>(Source) { SingleSelect = false };
6774
else
6875
Source.Selection = new TreeDataGridRowSelectionModel<FileTreeNodeModel>(Source) { SingleSelect = false };
69-
this.RaisePropertyChanged();
76+
OnPropertyChanged();
7077
}
7178
}
7279
}
@@ -86,7 +93,7 @@ public bool FlatList
8693
public string SelectedDrive
8794
{
8895
get => _selectedDrive;
89-
set => this.RaiseAndSetIfChanged(ref _selectedDrive, value);
96+
set => SetProperty(ref _selectedDrive, value);
9097
}
9198

9299
public string? SelectedPath
@@ -98,7 +105,7 @@ public string? SelectedPath
98105
public ITreeDataGridSource<FileTreeNodeModel> Source
99106
{
100107
get => _source;
101-
private set => this.RaiseAndSetIfChanged(ref _source, value);
108+
private set => SetProperty(ref _source, value);
102109
}
103110

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

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

0 commit comments

Comments
 (0)