Skip to content

Commit

Permalink
Merge pull request #170 from ZeroGachis/task/fwms-946-remove-business…
Browse files Browse the repository at this point in the history
…-logic

refactor(BarcodeInput): remove business logic from behavior
  • Loading branch information
AudBrou authored Nov 18, 2024
2 parents 6a9d681 + cf6823b commit 26e0583
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>()), Times.Never);
}

[Fact]
public void GencodeNotTotallyFilled()
{
Entry.Text = "297081207";

Check.That(Entry.Text).IsEqualTo("297081207");
Command.Verify(_ => _.Execute(It.IsAny<string>()), Times.Never);
}

[Fact]
Expand All @@ -62,27 +42,6 @@ public void GencodeFilledWithNotDigitChar()
Entry.Text = "x";

Check.That(Entry.Text).IsEqualTo(null);
Command.Verify(_ => _.Execute(It.IsAny<string>()), Times.Never);
}

[Fact]
public void GencodeFilledBigLength()
{
Entry.Text = "29708120757644";

Check.That(Entry.Text).IsEqualTo(null);
Command.Verify(_ => _.Execute(It.IsAny<string>()), Times.Never);
}

[Fact]
public void CanExecuteIsFalse()
{
Command.Setup(_ => _.CanExecute(It.IsAny<object>())).Returns(false);

Entry.Text = "2970812075764";

Check.That(Entry.Text).IsEqualTo("2970812075764");
Command.Verify(_ => _.Execute(It.IsAny<object>()), Times.Never);
}
}
}
27 changes: 10 additions & 17 deletions Smartway.UiComponent/Inputs/Barcode/BarcodeInputBehavior.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -21,6 +21,7 @@ protected override void OnAttachedTo(BindableObject bindable)
base.OnAttachedTo(bindable);

AssociatedObject.Focused += OnAssociatedObjectOnFocused;
AssociatedObject.Completed += Completed;
AssociatedObject.TextChanged += OnTextChanged;
}

Expand All @@ -29,6 +30,7 @@ protected override void OnDetachingFrom(BindableObject bindable)
base.OnDetachingFrom(bindable);

AssociatedObject.Focused -= OnAssociatedObjectOnFocused;
AssociatedObject.Completed -= Completed;
AssociatedObject.TextChanged -= OnTextChanged;
}

Expand All @@ -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);
}
}
}

0 comments on commit 26e0583

Please sign in to comment.