Bug 1409803 - Copy logic to prevent spaces in keywords to new Edit Bookmark dialogue. r=jwu

MozReview-Commit-ID: JXF0zwxhVv4

--HG--
extra : rebase_source : 336e5a31e691ab175f75370727b0f1f856bc9088
This commit is contained in:
Jan Henning 2017-10-18 22:34:14 +02:00
Родитель 1bd153659e
Коммит 82d83e2c1d
1 изменённых файлов: 65 добавлений и 13 удалений

Просмотреть файл

@ -131,7 +131,7 @@ public class BookmarkEditFragment extends DialogFragment implements SelectFolder
case R.id.done:
final String newUrl = locationText.getText().toString().trim();
final String newTitle = nameText.getText().toString();
final String newKeyword = keywordText.getText().toString();
final String newKeyword = keywordText.getText().toString().trim();
if (callbacks != null) {
if (TextUtils.equals(newTitle, bookmark.originalTitle) &&
TextUtils.equals(newUrl, bookmark.originalUrl) &&
@ -260,14 +260,20 @@ public class BookmarkEditFragment extends DialogFragment implements SelectFolder
final MenuItem doneItem = toolbar.getMenu().findItem(R.id.done);
doneItem.setEnabled(true);
// Add a TextWatcher to prevent invalid input(e.g. empty string).
// Add a TextWatcher to prevent invalid input (e.g. an empty string).
LocationTextWatcher locationTextWatcher = new LocationTextWatcher(doneItem);
KeywordTextWatcher keywordTextWatcher = new KeywordTextWatcher(doneItem);
// Cross reference the TextWatchers
locationTextWatcher.setPairedTextWatcher(keywordTextWatcher);
keywordTextWatcher.setPairedTextWatcher(locationTextWatcher);
if (bookmark.type == Bookmarks.TYPE_FOLDER) {
BookmarkTextWatcher nameTextWatcher = new BookmarkTextWatcher(doneItem);
nameText.addTextChangedListener(nameTextWatcher);
nameText.addTextChangedListener(locationTextWatcher);
} else {
BookmarkTextWatcher locationTextWatcher = new BookmarkTextWatcher(doneItem);
locationText.addTextChangedListener(locationTextWatcher);
}
keywordText.addTextChangedListener(keywordTextWatcher);
}
/**
@ -509,27 +515,39 @@ public class BookmarkEditFragment extends DialogFragment implements SelectFolder
}
/**
* This text watcher enables the menu item if the dialog contains valid information, or disables otherwise.
* This text watcher watches to enable or disable the Save button if the dialog contains
* valid information. This class is overridden to do data checking on different fields.
* By itself, it always enables the button.
*
* Callers can also assign a paired partner to the TextWatcher, and callers will check
* that both are enabled before enabling the Save button.
*/
private static final class BookmarkTextWatcher implements TextWatcher {
// A stored reference to the dialog containing the text field being watched.
private static class EditBookmarkTextWatcher implements TextWatcher {
// A stored reference to the menu that should be disabled/enabled in response to the text
// being watched.
private final WeakReference<MenuItem> doneItemWeakReference;
// Whether or not the menu item should be enabled.
private boolean enabled = true;
private EditBookmarkTextWatcher pairedTextWatcher;
private BookmarkTextWatcher(MenuItem doneItem) {
// Whether or not the menu item should be enabled.
protected boolean enabled = true;
private EditBookmarkTextWatcher(MenuItem doneItem) {
doneItemWeakReference = new WeakReference<>(doneItem);
}
public void setPairedTextWatcher(EditBookmarkTextWatcher textWatcher) {
pairedTextWatcher = textWatcher;
}
public boolean isEnabled() {
return enabled;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Disables the menu item if the input field is empty.
final boolean enabled = (s.toString().trim().length() > 0);
// Disable if we're disabled or the paired partner is disabled.
boolean enabled = this.enabled && (pairedTextWatcher == null || pairedTextWatcher.isEnabled());
final MenuItem doneItem = doneItemWeakReference.get();
if (doneItem != null) {
@ -542,4 +560,38 @@ public class BookmarkEditFragment extends DialogFragment implements SelectFolder
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
}
/**
* A version of the EditBookmarkTextWatcher for the URL field of the dialog.
* Only checks if the field is empty or not.
*/
private static final class LocationTextWatcher extends EditBookmarkTextWatcher {
private LocationTextWatcher(MenuItem doneItem) {
super(doneItem);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Disables the menu item if the input field is empty.
enabled = (s.toString().trim().length() > 0);
super.onTextChanged(s, start, before, count);
}
}
/**
* A version of the EditBookmarkTextWatcher for the keyword field of the dialog.
* Checks if the field has any (non leading or trailing) spaces.
*/
private static final class KeywordTextWatcher extends EditBookmarkTextWatcher {
private KeywordTextWatcher(MenuItem doneItem) {
super(doneItem);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Disable if the keyword contains spaces.
enabled = (s.toString().trim().indexOf(' ') == -1);
super.onTextChanged(s, start, before, count);
}
}
}