add-reactive-navigation
This commit is contained in:
parent
4617716a9f
commit
255a13337b
@ -1,14 +1,9 @@
|
||||
<Application xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
x:Class="Presence.Desktop.App"
|
||||
xmlns:local="using:Presence.Desktop"
|
||||
RequestedThemeVariant="Default">
|
||||
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
|
||||
|
||||
<Application.DataTemplates>
|
||||
<local:ViewLocator/>
|
||||
</Application.DataTemplates>
|
||||
|
||||
<Application.Styles>
|
||||
<FluentTheme />
|
||||
</Application.Styles>
|
||||
|
@ -20,13 +20,13 @@ namespace Presence.Desktop
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.AddCommonService();
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
var mainViewModel = services.GetRequiredService<MainWindowViewModel>();
|
||||
var mainViewModel = services.GetRequiredService<GroupViewModel>();
|
||||
|
||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||
{
|
||||
desktop.MainWindow = new MainWindow
|
||||
desktop.MainWindow = new MainWindow()
|
||||
{
|
||||
DataContext = mainViewModel,
|
||||
DataContext = new MainWindowViewModel(services),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ namespace Presence.Desktop.DI
|
||||
.AddDbContext<RemoteDatabaseContext>()
|
||||
.AddSingleton<IGroupRepository, SQLGroupRepository>()
|
||||
.AddTransient<IGroupUseCase, GroupService>()
|
||||
.AddTransient<MainWindowViewModel>();
|
||||
.AddTransient<GroupViewModel>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,31 +2,18 @@ using Avalonia.Controls;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Presence.Desktop.ViewModels;
|
||||
using System;
|
||||
using Presence.Desktop.Views;
|
||||
using ReactiveUI;
|
||||
|
||||
namespace Presence.Desktop
|
||||
{
|
||||
public class ViewLocator : IDataTemplate
|
||||
public class ViewLocator : IViewLocator
|
||||
{
|
||||
|
||||
public Control? Build(object? param)
|
||||
public IViewFor? ResolveView<T>(T? viewModel, string? contract = null) => viewModel switch
|
||||
{
|
||||
if (param is null)
|
||||
return null;
|
||||
|
||||
var name = param.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal);
|
||||
var type = Type.GetType(name);
|
||||
|
||||
if (type != null)
|
||||
{
|
||||
return (Control)Activator.CreateInstance(type)!;
|
||||
}
|
||||
|
||||
return new TextBlock { Text = "Not Found: " + name };
|
||||
}
|
||||
|
||||
public bool Match(object? data)
|
||||
{
|
||||
return data is ViewModelBase;
|
||||
}
|
||||
GroupViewModel groupViewModel => new GroupView { DataContext = groupViewModel },
|
||||
PresenceViewModel presenceViewModel => new PresenceView { DataContext = presenceViewModel },
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(viewModel))
|
||||
};
|
||||
}
|
||||
}
|
||||
|
74
Presence.Desktop/ViewModels/GroupViewModel.cs
Normal file
74
Presence.Desktop/ViewModels/GroupViewModel.cs
Normal file
@ -0,0 +1,74 @@
|
||||
using domain.UseCase;
|
||||
using DynamicData;
|
||||
using DynamicData.Binding;
|
||||
using Presence.Desktop.Models;
|
||||
using ReactiveUI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using Tmds.DBus.Protocol;
|
||||
|
||||
namespace Presence.Desktop.ViewModels
|
||||
{
|
||||
public class GroupViewModel : ViewModelBase, IRoutableViewModel
|
||||
{
|
||||
private readonly List<GroupPresenter> _groupPresentersDataSource = new List<GroupPresenter>();
|
||||
private ObservableCollection<GroupPresenter> _groups;
|
||||
public ObservableCollection<GroupPresenter> Groups => _groups;
|
||||
|
||||
public GroupPresenter? SelectedGroupItem
|
||||
{
|
||||
get => _selectedGroupItem;
|
||||
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
|
||||
}
|
||||
|
||||
private GroupPresenter? _selectedGroupItem;
|
||||
|
||||
|
||||
|
||||
public ObservableCollection<UserPresenter> Users { get => _users; }
|
||||
public ObservableCollection<UserPresenter> _users;
|
||||
public GroupViewModel(IGroupUseCase groupUseCase)
|
||||
{
|
||||
foreach (var item in groupUseCase.GetGroupsWithStudents())
|
||||
{
|
||||
GroupPresenter groupPresenter = new GroupPresenter
|
||||
{
|
||||
Id = item.Id,
|
||||
Name = item.Name,
|
||||
users = item.Users?.Select(user => new UserPresenter
|
||||
{
|
||||
Name = user.Name,
|
||||
Guid = user.Guid,
|
||||
Group = new GroupPresenter { Id = item.Id, Name = item.Name }
|
||||
}
|
||||
).ToList()
|
||||
};
|
||||
_groupPresentersDataSource.Add(groupPresenter);
|
||||
}
|
||||
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
|
||||
|
||||
_users = new ObservableCollection<UserPresenter>();
|
||||
|
||||
this.WhenAnyValue(vm => vm.SelectedGroupItem)
|
||||
.Subscribe(_ => SetUsers());
|
||||
|
||||
}
|
||||
|
||||
private void SetUsers()
|
||||
{
|
||||
if (SelectedGroupItem == null) return;
|
||||
if (SelectedGroupItem.users == null) return;
|
||||
Users.Clear();
|
||||
foreach (var item in SelectedGroupItem.users)
|
||||
{
|
||||
Users.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public string? UrlPathSegment { get; }
|
||||
public IScreen HostScreen { get; }
|
||||
}
|
||||
}
|
@ -1,73 +1,16 @@
|
||||
using domain.UseCase;
|
||||
using DynamicData;
|
||||
using DynamicData.Binding;
|
||||
using Presence.Desktop.Models;
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ReactiveUI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using Tmds.DBus.Protocol;
|
||||
|
||||
namespace Presence.Desktop.ViewModels
|
||||
namespace Presence.Desktop.ViewModels;
|
||||
|
||||
public class MainWindowViewModel : ViewModelBase, IScreen
|
||||
{
|
||||
public class MainWindowViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IGroupUseCase _groupService;
|
||||
private List<GroupPresenter> groupPresentersDataSource = new List<GroupPresenter>();
|
||||
private ObservableCollection<GroupPresenter> _groups;
|
||||
public ObservableCollection<GroupPresenter> Groups => _groups;
|
||||
public RoutingState Router { get; } = new RoutingState();
|
||||
|
||||
public GroupPresenter? SelectedGroupItem
|
||||
public MainWindowViewModel(IServiceProvider serviceProvider)
|
||||
{
|
||||
get => _selectedGroupItem;
|
||||
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
|
||||
}
|
||||
|
||||
private GroupPresenter? _selectedGroupItem;
|
||||
|
||||
public ObservableCollection<UserPresenter> Users { get => _users; }
|
||||
public ObservableCollection<UserPresenter> _users;
|
||||
public MainWindowViewModel(IGroupUseCase groupUseCase)
|
||||
{
|
||||
_groupService = groupUseCase;
|
||||
|
||||
foreach (var item in _groupService.GetGroupsWithStudents())
|
||||
{
|
||||
GroupPresenter groupPresenter = new GroupPresenter
|
||||
{
|
||||
Id = item.Id,
|
||||
Name = item.Name,
|
||||
users = item.Users?.Select(user => new UserPresenter
|
||||
{
|
||||
Name = user.Name,
|
||||
Guid = user.Guid,
|
||||
Group = new GroupPresenter { Id = item.Id, Name = item.Name }
|
||||
}
|
||||
).ToList()
|
||||
};
|
||||
groupPresentersDataSource.Add(groupPresenter);
|
||||
}
|
||||
_groups = new ObservableCollection<GroupPresenter>(groupPresentersDataSource);
|
||||
|
||||
_users = new ObservableCollection<UserPresenter>();
|
||||
|
||||
this.WhenAnyValue(vm => vm.SelectedGroupItem)
|
||||
.Subscribe(_ => SetUsers());
|
||||
|
||||
}
|
||||
|
||||
private void SetUsers()
|
||||
{
|
||||
if (SelectedGroupItem == null) return;
|
||||
if (SelectedGroupItem.users == null) return;
|
||||
Users.Clear();
|
||||
foreach (var item in SelectedGroupItem.users)
|
||||
{
|
||||
Users.Add(item);
|
||||
}
|
||||
}
|
||||
var groupViewModel = serviceProvider.GetRequiredService<GroupViewModel>();
|
||||
Router.Navigate.Execute(groupViewModel);
|
||||
}
|
||||
}
|
||||
|
||||
|
9
Presence.Desktop/ViewModels/PresenceViewModel.cs
Normal file
9
Presence.Desktop/ViewModels/PresenceViewModel.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using ReactiveUI;
|
||||
|
||||
namespace Presence.Desktop.ViewModels;
|
||||
|
||||
public class PresenceViewModel : ViewModelBase, IRoutableViewModel
|
||||
{
|
||||
public string? UrlPathSegment { get; }
|
||||
public IScreen HostScreen { get; }
|
||||
}
|
45
Presence.Desktop/Views/GroupView.axaml
Normal file
45
Presence.Desktop/Views/GroupView.axaml
Normal file
@ -0,0 +1,45 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vm="using:Presence.Desktop.ViewModels"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="Presence.Desktop.GroupView"
|
||||
x:DataType="vm:GroupViewModel">
|
||||
|
||||
<Design.DataContext>
|
||||
<vm:GroupViewModel/>
|
||||
</Design.DataContext>
|
||||
<DockPanel Background="Azure">
|
||||
<StackPanel DockPanel.Dock="Bottom">
|
||||
<TextBlock Text="List ↑" HorizontalAlignment="Center"/>
|
||||
</StackPanel>
|
||||
<StackPanel
|
||||
Spacing="10"
|
||||
HorizontalAlignment="Center"
|
||||
DockPanel.Dock="Top"
|
||||
Orientation="Horizontal">
|
||||
<TextBlock Text="Combobox ->"/>
|
||||
<ComboBox ItemsSource="{Binding Groups}" SelectedValue="{Binding SelectedGroupItem}">
|
||||
<ComboBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Name}"/>
|
||||
</DataTemplate>
|
||||
</ComboBox.ItemTemplate>
|
||||
</ComboBox>
|
||||
<ComboBox/>
|
||||
<ComboBox/>
|
||||
</StackPanel>
|
||||
<Border>
|
||||
<ListBox Background="Bisque" ItemsSource="{Binding Users}">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="{Binding Name}" />
|
||||
</StackPanel>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
</Border>
|
||||
</DockPanel>
|
||||
</UserControl>
|
17
Presence.Desktop/Views/GroupView.axaml.cs
Normal file
17
Presence.Desktop/Views/GroupView.axaml.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Avalonia.ReactiveUI;
|
||||
using Presence.Desktop.ViewModels;
|
||||
using ReactiveUI;
|
||||
|
||||
namespace Presence.Desktop;
|
||||
|
||||
public partial class GroupView : ReactiveUserControl<GroupViewModel>
|
||||
{
|
||||
public GroupView()
|
||||
{
|
||||
this.WhenActivated(disposables => { });
|
||||
AvaloniaXamlLoader.Load(this);
|
||||
}
|
||||
}
|
@ -1,48 +1,19 @@
|
||||
<Window xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:vm="using:Presence.Desktop.ViewModels"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:app="clr-namespace:Presence.Desktop"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vm="using:Presence.Desktop.ViewModels"
|
||||
xmlns:reactiveUi="http://reactiveui.net"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="Presence.Desktop.Views.MainWindow"
|
||||
x:DataType="vm:MainWindowViewModel"
|
||||
Icon="/Assets/avalonia-logo.ico"
|
||||
Title="Presence.Desktop">
|
||||
|
||||
<Design.DataContext>
|
||||
<vm:MainWindowViewModel/>
|
||||
</Design.DataContext>
|
||||
<DockPanel Background="Azure">
|
||||
<StackPanel DockPanel.Dock="Bottom">
|
||||
<TextBlock Text="List ↑" HorizontalAlignment="Center"/>
|
||||
</StackPanel>
|
||||
<StackPanel
|
||||
Spacing="10"
|
||||
HorizontalAlignment="Center"
|
||||
DockPanel.Dock="Top"
|
||||
Orientation="Horizontal">
|
||||
<TextBlock Text="Combobox ->"/>
|
||||
<ComboBox ItemsSource="{Binding Groups}" SelectedValue="{Binding SelectedGroupItem}">
|
||||
<ComboBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Name}"/>
|
||||
</DataTemplate>
|
||||
</ComboBox.ItemTemplate>
|
||||
</ComboBox>
|
||||
<ComboBox/>
|
||||
<ComboBox/>
|
||||
</StackPanel>
|
||||
<Border>
|
||||
<ListBox Background="Bisque" ItemsSource="{Binding Users}">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="{Binding Name}" />
|
||||
</StackPanel>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
</Border>
|
||||
Title="MainWindow">
|
||||
<DockPanel>
|
||||
<reactiveUi:RoutedViewHost Router="{Binding Router}" DockPanel.Dock="Right" Background="AliceBlue">
|
||||
<reactiveUi:RoutedViewHost.ViewLocator>
|
||||
<app:ViewLocator/>
|
||||
</reactiveUi:RoutedViewHost.ViewLocator>
|
||||
</reactiveUi:RoutedViewHost>
|
||||
</DockPanel>
|
||||
|
||||
</Window>
|
||||
|
@ -1,12 +1,18 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Avalonia.ReactiveUI;
|
||||
using Presence.Desktop.ViewModels;
|
||||
using ReactiveUI;
|
||||
|
||||
namespace Presence.Desktop.Views
|
||||
namespace Presence.Desktop.Views;
|
||||
|
||||
public partial class MainWindow : ReactiveWindow<MainWindowViewModel>
|
||||
{
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
this.WhenActivated(disposables => { });
|
||||
AvaloniaXamlLoader.Load(this);
|
||||
|
||||
}
|
||||
}
|
8
Presence.Desktop/Views/PresenceView.axaml
Normal file
8
Presence.Desktop/Views/PresenceView.axaml
Normal file
@ -0,0 +1,8 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="Presence.Desktop.Views.PresenceView">
|
||||
Welcome to Avalonia!
|
||||
</UserControl>
|
17
Presence.Desktop/Views/PresenceView.axaml.cs
Normal file
17
Presence.Desktop/Views/PresenceView.axaml.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Avalonia.ReactiveUI;
|
||||
using Presence.Desktop.ViewModels;
|
||||
using ReactiveUI;
|
||||
|
||||
namespace Presence.Desktop.Views;
|
||||
|
||||
public partial class PresenceView : ReactiveUserControl<PresenceViewModel>
|
||||
{
|
||||
public PresenceView()
|
||||
{
|
||||
this.WhenActivated(disposables => { });
|
||||
AvaloniaXamlLoader.Load(this);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user