show errors if the user doesn't fill the fields or add tasks

This commit is contained in:
Negue 2017-04-23 18:04:35 +02:00
parent b7011d8f82
commit e083a52b45
3 changed files with 86 additions and 3 deletions

View file

@ -29,7 +29,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/brand_500"
app:hintTextAppearance="@style/TextAppearance.AppCompat">
app:hintTextAppearance="@style/TextAppearance.AppCompat"
android:id="@+id/create_challenge_title_input_layout">
<EditText
android:layout_width="match_parent"
@ -49,6 +50,7 @@
android:layout_height="wrap_content"
android:textColorHint="@color/brand_500"
android:id="@+id/create_challenge_description_input_layout"
app:hintTextAppearance="@style/TextAppearance.AppCompat">
<EditText
@ -172,15 +174,39 @@
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:id="@+id/create_challenge_tag"
android:hint="Identify your challenge with a tag .."
android:hint="@string/identify_your_challenge_with_a_tag"
android:textColorHint="#61000000" />
<TextView
style="@style/Caption3"
android:id="@+id/create_challenge_tag_error"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:textColor="#f74e52"
android:text="@string/challenge_create_error_tag"
android:visibility="gone"
tools:visibility="visible"/>
<TextView
style="@style/Subheader2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tasks" />
<TextView
style="@style/Caption3"
android:id="@+id/create_challenge_task_error"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:textColor="#f74e52"
android:text="@string/challenge_create_error_no_tasks"
android:visibility="gone"
tools:visibility="visible"/>
</LinearLayout>

View file

@ -578,4 +578,9 @@
<string name="edit_challenge">Edit Challenge</string>
<string name="challenge_create_error_tavern_one_gem">You need at least 1 gem to create a challenge in Tavern.</string>
<string name="challenge_create_error_enough_gems">You don\'t have enough gems to create a challenge.</string>
<string name="identify_your_challenge_with_a_tag">Identify your challenge with a tag ..</string>
<string name="challenge_create_error_tag">Please set a tag.</string>
<string name="challenge_create_error_no_tasks">Please add at least one task.</string>
<string name="challenge_create_error_title">Please set a title.</string>
<string name="challenge_create_error_description">Please set a description.</string>
</resources>

View file

@ -7,6 +7,7 @@ import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.AppCompatCheckedTextView;
import android.support.v7.widget.AppCompatTextView;
@ -54,9 +55,15 @@ import butterknife.OnClick;
public class CreateChallengeActivity extends BaseActivity {
public static final String CHALLENGE_ID_KEY = "challengeId";
@BindView(R.id.create_challenge_title_input_layout)
TextInputLayout create_challenge_title_input_layout;
@BindView(R.id.create_challenge_title)
EditText create_challenge_title;
@BindView(R.id.create_challenge_description_input_layout)
TextInputLayout create_challenge_description_input_layout;
@BindView(R.id.create_challenge_description)
EditText create_challenge_description;
@ -69,6 +76,12 @@ public class CreateChallengeActivity extends BaseActivity {
@BindView(R.id.create_challenge_gem_error)
TextView create_challenge_gem_error;
@BindView(R.id.create_challenge_tag_error)
TextView create_challenge_tag_error;
@BindView(R.id.create_challenge_task_error)
TextView create_challenge_task_error;
@BindView(R.id.challenge_location_spinner)
Spinner challenge_location_spinner;
@ -122,7 +135,7 @@ public class CreateChallengeActivity extends BaseActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_save) {
if (item.getItemId() == R.id.action_save && validateAllFields()) {
if (editMode) {
updateChallenge();
} else {
@ -133,6 +146,41 @@ public class CreateChallengeActivity extends BaseActivity {
return super.onOptionsItemSelected(item);
}
private boolean validateAllFields(){
int errors = 0;
if(getEditTextString(create_challenge_title).isEmpty()){
errors++;
create_challenge_title_input_layout.setError(getString(R.string.challenge_create_error_title));
} else {
create_challenge_title_input_layout.setErrorEnabled(false);
}
if(getEditTextString(create_challenge_description).isEmpty()){
errors++;
create_challenge_description_input_layout.setError(getString(R.string.challenge_create_error_description));
} else{
create_challenge_description_input_layout.setErrorEnabled(false);
}
if(getEditTextString(create_challenge_tag).isEmpty()){
errors++;
create_challenge_tag_error.setVisibility(View.VISIBLE);
} else{
create_challenge_tag_error.setVisibility(View.GONE);
}
// all "Add {*}"-Buttons are one task itself, so we need atleast more than 4
if(challengeTasks.getTaskList().size() <= 4){
errors++;
create_challenge_task_error.setVisibility(View.VISIBLE);
} else {
create_challenge_task_error.setVisibility(View.GONE);
}
return errors == 0;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -488,6 +536,10 @@ public class CreateChallengeActivity extends BaseActivity {
}
}
private String getEditTextString(EditText editText){
return editText.getText().toString();
}
private class GroupArrayAdapter extends ArrayAdapter<Group> {
public GroupArrayAdapter(@NonNull Context context) {
super(context, android.R.layout.simple_spinner_item);