Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(BarcodeInput): remove business logic from behavior #170

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(_));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On veux pas garder la saisie unique de chiffre ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si t'as raison, j'ai eu un doute parce que le code39 n'est pas limité aux chiffres par construction, mais j'ai re vérifié l'example mapping et on avait bien écrit que des chiffres alors gardons ce comportement.


private void Completed(object sender, EventArgs e)
{
var entry = (Entry)sender;
Command.Execute(entry.Text);
}
}
}
Loading