From cf6823b1f2831f670467d8d7c4dc90c3dfd0b701 Mon Sep 17 00:00:00 2001 From: Audrey Brouard Date: Fri, 15 Nov 2024 16:45:54 +0100 Subject: [PATCH] refactor(BarcodeInput): remove business logic from behavior --- .../Barcode/BarcodeInputBehaviorTest.cs | 41 ------------------- .../Inputs/Barcode/BarcodeInputBehavior.cs | 27 +++++------- 2 files changed, 10 insertions(+), 58 deletions(-) diff --git a/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs b/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs index c39a632..c43966a 100644 --- a/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs +++ b/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs @@ -34,26 +34,6 @@ public void ValidGencodeFilled() Entry.Text = "2970812075764"; Check.That(Entry.Text).IsEqualTo("2970812075764"); - Command.Verify(_ => _.Execute("2970812075764"), Times.Once); - } - - [Fact] - public void UnvalidGencodeFilled() - { - Entry.Text = "297081207576"; - Entry.Text = "2970812075765"; - - Check.That(Entry.Text).IsEqualTo("297081207576"); - Command.Verify(_ => _.Execute(It.IsAny()), Times.Never); - } - - [Fact] - public void GencodeNotTotallyFilled() - { - Entry.Text = "297081207"; - - Check.That(Entry.Text).IsEqualTo("297081207"); - Command.Verify(_ => _.Execute(It.IsAny()), Times.Never); } [Fact] @@ -62,27 +42,6 @@ public void GencodeFilledWithNotDigitChar() Entry.Text = "x"; Check.That(Entry.Text).IsEqualTo(null); - Command.Verify(_ => _.Execute(It.IsAny()), Times.Never); - } - - [Fact] - public void GencodeFilledBigLength() - { - Entry.Text = "29708120757644"; - - Check.That(Entry.Text).IsEqualTo(null); - Command.Verify(_ => _.Execute(It.IsAny()), Times.Never); - } - - [Fact] - public void CanExecuteIsFalse() - { - Command.Setup(_ => _.CanExecute(It.IsAny())).Returns(false); - - Entry.Text = "2970812075764"; - - Check.That(Entry.Text).IsEqualTo("2970812075764"); - Command.Verify(_ => _.Execute(It.IsAny()), Times.Never); } } } diff --git a/Smartway.UiComponent/Inputs/Barcode/BarcodeInputBehavior.cs b/Smartway.UiComponent/Inputs/Barcode/BarcodeInputBehavior.cs index f644d48..d1159b5 100644 --- a/Smartway.UiComponent/Inputs/Barcode/BarcodeInputBehavior.cs +++ b/Smartway.UiComponent/Inputs/Barcode/BarcodeInputBehavior.cs @@ -1,6 +1,6 @@ -using System.Linq; +using System; +using System.Linq; using System.Windows.Input; -using Smartway.Barcode.Ean13; using Smartway.UiComponent.Behaviors; using Xamarin.Forms; @@ -21,6 +21,7 @@ protected override void OnAttachedTo(BindableObject bindable) base.OnAttachedTo(bindable); AssociatedObject.Focused += OnAssociatedObjectOnFocused; + AssociatedObject.Completed += Completed; AssociatedObject.TextChanged += OnTextChanged; } @@ -29,6 +30,7 @@ protected override void OnDetachingFrom(BindableObject bindable) base.OnDetachingFrom(bindable); AssociatedObject.Focused -= OnAssociatedObjectOnFocused; + AssociatedObject.Completed -= Completed; AssociatedObject.TextChanged -= OnTextChanged; } @@ -45,27 +47,18 @@ private void OnTextChanged(object sender, TextChangedEventArgs e) if (InputIsInvalid(e.NewTextValue)) { AssociatedObject.Text = e.OldTextValue; - return; } - - if (e.NewTextValue.Length != Ean13.CheckedLength) - return; - - if (Command == null || !Command.CanExecute(null)) - return; - - Command.Execute(e.NewTextValue); } private bool InputIsInvalid(string input) { - if (input.Length > Ean13.CheckedLength) - return true; - - if (input.Length == Ean13.CheckedLength && !Ean13.Check(input)) - return true; - return input.ToCharArray().Any(_ => !char.IsDigit(_)); } + + private void Completed(object sender, EventArgs e) + { + var entry = (Entry)sender; + Command.Execute(entry.Text); + } } }