Skip to content

Commit 8b8f578

Browse files
heveyhartez
andauthored
Implement IsTextPredictionEnabled property on Editor (#515)
* Initial implementation * Added Tests * Added PortHandler Attributes * Added Tests * Handle turning text prediction back on Co-authored-by: E.Z. Hart <[email protected]>
1 parent df48b15 commit 8b8f578

File tree

14 files changed

+64
-6
lines changed

14 files changed

+64
-6
lines changed

src/Compatibility/Core/src/Android/Renderers/EditorRenderer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ protected virtual void UpdateFont()
216216
EditText.SetTextSize(ComplexUnitType.Sp, (float)Element.FontSize);
217217
}
218218

219+
[PortHandler("Partially Ported")]
219220
void UpdateInputType()
220221
{
221222
Editor model = Element;

src/Compatibility/Core/src/iOS/Renderers/EditorRenderer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ protected internal virtual void UpdateFont()
306306
TextView.Font = font;
307307
}
308308

309+
[PortHandler("Partially Ported")]
309310
void UpdateKeyboard()
310311
{
311312
var keyboard = Element.Keyboard;

src/Controls/samples/Controls.Sample/Pages/MainPage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void SetupMauiLayout()
8585

8686
verticalStack.Add(new Editor());
8787
verticalStack.Add(new Editor { Text = "Editor" });
88+
verticalStack.Add(new Editor { Text = "Predictive Text Off", IsTextPredictionEnabled = false });
8889

8990
var entry = new Entry();
9091
entry.TextChanged += (sender, e) =>

src/Core/src/Core/IEditor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
/// </summary>
66
public interface IEditor : IView, IText
77
{
8-
8+
/// <summary>
9+
/// Gets a value that controls whether text prediction and automatic text correction is on or off.
10+
/// </summary>
11+
bool IsTextPredictionEnabled { get; }
912
}
1013
}

src/Core/src/Handlers/Editor/EditorHandler.Android.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@ public static void MapCharacterSpacing(EditorHandler handler, IEditor editor)
3030
{
3131
handler.TypedNativeView?.UpdateCharacterSpacing(editor);
3232
}
33+
34+
public static void MapPredictiveText(EditorHandler handler, IEditor editor)
35+
{
36+
handler.TypedNativeView?.UpdatePredictiveText(editor);
37+
}
3338
}
3439
}

src/Core/src/Handlers/Editor/EditorHandler.Standard.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public partial class EditorHandler : AbstractViewHandler<IEditor, object>
77
protected override object CreateNativeView() => throw new NotImplementedException();
88

99
public static void MapText(IViewHandler handler, IEditor editor) { }
10+
1011
public static void MapCharacterSpacing(IViewHandler handler, IEditor editor) { }
12+
13+
public static void MapPredictiveText(EditorHandler handler, IEditor editor) { }
1114
}
1215
}

src/Core/src/Handlers/Editor/EditorHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ public partial class EditorHandler
55
public static PropertyMapper<IEditor, EditorHandler> EditorMapper = new PropertyMapper<IEditor, EditorHandler>(ViewHandler.ViewMapper)
66
{
77
[nameof(IEditor.Text)] = MapText,
8-
[nameof(IEditor.CharacterSpacing)] = MapCharacterSpacing
8+
[nameof(IEditor.CharacterSpacing)] = MapCharacterSpacing,
9+
[nameof(IEditor.IsTextPredictionEnabled)] = MapPredictiveText
910
};
1011

1112
public EditorHandler() : base(EditorMapper)

src/Core/src/Handlers/Editor/EditorHandler.iOS.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CoreGraphics;
1+
using System;
2+
using CoreGraphics;
23
using UIKit;
34

45
namespace Microsoft.Maui.Handlers
@@ -24,5 +25,10 @@ public static void MapCharacterSpacing(EditorHandler handler, IEditor editor)
2425
{
2526
handler.TypedNativeView?.UpdateCharacterSpacing(editor);
2627
}
28+
29+
public static void MapPredictiveText(EditorHandler handler, IEditor editor)
30+
{
31+
handler.TypedNativeView?.UpdatePredictiveText(editor);
32+
}
2733
}
2834
}

