Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/payments/ui/EditorFieldModel.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/payments/ui/EditorFieldModel.java b/chrome/android/java/src/org/chromium/chrome/browser/payments/ui/EditorFieldModel.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a9373c0cc6c0bf3e44ca03539a99f8d25f468681 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/payments/ui/EditorFieldModel.java |
| @@ -0,0 +1,144 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.payments.ui; |
| + |
| +import android.text.TextUtils; |
| + |
| +import java.util.List; |
| + |
| +/** |
| + * Representation of a single input text field in an editor. Can be used, for example, for a phone |
| + * input field. |
| + */ |
| +public class EditorFieldModel { |
| + /** |
| + * The interface to be implemented by the field validator. |
| + */ |
| + public interface EditorFieldValidator { |
| + /** |
| + * Called to check the validity of the field value. |
| + * |
| + * @param value The value of the field to check. |
| + * @return True if the value is valid. |
| + */ |
| + boolean isValid(CharSequence value); |
| + } |
| + |
| + /** |
| + * Indicates a phone field. |
|
gone
2016/06/21 06:04:38
Save some space:
/** Indicates a phone field. */
please use gerrit instead
2016/06/22 02:12:30
Done.
|
| + */ |
| + public static final int INPUT_TYPE_HINT_PHONE = 1; |
| + |
| + /** |
| + * Indicates an email field. |
| + */ |
| + public static final int INPUT_TYPE_HINT_EMAIL = 2; |
| + |
| + private final int mInputTypeHint; |
| + private final CharSequence mLabel; |
| + private final List<CharSequence> mSuggestions; |
| + private final EditorFieldValidator mValidator; |
| + private final CharSequence mRequiredErrorMessage; |
| + private final CharSequence mInvalidErrorMessage; |
| + private CharSequence mErrorMessage; |
| + private CharSequence mValue; |
| + |
| + /** |
| + * Constructs a field model. |
| + * |
| + * @param inputTypeHint The type of input. For example, INPUT_TYPE_HINT_PHONE. |
| + * @param label The human-readable label for user to understand the type of data |
| + * that should be entered into this field. |
| + * @param suggestions Optional list of value to suggest to the user. |
|
gone
2016/06/21 06:04:38
values
please use gerrit instead
2016/06/22 02:12:30
Done.
|
| + * @param validator Optional validator for the values in this field. |
| + * @param requiredErrorMessage The optional error message that indicates to the user that they |
| + * cannot leave this field empty. |
| + * @param invalidErrorMessage The optional error message that indicates to the user that the |
| + * value they have entered is not valid. |
| + * @param value Optional initial value of this field. |
| + */ |
| + public EditorFieldModel(int inputTypeHint, CharSequence label, List<CharSequence> suggestions, |
| + EditorFieldValidator validator, CharSequence requiredErrorMessage, |
|
gone
2016/06/21 06:04:39
If requiredErrorMessage and invalidErrorMessage ar
please use gerrit instead
2016/06/22 02:12:30
Done.
|
| + CharSequence invalidErrorMessage, CharSequence value) { |
| + mInputTypeHint = inputTypeHint; |
| + mLabel = label; |
| + mSuggestions = suggestions; |
| + mValidator = validator; |
| + mRequiredErrorMessage = requiredErrorMessage; |
| + mInvalidErrorMessage = invalidErrorMessage; |
| + mValue = value; |
| + } |
| + |
| + /** |
| + * Returns the type of input, for example, INPUT_TYPE_HINT_PHONE. |
| + * |
| + * @return The type of input. |
| + */ |
| + public int getInputTypeHint() { |
| + return mInputTypeHint; |
| + } |
| + |
| + /** |
| + * Returns the human-readable label for this field. |
| + * |
| + * @return The human-readable label for this field. |
| + */ |
| + public CharSequence getLabel() { |
| + return mLabel; |
| + } |
| + |
| + /** |
| + * Returns the full list of suggested values for this field. |
| + * |
| + * @return Suggested values for this field. Can be null. |
| + */ |
| + public List<CharSequence> getSuggestions() { |
| + return mSuggestions; |
| + } |
| + |
| + /** |
| + * Returns true if the field value is valid. Also updates the error message. |
| + * |
| + * @return Whether the field value is valid. |
| + */ |
| + public boolean isValid() { |
| + mErrorMessage = TextUtils.isEmpty(mValue) || TextUtils.getTrimmedLength(mValue) == 0 |
| + ? mRequiredErrorMessage |
| + : null; |
| + if (mErrorMessage != null) return false; |
| + |
| + mErrorMessage = |
| + mValidator != null && !mValidator.isValid(mValue) ? mInvalidErrorMessage : null; |
| + return mErrorMessage == null; |
|
gone
2016/06/21 06:04:38
Little obtuse. Gain some readability by making it
please use gerrit instead
2016/06/22 02:12:30
Done.
|
| + } |
| + |
| + /** |
| + * Returns the error message determine during the last validation. |
| + * |
| + * @return The error message for the last validation. Can be null. |
|
gone
2016/06/21 06:04:39
Can be null if no error was reported.
please use gerrit instead
2016/06/22 02:12:30
Done.
|
| + */ |
| + public CharSequence getErrorMessage() { |
| + return mErrorMessage; |
| + } |
| + |
| + /** |
| + * Returns the value that the user has entered into the field. |
| + * |
| + * @return The value of this field. Can be null. |
| + */ |
| + public CharSequence getValue() { |
| + return mValue; |
| + } |
| + |
| + /** |
| + * Updates the value of this field. Does not trigger validation or update the last error |
| + * message. |
| + * |
| + * @param value The new value that the user has entered. |
| + */ |
| + public void setValue(CharSequence value) { |
| + mValue = value; |
| + } |
| +} |