Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Improve listener situation.
Browse files Browse the repository at this point in the history
Replaces "implements .*Listener" with
local variables where possible.
  • Loading branch information
keyboardsurfer committed Jul 6, 2015
1 parent ce8f827 commit 9098f99
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 148 deletions.
Expand Up @@ -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);
}
}

Expand Down
Expand Up @@ -54,8 +54,7 @@
* @param <Q> The type of {@link com.google.samples.apps.topeka.model.quiz.Quiz} you want to
* display.
*/
public abstract class AbsQuizView<Q extends Quiz> extends FrameLayout implements
View.OnClickListener {
public abstract class AbsQuizView<Q extends Quiz> extends FrameLayout {

/** Property for animating the foreground color */
public static final Property<FrameLayout, Integer> FOREGROUND_COLOR =
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
*/
Expand Down
Expand Up @@ -31,15 +31,13 @@
import java.util.List;

@SuppressLint("ViewConstructor")
public class AlphaPickerQuizView extends AbsQuizView<AlphaPickerQuiz> implements
SeekBar.OnSeekBarChangeListener {
public class AlphaPickerQuizView extends AbsQuizView<AlphaPickerQuiz> {

private static final String KEY_SELECTION = "SELECTION";

private TextView mCurrentSelection;

private List<String> mAlphabet;
private SeekBar mSeekBar;
private List<String> mAlphabet;

public AlphaPickerQuizView(Context context, Category category, AlphaPickerQuiz quiz) {
super(context, category, quiz);
Expand All @@ -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;
}

Expand All @@ -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<String> getAlphabet() {
if (null == mAlphabet) {
Expand Down
Expand Up @@ -28,8 +28,7 @@
import com.google.samples.apps.topeka.model.quiz.FourQuarterQuiz;

@SuppressLint("ViewConstructor")
public class FourQuarterQuizView extends AbsQuizView<FourQuarterQuiz>
implements AdapterView.OnItemClickListener {
public class FourQuarterQuizView extends AbsQuizView<FourQuarterQuiz> {

private static final String KEY_ANSWER = "ANSWER";
private int mAnswered = -1;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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});
Expand Down
Expand Up @@ -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;
Expand All @@ -31,11 +30,9 @@
import com.google.samples.apps.topeka.model.quiz.MultiSelectQuiz;

@SuppressLint("ViewConstructor")
public class MultiSelectQuizView extends AbsQuizView<MultiSelectQuiz>
implements AdapterView.OnItemClickListener {
public class MultiSelectQuizView extends AbsQuizView<MultiSelectQuiz> {

private static final String KEY_ANSWER = "ANSWER";
private static final String TAG = "MultiSelectQuizView";

private ListView mListView;

Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
}
}
Expand Up @@ -28,8 +28,7 @@
import com.google.samples.apps.topeka.model.quiz.PickerQuiz;

@SuppressLint("ViewConstructor")
public final class PickerQuizView extends AbsQuizView<PickerQuiz>
implements SeekBar.OnSeekBarChangeListener {
public final class PickerQuizView extends AbsQuizView<PickerQuiz> {

private static final String KEY_ANSWER = "ANSWER";

Expand All @@ -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);
Expand Down Expand Up @@ -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 */
}
}
Expand Up @@ -32,8 +32,7 @@
import com.google.samples.apps.topeka.model.quiz.SelectItemQuiz;

@SuppressLint("ViewConstructor")
public class SelectItemQuizView extends AbsQuizView<SelectItemQuiz>
implements AdapterView.OnItemClickListener {
public class SelectItemQuizView extends AbsQuizView<SelectItemQuiz> {

private static final String KEY_ANSWERS = "ANSWERS";

Expand All @@ -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;
}

Expand Down Expand Up @@ -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];
}
Expand Down
Expand Up @@ -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<Q extends Quiz> extends AbsQuizView<Q> implements
TextWatcher, TextView.OnEditorActionListener {
public abstract class TextInputQuizView<Q extends Quiz> extends AbsQuizView<Q>
implements TextWatcher, TextView.OnEditorActionListener {

public TextInputQuizView(Context context, Category category, Q quiz) {
super(context, category, quiz);
Expand All @@ -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();
}

/**
Expand All @@ -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;
}
Expand All @@ -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));
}
}

0 comments on commit 9098f99

Please sign in to comment.