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

Implement ReturnType property in EntryHandlers #518

Merged
merged 1 commit into from
Mar 17, 2021
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 @@ -4,7 +4,7 @@ namespace Microsoft.Maui.Controls.Compatibility.Platform.Android
{
internal static class EntryRendererExtensions
{

[PortHandler]
internal static ImeAction ToAndroidImeAction(this ReturnType returnType)
{
switch (returnType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ void UpdateCharacterSpacing()
}
}

[PortHandler]
void UpdateReturnType()
{
if (Control == null || Element == null)
Expand Down
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/iOS/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ internal static UISearchBarStyle ToNativeSearchBarStyle(this PlatformConfigurati
}
}

[PortHandler]
internal static UIReturnKeyType ToUIReturnKeyType(this ReturnType returnType)
{
switch (returnType)
Expand Down
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/iOS/Renderers/EntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ bool ShouldChangeCharacters(UITextField textField, NSRange range, string replace
return newLength <= Element?.MaxLength;
}

[PortHandler]
void UpdateReturnType()
{
if (Control == null || Element == null)
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Core/IEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public interface IEntry : IView, IText, ITextInput, ITextAlignment
/// Gets a value that controls whether text prediction and automatic text correction is on or off.
/// </summary>
bool IsTextPredictionEnabled { get; }

/// <summary>
/// Gets an enumeration value that controls the appearance of the return button.
/// </summary>
ReturnType ReturnType { get; }
}
}
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public static void MapIsReadOnly(EntryHandler handler, IEntry entry)
handler.TypedNativeView?.UpdateIsReadOnly(entry);
}

public static void MapReturnType(EntryHandler handler, IEntry entry)
{
handler.TypedNativeView?.UpdateReturnType(entry);
}

void OnTextChanged(string? text)
{
if (VirtualView == null || TypedNativeView == null)
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/Handlers/Entry/EntryHandler.Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public static void MapIsTextPredictionEnabled(IViewHandler handler, IEntry entry
public static void MapPlaceholder(IViewHandler handler, IEntry entry) { }
public static void MapIsReadOnly(IViewHandler handler, IEntry entry) { }
public static void MapFont(IViewHandler handler, IEntry entry) { }
public static void MapReturnType(IViewHandler handler, IEntry entry) { }
}
}
3 changes: 2 additions & 1 deletion src/Core/src/Handlers/Entry/EntryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public partial class EntryHandler
[nameof(IEntry.IsTextPredictionEnabled)] = MapIsTextPredictionEnabled,
[nameof(IEntry.Placeholder)] = MapPlaceholder,
[nameof(IEntry.IsReadOnly)] = MapIsReadOnly,
[nameof(IEntry.Font)] = MapFont
[nameof(IEntry.Font)] = MapFont,
[nameof(IEntry.ReturnType)] = MapReturnType
};

public EntryHandler() : base(EntryMapper)
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/Entry/EntryHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public static void MapIsReadOnly(EntryHandler handler, IEntry entry)
handler.TypedNativeView?.UpdateIsReadOnly(entry);
}

public static void MapReturnType(EntryHandler handler, IEntry entry)
{
handler.TypedNativeView?.UpdateReturnType(entry);
}

void OnEditingChanged(object? sender, EventArgs e) => OnTextChanged();

void OnEditingEnded(object? sender, EventArgs e) => OnTextChanged();
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Platform/Android/EntryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,10 @@ internal static void SetInputType(this AppCompatEditText editText, IEntry entry)
if (entry.IsReadOnly)
editText.InputType = InputTypes.Null;
}

public static void UpdateReturnType(this AppCompatEditText editText, IEntry entry)
{
editText.ImeOptions = entry.ReturnType.ToNative();
}
}
}
29 changes: 29 additions & 0 deletions src/Core/src/Platform/Android/ImeActionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Android.Views.InputMethods;

namespace Microsoft.Maui
{
public static class ImeActionExtensions
{
public static ImeAction ToNative(this ReturnType returnType)
{
switch (returnType)
{
case ReturnType.Go:
return ImeAction.Go;
case ReturnType.Next:
return ImeAction.Next;
case ReturnType.Send:
return ImeAction.Send;
case ReturnType.Search:
return ImeAction.Search;
case ReturnType.Done:
return ImeAction.Done;
case ReturnType.Default:
return ImeAction.Done;
default:
throw new NotImplementedException($"ReturnType {returnType} not supported");
}
}
}
}
5 changes: 5 additions & 0 deletions src/Core/src/Platform/iOS/EntryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,10 @@ public static void UpdateFont(this UITextField textField, IEntry entry, IFontMan
var uiFont = fontManager.GetFont(entry.Font);
textField.Font = uiFont;
}