src/Core/src/Platform/Android/EditorExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using AndroidX.AppCompat.Widget;
1+
using Android.Text;
2+
using AndroidX.AppCompat.Widget;
23

34
namespace Microsoft.Maui
45
{
@@ -23,5 +24,13 @@ public static void UpdateCharacterSpacing(this AppCompatEditText editText, IEdit
2324
{
2425
editText.LetterSpacing = editor.CharacterSpacing.ToEm();
2526
}
27+
28+
public static void UpdatePredictiveText(this AppCompatEditText editText, IEditor editor)
29+
{
30+
if(editor.IsTextPredictionEnabled)
31+
return;
32+
33+
editText.InputType |= InputTypes.TextFlagNoSuggestions;
34+
}
2635
}
2736
}

src/Core/src/Platform/iOS/EditorExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@ public static void UpdateCharacterSpacing(this UITextView textView, IEditor edit
2323

2424
// TODO: Include AttributedText to Label Placeholder
2525
}
26+
27+
public static void UpdatePredictiveText(this UITextView textView, IEditor editor)
28+
{
29+
textView.AutocorrectionType = editor.IsTextPredictionEnabled
30+
? UITextAutocorrectionType.Yes : UITextAutocorrectionType.No;
31+
}
2632
}
2733
}

src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.Android.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System.Threading.Tasks;
1+
using Android.Text;
22
using AndroidX.AppCompat.Widget;
33
using Microsoft.Maui.DeviceTests.Stubs;
44
using Microsoft.Maui.Handlers;
5+
using System.Threading.Tasks;
56
using Xunit;
67

78
namespace Microsoft.Maui.DeviceTests
@@ -39,6 +40,9 @@ AppCompatEditText GetNativeEditor(EditorHandler editorHandler) =>
3940

4041
string GetNativeText(EditorHandler editorHandler) =>
4142
GetNativeEditor(editorHandler).Text;
43+
44+
bool GetNativeIsTextPredictionEnabled(EditorHandler editorHandler) =>
45+
!GetNativeEditor(editorHandler).InputType.HasFlag(InputTypes.TextFlagNoSuggestions);
4246

4347
double GetNativeCharacterSpacing(EditorHandler editorHandler)
4448
{

src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,18 @@ await ValidatePropertyUpdatesValue(
4646
setValue,
4747
unsetValue);
4848
}
49+
50+
[Theory(DisplayName = "Is Text Prediction Enabled")]
51+
[InlineData(true)]
52+
[InlineData(false)]
53+
public async Task IsTextPredictionEnabledCorrectly(bool isEnabled)
54+
{
55+
var editor = new EditorStub()
56+
{
57+
IsTextPredictionEnabled = isEnabled
58+
};
59+
60+
await ValidatePropertyInitValue(editor, () => editor.IsTextPredictionEnabled, GetNativeIsTextPredictionEnabled, isEnabled);
61+
}
4962
}
5063
}

src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.iOS.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,8 @@ double GetNativeCharacterSpacing(EditorHandler editorHandler)
4444
var editor = GetNativeEditor(editorHandler);
4545
return editor.AttributedText.GetCharacterSpacing();
4646
}
47-
}
47+
48+
bool GetNativeIsTextPredictionEnabled(EditorHandler editorHandler) =>
49+
GetNativeEditor(editorHandler).AutocorrectionType == UITextAutocorrectionType.Yes;
50+
}
4851
}

src/Core/tests/DeviceTests/Stubs/EditorStub.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ public partial class EditorStub : StubBase, IEditor
99
public Font Font { get; set; }
1010

1111
public double CharacterSpacing { get; set; }
12+
13+
public bool IsTextPredictionEnabled { get; set; }
1214
}
1315
}

0 commit comments

Comments
 (0)