Skip to content

Commit

Permalink
[C] Added ability to predefine value to prompt (xamarin#8362) fixes x…
Browse files Browse the repository at this point in the history
…amarin#8346

* Added ability to predefine value

* Prevent ABI break

* Update Issue6713.cs
  • Loading branch information
jfversluis authored and felipebaltazar committed Dec 1, 2019
1 parent b336b30 commit e33fb41
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
Expand Up @@ -7,6 +7,8 @@ namespace Xamarin.Forms.Controls.Issues
[Issue(IssueTracker.Github, 6713, "[Enhancement] Display prompts", PlatformAffected.iOS | PlatformAffected.Android)]
public class Issue6713 : TestContentPage // or TestMasterDetailPage, etc ...
{
const string PrefilledValue = "1337";

protected override void Init()
{
var scrollView = new ScrollView();
Expand All @@ -22,7 +24,7 @@ protected override void Init()
var button = new Button { Text = "Default keyboard" };
button.Clicked += async (sender, e) =>
{
var result = await DisplayPromptAsync("What’s the most useless product around today?", "The USB pet rock is definitely up there. What items do you have a hard time believing they actually exist?");
var result = await DisplayPromptAsync("What’s the most useless product around today?", "The USB pet rock is definitely up there. What items do you have a hard time believing they actually exist?", initialValue: "");
if (result != null)
(sender as Button).Text = result;
Expand All @@ -32,13 +34,23 @@ protected override void Init()
var button2 = new Button { Text = "Numeric keyboard" };
button2.Clicked += async (sender, e) =>
{
var result = await DisplayPromptAsync("What’s the meaning of life?", "You know that number.", maxLength:2, keyboard:Keyboard.Numeric);
var result = await DisplayPromptAsync("What’s the meaning of life?", "You know that number.", maxLength:2, keyboard:Keyboard.Numeric, initialValue: "");
if (result != null)
(sender as Button).Text = result;
};
stackLayout.Children.Add(button2);

var button3 = new Button { Text = "Prefilled" };
button3.Clicked += async (sender, e) =>
{
var result = await DisplayPromptAsync("The input field should have a value already", $"And it should be {PrefilledValue}", initialValue: PrefilledValue);
if (result != null)
(sender as Button).Text = result;
};
stackLayout.Children.Add(button3);

scrollView.Content = stackLayout;
Content = scrollView;
}
Expand Down
9 changes: 8 additions & 1 deletion Xamarin.Forms.Core/Page.cs
Expand Up @@ -209,9 +209,16 @@ public Task<bool> DisplayAlert(string title, string message, string accept, stri
return args.Result.Task;
}

[Obsolete("DisplayPromptAsync overload is obsolete as of version 4.5.0 and is no longer supported.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public Task<string> DisplayPromptAsync(string title, string message, string accept = "OK", string cancel = "Cancel", string placeholder = null, int maxLength = -1, Keyboard keyboard = default(Keyboard))
{
var args = new PromptArguments(title, message, accept, cancel, placeholder, maxLength, keyboard);
return DisplayPromptAsync(title, message, accept, cancel, placeholder, maxLength, keyboard, "");
}

public Task<string> DisplayPromptAsync(string title, string message, string accept = "OK", string cancel = "Cancel", string placeholder = null, int maxLength = -1, Keyboard keyboard = default(Keyboard), string initialValue = "")
{
var args = new PromptArguments(title, message, accept, cancel, placeholder, maxLength, keyboard, initialValue);
MessagingCenter.Send(this, PromptSignalName, args);
return args.Result.Task;
}
Expand Down
12 changes: 11 additions & 1 deletion Xamarin.Forms.Core/PromptArguments.cs
@@ -1,18 +1,26 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using System.Threading.Tasks;

namespace Xamarin.Forms.Internals
{
[EditorBrowsable(EditorBrowsableState.Never)]
public class PromptArguments
{
[Obsolete("PromptArguments overload is obsolete as of version 4.5.0 and is no longer supported.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public PromptArguments(string title, string message, string accept = "OK", string cancel = "Cancel", string placeholder = null, int maxLength = -1, Keyboard keyboard = default(Keyboard))
: this (title, message, accept, cancel, placeholder, maxLength, keyboard, "")
{ }

public PromptArguments(string title, string message, string accept = "OK", string cancel = "Cancel", string placeholder = null, int maxLength = -1, Keyboard keyboard = default(Keyboard), string initialValue = "")
{
Title = title;
Message = message;
Accept = accept;
Cancel = cancel;
Placeholder = placeholder;
InitialValue = initialValue;
MaxLength = maxLength;
Keyboard = keyboard ?? Keyboard.Default;
Result = new TaskCompletionSource<string>();
Expand All @@ -28,6 +36,8 @@ public PromptArguments(string title, string message, string accept = "OK", strin

public string Placeholder { get; }

public string InitialValue { get; }

public int MaxLength { get; }

public Keyboard Keyboard { get; }
Expand Down
2 changes: 1 addition & 1 deletion Xamarin.Forms.Platform.Android/PopupManager.cs
Expand Up @@ -141,7 +141,7 @@ void OnPromptRequested(Page sender, PromptArguments arguments)
alertDialog.SetMessage(arguments.Message);

var frameLayout = new FrameLayout(Activity);
var editText = new EditText(Activity) { Hint = arguments.Placeholder };
var editText = new EditText(Activity) { Hint = arguments.Placeholder, Text = arguments.InitialValue };
var layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent)
{
LeftMargin = (int)(22 * Activity.Resources.DisplayMetrics.Density),
Expand Down
1 change: 1 addition & 0 deletions Xamarin.Forms.Platform.iOS/Platform.cs
Expand Up @@ -366,6 +366,7 @@ void PresentPrompt(PromptArguments arguments)
alert.AddTextField(uiTextField =>
{
uiTextField.Placeholder = arguments.Placeholder;
uiTextField.Text = arguments.InitialValue;
uiTextField.ShouldChangeCharacters = (field, range, replacementString) => arguments.MaxLength <= -1 || field.Text.Length + replacementString.Length - range.Length <= arguments.MaxLength;
uiTextField.ApplyKeyboard(arguments.Keyboard);
});
Expand Down

0 comments on commit e33fb41

Please sign in to comment.