Files
hiha-arvio/src/HihaArvio/ViewModels/HistoryViewModel.cs
Ismo Vuorinen 48a844b0c1 feat: implement ViewModels layer with TDD (Milestone 3)
Implemented all ViewModels using strict TDD (RED-GREEN-REFACTOR) with CommunityToolkit.Mvvm.

**ViewModels Implemented:**
- MainViewModel: Coordinates shake detection, estimate generation, and display
- HistoryViewModel: Manages estimate history display and clearing
- SettingsViewModel: Handles app settings (mode selection, history size)

**MainViewModel Features:**
- Subscribes to ShakeDetectionService events
- Generates estimates when shake stops (transition from shaking→not shaking)
- Automatically saves estimates to storage
- Resets shake detection after estimate generation
- Loads and saves settings (SelectedMode)
- Proper disposal pattern (unsubscribe, stop monitoring)

**HistoryViewModel Features:**
- ObservableCollection<EstimateResult> for UI binding
- LoadHistoryCommand to fetch from storage
- ClearHistoryCommand to remove all history
- IsEmpty property for conditional UI display
- Replaces collection contents on reload

**SettingsViewModel Features:**
- SelectedMode property (Work/Generic)
- MaxHistorySize property
- SaveSettingsCommand to persist changes
- Loads settings on initialization

**Tests:**
- MainViewModel: 18 tests (RED-GREEN-REFACTOR)
- HistoryViewModel: 15 tests (RED-GREEN-REFACTOR)
- SettingsViewModel: 13 tests (RED-GREEN-REFACTOR)
- Total: 165 tests, all passing (48 models + 71 services + 46 ViewModels)

**Quality:**
- Build: 0 warnings, 0 errors across all platforms
- All tests use NSubstitute for mocking
- Property change notifications verified
- Async operations properly tested

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:44:07 +02:00

52 lines
1.2 KiB
C#

using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HihaArvio.Models;
using HihaArvio.Services.Interfaces;
namespace HihaArvio.ViewModels;
/// <summary>
/// ViewModel for displaying and managing estimate history.
/// </summary>
public partial class HistoryViewModel : ObservableObject
{
private readonly IStorageService _storageService;
[ObservableProperty]
private ObservableCollection<EstimateResult> _history;
[ObservableProperty]
private bool _isEmpty;
public HistoryViewModel(IStorageService storageService)
{
_storageService = storageService;
_history = new ObservableCollection<EstimateResult>();
_isEmpty = true;
}
[RelayCommand]
private async Task LoadHistoryAsync()
{
var estimates = await _storageService.GetHistoryAsync();
History.Clear();
foreach (var estimate in estimates)
{
History.Add(estimate);
}
IsEmpty = History.Count == 0;
}
[RelayCommand]
private async Task ClearHistoryAsync()
{
await _storageService.ClearHistoryAsync();
History.Clear();
IsEmpty = true;
}
}