144 lines
4.1 KiB
C#
144 lines
4.1 KiB
C#
using System.Collections.ObjectModel;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using WorkTime.Mobile.Pages.Components;
|
|
using WorkTime.Models;
|
|
using WorkTime.Models.Repositories;
|
|
|
|
namespace WorkTime.Mobile.Pages;
|
|
|
|
public partial class CapturePage : ContentPage {
|
|
private readonly CapturePageModel _model;
|
|
|
|
public CapturePage(CapturePageModel model) {
|
|
InitializeComponent();
|
|
BindingContext = model;
|
|
_model = model;
|
|
}
|
|
|
|
protected override void OnAppearing() {
|
|
base.OnAppearing();
|
|
_model.Navigation = Navigation;
|
|
_model.Window = Window!;
|
|
|
|
if (_model.AppearingCommand.CanExecute(null))
|
|
_model.AppearingCommand.Execute(null);
|
|
}
|
|
}
|
|
|
|
public partial class CapturePageModel(ITimeEntryRepository entryRepository, IServiceProvider provider) : ObservableObject {
|
|
|
|
private DateOnly _currentDate = DateOnly.FromDateTime(DateTime.Now);
|
|
|
|
public INavigation Navigation = null!;
|
|
public Window Window = null!;
|
|
|
|
[ObservableProperty]
|
|
public partial ObservableCollection<TimeEntry> Entries { get; set; } = new();
|
|
|
|
[ObservableProperty]
|
|
public partial EntryType CurrentType { get; set; } = EntryType.Login;
|
|
|
|
[ObservableProperty]
|
|
public partial bool IsToday { get; set; }
|
|
|
|
[ObservableProperty]
|
|
public partial TimeEntry? SelectedEntry { get; set; }
|
|
|
|
public string CurrentTypeName => CurrentType switch {
|
|
EntryType.Login => "Einstempeln",
|
|
EntryType.LoginHome => "Einstempeln (mobA)",
|
|
EntryType.LoginTrip => "Dienstreise starten",
|
|
EntryType.Logout => "Ausstempeln",
|
|
EntryType.LogoutHome => "Ausstempeln (mobA)",
|
|
EntryType.LogoutTrip => "Dienstreise beenden",
|
|
_ => "UNKNOWN"
|
|
};
|
|
|
|
partial void OnCurrentTypeChanged(EntryType value) {
|
|
OnPropertyChanged(nameof(CurrentTypeName));
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task LoadDate(DateOnly date) {
|
|
_currentDate = date;
|
|
IsToday = date == DateOnly.FromDateTime(DateTime.Today);
|
|
Entries.Clear();
|
|
|
|
var result = await entryRepository.GetTimeEntries(date);
|
|
foreach (var entry in result) {
|
|
Entries.Add(entry);
|
|
}
|
|
|
|
UpdateCurrentType();
|
|
}
|
|
|
|
private void UpdateCurrentType() {
|
|
var last = Entries
|
|
.LastOrDefault(e => e.Timestamp <= DateTime.Now);
|
|
|
|
if (last is null) {
|
|
CurrentType = EntryType.Login;
|
|
return;
|
|
}
|
|
|
|
CurrentType = last.Type switch {
|
|
EntryType.Login => EntryType.Logout,
|
|
EntryType.LoginHome => EntryType.LogoutHome,
|
|
EntryType.LoginTrip => EntryType.LogoutTrip,
|
|
|
|
EntryType.Logout => EntryType.Login,
|
|
EntryType.LogoutHome => EntryType.LoginHome,
|
|
EntryType.LogoutTrip => EntryType.LoginTrip,
|
|
|
|
_ => EntryType.Login
|
|
};
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task OnAppearing() {
|
|
await LoadDate(_currentDate);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task RegisterEntry(TimeEntry? entry = null) {
|
|
entry ??= new TimeEntry {
|
|
Timestamp = DateTime.Now,
|
|
Type = CurrentType
|
|
};
|
|
|
|
await entryRepository.AddTimeEntry(entry);
|
|
await LoadDate(_currentDate);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task DeleteEntry(TimeEntry entry) {
|
|
SelectedEntry = null;
|
|
if (!IsToday) return;
|
|
|
|
var confirmed = await Window.Page!.DisplayAlertAsync(
|
|
"Achtung!",
|
|
"Möchten sie diesen Eintrag wirklich löschen?",
|
|
"Löschen",
|
|
"Abbrechen");
|
|
|
|
if (!confirmed) return;
|
|
|
|
await entryRepository.DeleteTimeEntry(entry.Id);
|
|
await LoadDate(_currentDate);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task OpenPopup() {
|
|
var modal = new AddEntryModal(provider.GetRequiredService<AddEntryModel>());
|
|
await Navigation.PushModalAsync(modal);
|
|
|
|
var result = await modal.Result.Task;
|
|
await Navigation.PopModalAsync();
|
|
|
|
if (result is not null)
|
|
await RegisterEntry(result);
|
|
}
|
|
|
|
}
|