Avalonia-Grundgeruest: plattformneutrale App laeuft (Shell + erstes Fenster)

Neues Projekt src/PolyTrader.App.Avalonia (net10.0, Avalonia 12.1.1, LiveCharts2 2.0.5)
- laeuft unter Windows und Linux aus derselben Quelle.

Enthalten:
- Program.cs mit bewusst getrenntem Aufbau: BuildHost() stellt Persistenz, Dienste und
  Module ohne jeden UI-Bezug zusammen, erst Main haengt Avalonia daran. Damit ist der
  kopflose Linux-Betrieb (--headless, Stufe L2) ohne Umbau erreichbar - der Schalter ist
  bereits drin.
- AvaloniaUiHost als IModuleUiHost: gleiche Semantik wie die WinForms-Shell (ein Fenster
  je View, offene nach vorn holen, alles maximiert).
- Fenster-Menueleiste vollstaendig DEKLARATIV (Controls/WindowMenuBar.axaml + ItemsSource
  auf WindowMenuModel.Entries). Loest die alte Fassung ab, die menu.Items zur Laufzeit
  leerte und neu befuellte - genau der Punkt, den die neue Layout-Regel verbietet.
- ViewIcons fuer Avalonia: dieselben Schluessel und dieselben PNGs wie zuvor, Core und
  Module bleiben unveraendert.
- LauncherWindow, JobsWindow, ShutdownConfirmWindow (inkl. der 10-Sekunden-Sperre).
- --smoke-ui als Nachfolger der WinForms-Konstruktionspruefung; startet den Host bewusst
  NICHT, damit ein reiner UI-Test nicht die Trading-Engine gegen echte Endpunkte anwirft.

Dabei aufgeraeumt:
- JobManager.Jobs: BindingList -> ObservableCollection. BindingList implementiert kein
  INotifyCollectionChanged; neu registrierte Jobs waeren in Avalonia unsichtbar geblieben.
- StartupHydrationService aufgeteilt in CoreStateHydrationService (Core: Accounts +
  Demo-Positionen) und CopyTradingHydrationService (Modul: Settings + Trader). Behebt einen
  latenten Fehler: bei deaktiviertem Copytrading-Modul waeren die Accounts gar nicht mehr
  hydriert worden, obwohl sie zum Core gehoeren.

Verifiziert: Solution baut, 442 Tests gruen, --smoke-ui gruen, die App laeuft real mit
Fenster und allen Trading-Diensten (Market-Sync, Master-Trader-Analyse, RF-Scanner),
und publisht fuer linux-x64.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Richard
2026-08-06 17:55:05 +02:00
co-authored by Claude Opus 5
parent a2b1c18ee3
commit cd59e5c0a5
26 changed files with 1352 additions and 43 deletions
-115
View File
@@ -1,115 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using PolyTrader.Core.Persistence;
using PolyTrader.Modules.CopyTrading.Logic;
using PolyTrader.Modules.CopyTrading.Persistence;
using PolyTraderSharp.Models;
namespace PolyTraderSharp.Services
{
/// <summary>
/// Hydriert den TradingState (Accounts, Demo-Positionen, Trader) EINMALIG beim App-Start.
/// Registriert als erster IHostedService, damit die Hydration abgeschlossen ist, bevor die
/// Trading-BackgroundServices (TraderMonitor, CopyTradingEngine, ...) ihre ExecuteAsync
/// starten. Behebt den Startup-Race, bei dem Services gegen einen leeren State anliefen
/// (früher lag diese Logik in frm_main.LoadDatabaseAndState und lief erst NACH AppHost.Start()).
/// </summary>
public class StartupHydrationService : IHostedService
{
private readonly TradingState _state;
private readonly CopyTradingState _copyState;
private readonly IAccountRepository _accountRepo;
private readonly IPositionRepository _positionRepo;
private readonly ICopyTradingAccountSettingsRepository _accountSettingsRepo;
private readonly ITrackedTraderRepository _traderRepo;
private readonly TerminalLogger _logger;
public StartupHydrationService(
TradingState state,
CopyTradingState copyState,
IAccountRepository accountRepo,
IPositionRepository positionRepo,
ICopyTradingAccountSettingsRepository accountSettingsRepo,
ITrackedTraderRepository traderRepo,
TerminalLogger logger)
{
_state = state;
_copyState = copyState;
_accountRepo = accountRepo;
_positionRepo = positionRepo;
_accountSettingsRepo = accountSettingsRepo;
_traderRepo = traderRepo;
_logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
try
{
var accounts = _accountRepo.GetAll();
foreach (var acc in accounts)
{
if (acc.IsDemo)
{
foreach (var pos in _positionRepo.GetDemo(acc.AccountId))
{
acc.OpenPositions.TryAdd(pos.TokenId, pos);
}
}
_state.Accounts[acc.AccountId] = acc;
}
HydrateAccountSettings(accounts);
foreach (var trd in _traderRepo.GetAll())
{
_copyState.Traders[trd.Id] = trd;
}
_logger.Info($"Startup-Hydration abgeschlossen: {_state.Accounts.Count} Accounts, {_copyState.Traders.Count} Trader geladen.");
}
catch (System.Exception ex)
{
_logger.Error($"Startup-Hydration fehlgeschlagen: {ex.Message}");
}
return Task.CompletedTask;
}
/// <summary>
/// Lädt die Copytrading-Account-Settings in den State. Existiert für einen Account noch
/// kein Eintrag, wird ein Default-Eintrag angelegt und persistiert.
/// </summary>
private void HydrateAccountSettings(List<AccountState> accounts)
{
var existing = _accountSettingsRepo.GetAll().ToDictionary(s => s.AccountId);
int created = 0;
foreach (var acc in accounts)
{
if (existing.TryGetValue(acc.AccountId, out var s))
{
_copyState.AccountSettings[acc.AccountId] = s;
// Konfig-Plausibilität: BUY-Preisabstand ≥ SELL-Floor → Leiter startet am Floor.
if (SellLogic.IsLadderConfigInverted(s.MaxPriceDifference, s.SellFloorPct))
_logger.Warning($"⚠️ [Settings] {acc.Name}: Max. Preisabstand ({s.MaxPriceDifference:F1}%) ≥ SELL-Floor " +
$"({s.SellFloorPct:F1}%) die SELL-Leiter startet direkt am Floor (keine echte Eskalation). Floor erhöhen oder Abstand senken.");
continue;
}
var settings = new CopyTradingAccountSettings { AccountId = acc.AccountId };
_accountSettingsRepo.Upsert(settings);
_copyState.AccountSettings[acc.AccountId] = settings;
created++;
}
if (created > 0)
_logger.Info($"Copytrading-Account-Settings: {created} Default-Eintrag/-Einträge angelegt.");
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}