Files
Sharepoint-Toolbox/SharepointToolbox/ViewModels/MainWindowViewModel.cs
Dev 5920d42614 feat(01-06): build WPF shell — MainWindow XAML, ViewModels, LogPanelSink wiring
- Add FeatureTabBase UserControl with ProgressBar/TextBlock/CancelButton strip
  (Visibility bound to IsRunning, shown only during operations)
- Add MainWindowViewModel with TenantProfiles ObservableCollection, ConnectCommand,
  ClearSessionCommand, ManageProfilesCommand, ProgressUpdatedMessage subscription
- Add ProfileManagementViewModel wrapping ProfileService CRUD with input validation
- Add SettingsViewModel (extends FeatureViewModelBase) with language/folder settings
- Update MainWindow.xaml: DockPanel shell with Toolbar, TabControl (8 tabs), 150px
  RichTextBox LogPanel, StatusBar (tenant name | ProgressStatus | ProgressPercentage)
- MainWindow.xaml.cs: DI constructor, DataContext=viewModel, LoadProfilesAsync on Loaded
- App.xaml.cs: register all services, wire LogPanelSink after MainWindow resolved,
  register DispatcherUnhandledException and UnobservedTaskException global handlers
- App.xaml: add BoolToVisibilityConverter resource
2026-04-02 12:32:41 +02:00

128 lines
3.9 KiB
C#

using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.Logging;
using SharepointToolbox.Core.Messages;
using SharepointToolbox.Core.Models;
using SharepointToolbox.Services;
namespace SharepointToolbox.ViewModels;
public partial class MainWindowViewModel : ObservableRecipient
{
private readonly ProfileService _profileService;
private readonly SessionManager _sessionManager;
private readonly ILogger<MainWindowViewModel> _logger;
[ObservableProperty]
private TenantProfile? _selectedProfile;
[ObservableProperty]
private string _connectionStatus = "Not connected";
[ObservableProperty]
private string _progressStatus = string.Empty;
[ObservableProperty]
private int _progressPercentage;
public ObservableCollection<TenantProfile> TenantProfiles { get; } = new();
public IAsyncRelayCommand ConnectCommand { get; }
public IAsyncRelayCommand ClearSessionCommand { get; }
public RelayCommand ManageProfilesCommand { get; }
public MainWindowViewModel(
ProfileService profileService,
SessionManager sessionManager,
ILogger<MainWindowViewModel> logger)
{
_profileService = profileService;
_sessionManager = sessionManager;
_logger = logger;
ConnectCommand = new AsyncRelayCommand(ConnectAsync, () => SelectedProfile != null);
ClearSessionCommand = new AsyncRelayCommand(ClearSessionAsync, () => SelectedProfile != null);
ManageProfilesCommand = new RelayCommand(OpenProfileManagement);
IsActive = true;
}
protected override void OnActivated()
{
base.OnActivated();
Messenger.Register<ProgressUpdatedMessage>(this, (r, m) =>
{
var vm = (MainWindowViewModel)r;
vm.ProgressStatus = m.Value.Message;
vm.ProgressPercentage = m.Value.Total > 0
? (int)(100.0 * m.Value.Current / m.Value.Total)
: 0;
});
}
partial void OnSelectedProfileChanged(TenantProfile? value)
{
if (value != null)
{
WeakReferenceMessenger.Default.Send(new TenantSwitchedMessage(value));
}
ConnectCommand.NotifyCanExecuteChanged();
ClearSessionCommand.NotifyCanExecuteChanged();
}
public async Task LoadProfilesAsync()
{
try
{
var profiles = await _profileService.GetProfilesAsync();
TenantProfiles.Clear();
foreach (var profile in profiles)
TenantProfiles.Add(profile);
if (TenantProfiles.Count > 0)
SelectedProfile = TenantProfiles[0];
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to load tenant profiles.");
}
}
private async Task ConnectAsync()
{
if (SelectedProfile == null) return;
try
{
ConnectionStatus = "Connecting...";
await _sessionManager.GetOrCreateContextAsync(SelectedProfile, CancellationToken.None);
ConnectionStatus = SelectedProfile.Name;
}
catch (Exception ex)
{
ConnectionStatus = "Connection failed";
_logger.LogError(ex, "Failed to connect to tenant {TenantUrl}.", SelectedProfile.TenantUrl);
}
}
private async Task ClearSessionAsync()
{
if (SelectedProfile == null) return;
try
{
await _sessionManager.ClearSessionAsync(SelectedProfile.TenantUrl);
ConnectionStatus = "Not connected";
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to clear session for {TenantUrl}.", SelectedProfile.TenantUrl);
}
}
private void OpenProfileManagement()
{
// Profile management dialog opened by View layer (plan 01-07)
}
}