From da94963c4fae3230c84005c5ee85e67e12c1c7d3 Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Tue, 23 Jul 2024 20:20:01 +0200 Subject: [PATCH] Setup Test UI and Added Prototype DataGrid --- Stars Assistant/App.axaml | 1 + Stars Assistant/Model/Planet.cs | 2 +- Stars Assistant/Model/StarsDatabase.cs | 2 +- Stars Assistant/Program.cs | 47 +++++++++++++++++-- Stars Assistant/Stars Assistant.csproj | 2 +- .../ViewModels/MainWindowViewModel.cs | 9 +++- Stars Assistant/ViewModels/PlanetViewModel.cs | 26 ++++++++++ Stars Assistant/ViewModels/ViewModelBase.cs | 4 +- Stars Assistant/Views/MainWindow.axaml | 20 ++++++++ 9 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 Stars Assistant/ViewModels/PlanetViewModel.cs diff --git a/Stars Assistant/App.axaml b/Stars Assistant/App.axaml index d7f228d..d378de6 100644 --- a/Stars Assistant/App.axaml +++ b/Stars Assistant/App.axaml @@ -11,5 +11,6 @@ + \ No newline at end of file diff --git a/Stars Assistant/Model/Planet.cs b/Stars Assistant/Model/Planet.cs index 9a5c54e..39332a2 100644 --- a/Stars Assistant/Model/Planet.cs +++ b/Stars Assistant/Model/Planet.cs @@ -2,7 +2,7 @@ using System.Globalization; using CSV = CsvHelper.Configuration.Attributes; -namespace net.nehmer.sa.model; +namespace net.nehmer.sa.Model; public class Planet { diff --git a/Stars Assistant/Model/StarsDatabase.cs b/Stars Assistant/Model/StarsDatabase.cs index 7fea20e..46b22b8 100644 --- a/Stars Assistant/Model/StarsDatabase.cs +++ b/Stars Assistant/Model/StarsDatabase.cs @@ -3,7 +3,7 @@ using System; using System.Collections.Generic; using System.Numerics; -namespace net.nehmer.sa.model; +namespace net.nehmer.sa.Model; public class StarsDatabase(string DbPath) : DbContext { diff --git a/Stars Assistant/Program.cs b/Stars Assistant/Program.cs index 3b890cc..3487ed8 100644 --- a/Stars Assistant/Program.cs +++ b/Stars Assistant/Program.cs @@ -1,6 +1,12 @@ using System; using Avalonia; -using Avalonia.ReactiveUI; + +using Microsoft.EntityFrameworkCore; +using CsvHelper.Configuration; +using CsvHelper; +using net.nehmer.sa.Model; +using System.Globalization; + namespace net.nehmer.sa; @@ -10,16 +16,49 @@ sealed class Program // SynchronizationContext-reliant code before AppMain is called: things aren't initialized // yet and stuff might break. [STAThread] - public static void Main(string[] args) => BuildAvaloniaApp() + public static void Main(string[] args) + { + // __createTestData(); + + BuildAvaloniaApp() .StartWithClassicDesktopLifetime(args); + } // Avalonia configuration, don't remove; also used by visual designer. public static AppBuilder BuildAvaloniaApp() => AppBuilder.Configure() .UsePlatformDetect() .WithInterFont() - .LogToTrace() - .UseReactiveUI(); + .LogToTrace(); + + + public static void __createTestData () + { + using var db = new StarsDatabase("stars.sqlite"); + + db.Database.EnsureDeleted(); + db.Database.EnsureCreated(); + + // Note: This sample requires the database to be created before running. + Console.WriteLine($"Database path: {db.DbPath}."); + + var config = CsvConfiguration.FromAttributes(CultureInfo.InvariantCulture); + config.Delimiter = "\t"; + config.Escape = '\0'; + config.MissingFieldFound = null; + + using (var reader = new StreamReader("/home/torben/goingth/GOINGTH.p1", System.Text.Encoding.Latin1)) + using (var csv = new CsvReader(reader, config)) + { + List records = csv.GetRecords().ToList(); + foreach (Planet p in records) + { + db.Add(p); + } + db.SaveChanges(); + } + + } } diff --git a/Stars Assistant/Stars Assistant.csproj b/Stars Assistant/Stars Assistant.csproj index dd95249..6c2c0b2 100644 --- a/Stars Assistant/Stars Assistant.csproj +++ b/Stars Assistant/Stars Assistant.csproj @@ -23,12 +23,12 @@ + - diff --git a/Stars Assistant/ViewModels/MainWindowViewModel.cs b/Stars Assistant/ViewModels/MainWindowViewModel.cs index a00aa19..2822b9d 100644 --- a/Stars Assistant/ViewModels/MainWindowViewModel.cs +++ b/Stars Assistant/ViewModels/MainWindowViewModel.cs @@ -1,8 +1,13 @@ -namespace net.nehmer.sa.ViewModels; +using System.Collections.ObjectModel; -public class MainWindowViewModel : ViewModelBase +namespace net.nehmer.sa.ViewModels; + +public partial class MainWindowViewModel : ViewModelBase { #pragma warning disable CA1822 // Mark members as static public string Greeting => "Welcome to Stars Assistant!"; #pragma warning restore CA1822 // Mark members as static + + public ObservableCollection Planets { get; } = PlanetViewModel.LoadAll(); + } diff --git a/Stars Assistant/ViewModels/PlanetViewModel.cs b/Stars Assistant/ViewModels/PlanetViewModel.cs new file mode 100644 index 0000000..5d250f6 --- /dev/null +++ b/Stars Assistant/ViewModels/PlanetViewModel.cs @@ -0,0 +1,26 @@ +using System.Collections.ObjectModel; +using net.nehmer.sa.Model; + +namespace net.nehmer.sa.ViewModels; + +public partial class PlanetViewModel(Planet planet) : ViewModelBase +{ + public static ObservableCollection LoadAll() { + var context = new StarsDatabase("stars.sqlite"); + var result = new ObservableCollection(); + foreach (Planet planet in context.Planets.ToList()) + { + var vm = new PlanetViewModel(planet); + result.Add(vm); + } + return result; + } + + private readonly Planet planet = planet; + + public string Name => planet.Name; + + public string Owner => planet.Owner ?? String.Empty; + + public int Value => planet.Value ?? 0; +} diff --git a/Stars Assistant/ViewModels/ViewModelBase.cs b/Stars Assistant/ViewModels/ViewModelBase.cs index a245a88..8c79838 100644 --- a/Stars Assistant/ViewModels/ViewModelBase.cs +++ b/Stars Assistant/ViewModels/ViewModelBase.cs @@ -1,7 +1,7 @@ -using ReactiveUI; +using CommunityToolkit.Mvvm.ComponentModel; namespace net.nehmer.sa.ViewModels; -public class ViewModelBase : ReactiveObject +public class ViewModelBase : ObservableObject { } diff --git a/Stars Assistant/Views/MainWindow.axaml b/Stars Assistant/Views/MainWindow.axaml index 9c307e7..4323b39 100644 --- a/Stars Assistant/Views/MainWindow.axaml +++ b/Stars Assistant/Views/MainWindow.axaml @@ -27,6 +27,26 @@ + + + + +