-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainViewModel.cs
More file actions
137 lines (122 loc) · 5.52 KB
/
MainViewModel.cs
File metadata and controls
137 lines (122 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using ScreenTimeTracker.Models;
namespace ScreenTimeTracker
{
/// <summary>
/// Minimal ViewModel that will progressively absorb state from <see cref="MainWindow"/>.
/// For now it only exposes the records collection and the selected date plus two placeholder
/// commands that will get wired up later. Keeping it small ensures the project compiles at
/// every incremental refactor step.
/// </summary>
public sealed class MainViewModel : INotifyPropertyChanged
{
// Domain collection to which the UI ListView/Chart will bind
public ObservableCollection<AppUsageRecord> Records { get; } = new();
// Aggregated (merged) records used for summary/chart views
public ObservableCollection<AppUsageRecord> AggregatedRecords { get; } = new();
private DateTime _selectedDate = DateTime.Today;
public DateTime SelectedDate
{
get => _selectedDate;
set
{
SetProperty(ref _selectedDate, value);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FormattedSelectedDate)));
}
}
private DateTime? _selectedEndDate;
public DateTime? SelectedEndDate
{
get => _selectedEndDate;
set => SetProperty(ref _selectedEndDate, value);
}
private bool _isDateRangeSelected;
public bool IsDateRangeSelected
{
get => _isDateRangeSelected;
set => SetProperty(ref _isDateRangeSelected, value);
}
private TimePeriod _currentTimePeriod = TimePeriod.Daily;
public TimePeriod CurrentTimePeriod
{
get => _currentTimePeriod;
set => SetProperty(ref _currentTimePeriod, value);
}
private ChartViewMode _currentChartViewMode = ChartViewMode.Hourly;
public ChartViewMode CurrentChartViewMode
{
get => _currentChartViewMode;
set
{
SetProperty(ref _currentChartViewMode, value);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ChartViewModeLabel)));
}
}
private bool _isTracking;
public bool IsTracking
{
get => _isTracking;
set
{
SetProperty(ref _isTracking, value);
// Notify dependent properties
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TrackingStatusText)));
}
}
// Properties to replace converters
public string TrackingStatusText => IsTracking ? "Active" : "Paused";
public string ChartViewModeLabel => CurrentChartViewMode == ChartViewMode.Hourly ? "Hourly View" : "Daily View";
public string FormattedSelectedDate => SelectedDate.ToString("MMM dd, yyyy");
public ICommand StartTrackingCommand { get; }
public ICommand StopTrackingCommand { get; }
public ICommand ToggleTrackingCommand { get; }
public ICommand PickDateCommand { get; }
public ICommand ToggleViewModeCommand { get; }
public MainViewModel()
{
StartTrackingCommand = new RelayCommand(_ => OnStartTrackingRequested?.Invoke(this, EventArgs.Empty));
StopTrackingCommand = new RelayCommand(_ => OnStopTrackingRequested?.Invoke(this, EventArgs.Empty));
ToggleTrackingCommand = new RelayCommand(_ => OnToggleTrackingRequested?.Invoke(this, EventArgs.Empty));
PickDateCommand = new RelayCommand(_ => OnPickDateRequested?.Invoke(this, EventArgs.Empty));
ToggleViewModeCommand = new RelayCommand(_ => OnToggleViewModeRequested?.Invoke(this, EventArgs.Empty));
}
// Events raised when commands fire – MainWindow will subscribe for now
public event EventHandler? OnStartTrackingRequested;
public event EventHandler? OnStopTrackingRequested;
public event EventHandler? OnToggleTrackingRequested;
public event EventHandler? OnPickDateRequested;
public event EventHandler? OnToggleViewModeRequested;
#region INotifyPropertyChanged
public event PropertyChangedEventHandler? PropertyChanged;
private void SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (!Equals(field, value))
{
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
/// <summary>
/// Very small ICommand implementation to keep dependencies minimal during refactor.
/// </summary>
private sealed class RelayCommand : ICommand
{
private readonly Action<object?> _execute;
private readonly Func<object?, bool>? _canExecute;
public RelayCommand(Action<object?> execute, Func<object?, bool>? canExecute = null)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object? parameter) => _canExecute?.Invoke(parameter) ?? true;
public void Execute(object? parameter) => _execute(parameter);
public event EventHandler? CanExecuteChanged;
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
}