package de.westnordost.streetcomplete.quests.place_name;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import java.util.List;
import de.westnordost.streetcomplete.R;
import de.westnordost.streetcomplete.quests.AbstractQuestFormAnswerFragment;
import de.westnordost.streetcomplete.view.dialogs.AlertDialogBuilder;
public class AddPlaceNameForm extends AbstractQuestFormAnswerFragment
{
public static final String NO_NAME = "no_name";
public static final String NAME = "name";
private EditText nameInput;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = super.onCreateView(inflater, container, savedInstanceState);
setTitle(R.string.quest_placeName_title);
View contentView = setContentView(R.layout.quest_placename);
nameInput = (EditText) contentView.findViewById(R.id.nameInput);
return view;
}
@Override protected void onClickOk()
{
Bundle data = new Bundle();
String name = nameInput.getText().toString().trim();
data.putString(NAME, name);
applyFormAnswer(data);
}
@Override protected List<Integer> getOtherAnswerResourceIds()
{
List<Integer> answers = super.getOtherAnswerResourceIds();
answers.add(R.string.quest_name_answer_noName);
return answers;
}
@Override protected boolean onClickOtherAnswer(int itemResourceId)
{
if(super.onClickOtherAnswer(itemResourceId)) return true;
if(itemResourceId == R.string.quest_name_answer_noName)
{
confirmNoName();
return true;
}
return false;
}
private void confirmNoName()
{
DialogInterface.OnClickListener onYes = new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Bundle data = new Bundle();
data.putBoolean(NO_NAME, true);
applyImmediateAnswer(data);
}
};
DialogInterface.OnClickListener onNo = new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// nothing, just go back
}
};
new AlertDialogBuilder(getActivity())
.setTitle(R.string.quest_name_answer_noName_confirmation_title)
.setPositiveButton(R.string.quest_name_noName_confirmation_positive, onYes)
.setNegativeButton(R.string.quest_generic_confirmation_no, onNo)
.show();
}
@Override public boolean hasChanges()
{
return !nameInput.getText().toString().trim().isEmpty();
}
}