From 3f620cd963462ccc77c394e942ae1b92ca2e84a1 Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Mon, 23 Sep 2024 22:40:18 +0200 Subject: [PATCH] improve ui --- Stars Assistant/App.axaml | 2 + Stars Assistant/Stars Assistant.csproj | 1 + Stars Assistant/ViewModels/BuColViewModel.cs | 4 +- .../ViewModels/MainWindowViewModel.cs | 20 +++++--- Stars Assistant/ViewModels/PlanetViewModel.cs | 51 ++++++++++++------- Stars Assistant/ViewModels/ViewModelBase.cs | 4 +- Stars Assistant/Views/BuColView.axaml.cs | 3 +- Stars Assistant/Views/MainWindow.axaml | 43 +++++++++++----- Stars Assistant/Views/MainWindow.axaml.cs | 10 +++- 9 files changed, 95 insertions(+), 43 deletions(-) diff --git a/Stars Assistant/App.axaml b/Stars Assistant/App.axaml index 9bb9b54..614985a 100644 --- a/Stars Assistant/App.axaml +++ b/Stars Assistant/App.axaml @@ -1,5 +1,6 @@ @@ -11,6 +12,7 @@ + \ No newline at end of file diff --git a/Stars Assistant/Stars Assistant.csproj b/Stars Assistant/Stars Assistant.csproj index ef96db4..9ceef65 100644 --- a/Stars Assistant/Stars Assistant.csproj +++ b/Stars Assistant/Stars Assistant.csproj @@ -18,6 +18,7 @@ + diff --git a/Stars Assistant/ViewModels/BuColViewModel.cs b/Stars Assistant/ViewModels/BuColViewModel.cs index 5009287..75e6c84 100644 --- a/Stars Assistant/ViewModels/BuColViewModel.cs +++ b/Stars Assistant/ViewModels/BuColViewModel.cs @@ -8,10 +8,8 @@ using Splat; namespace StarsAssistant.ViewModels; -public partial class BuColViewModel : ViewModelBase, IActivatableViewModel +public partial class BuColViewModel : ViewModelBase { - public ViewModelActivator Activator { get; } = new ViewModelActivator(); - public BuColViewModel() { this.WhenActivated((CompositeDisposable disposables) => diff --git a/Stars Assistant/ViewModels/MainWindowViewModel.cs b/Stars Assistant/ViewModels/MainWindowViewModel.cs index 1eaf568..3701acc 100644 --- a/Stars Assistant/ViewModels/MainWindowViewModel.cs +++ b/Stars Assistant/ViewModels/MainWindowViewModel.cs @@ -1,5 +1,4 @@ - -using System.Reactive.Disposables; +using System.Reactive.Disposables; using System.Reactive.Linq; using ReactiveUI; using ReactiveUI.SourceGenerators; @@ -8,10 +7,8 @@ using StarsAssistant.Model; namespace StarsAssistant.ViewModels; -public partial class MainWindowViewModel : ViewModelBase, IActivatableViewModel +public partial class MainWindowViewModel : ViewModelBase { - public ViewModelActivator Activator { get; } = new ViewModelActivator(); - [Reactive] private string _dbPath; @@ -20,6 +17,8 @@ public partial class MainWindowViewModel : ViewModelBase, IActivatableViewModel public MainWindowViewModel() { + InitializeCommands(); + using var db = Locator.Current.GetService()!; DbPath = Path.GetFullPath(db.DbPath); @@ -32,6 +31,15 @@ public partial class MainWindowViewModel : ViewModelBase, IActivatableViewModel }); } - + [ReactiveCommand] + private void New() + { + this.Log().Info("New button hit"); + } + [ReactiveCommand] + private void Open() + { + this.Log().Info("Open button hit"); + } } diff --git a/Stars Assistant/ViewModels/PlanetViewModel.cs b/Stars Assistant/ViewModels/PlanetViewModel.cs index e1a3dfc..9ed7cf3 100644 --- a/Stars Assistant/ViewModels/PlanetViewModel.cs +++ b/Stars Assistant/ViewModels/PlanetViewModel.cs @@ -1,13 +1,44 @@ using System.Collections.ObjectModel; +using System.Reactive.Disposables; using Microsoft.EntityFrameworkCore; +using ReactiveUI; using StarsAssistant.Model; using Splat; namespace StarsAssistant.ViewModels; -public partial class PlanetViewModel(Planet planet) : ViewModelBase +public partial class PlanetViewModel : ViewModelBase { - public static ObservableCollection LoadAll() + public PlanetViewModel(Planet planet) + { + Planet = planet; + + this.WhenActivated((CompositeDisposable disposables) => + { + // /* handle activation */ + // Disposable + // .Create(() => { /* handle deactivation */ }) + // .DisposeWith(disposables); + }); + } + + private readonly Planet Planet; + + public string Name => Planet.Name; + + public string Owner => Planet.OwnerId ?? String.Empty; + + public int Value => Planet.Value ?? 0; + + public int Population => Planet.Population ?? 0; + + public int SurfaceIronium => Planet.SurfaceIronium ?? 0; + + public int SurfaceBoranium => Planet.SurfaceBoranium ?? 0; + + public int SurfaceGermanium => Planet.SurfaceGermanium ?? 0; + + public static ObservableCollection LoadAll() { using var db = Locator.Current.GetService()!; var result = new ObservableCollection(); @@ -18,20 +49,4 @@ public partial class PlanetViewModel(Planet planet) : ViewModelBase } return result; } - - private readonly Planet planet = planet; - - public string Name => planet.Name; - - public string Owner => planet.OwnerId ?? String.Empty; - - public int Value => planet.Value ?? 0; - - public int Population => planet.Population ?? 0; - - public int SurfaceIronium => planet.SurfaceIronium ?? 0; - - public int SurfaceBoranium => planet.SurfaceBoranium ?? 0; - - public int SurfaceGermanium => planet.SurfaceGermanium ?? 0; } diff --git a/Stars Assistant/ViewModels/ViewModelBase.cs b/Stars Assistant/ViewModels/ViewModelBase.cs index 6bbc936..dda0d80 100644 --- a/Stars Assistant/ViewModels/ViewModelBase.cs +++ b/Stars Assistant/ViewModels/ViewModelBase.cs @@ -1,7 +1,9 @@ using ReactiveUI; +using Splat; namespace StarsAssistant.ViewModels; -public class ViewModelBase : ReactiveObject +public class ViewModelBase : ReactiveObject, IEnableLogger, IActivatableViewModel { + public ViewModelActivator Activator { get; } = new ViewModelActivator(); } diff --git a/Stars Assistant/Views/BuColView.axaml.cs b/Stars Assistant/Views/BuColView.axaml.cs index d6afd39..8b2f007 100644 --- a/Stars Assistant/Views/BuColView.axaml.cs +++ b/Stars Assistant/Views/BuColView.axaml.cs @@ -13,10 +13,11 @@ public partial class BuColView : ReactiveUserControl { public BuColView() { + InitializeComponent(); + this.WhenActivated(disposables => { }); - InitializeComponent(); } } \ No newline at end of file diff --git a/Stars Assistant/Views/MainWindow.axaml b/Stars Assistant/Views/MainWindow.axaml index b007376..74b66c2 100644 --- a/Stars Assistant/Views/MainWindow.axaml +++ b/Stars Assistant/Views/MainWindow.axaml @@ -4,6 +4,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="using:StarsAssistant.ViewModels" xmlns:views="clr-namespace:StarsAssistant.Views" + xmlns:materialIcons="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="StarsAssistant.Views.MainWindow" x:DataType="vm:MainWindowViewModel" @@ -18,16 +19,34 @@ - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + diff --git a/Stars Assistant/Views/MainWindow.axaml.cs b/Stars Assistant/Views/MainWindow.axaml.cs index 1e6100e..84bd4c1 100644 --- a/Stars Assistant/Views/MainWindow.axaml.cs +++ b/Stars Assistant/Views/MainWindow.axaml.cs @@ -13,13 +13,19 @@ public partial class MainWindow : ReactiveWindow { public MainWindow() { + InitializeComponent(); + this.WhenActivated(disposables => { this.OneWayBind(ViewModel, vm => vm.DbPath, v => v.DbPath.Text) .DisposeWith(disposables); + + this.BindCommand(ViewModel, vm => vm.NewCommand, v => v.NewButton) + .DisposeWith(disposables); + + this.BindCommand(ViewModel, vm => vm.OpenCommand, v => v.OpenButton) + .DisposeWith(disposables); }); - - InitializeComponent(); } } \ No newline at end of file