Refactor: Remove GlobalVar and replace with IAppSettings; restructure affected infrastructure, services, and view models for dependency injection.
This commit is contained in:
@@ -15,6 +15,7 @@ namespace Jugenddienst_Stunden.ViewModels;
|
||||
/// </summary>
|
||||
public partial class StundenViewModel : ObservableObject, IQueryAttributable, INotifyPropertyChanged {
|
||||
private readonly IHoursService _hoursService;
|
||||
private readonly IAppSettings _settings;
|
||||
|
||||
public ICommand NewEntryCommand { get; }
|
||||
public ICommand SelectEntryCommand { get; }
|
||||
@@ -50,7 +51,13 @@ public partial class StundenViewModel : ObservableObject, IQueryAttributable, IN
|
||||
/// </summary>
|
||||
[ObservableProperty] private List<DayTime> dayTimes = new List<DayTime>();
|
||||
|
||||
public string Title { get; set; } = GlobalVar.Name + " " + GlobalVar.Surname;
|
||||
/// <summary>
|
||||
/// Der Titel der Stundenübersicht ist der aktuelle Benutzername
|
||||
/// </summary>
|
||||
public string Title {
|
||||
get => _settings.Name + " " + _settings.Surname;
|
||||
set;
|
||||
}
|
||||
|
||||
[ObservableProperty] private Hours hours;
|
||||
|
||||
@@ -82,14 +89,10 @@ public partial class StundenViewModel : ObservableObject, IQueryAttributable, IN
|
||||
LoadOverview = "Lade Summen für " + dateToday.ToString("MMMM yy");
|
||||
// Task.Run(() => LoadDay(value));
|
||||
// NICHT Task.Run: LoadDay aktualisiert UI-gebundene Properties
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
MainThread.BeginInvokeOnMainThread(async () => {
|
||||
try {
|
||||
await LoadDay(dateToday);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
} catch (Exception ex) {
|
||||
AlertEvent?.Invoke(this, ex.Message);
|
||||
}
|
||||
});
|
||||
@@ -162,8 +165,9 @@ public partial class StundenViewModel : ObservableObject, IQueryAttributable, IN
|
||||
/// <summary>
|
||||
/// CTOR (DI)
|
||||
/// </summary>
|
||||
public StundenViewModel(IHoursService hoursService) {
|
||||
public StundenViewModel(IHoursService hoursService, IAppSettings appSettings) {
|
||||
_hoursService = hoursService;
|
||||
_settings = appSettings;
|
||||
Hours = new Hours();
|
||||
|
||||
LoadOverview = "Lade Summen für " + DateToday.ToString("MMMM");
|
||||
@@ -177,19 +181,15 @@ public partial class StundenViewModel : ObservableObject, IQueryAttributable, IN
|
||||
// Task task = LoadDay(DateTime.Today);
|
||||
// Beim Startup NICHT direkt im CTOR laden (kann Startup/Navigation blockieren)
|
||||
// Stattdessen via Dispatcher "nach" dem Aufbau starten:
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
MainThread.BeginInvokeOnMainThread(async () => {
|
||||
try {
|
||||
await LoadDay(DateTime.Today);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
} catch (Exception ex) {
|
||||
AlertEvent?.Invoke(this, ex.Message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -244,16 +244,14 @@ public partial class StundenViewModel : ObservableObject, IQueryAttributable, IN
|
||||
/// </summary>
|
||||
public async Task LoadDay(DateTime date) {
|
||||
// kleine Initialwerte sind ok, aber UI-Thread sicher setzen:
|
||||
await MainThread.InvokeOnMainThreadAsync(() =>
|
||||
{
|
||||
await MainThread.InvokeOnMainThreadAsync(() => {
|
||||
DayTotal = new TimeOnly(0);
|
||||
Sollstunden = new TimeOnly(0);
|
||||
});
|
||||
try {
|
||||
var (dayTimes, settings) = await _hoursService.GetDayWithSettingsAsync(date);
|
||||
|
||||
await MainThread.InvokeOnMainThreadAsync(() =>
|
||||
{
|
||||
await MainThread.InvokeOnMainThreadAsync(() => {
|
||||
DayTimes = dayTimes;
|
||||
Settings = settings;
|
||||
GemeindeAktivSet = Settings.GemeindeAktivSet;
|
||||
@@ -275,8 +273,7 @@ public partial class StundenViewModel : ObservableObject, IQueryAttributable, IN
|
||||
}
|
||||
|
||||
_soll = Settings.Nominal.Where(w => w.Timetable == dt.TimeTable && w.Wochentag == dt.Wday).ToList();
|
||||
if (_soll.Count > 0)
|
||||
{
|
||||
if (_soll.Count > 0) {
|
||||
var soll = TimeOnly.FromTimeSpan(TimeSpan.FromHours(_soll[0].Zeit));
|
||||
await MainThread.InvokeOnMainThreadAsync(() => Sollstunden = soll);
|
||||
}
|
||||
@@ -288,17 +285,15 @@ public partial class StundenViewModel : ObservableObject, IQueryAttributable, IN
|
||||
//Nach der Tagessumme die anderen Tage anhängen
|
||||
if (DayTimes != null) {
|
||||
var more = await _hoursService.GetDayRangeAsync(date.AddDays(1), date.AddDays(3));
|
||||
if (more != null && more.Count > 0)
|
||||
{
|
||||
if (more != null && more.Count > 0) {
|
||||
await MainThread.InvokeOnMainThreadAsync(() =>
|
||||
DayTimes = DayTimes.Concat(more).ToList()
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
await MainThread.InvokeOnMainThreadAsync(() =>
|
||||
{
|
||||
|
||||
await MainThread.InvokeOnMainThreadAsync(() => {
|
||||
DayTimes = new List<DayTime>();
|
||||
//TODO: hier könnte auch ein Fehler kommen, dann wäre InfoEvent falsch.
|
||||
|
||||
@@ -310,12 +305,11 @@ public partial class StundenViewModel : ObservableObject, IQueryAttributable, IN
|
||||
InfoEvent?.Invoke(this, e.Message);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} finally {
|
||||
await MainThread.InvokeOnMainThreadAsync(() =>
|
||||
{
|
||||
await MainThread.InvokeOnMainThreadAsync(() => {
|
||||
OnPropertyChanged(nameof(DayTotal));
|
||||
OnPropertyChanged(nameof(Sollstunden));
|
||||
OnPropertyChanged(nameof(DateToday));
|
||||
|
||||
Reference in New Issue
Block a user