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

Add ISearchBar handler and implement Text #450

Merged
merged 16 commits into from Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -253,6 +253,7 @@ void UpdatePlaceholderColor()
_hintColorSwitcher?.UpdateTextColor(_editText, Element.PlaceholderColor, _editText.SetHintTextColor);
}

[PortHandler]
void UpdateText()
{
string query = Control.Query;
Expand Down
Expand Up @@ -332,6 +332,7 @@ void UpdatePlaceholder()
}
}

[PortHandler]
void UpdateText()
{
// There is at least one scenario where modifying the Element's Text value from TextChanged
Expand Down
4 changes: 4 additions & 0 deletions src/Controls/samples/Controls.Sample/Pages/MainPage.cs
Expand Up @@ -26,6 +26,10 @@ void SetupMauiLayout()
var verticalStack = new VerticalStackLayout() { Spacing = 5, BackgroundColor = Color.AntiqueWhite };
var horizontalStack = new HorizontalStackLayout() { Spacing = 2, BackgroundColor = Color.CornflowerBlue };

var searchBar = new SearchBar();
searchBar.Text = "A search query";
verticalStack.Add(searchBar);

var label = new Label { Text = "This will disappear in ~5 seconds", BackgroundColor = Color.Fuchsia };
label.Margin = new Thickness(15, 10, 20, 15);

Expand Down
7 changes: 7 additions & 0 deletions src/Core/src/Core/ISearchBar.cs
@@ -0,0 +1,7 @@
namespace Microsoft.Maui
{
public interface ISearchBar : IView
Copy link
Member

Choose a reason for hiding this comment

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

Should this maybe inherit from IText as well and not have the Text` property? For the additional properties, just let then exist and only add the mapping for text in this PR. The rest can be done later and we probably can and should re-use the code from Entry.

{
string Text { get; }
}
}
17 changes: 17 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Android.cs
@@ -0,0 +1,17 @@
using AndroidX.AppCompat.Widget;

namespace Microsoft.Maui.Handlers
{
public partial class SearchBarHandler : AbstractViewHandler<ISearchBar, SearchView>
rogihee marked this conversation as resolved.
Show resolved Hide resolved
{
protected override SearchView CreateNativeView()
{
return new SearchView(Context);
}

public static void MapText(SearchBarHandler handler, ISearchBar searchBar)
{
handler.TypedNativeView?.UpdateText(searchBar);
}
}
}
11 changes: 11 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Standard.cs
@@ -0,0 +1,11 @@
using System;

namespace Microsoft.Maui.Handlers
{
public partial class SearchBarHandler : AbstractViewHandler<ISearchBar, object>
{
protected override object CreateNativeView() => throw new NotImplementedException();

public static void MapText(IViewHandler handler, ISearchBar searchBar) { }
}
}
20 changes: 20 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.cs
@@ -0,0 +1,20 @@
namespace Microsoft.Maui.Handlers
{
public partial class SearchBarHandler
{
public static PropertyMapper<ISearchBar, SearchBarHandler> SearchBarMapper = new PropertyMapper<ISearchBar, SearchBarHandler>(ViewHandler.ViewMapper)
{
[nameof(ISearchBar.Text)] = MapText
};

public SearchBarHandler() : base(SearchBarMapper)
{

}

public SearchBarHandler(PropertyMapper mapper) : base(mapper ?? SearchBarMapper)
{

}
}
}
15 changes: 15 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.iOS.cs
@@ -0,0 +1,15 @@
using System;
using UIKit;

namespace Microsoft.Maui.Handlers
{
public partial class SearchBarHandler : AbstractViewHandler<ISearchBar, UISearchBar>
{
protected override UISearchBar CreateNativeView() => new UISearchBar();

public static void MapText(SearchBarHandler handler, ISearchBar searchBar)
{
handler.TypedNativeView?.UpdateText(searchBar);
}
}
}
12 changes: 12 additions & 0 deletions src/Core/src/Platform/Android/SearchBarExtensions.cs
@@ -0,0 +1,12 @@
using AndroidX.AppCompat.Widget;

namespace Microsoft.Maui
{
public static class SearchBarExtensions
{
public static void UpdateText(this SearchView searchView, ISearchBar searchBar)
{
searchView.SetQuery(searchBar.Text, false);
}
}
}
12 changes: 12 additions & 0 deletions src/Core/src/Platform/iOS/SearchBarExtensions.cs
@@ -0,0 +1,12 @@
using UIKit;

namespace Microsoft.Maui
{
public static class SearchBarExtensions
{
public static void UpdateText(this UISearchBar uiSearchBar, ISearchBar searchBar)
{
uiSearchBar.Text = searchBar.Text;
}
}
}
@@ -0,0 +1,14 @@
using Android.Widget;
using Microsoft.Maui.Handlers;

namespace Microsoft.Maui.DeviceTests
{
public partial class SearchBarHandlerTests
{
SearchView GetNativeSearchBar(SearchBarHandler searchBarHandler) =>
(SearchView)searchBarHandler.View;

string GetNativeText(SearchBarHandler searchBarHandler) =>
GetNativeSearchBar(searchBarHandler).Query;
}
}
@@ -0,0 +1,49 @@
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.SearchBar)]
public partial class SearchBarHandlerTests : HandlerTestBase<SearchBarHandler>
{
public SearchBarHandlerTests(HandlerTestFixture fixture) : base(fixture)
{
}

[Fact(DisplayName = "Text Initializes Correctly")]
public async Task TextInitializesCorrectly()
{
var searchBar = new SearchBarStub
{
Text = "Test"
};

await ValidatePropertyInitValue(searchBar, () => searchBar.Text, GetNativeText, searchBar.Text);
}

[Theory(DisplayName = "Query Text Updates Correctly")]
[InlineData(null, null)]
[InlineData(null, "Query")]
[InlineData("Query", null)]
[InlineData("Query", "Another search query")]
public async Task TextUpdatesCorrectly(string setValue, string unsetValue)
{
var searchBar = new SearchBarStub();

await ValidatePropertyUpdatesValue(
searchBar,
nameof(ISearchBar.Text),
h =>
{
var n = GetNativeText(h);
if (string.IsNullOrEmpty(n))
n = null; // native platforms may not support null text
return n;
},
setValue,
unsetValue);
}
}
}
@@ -0,0 +1,14 @@
using Microsoft.Maui.Handlers;
using UIKit;

namespace Microsoft.Maui.DeviceTests
{
public partial class SearchBarHandlerTests
{
UISearchBar GetNativeEntry(SearchBarHandler searchBarHandler) =>
(UISearchBar)searchBarHandler.View;

string GetNativeText(SearchBarHandler searchBarHandler) =>
GetNativeEntry(searchBarHandler).Text;
}
}
11 changes: 11 additions & 0 deletions src/Core/tests/DeviceTests/Stubs/SearchBarStub.cs
@@ -0,0 +1,11 @@
namespace Microsoft.Maui.DeviceTests.Stubs
{
public partial class SearchBarStub : StubBase, ISearchBar
{
private string _text;

public string Text { get => _text; set => SetProperty(ref _text, value); }

public Color TextColor { get; set; }
}
}
1 change: 1 addition & 0 deletions src/Core/tests/DeviceTests/TestCategory.cs
Expand Up @@ -5,6 +5,7 @@ public static class TestCategory
public const string Button = "Button";
public const string Entry = "Entry";
public const string Label = "Label";
public const string SearchBar = "SearchBar";
public const string Slider = "Slider";
public const string Switch = "Switch";
public const string Layout = "Layout";
Expand Down