Пример #1
0
        public override async Task <QuestionResponse <TResponse> > AskQuestionAsync <TResponse>(
            IQuestion <TResponse> question)
        {
            AssertArg.IsNotNull(question, nameof(question));

            if (question is TextQuestion textQuestion)
            {
                var response = await ShowTextDialogAsync(textQuestion);

                if (response.Item1)
                {
                    var r = new TextResponse(OkCancelQuestionResult.OK, response.Item2);
                    return(new QuestionResponse <TResponse>((TResponse)(object)r, question));
                }
                var noResponse = new TextResponse(OkCancelQuestionResult.Cancel, null);
                return(new QuestionResponse <TResponse>((TResponse)(object)noResponse, question));
            }

            throw new NotSupportedException();
        }
Пример #2
0
        public override Task <QuestionResponse <TResponse> > AskQuestionAsync <TResponse>(
            IQuestion <TResponse> question)
        {
            Context context = ResolveContext();

            AlertDialog.Builder builder = new AlertDialog.Builder(context);

            var trq = question as TextQuestion;

            if (trq == null)
            {
                throw new NotSupportedException(
                          "Only TextQuestion is supported at this time.");
            }

            var caption = trq.Caption;

            if (!string.IsNullOrWhiteSpace(caption))
            {
                builder.SetTitle(caption.Parse());
            }

            var message = trq.Question;

            if (!string.IsNullOrWhiteSpace(message))
            {
                builder.SetMessage(message.Parse());
            }

            EditText editText = new EditText(context);

            if (trq.InputScope != InputScopeNameValue.Default)
            {
                var converter     = Dependency.Resolve <IAndroidInputScopeConverter>();
                var platformValue = converter.ToNativeType(trq.InputScope);
                editText.InputType = platformValue;
            }

            if (!trq.MultiLine)
            {
                editText.SetSingleLine(true);
                editText.SetMaxLines(1);
            }

            if (!trq.SpellCheckEnabled)
            {
                editText.InputType = editText.InputType | InputTypes.TextFlagNoSuggestions;
            }

            if (trq.InputScope == InputScopeNameValue.Password ||
                trq.InputScope == InputScopeNameValue.NumericPassword)
            {
                editText.TransformationMethod = new PasswordTransformationMethod();
            }

            //var color = context.Resources.GetColor(Resources.Color.dialog_textcolor);
            //textBox.SetTextColor(Color.Black);
            editText.Text = trq.DefaultResponse;
            builder.SetView(editText);

            var manager = (InputMethodManager)context.GetSystemService(Context.InputMethodService);

            var source = new TaskCompletionSource <QuestionResponse <TResponse> >();

            builder.SetPositiveButton(Strings.Okay(),
                                      (s, e) =>
            {
                Interlocked.Decrement(ref openDialogCount);

                var textReponse = new TextResponse(OkCancelQuestionResult.OK, editText.Text);
                var result      = new QuestionResponse <TResponse>((TResponse)(object)textReponse, question);

                manager.HideSoftInputFromWindow(editText.WindowToken, HideSoftInputFlags.None);

                source.TrySetResult(result);
            });

            builder.SetNegativeButton(Strings.Cancel(), (s, e) =>
            {
                Interlocked.Decrement(ref openDialogCount);

                var textReponse = new TextResponse {
                    OkCancelQuestionResult = OkCancelQuestionResult.Cancel
                };
                var result = new QuestionResponse <TResponse>((TResponse)(object)textReponse, question);

                manager.HideSoftInputFromWindow(editText.WindowToken, HideSoftInputFlags.None);

                source.TrySetResult(result);
            });

            Interlocked.Increment(ref openDialogCount);

            builder.Show();

            /* Focussing the EditText and showing the keyboard,
             * must be done after the alert is show, else it has no effect. */
            var looper  = context.MainLooper;
            var handler = new Handler(looper);

            handler.Post(() =>
            {
                editText.RequestFocus();

                manager.ShowSoftInput(editText, ShowFlags.Forced);
            });

            return(source.Task);
        }
Пример #3
0
        public override Task <QuestionResponse <TResponse> > AskQuestionAsync <TResponse>(
            IQuestion <TResponse> question)
        {
            Context context = ResolveContext();

            AlertDialog.Builder builder = new AlertDialog.Builder(context);

            var trq = question as TextQuestion;

            if (trq == null)
            {
                throw new NotSupportedException(
                          "Only TextQuestion is supported at this time.");
            }

            var caption = trq.Caption;

            if (!string.IsNullOrWhiteSpace(caption))
            {
                builder.SetTitle(caption.Parse());
            }

            var message = trq.Question;

            if (!string.IsNullOrWhiteSpace(message))
            {
                builder.SetMessage(message.Parse());
            }

            //var color = context.Resources.GetColor(Resources.Color.dialog_textcolor);
            EditText editText = new EditText(context);

            //textBox.SetTextColor(Color.Black);
            editText.Text = trq.DefaultResponse;
            builder.SetView(editText);

            var manager = (InputMethodManager)context.GetSystemService(Context.InputMethodService);

            var source = new TaskCompletionSource <QuestionResponse <TResponse> >();

            builder.SetPositiveButton(Strings.Okay(),
                                      (s, e) =>
            {
                Interlocked.Decrement(ref openDialogCount);

                var textReponse = new TextResponse(OkCancelQuestionResult.OK, editText.Text);
                var result      = new QuestionResponse <TResponse>((TResponse)(object)textReponse, question);

                manager.HideSoftInputFromWindow(editText.WindowToken, HideSoftInputFlags.None);

                source.SetResult(result);
            });

            builder.SetNegativeButton(Strings.Cancel(), (s, e) =>
            {
                Interlocked.Decrement(ref openDialogCount);

                var textReponse = new TextResponse {
                    OkCancelQuestionResult = OkCancelQuestionResult.Cancel
                };
                var result = new QuestionResponse <TResponse>((TResponse)(object)textReponse, question);

                manager.HideSoftInputFromWindow(editText.WindowToken, HideSoftInputFlags.None);

                source.SetResult(result);
            });

            Interlocked.Increment(ref openDialogCount);

            builder.Show();

            /* Focussing the EditText and showing the keyboard,
             * must be done after the alert is show, else it has no effect. */
            var looper  = context.MainLooper;
            var handler = new Handler(looper);

            handler.Post(() =>
            {
                editText.RequestFocus();

                manager.ShowSoftInput(editText, ShowFlags.Forced);
            });

            return(source.Task);
        }