Finished capture page
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using WorkTime.Mobile.Pages.Components;
|
||||
using WorkTime.Models;
|
||||
using WorkTime.Models.Repositories;
|
||||
|
||||
@@ -17,21 +18,32 @@ public partial class CapturePage : ContentPage {
|
||||
|
||||
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) : ObservableObject {
|
||||
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",
|
||||
@@ -48,8 +60,9 @@ public partial class CapturePageModel(ITimeEntryRepository entryRepository) : Ob
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public async Task LoadDate(DateOnly date) {
|
||||
private async Task LoadDate(DateOnly date) {
|
||||
_currentDate = date;
|
||||
IsToday = date == DateOnly.FromDateTime(DateTime.Today);
|
||||
Entries.Clear();
|
||||
|
||||
var result = await entryRepository.GetTimeEntries(date);
|
||||
@@ -61,7 +74,8 @@ public partial class CapturePageModel(ITimeEntryRepository entryRepository) : Ob
|
||||
}
|
||||
|
||||
private void UpdateCurrentType() {
|
||||
var last = Entries.LastOrDefault();
|
||||
var last = Entries
|
||||
.LastOrDefault(e => e.Timestamp <= DateTime.Now);
|
||||
|
||||
if (last is null) {
|
||||
CurrentType = EntryType.Login;
|
||||
@@ -82,12 +96,12 @@ public partial class CapturePageModel(ITimeEntryRepository entryRepository) : Ob
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public async Task OnAppearing() {
|
||||
private async Task OnAppearing() {
|
||||
await LoadDate(_currentDate);
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public async Task RegisterEntry(TimeEntry? entry = null) {
|
||||
private async Task RegisterEntry(TimeEntry? entry = null) {
|
||||
entry ??= new TimeEntry {
|
||||
Timestamp = DateTime.Now,
|
||||
Type = CurrentType
|
||||
@@ -98,9 +112,32 @@ public partial class CapturePageModel(ITimeEntryRepository entryRepository) : Ob
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public async Task DeleteEntry(Guid entryId) {
|
||||
await entryRepository.DeleteTimeEntry(entryId);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user