Skip to content

Commit

Permalink
refactor(BarcodeInput): remove business logic from behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
AudBrou committed Nov 14, 2024
1 parent 6a9d681 commit 2165e70
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 119 deletions.

This file was deleted.

38 changes: 7 additions & 31 deletions Smartway.UiComponent/Inputs/Barcode/BarcodeInputBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Windows.Input;
using Smartway.Barcode.Ean13;
using Smartway.UiComponent.Behaviors;
Expand All @@ -21,51 +22,26 @@ protected override void OnAttachedTo(BindableObject bindable)
base.OnAttachedTo(bindable);

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

protected override void OnDetachingFrom(BindableObject bindable)
{
base.OnDetachingFrom(bindable);

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

private void OnAssociatedObjectOnFocused(object sender, FocusEventArgs e)
{
AssociatedObject.Keyboard = Keyboard.Numeric;
}

private void OnTextChanged(object sender, TextChangedEventArgs e)
private void Completed(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(e.NewTextValue))
return;

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(_));
var entry = (Entry)sender;
Command.Execute(entry.Text);
}
}
}

0 comments on commit 2165e70

Please sign in to comment.