using System.Collections.ObjectModel; using System.Windows.Input; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using LageBuch.Domain; using LageBuch.Domain.Etb; using LageBuch.Domain.Time; using LageBuch.Persistence.MasterData; using LageBuch.Sync; namespace LageBuch.AppLogic.ViewModels; public sealed partial class EtbViewModel : ObservableObject, INarrowAware, IEntryForm, IDisposable { // A real Einsatz showed the Lagebuchführer never gained anything from picking Eingang, Ausgang // or Intern, so the dock no longer asks. The domain still records a direction on every entry, // and Internal is the neutral one: never shown, editable, and untouched by the System filter. // Incoming and Outgoing stay valid input -- an older peer still sends them over sync, and the // ILS reminder still writes Outgoing. private const EtbDirection ManualDirection = EtbDirection.Internal; private readonly IIncidentSession _session; private readonly IClock _clock; private readonly Action _onChanged; // Opens the create-task overlay pre-filled with an entry's text (#88, #247); null where the // host offers no task feature, which disables the "add & create task" dock button and hides // every row's create-task icon too. private readonly Action? _createTaskFromEntry; // Brings the Aufgaben given out from an entry forward, from the row's badge; null where the // host has no Aufgaben tab to show them in. private readonly Action? _showTasksFrom; // #415: an entry addressed to the Leitstelle is usually the Rückmeldung itself, so adding one // offers to restart the reminder. Not automatic: not every message to the Leitstelle is one. private readonly Action? _offerReminderReset; private readonly Action? _openSpellingHelper; private readonly string _dispatchCentreName; // Every rendered row, oldest-first like the journal, regardless of the filter. Entries is the // visible subset, newest-first; keeping the full list here lets a filter toggle rebuild Entries // without re-reading the journal. Oldest-first so a new entry is an append, not a shift (#529). private readonly List _all = new(); // Id -> row, so Sync()'s edit-detection pass finds a row in O(1) instead of scanning _all. private readonly Dictionary _byId = new(); public EtbViewModel( IIncidentSession session, IClock clock, MasterDataSet masterData, Action onChanged, Action? createTaskFromEntry = null, Action? offerReminderReset = null, Action? showTasksFrom = null, Action? openSpellingHelper = null) { ArgumentNullException.ThrowIfNull(session); ArgumentNullException.ThrowIfNull(masterData); _session = session; _clock = clock; _onChanged = onChanged; _createTaskFromEntry = createTaskFromEntry; _offerReminderReset = offerReminderReset; _showTasksFrom = showTasksFrom; _openSpellingHelper = openSpellingHelper; _dispatchCentreName = masterData.Settings.DispatchCentreName; IsReadOnly = session.IsReadOnly; CallSignOptions = masterData.RadioCallSigns; TextSnippets = masterData.TextSnippets.Where(s => s.ForEtb).ToList(); Entries = new ObservableCollection(); // Any change to the incident — from this tab, another tab, or (when joined) another device — // brings the journal up to date through the same path. _session.Changed += Sync; Sync(); } public void Dispose() => _session.Changed -= Sync; /// /// Brings the list up to date with the journal. Entries reach the journal from every module -- /// Kräfte, Atemschutz, the ILS reminder -- not only from this tab, and without this they stayed /// invisible until the Einsatz was closed, resumed or reopened. /// /// The journal is append-only, so the first pass renders just the tail it has not rendered yet: /// an append to _all, and an insert at the top of Entries to keep it newest-first. That insert /// shifts the visible rows once per new visible entry, so a large batch (joining mid-Einsatz) /// costs O(k·n) pointer moves. It is accepted on purpose: the alternative, a rebuild, resets the /// grid's scroll and selection, and leaving existing rows untouched is what this method is for. /// It also makes the method idempotent, so calling it on every save is free. /// /// A second pass handles the one way an already-rendered row's content can change without a new /// journal entry: an edit (this device's Save, or a remote device's edit arriving via Changed). /// It walks every entry once, finds its row by id (O(1) via _byId) and updates that row in place /// wherever the edit count no longer matches -- O(n) per call, and no collection change at all, /// so the grid keeps the edited row selected (#529). /// public void Sync() { var journal = _session.Incident.Journal; var reportNumbers = SituationReportNumbers(); for (var i = _all.Count; i < journal.Count; i++) { var row = ToRow(journal[i]); row.SituationReportNumber = reportNumbers.TryGetValue(row.Id, out var number) ? number : null; _all.Add(row); _byId[row.Id] = row; if (IsVisible(row)) { Entries.Insert(0, row); } } foreach (var entry in journal) { if (!_byId.TryGetValue(entry.Id, out var current) || (current.Edits.Count == entry.Edits.Count && string.Equals(current.Notes, entry.Notes, StringComparison.Ordinal))) { continue; } current.Update(entry); if (EditingEntry?.Id == entry.Id) { CancelEdit(); // the entry being edited changed underneath us (another device saved first) } } RefreshTaskBadges(); // A Lagemeldung's entry is written in the same change that numbers it, so the marker is // normally set on the first pass; a snapshot that lands in two steps catches up here. var marked = false; foreach (var (entryId, number) in reportNumbers) { if (_byId.TryGetValue(entryId, out var row) && row.SituationReportNumber != number) { row.SituationReportNumber = number; marked = true; } } if (marked && ShowOnlySituationReports) { ReconcileVisible(); } } // ETB entry id -> Lagemeldung Nr., for every report that has been sent. private Dictionary SituationReportNumbers() { var numbers = new Dictionary(); foreach (var report in _session.Incident.SituationReports) { if (report is { EtbEntryId: { } entryId, Number: { } number }) { numbers[entryId] = number; } } return numbers; } // Every change can add, complete or reopen an Aufgabe given out from any entry, here or on // another device, so every row's count is brought up to date -- one pass over the tasks and // one over the rows, and a row whose count did not move raises nothing. private void RefreshTaskBadges() { var bySource = _session.Incident.Tasks .Where(t => t.SourceEntryId is not null) .GroupBy(t => t.SourceEntryId!.Value) .ToDictionary(g => g.Key, g => (Total: g.Count(), Open: g.Count(t => !t.IsCompleted))); foreach (var row in _all) { var (total, open) = bySource.TryGetValue(row.Id, out var counts) ? counts : (0, 0); row.SetTaskCounts(total, open); } } public bool IsReadOnly { get; } public IReadOnlyList CallSignOptions { get; } /// The Textbausteine ticked for the ETB, which the entry and edit fields offer on ::. public IReadOnlyList TextSnippets { get; } public ObservableCollection Entries { get; } // System-generated lines (Kräfte, Atemschutz, Einsatz-Lebenszyklus) are usually less important // than human entries, so the operator can hide them. On by default (#223): a fresh incident // should open on an empty-looking journal, not the "Einsatz begonnen" trace. // // Deliberately narrower than EtbDirections.IsAppWritten: EtbDirection.Measurement is // app-written too, but it is a professional record of what a Trupp measured, not bookkeeping. // Hiding CO readings behind a checkbox nobody knows about is exactly what made them look // undocumented (#424), so this filter tests for System alone. [ObservableProperty] private bool _hideSystemEntries = true; /// Only the Lagemeldungen sent to the Leitstelle (#592), to find the last one quickly. [ObservableProperty] private bool _showOnlySituationReports; partial void OnHideSystemEntriesChanged(bool value) => ReconcileVisible(); partial void OnShowOnlySituationReportsChanged(bool value) => ReconcileVisible(); // In place, never Clear()+re-add: a Reset takes the grid's selected row and focus with it (#542). private void ReconcileVisible() => RowReconciler.Reconcile(Entries, VisibleNewestFirst(), r => r.Id, r => r.Id, r => r, (_, _) => { }); private List VisibleNewestFirst() { // _all is oldest-first, Entries newest-first. var visible = new List(_all.Count); for (var i = _all.Count - 1; i >= 0; i--) { if (IsVisible(_all[i])) { visible.Add(_all[i]); } } return visible; } private bool IsVisible(EtbEntryRow row) => (!HideSystemEntries || row.DirectionValue != EtbDirection.System) && (!ShowOnlySituationReports || row.SituationReportNumber is not null); // The dock and the edit panel below the grid are two separate forms, so each keeps its own // "the operator has asked" state: pressing one must not light up a field in the other (#412). private bool _addErrorsShown; private bool _editErrorsShown; [ObservableProperty] [NotifyPropertyChangedFor(nameof(NewTextError))] [NotifyPropertyChangedFor(nameof(ErrorSummary))] private string _newText = string.Empty; [ObservableProperty] private string? _newFrom; [ObservableProperty] private string? _newTo; /// Whether the Eintrag is still missing, once the operator has asked (#412). /// Everything this form is still waiting on, on one line beneath its fields (#412). public string? ErrorSummary => ValidationMessages.Summarize( NewTextError); public string? NewTextError => _addErrorsShown && string.IsNullOrWhiteSpace(NewText) ? ValidationMessages.Required : null; // Only the read-only rule gates the buttons; the empty field answers on the press (#412). private bool CanAddEntry => !IsReadOnly; /// [ObservableProperty] [NotifyPropertyChangedFor(nameof(ShowComposer))] [NotifyPropertyChangedFor(nameof(ShowComposerButton))] private bool _isNarrow; /// /// Whether the narrow layout's add-entry sheet is open. The dock is three fields and two /// buttons across ~900px; a phone shows it stacked over the list instead, opened by one /// button and closed again the moment an entry lands. Ignored by the wide layout, where the /// dock is simply always there. /// [ObservableProperty] [NotifyPropertyChangedFor(nameof(ShowComposer))] [NotifyPropertyChangedFor(nameof(ShowComposerButton))] private bool _isComposerOpen; /// /// Whether the add-entry dock is on screen: always on a wide window, only while composing on /// a phone. One property rather than two bindings, because a XAML binding is a local value and /// would win over any container query trying to restore the dock on the desktop. /// public bool ShowComposer => !IsNarrow || IsComposerOpen; /// The phone's "add an entry" affordance, shown exactly when the dock is not. public bool ShowComposerButton => IsNarrow && !IsComposerOpen; [RelayCommand(CanExecute = nameof(CanAddEntry))] private void OpenComposer() => IsComposerOpen = true; [RelayCommand] private void CloseComposer() { IsComposerOpen = false; ShowAddErrors(false); // a dismissed sheet must not reopen still complaining } /// public event EventHandler? EntrySubmitted; [RelayCommand(CanExecute = nameof(CanAddEntry))] private void AddEntry() { if (!ValidateAdd()) { return; } _session.AddJournalEntry(ManualDirection, NewText, NewFrom, NewTo); // Changed → Sync() renders it var toDispatchCentre = IsToDispatchCentre(NewTo); ClearNewEntry(); _onChanged(); EntrySubmitted?.Invoke(this, EntrySubmittedEventArgs.Succeeded); OfferReminderResetIf(toDispatchCentre); } [RelayCommand(CanExecute = nameof(CanAddEntry))] private void AddEntryAndCreateTask() { if (!ValidateAdd()) { return; } var entryId = _session.AddJournalEntry(ManualDirection, NewText, NewFrom, NewTo); var text = NewText; var toDispatchCentre = IsToDispatchCentre(NewTo); ClearNewEntry(); _onChanged(); OfferReminderResetIf(toDispatchCentre); _createTaskFromEntry?.Invoke(text, entryId); } // An "An" field is free text: match the configured name exactly, ignoring case and padding. private bool IsToDispatchCentre(string? to) => string.Equals(to?.Trim(), _dispatchCentreName, StringComparison.OrdinalIgnoreCase); private void OfferReminderResetIf(bool toDispatchCentre) { if (toDispatchCentre) { _offerReminderReset?.Invoke(); } } private void ClearNewEntry() { NewText = string.Empty; NewFrom = null; NewTo = null; ShowAddErrors(false); // the cleared field must not read as a fresh complaint // Both add paths land here, and only on success — so this is where the phone's sheet // closes and gives the list back. A failed validation returns before ever reaching it. IsComposerOpen = false; } // A refusal is reported from both add paths; only "Hinzufügen" reports a success, because // "Hinzufügen & Aufgabe" opens the task dialog, which owns focus from there (#540). private bool ValidateAdd() { ShowAddErrors(true); if (NewTextError is null) { return true; } EntrySubmitted?.Invoke(this, EntrySubmittedEventArgs.Rejected); return false; } private void ShowAddErrors(bool shown) { _addErrorsShown = shown; OnPropertyChanged(nameof(NewTextError)); OnPropertyChanged(nameof(ErrorSummary)); } // --- Edit an existing manual entry: a small panel below the grid, not inline cell editing. --- [ObservableProperty] [NotifyPropertyChangedFor(nameof(IsEditing))] [NotifyCanExecuteChangedFor(nameof(SaveEditCommand))] private EtbEntryRow? _editingEntry; [ObservableProperty] [NotifyPropertyChangedFor(nameof(EditTextError))] [NotifyPropertyChangedFor(nameof(EditErrorSummary))] private string _editText = string.Empty; /// The NOTIZ field of the edit panel (#973); blank clears the entry's notes. [ObservableProperty] private string _editNotes = string.Empty; public bool IsEditing => EditingEntry is not null; private bool CanEdit(EtbEntryRow row) => !IsReadOnly && row.IsEditable; private void BeginEdit(EtbEntryRow row) { EditingEntry = row; EditText = row.Text; EditNotes = row.Notes ?? string.Empty; ShowEditErrors(false); // a freshly opened panel starts quiet HistoryEntry = null; // editing and viewing history are separate panels; only one at a time } /// Whether the edited Eintrag was emptied, once the operator has asked (#412). /// Everything this form is still waiting on, on one line beneath its fields (#412). public string? EditErrorSummary => ValidationMessages.Summarize( EditTextError); public string? EditTextError => _editErrorsShown && string.IsNullOrWhiteSpace(EditText) ? ValidationMessages.Required : null; // IsEditing stays: with no panel open there is no field to name, so there is nothing to explain. private bool CanSaveEdit => IsEditing; [RelayCommand(CanExecute = nameof(CanSaveEdit))] private void SaveEdit() { ShowEditErrors(true); if (EditTextError is not null || EditingEntry is not { } row) { return; } // Read everything first: the first write raises Changed, and Sync() closes the panel when // the entry it shows has changed. Each half goes out only when it changed, so a notes-only // save leaves no "bearbeitet" line and sends a joined host one command, not two. var text = EditText; var notes = string.IsNullOrWhiteSpace(EditNotes) ? null : EditNotes.Trim(); if (!string.Equals(text.Trim(), row.Text, StringComparison.Ordinal)) { _session.EditJournalEntry(row.Id, text); // Changed → Sync() renders it } if (!string.Equals(notes, row.Notes, StringComparison.Ordinal)) { _session.SetJournalEntryNotes(row.Id, notes); } CancelEdit(); _onChanged(); } [RelayCommand] private void CancelEdit() { EditingEntry = null; EditText = string.Empty; EditNotes = string.Empty; ShowEditErrors(false); } private void ShowEditErrors(bool shown) { _editErrorsShown = shown; OnPropertyChanged(nameof(EditTextError)); OnPropertyChanged(nameof(EditErrorSummary)); } // --- View an edited entry's history: available whenever WasEdited, independent of IsReadOnly // and of the edit panel above -- a closed incident must still let its history be read. --- [ObservableProperty] private EtbEntryRow? _historyEntry; private void ShowHistory(EtbEntryRow row) { HistoryEntry = row; CancelEdit(); // editing and viewing history are separate panels; only one at a time } [RelayCommand] private void CloseHistory() => HistoryEntry = null; // Reopens the same overlay #88 wired to the input dock's "add & create task" button (#247), // pre-filled from the row's text; the timer still starts from "now", same as the dock button, // since anchoring it to a possibly much older entry's timestamp would be misleading. private void CreateTaskFromRow(EtbEntryRow row) => _createTaskFromEntry?.Invoke(row.Text, row.Id); /// Opens the Buchstabier-Helfer (#592), for a name that has to be spelled over the radio. [RelayCommand(CanExecute = nameof(CanOpenSpellingHelper))] private void OpenSpellingHelper() => _openSpellingHelper?.Invoke(); private bool CanOpenSpellingHelper => _openSpellingHelper is not null; private bool CanCreateTaskFromRow(EtbEntryRow row) => !IsReadOnly && _createTaskFromEntry is not null; private void ShowTasksFromRow(EtbEntryRow row) => _showTasksFrom?.Invoke(row.Id); private EtbEntryRow ToRow(EtbEntry e) => new(e, BeginEdit, CanEdit, ShowHistory, CreateTaskFromRow, CanCreateTaskFromRow, ShowTasksFromRow); } /// /// One rendered ETB row. Carries its own (rather than the view /// reaching back up to via a $parent binding, mirroring /// 's reasoning). An edit happens through 's edit /// panel, not a two-way binding on the row, and reaches the row through : the /// same instance stays in the list, so the grid keeps it selected and an open history panel follows /// the new version (#529). /// public sealed class EtbEntryRow : ObservableObject { private int? _situationReportNumber; private bool _isNotesExpanded; public EtbEntryRow( EtbEntry entry, Action beginEdit, Func canEdit, Action showHistory, Action createTask, Func canCreateTask, Action? showTasks = null) { ArgumentNullException.ThrowIfNull(entry); ArgumentNullException.ThrowIfNull(canCreateTask); Id = entry.Id; Time = Formatting.Timestamp(entry.Timestamp); From = entry.From; To = entry.To; Text = entry.Text; EnteredBy = entry.EnteredBy; DirectionValue = entry.Direction; WasEdited = entry.Edits.Count > 0; Edits = entry.Edits; IsEditable = !EtbDirections.IsAppWritten(entry.Direction); Notes = entry.Notes; BeginEditCommand = new RelayCommand(() => beginEdit(this), () => canEdit(this)); // A click on the row folds its notes in and out (#973); a row without notes has nothing to // fold. Not gated on IsReadOnly: reading the notes of a closed incident is still reading. ToggleNotesCommand = new RelayCommand(() => IsNotesExpanded = HasNotes && !IsNotesExpanded, () => HasNotes); // Deliberately not gated on IsReadOnly/IsEditable like BeginEditCommand: a closed or // remotely-joined-read-only incident must still let its history be read, since that is the // one thing that makes an edit acceptable in the first place. ShowHistoryCommand = new RelayCommand(() => showHistory(this), () => WasEdited); // Unlike BeginEditCommand, hidden entirely (not just disabled) when unavailable (#247): a // read-only/closed session can never save the resulting task, and a host that offers no // task feature at all has nowhere to send it either. CanCreateTask = canCreateTask(this); CreateTaskCommand = new RelayCommand(() => createTask(this), () => canCreateTask(this)); ShowTasksCommand = new RelayCommand(() => showTasks?.Invoke(this), () => showTasks is not null && TaskCount > 0); } public Guid Id { get; } public string Time { get; } public string? From { get; } public string? To { get; } public string Text { get; private set; } public string EnteredBy { get; } public EtbDirection DirectionValue { get; } public bool WasEdited { get; private set; } public IReadOnlyList Edits { get; private set; } public bool IsEditable { get; } /// The entry's notes (#973), or null when it has none. public string? Notes { get; private set; } public bool HasNotes => Notes is not null; /// Whether the notes show below the line. Each row starts folded. public bool IsNotesExpanded { get => _isNotesExpanded; private set { if (SetProperty(ref _isNotesExpanded, value)) { OnPropertyChanged(nameof(NotesToggleLabel)); } } } /// What the fold glyph does next, for its tooltip and automation name. public string NotesToggleLabel => IsNotesExpanded ? "Notiz ausblenden" : "Notiz einblenden"; public IRelayCommand ToggleNotesCommand { get; } public bool CanCreateTask { get; } public ICommand BeginEditCommand { get; } public IRelayCommand ShowHistoryCommand { get; } public ICommand CreateTaskCommand { get; } /// The badge's tap: brings the Aufgaben given out from this entry forward. public IRelayCommand ShowTasksCommand { get; } /// How many Aufgaben were given out from this entry. public int TaskCount { get; private set; } /// How many of them are still open. public int OpenTaskCount { get; private set; } public bool HasTasks => TaskCount > 0; /// "2 Aufgaben · 1 offen", "1 Aufgabe · erledigt", or empty while there is none. public string TaskBadge => TaskCount switch { 0 => string.Empty, _ => $"{(TaskCount == 1 ? "1 Aufgabe" : $"{TaskCount} Aufgaben")} · {(OpenTaskCount == 0 ? "erledigt" : $"{OpenTaskCount} offen")}", }; /// Whether any of them is still open: the badge reads as work outstanding then. public bool HasOpenTasks => OpenTaskCount > 0; /// Updates the counts; raises nothing when they did not move. public void SetTaskCounts(int total, int open) { if (total == TaskCount && open == OpenTaskCount) { return; } TaskCount = total; OpenTaskCount = open; OnPropertyChanged(nameof(TaskCount)); OnPropertyChanged(nameof(OpenTaskCount)); OnPropertyChanged(nameof(HasTasks)); OnPropertyChanged(nameof(HasOpenTasks)); OnPropertyChanged(nameof(TaskBadge)); ShowTasksCommand.NotifyCanExecuteChanged(); } /// The Nr. of the Lagemeldung this entry was sent as (#592), or null for any other entry. public int? SituationReportNumber { get => _situationReportNumber; set { if (SetProperty(ref _situationReportNumber, value)) { OnPropertyChanged(nameof(SituationReportLabel)); } } } /// "LM 2" for the row's marker; null when the entry is not a Lagemeldung. public string? SituationReportLabel => SituationReportNumber is { } n ? $"LM {n}" : null; /// /// Re-points this row at the current version of its entry, instead of the view model replacing /// the row object, which the grid sees as a different item and deselects. An edit changes only /// the text and its history () or the notes /// (), so nothing else is touched. /// public void Update(EtbEntry entry) { ArgumentNullException.ThrowIfNull(entry); if (entry.Id != Id) { throw new ArgumentException("Eintrag gehört nicht zu dieser Zeile.", nameof(entry)); } Text = entry.Text; Edits = entry.Edits; WasEdited = entry.Edits.Count > 0; OnPropertyChanged(nameof(Text)); OnPropertyChanged(nameof(Edits)); OnPropertyChanged(nameof(WasEdited)); // The first edit is what makes the history viewable at all. ShowHistoryCommand.NotifyCanExecuteChanged(); if (!string.Equals(Notes, entry.Notes, StringComparison.Ordinal)) { Notes = entry.Notes; OnPropertyChanged(nameof(Notes)); OnPropertyChanged(nameof(HasNotes)); if (Notes is null) { IsNotesExpanded = false; // nothing left to show } ToggleNotesCommand.NotifyCanExecuteChanged(); } } }