void UpdateMaxLength() { _editText = _editText ?? Control.GetChildrenOfType <EditText>().FirstOrDefault(); var currentFilters = new List <IInputFilter>(_editText?.GetFilters() ?? new IInputFilter[0]); for (var i = 0; i < currentFilters.Count; i++) { if (currentFilters[i] is InputFilterLengthFilter) { currentFilters.RemoveAt(i); break; } } currentFilters.Add(new InputFilterLengthFilter(Element.MaxLength)); _editText?.SetFilters(currentFilters.ToArray()); var currentControlText = Control.Query; if (currentControlText.Length > Element.MaxLength) { Control.SetQuery(currentControlText.Substring(0, Element.MaxLength), false); } }
public static void SetLengthFilter(this EditText editText, int maxLength) { var currentFilters = new List <IInputFilter>(editText.GetFilters() ?? new IInputFilter[0]); var changed = false; for (var i = 0; i < currentFilters.Count; i++) { if (currentFilters[i] is InputFilterLengthFilter) { currentFilters.RemoveAt(i); changed = true; break; } } if (maxLength > 0) { currentFilters.Add(new InputFilterLengthFilter(maxLength)); changed = true; } if (changed) { editText.SetFilters(currentFilters.ToArray()); } }
////////////////////////////////////////////////////////////////////////////////////////////////// // Helper methods ////////////////////////////////////////////////////////////////////////////////////////////////// protected void AddWillChangeDelegate(EditText editText, Func <string, bool> willChangeDelegate) { var oldFilters = editText.GetFilters(); var newFilters = new IInputFilter[oldFilters.Length + 1]; newFilters[newFilters.Length - 1] = new DelegatedInputFilter(willChangeDelegate); editText.SetFilters(newFilters); }
public void SetFilters_WhenCalledWithoutFilters_DoNothing() { EditText editText = new EditText(MainActivity.Current); editText.SetFilters(); var appliedFilters = editText.GetFilters(); if (appliedFilters != null) { Assert.Empty(appliedFilters); } }
public void SetFilters_WhenCalledWithMultipleFilters_AppliesAllFilters() { EditText editText = new EditText(MainActivity.Current); var filter1 = new MockInputFilter(); var filter2 = new MockInputFilter(); var filter3 = new MockInputFilter(); var filters = new IInputFilter[] { filter1, filter2, filter3 }; editText.SetFilters(filter1, filter2, filter3); var appliedFilters = editText.GetFilters(); Assert.Equal(filters, appliedFilters); }
public void SetFilters_WhenCalledWithMultipleFiltersWithNullAndDuplicates_AppliesAllFilters() { EditText editText = new EditText(MainActivity.Current); var filter1 = new MockInputFilter(); var filter2 = new MockInputFilter(); var filter3 = new MockInputFilter(); var nullFilter = null as IInputFilter; var filters = new IInputFilter[] { filter1, filter2, nullFilter, filter3, filter2, nullFilter, filter1, filter2 }; editText.SetFilters(filter1, filter2, nullFilter, filter3, filter2, nullFilter, filter1, filter2); var appliedFilters = editText.GetFilters(); Assert.Equal(filters, appliedFilters); }