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 15 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 @@ -253,6 +253,7 @@ void UpdatePlaceholderColor()
_hintColorSwitcher?.UpdateTextColor(_editText, Element.PlaceholderColor, _editText.SetHintTextColor);
}

[PortHandler]
void UpdateText()
{
string query = Control.Query;
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Microsoft.Maui
{
public interface ISearchBar : IView
{
string Text { get; }
}
}
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Label/LabelHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void MapTextDecorations(LabelHandler handler, ILabel label)
handler.TypedNativeView?.UpdateTextDecorations(label);
}

static void MapFont(LabelHandler handler, ILabel label)
public static void MapFont(LabelHandler handler, ILabel label)
{
var services = App.Current?.Services
?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Label/LabelHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void MapTextDecorations(LabelHandler handler, ILabel label)
handler.TypedNativeView?.UpdateTextDecorations(label);
}

static void MapFont(LabelHandler handler, ILabel label)
public static void MapFont(LabelHandler handler, ILabel label)
{
var services = App.Current?.Services ??
throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
Expand Down
17 changes: 17 additions & 0 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Android.Widget;
using Microsoft.Maui.Handlers;
using SearchView = AndroidX.AppCompat.Widget.SearchView;

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

string GetNativeText(SearchBarHandler searchBarHandler) =>
GetNativeSearchBar(searchBarHandler).Query;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions src/Core/tests/DeviceTests/Stubs/SearchBarStub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Microsoft.Maui.DeviceTests.Stubs
{
public partial class SearchBarStub : StubBase, ISearchBar
{
private string _text;

public string Text { get => _text; set => SetProperty(ref _text, value); }
}
}
1 change: 1 addition & 0 deletions src/Core/tests/DeviceTests/TestCategory.cs
Original file line number Diff line number Diff line change
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