diff --git a/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs b/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs index c39a632..21bebb3 100644 --- a/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs +++ b/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs @@ -37,25 +37,6 @@ public void ValidGencodeFilled() 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] public void GencodeFilledWithNotDigitChar() { @@ -64,25 +45,5 @@ public void GencodeFilledWithNotDigitChar() 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); + } } }