package org.wikipedia.readinglist;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.apache.commons.lang3.StringUtils;
import org.wikipedia.R;
import org.wikipedia.views.TextInputDialog;

import java.util.List;

public final class ReadingListTitleDialog {

    public interface Callback {
        void onSuccess(@NonNull CharSequence text);
    }

    public static TextInputDialog readingListTitleDialog(@NonNull Context context,
                                                         @NonNull final String initialTitle,
                                                         @NonNull final List<String> otherTitles,
                                                         @Nullable final Callback callback) {
        return TextInputDialog.newInstance(context,
                new TextInputDialog.Callback() {
                    @Override
                    public void onShow(@NonNull TextInputDialog dialog) {
                        dialog.setHint(R.string.reading_list_name_hint);
                        dialog.setText(initialTitle);
                        dialog.selectAll();
                    }

                    @Override
                    public void onTextChanged(@NonNull CharSequence text, @NonNull TextInputDialog dialog) {
                        if (StringUtils.isEmpty(text)) {
                            dialog.setError(dialog.getContext().getString(R.string.reading_list_entry_empty));
                            dialog.setPositiveButtonEnabled(false);
                        } else if (otherTitles.contains(text.toString())) {
                            dialog.setError(dialog.getContext().getString(R.string.reading_list_title_exists, text.toString()));
                            dialog.setPositiveButtonEnabled(false);
                        } else {
                            dialog.setError(null);
                            dialog.setPositiveButtonEnabled(true);
                        }
                    }

                    @Override
                    public void onSuccess(@NonNull CharSequence text) {
                        if (callback != null) {
                            callback.onSuccess(text);
                        }
                    }

                    @Override
                    public void onCancel() {
                    }
                });
    }

    private ReadingListTitleDialog() {
    }
}

@@ -1321,32 +1321,95 @@
 ialog dialog) %7B%0A
+                        String title = text.toString().trim();%0A
                 
@@ -1441,19 +1441,20 @@
 sEmpty(t
-ext
+itle
 )) %7B%0A   
@@ -1686,30 +1686,20 @@
 ntains(t
-ext.toString()
+itle
 )) %7B%0A   
@@ -1806,30 +1806,20 @@
 xists, t
-ext.toString()
+itle
 ));%0A    
@@ -2228,24 +2228,24 @@
  != null) %7B%0A
-
             
@@ -2279,24 +2279,42 @@
 Success(text
+.toString().trim()
 );%0A         

Trim whitespace from reading list title names.