public static void UpdateReturnType(this UITextField textField, IEntry entry)
{
textField.ReturnKeyType = entry.ReturnType.ToNative();
}
}
}
29 changes: 29 additions & 0 deletions src/Core/src/Platform/iOS/ReturnKeyTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using UIKit;

namespace Microsoft.Maui
{
public static class ReturnKeyTypeExtensions
{
public static UIReturnKeyType ToNative(this ReturnType returnType)
{
switch (returnType)
{
case ReturnType.Go:
return UIReturnKeyType.Go;
case ReturnType.Next:
return UIReturnKeyType.Next;
case ReturnType.Send:
return UIReturnKeyType.Send;
case ReturnType.Search:
return UIReturnKeyType.Search;
case ReturnType.Done:
return UIReturnKeyType.Done;
case ReturnType.Default:
return UIReturnKeyType.Default;
default:
throw new NotImplementedException($"ReturnType {returnType} not supported");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Threading.Tasks;
using Android.Text;
using Android.Views.InputMethods;
using AndroidX.AppCompat.Widget;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Xunit;
using AColor = global::Android.Graphics.Color;
using AColor = Android.Graphics.Color;

namespace Microsoft.Maui.DeviceTests
{
Expand Down Expand Up @@ -38,6 +39,31 @@ public async Task FontFamilyInitializesCorrectly(string family)
Assert.NotEqual(fontManager.DefaultTypeface, nativeEntry.Typeface);
}

[Fact(DisplayName = "ReturnType Initializes Correctly")]
public async Task ReturnTypeInitializesCorrectly()
{
var xplatReturnType = ReturnType.Next;
var entry = new EntryStub()
{
Text = "Test",
ReturnType = xplatReturnType
};

ImeAction expectedValue = ImeAction.Next;

var values = await GetValueAsync(entry, (handler) =>
{
return new
{
ViewValue = entry.ReturnType,
NativeViewValue = GetNativeReturnType(handler)
};
});

Assert.Equal(xplatReturnType, values.ViewValue);
Assert.Equal(expectedValue, values.NativeViewValue);
}

[Fact(DisplayName = "Horizontal TextAlignment Initializes Correctly")]
public async Task HorizontalTextAlignmentInitializesCorrectly()
{
Expand Down Expand Up @@ -113,5 +139,8 @@ bool GetNativeIsItalic(EntryHandler entryHandler) =>

Android.Views.TextAlignment GetNativeTextAlignment(EntryHandler entryHandler) =>
GetNativeEntry(entryHandler).TextAlignment;

ImeAction GetNativeReturnType(EntryHandler entryHandler) =>
GetNativeEntry(entryHandler).ImeOptions;
}
}
29 changes: 29 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ public async Task HorizontalTextAlignmentInitializesCorrectly()
Assert.Equal(xplatHorizontalTextAlignment, values.ViewValue);
values.NativeViewValue.AssertHasFlag(expectedValue);
}

[Fact(DisplayName = "ReturnType Initializes Correctly")]
public async Task ReturnTypeInitializesCorrectly()
{
var xplatReturnType = ReturnType.Next;
var entry = new EntryStub()
{
Text = "Test",
ReturnType = xplatReturnType
};

UIReturnKeyType expectedValue = UIReturnKeyType.Next;

var values = await GetValueAsync(entry, (handler) =>
{
return new
{
ViewValue = entry.ReturnType,
NativeViewValue = GetNativeReturnType(handler)
};
});

Assert.Equal(xplatReturnType, values.ViewValue);
Assert.Equal(expectedValue, values.NativeViewValue);
}


UITextField GetNativeEntry(EntryHandler entryHandler) =>
(UITextField)entryHandler.View;
Expand Down Expand Up @@ -95,5 +121,8 @@ bool GetNativeIsItalic(EntryHandler entryHandler) =>

UITextAlignment GetNativeTextAlignment(EntryHandler entryHandler) =>
GetNativeEntry(entryHandler).TextAlignment;

UIReturnKeyType GetNativeReturnType(EntryHandler entryHandler) =>
GetNativeEntry(entryHandler).ReturnKeyType;
}
}
2 changes: 2 additions & 0 deletions src/Core/tests/DeviceTests/Stubs/EntryStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public string Text

public TextAlignment HorizontalTextAlignment { get; set; }

public ReturnType ReturnType { get; set; }

public event EventHandler<StubPropertyChangedEventArgs<string>> TextChanged;

void OnTextChanged(string oldValue, string newValue) =>
Expand Down