From 9098f991d41d3783ab6722b769c5cd43d1383bb2 Mon Sep 17 00:00:00 2001 From: Benjamin Weiss Date: Mon, 6 Jul 2015 11:07:51 +0100 Subject: [PATCH] Improve listener situation. Replaces "implements .*Listener" with local variables where possible. --- .../apps/topeka/fragment/SignInFragment.java | 6 +-- .../apps/topeka/widget/quiz/AbsQuizView.java | 23 +++------ .../widget/quiz/AlphaPickerQuizView.java | 39 ++++++++------- .../widget/quiz/FourQuarterQuizView.java | 17 ++++--- .../widget/quiz/MultiSelectQuizView.java | 18 +++---- .../topeka/widget/quiz/PickerQuizView.java | 47 +++++++++---------- .../widget/quiz/SelectItemQuizView.java | 17 ++++--- .../topeka/widget/quiz/TextInputQuizView.java | 47 ++++++++----------- .../widget/quiz/ToggleTranslateQuizView.java | 25 +++++----- .../topeka/widget/quiz/TrueFalseQuizView.java | 34 +++++++------- 10 files changed, 125 insertions(+), 148 deletions(-) diff --git a/app/src/main/java/com/google/samples/apps/topeka/fragment/SignInFragment.java b/app/src/main/java/com/google/samples/apps/topeka/fragment/SignInFragment.java index ae87addb..7cf1c410 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/fragment/SignInFragment.java +++ b/app/src/main/java/com/google/samples/apps/topeka/fragment/SignInFragment.java @@ -133,10 +133,10 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // showing the floating action button if text is entered - if (count > 0) { - mDoneFab.setVisibility(View.VISIBLE); - } else { + if (s.length() == 0) { mDoneFab.setVisibility(View.GONE); + } else { + mDoneFab.setVisibility(View.VISIBLE); } } diff --git a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/AbsQuizView.java b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/AbsQuizView.java index 23d6a142..216c72c8 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/AbsQuizView.java +++ b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/AbsQuizView.java @@ -54,8 +54,7 @@ * @param The type of {@link com.google.samples.apps.topeka.model.quiz.Quiz} you want to * display. */ -public abstract class AbsQuizView extends FrameLayout implements - View.OnClickListener { +public abstract class AbsQuizView extends FrameLayout { /** Property for animating the foreground color */ public static final Property FOREGROUND_COLOR = @@ -186,10 +185,12 @@ private FloatingActionButton getSubmitButton(Context context) { mSubmitAnswer.setVisibility(GONE); mSubmitAnswer.setScaleY(0); mSubmitAnswer.setScaleX(0); - //Set QuizActivity to handle clicks on answer submission. - if (context instanceof QuizActivity) { - mSubmitAnswer.setOnClickListener(this); - } + mSubmitAnswer.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + submitAnswer(v); + } + }); } return mSubmitAnswer; } @@ -267,16 +268,6 @@ protected void allowAnswer() { } } - @Override - public void onClick(View v) { - switch (v.getId()) { - case R.id.submitAnswer: { - submitAnswer(v); - break; - } - } - } - /** * Allows children to submit an answer via code. */ diff --git a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/AlphaPickerQuizView.java b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/AlphaPickerQuizView.java index 87c5ac30..ec8caaa9 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/AlphaPickerQuizView.java +++ b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/AlphaPickerQuizView.java @@ -31,15 +31,13 @@ import java.util.List; @SuppressLint("ViewConstructor") -public class AlphaPickerQuizView extends AbsQuizView implements - SeekBar.OnSeekBarChangeListener { +public class AlphaPickerQuizView extends AbsQuizView { private static final String KEY_SELECTION = "SELECTION"; private TextView mCurrentSelection; - - private List mAlphabet; private SeekBar mSeekBar; + private List mAlphabet; public AlphaPickerQuizView(Context context, Category category, AlphaPickerQuiz quiz) { super(context, category, quiz); @@ -53,7 +51,23 @@ protected View createQuizContentView() { mCurrentSelection.setText(getAlphabet().get(0)); mSeekBar = (SeekBar) layout.findViewById(R.id.seekbar); mSeekBar.setMax(getAlphabet().size() - 1); - mSeekBar.setOnSeekBarChangeListener(this); + mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + mCurrentSelection.setText(getAlphabet().get(progress)); + allowAnswer(); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + /* no-op */ + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + /* no-op */ + } + }); return layout; } @@ -78,21 +92,6 @@ public void setUserInput(Bundle savedInput) { mSeekBar.setProgress(getAlphabet().indexOf(userInput)); } - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - mCurrentSelection.setText(getAlphabet().get(progress)); - allowAnswer(); - } - - @Override - public void onStartTrackingTouch(SeekBar seekBar) { - /* no-op */ - } - - @Override - public void onStopTrackingTouch(SeekBar seekBar) { - /* no-op */ - } private List getAlphabet() { if (null == mAlphabet) { diff --git a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/FourQuarterQuizView.java b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/FourQuarterQuizView.java index e0ffa9a4..6e346b35 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/FourQuarterQuizView.java +++ b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/FourQuarterQuizView.java @@ -28,8 +28,7 @@ import com.google.samples.apps.topeka.model.quiz.FourQuarterQuiz; @SuppressLint("ViewConstructor") -public class FourQuarterQuizView extends AbsQuizView - implements AdapterView.OnItemClickListener { +public class FourQuarterQuizView extends AbsQuizView { private static final String KEY_ANSWER = "ANSWER"; private int mAnswered = -1; @@ -46,7 +45,13 @@ protected View createQuizContentView() { mAnswerView.setNumColumns(2); mAnswerView.setAdapter(new OptionsQuizAdapter(getQuiz().getOptions(), R.layout.item_answer)); - mAnswerView.setOnItemClickListener(this); + mAnswerView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + allowAnswer(); + mAnswered = position; + } + }); return mAnswerView; } @@ -86,12 +91,6 @@ private void setUpUserInput() { mAnswerView.setSelection(mAnswered); } - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - allowAnswer(); - mAnswered = position; - } - @Override protected boolean isAnswerCorrect() { return getQuiz().isAnswerCorrect(new int[]{mAnswered}); diff --git a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/MultiSelectQuizView.java b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/MultiSelectQuizView.java index e88d1a62..c2e954da 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/MultiSelectQuizView.java +++ b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/MultiSelectQuizView.java @@ -18,7 +18,6 @@ import android.annotation.SuppressLint; import android.content.Context; import android.os.Bundle; -import android.util.Log; import android.util.SparseBooleanArray; import android.view.View; import android.widget.AbsListView; @@ -31,11 +30,9 @@ import com.google.samples.apps.topeka.model.quiz.MultiSelectQuiz; @SuppressLint("ViewConstructor") -public class MultiSelectQuizView extends AbsQuizView - implements AdapterView.OnItemClickListener { +public class MultiSelectQuizView extends AbsQuizView { private static final String KEY_ANSWER = "ANSWER"; - private static final String TAG = "MultiSelectQuizView"; private ListView mListView; @@ -51,7 +48,12 @@ protected View createQuizContentView() { android.R.layout.simple_list_item_multiple_choice)); mListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); mListView.setItemsCanFocus(false); - mListView.setOnItemClickListener(this); + mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + allowAnswer(); + } + }); return mListView; } @@ -99,10 +101,4 @@ private boolean[] getBundleableAnswer() { } return bundleableAnswer; } - - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - Log.d(TAG, "clicked pos: " + position); - allowAnswer(); - } } diff --git a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/PickerQuizView.java b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/PickerQuizView.java index 30237ae6..d731675d 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/PickerQuizView.java +++ b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/PickerQuizView.java @@ -28,8 +28,7 @@ import com.google.samples.apps.topeka.model.quiz.PickerQuiz; @SuppressLint("ViewConstructor") -public final class PickerQuizView extends AbsQuizView - implements SeekBar.OnSeekBarChangeListener { +public final class PickerQuizView extends AbsQuizView { private static final String KEY_ANSWER = "ANSWER"; @@ -53,10 +52,31 @@ protected View createQuizContentView() { mCurrentSelection.setText(String.valueOf(mMin)); mSeekBar = (SeekBar) layout.findViewById(R.id.seekbar); mSeekBar.setMax(getSeekBarMax()); - mSeekBar.setOnSeekBarChangeListener(this); + mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + setCurrentSelectionText(mMin + progress); + allowAnswer(); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + /* no-op */ + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + /* no-op */ + } + }); return layout; } + private void setCurrentSelectionText(int progress) { + mProgress = progress / mStep * mStep; + mCurrentSelection.setText(String.valueOf(mProgress)); + } + @Override protected boolean isAnswerCorrect() { return getQuiz().isAnswerCorrect(mProgress); @@ -97,25 +117,4 @@ private int getSeekBarMax() { final int realMax = Math.max(absMin, absMax); return realMax - realMin; } - - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - setCurrentSelectionText(mMin + progress); - allowAnswer(); - } - - private void setCurrentSelectionText(int progress) { - mProgress = progress / mStep * mStep; - mCurrentSelection.setText(String.valueOf(mProgress)); - } - - @Override - public void onStartTrackingTouch(SeekBar seekBar) { - /* no-op */ - } - - @Override - public void onStopTrackingTouch(SeekBar seekBar) { - /* no-op */ - } } diff --git a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/SelectItemQuizView.java b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/SelectItemQuizView.java index 1c33b1c2..aa17aae0 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/SelectItemQuizView.java +++ b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/SelectItemQuizView.java @@ -32,8 +32,7 @@ import com.google.samples.apps.topeka.model.quiz.SelectItemQuiz; @SuppressLint("ViewConstructor") -public class SelectItemQuizView extends AbsQuizView - implements AdapterView.OnItemClickListener { +public class SelectItemQuizView extends AbsQuizView { private static final String KEY_ANSWERS = "ANSWERS"; @@ -54,7 +53,13 @@ protected View createQuizContentView() { new OptionsQuizAdapter(getQuiz().getOptions(), R.layout.item_answer_start, getContext(), true)); mListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); - mListView.setOnItemClickListener(this); + mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + allowAnswer(); + toggleAnswerFor(position); + } + }); return mListView; } @@ -87,12 +92,6 @@ public void setUserInput(Bundle savedInput) { } } - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - allowAnswer(); - toggleAnswerFor(position); - } - private void toggleAnswerFor(int answerId) { getAnswers()[answerId] = !mAnswers[answerId]; } diff --git a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/TextInputQuizView.java b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/TextInputQuizView.java index 876a698e..3175b86c 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/TextInputQuizView.java +++ b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/TextInputQuizView.java @@ -30,8 +30,8 @@ import com.google.samples.apps.topeka.model.Category; import com.google.samples.apps.topeka.model.quiz.Quiz; -public abstract class TextInputQuizView extends AbsQuizView implements - TextWatcher, TextView.OnEditorActionListener { +public abstract class TextInputQuizView extends AbsQuizView + implements TextWatcher, TextView.OnEditorActionListener { public TextInputQuizView(Context context, Category category, Q quiz) { super(context, category, quiz); @@ -46,28 +46,9 @@ protected final EditText createEditText() { } @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - allowAnswer(after > 0); - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - /* no-op */ - } - - @Override - public void afterTextChanged(Editable s) { - /* no-op */ - } - - @Override - public void onClick(View v) { - switch (v.getId()) { - case R.id.submitAnswer: - hideKeyboard(v); - break; - } - super.onClick(v); + protected void submitAnswer() { + hideKeyboard(this); + super.submitAnswer(); } /** @@ -87,9 +68,6 @@ private InputMethodManager getInputMethodManager() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { - /* submit the answer and hide the keyboard once the action done - * has been tapped if text has been entered. - */ if (TextUtils.isEmpty(v.getText())) { return false; } @@ -101,4 +79,19 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { } return false; } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + /* no-op */ + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + /* no-op */ + } + + @Override + public void afterTextChanged(Editable s) { + allowAnswer(!TextUtils.isEmpty(s)); + } } diff --git a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/ToggleTranslateQuizView.java b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/ToggleTranslateQuizView.java index 741cd444..ae5d9440 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/ToggleTranslateQuizView.java +++ b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/ToggleTranslateQuizView.java @@ -33,8 +33,7 @@ import com.google.samples.apps.topeka.model.quiz.ToggleTranslateQuiz; @SuppressLint("ViewConstructor") -public class ToggleTranslateQuizView extends AbsQuizView - implements AdapterView.OnItemClickListener { +public class ToggleTranslateQuizView extends AbsQuizView { private static final String KEY_ANSWERS = "ANSWERS"; @@ -54,7 +53,17 @@ protected View createQuizContentView() { mListView.setAdapter(new OptionsQuizAdapter(getQuiz().getReadableOptions(), R.layout.item_answer)); mListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); - mListView.setOnItemClickListener(this); + mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + toggleAnswerFor(position); + if (view instanceof CompoundButton) { + ((CompoundButton) view).setChecked(mAnswers[position]); + } + + allowAnswer(); + } + }); return mListView; } @@ -87,16 +96,6 @@ public void setUserInput(Bundle savedInput) { } } - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - toggleAnswerFor(position); - if (view instanceof CompoundButton) { - ((CompoundButton) view).toggle(); - } - - allowAnswer(); - } - private void toggleAnswerFor(int answerId) { mAnswers[answerId] = !mAnswers[answerId]; } diff --git a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/TrueFalseQuizView.java b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/TrueFalseQuizView.java index 517d2cb1..a6b67920 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/TrueFalseQuizView.java +++ b/app/src/main/java/com/google/samples/apps/topeka/widget/quiz/TrueFalseQuizView.java @@ -50,10 +50,26 @@ public TrueFalseQuizView(Context context, Category category, TrueFalseQuiz quiz) protected View createQuizContentView() { final ViewGroup container = (ViewGroup) getLayoutInflater().inflate( R.layout.quiz_radio_group_true_false, this, false); + + OnClickListener clickListener = new OnClickListener() { + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.answerTrue: + mAnswer = true; + break; + case R.id.answerFalse: + mAnswer = false; + break; + } + allowAnswer(); + } + }; + mAnswerTrue = container.findViewById(R.id.answerTrue); - mAnswerTrue.setOnClickListener(this); + mAnswerTrue.setOnClickListener(clickListener); mAnswerFalse = container.findViewById(R.id.answerFalse); - mAnswerFalse.setOnClickListener(this); + mAnswerFalse.setOnClickListener(clickListener); return container; } @@ -82,18 +98,4 @@ private void performSelection(View selection) { selection.performClick(); selection.setSelected(true); } - - @Override - public void onClick(View v) { - super.onClick(v); - switch (v.getId()) { - case R.id.answerTrue: - mAnswer = true; - break; - case R.id.answerFalse: - mAnswer = false; - break; - } - allowAnswer(); - } }