diff --git a/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor b/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor index 5e3de39..f1c9f65 100644 --- a/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor +++ b/SteUp.Shared/Components/SingleElements/Modal/ModalFormScheda.razor @@ -896,29 +896,32 @@ var modal = await WithChildDialog(() => Dialog.OpenSelectArt(articoli)); + // Gli articoli si aggiungono sul thread della UI, come fanno le scansioni: + // toccare Scheda.Articoli da qui mentre arriva una lettura (o mentre la + // lista viene disegnata) puo' far perdere articoli. await InvokeAsync(() => { OnLoading = false; TextLoading = null; + if (modal is { Canceled: false, Data: List articoliSelezionati }) + { + // Quelli gia' sparati non si aggiungono di nuovo. + Scheda.Articoli.InsertRange(0, articoliSelezionati + .DistinctBy(x => x.Barcode) + .Where(x => !ContainsBarcode(x.Barcode)) + .Select(x => new SchedaArticolo + { + Barcode = x.Barcode, + Descrizione = x.Descrizione + }) + ); + + RecalcDirty(); + } + StateHasChanged(); }); - - if (modal is { Canceled: false, Data: List articoliSelezionati }) - { - Scheda.Articoli.InsertRange(0, articoliSelezionati.ConvertAll(x => new SchedaArticolo - { - Barcode = x.Barcode, - Descrizione = x.Descrizione - }) - ); - - await InvokeAsync(() => - { - RecalcDirty(); - StateHasChanged(); - }); - } }); } @@ -984,7 +987,7 @@ try { if (value is null) return; - if (Scheda.Articoli.Any(x => x.Barcode.Equals(value))) return; + if (ContainsBarcode(value)) return; await InvokeAsync(() => { @@ -999,6 +1002,11 @@ OnLoading = false; StateHasChanged(); + // Il controllo va ripetuto sul barcode restituito: il server puo' rispondere + // con un barcode diverso da quello letto (lo stesso gia' scelto dalla griglia), + // e due letture ravvicinate superano entrambe il controllo iniziale. + if (art != null && ContainsBarcode(art.Barcode)) return; + if (art != null) { Scheda.Articoli.Insert(0, new SchedaArticolo @@ -1032,6 +1040,8 @@ private void OnErrorScan(string? value) => OnError(new Exception(value), value); + private bool ContainsBarcode(string barcode) => Scheda.Articoli.Any(x => x.Barcode.Equals(barcode)); + #endregion private void OnError(Exception? e, string? errorMessage)