Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
Enhanced summary in input activity (#226)
Browse files Browse the repository at this point in the history
* Closed #221
  • Loading branch information
DreierF authored Jan 10, 2017
1 parent 32a44a1 commit 77d30c8
Show file tree
Hide file tree
Showing 21 changed files with 466 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,25 @@
import de.dreier.mytargets.test.base.UITestBase;

import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
import static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static de.dreier.mytargets.test.utils.PermissionGranter.allowPermissionsIfNeeded;
import static de.dreier.mytargets.test.utils.matchers.MatcherUtils.matchToolbarTitle;
import static de.dreier.mytargets.test.utils.matchers.MatcherUtils.withRecyclerView;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.startsWith;

@RunWith(AndroidJUnit4.class)
public class SettingsActivityTest extends UITestBase {
Expand Down Expand Up @@ -77,16 +79,16 @@ public void settingsActivityTest() {

matchToolbarTitle(getActivity().getString(R.string.input));

matchPreferenceSummary(0, "3.0x");
clickOnPreference(0);
matchPreferenceSummary(7, "3.0x");
clickOnPreference(7);
selectFromList("5.0x");
matchPreferenceSummary(0, "5.0x");
matchPreferenceSummary(7, "5.0x");
assertEquals(SettingsManager.getInputTargetZoom(), 5.0f);

matchPreferenceSummary(1, "1.0x");
clickOnPreference(1);
matchPreferenceSummary(8, "1.0x");
clickOnPreference(8);
selectFromList("3.5x");
matchPreferenceSummary(1, "3.5x");
matchPreferenceSummary(8, "3.5x");
assertEquals(SettingsManager.getInputArrowDiameterScale(), 3.5f);

pressBack();
Expand Down Expand Up @@ -165,24 +167,30 @@ private SettingsActivity getActivity() {

private void enterText(String text) {
onView(withId(android.R.id.edit))
.perform(scrollTo(), replaceText(text), closeSoftKeyboard());
.perform(replaceText(text), closeSoftKeyboard());

onView(allOf(withId(android.R.id.button1), withText(android.R.string.ok),
isDisplayed())).perform(click());
}

private void selectFromList(String text1) {
onView(allOf(withText(text1), withParent(withId(R.id.select_dialog_listview)),
isDisplayed())).perform(click());
private void selectFromList(String text) {
onData(hasToString(startsWith(text)))
.inAdapterView(withId(R.id.select_dialog_listview))
.perform(click());
}

private void matchPreferenceSummary(int index, String expectedSummary) {
onView(allOf(withRecyclerView(R.id.list).atPositionOnView(index, android.R.id.summary),
isDisplayed())).check(matches(withText(expectedSummary)));
private void matchPreferenceSummary(int position, String expectedSummary) {
onView(allOf(withId(R.id.list), isOnForegroundFragment()))
.perform(scrollToPosition(position));
onView(withRecyclerView(allOf(withId(R.id.list),
isOnForegroundFragment())).atPositionOnView(position, android.R.id.summary))
.check(matches(withText(expectedSummary)));
}

private void clickOnPreference(int position) {
onView(allOf(withId(R.id.list), isOnForegroundFragment(), isDisplayed()))
onView(allOf(withId(R.id.list), isOnForegroundFragment()))
.perform(scrollToPosition(position));
onView(allOf(withId(R.id.list), isOnForegroundFragment()))
.perform(actionOnItemAtPosition(position, click()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import de.dreier.mytargets.app.ApplicationInstance;
import de.dreier.mytargets.features.settings.backup.EBackupInterval;
import de.dreier.mytargets.features.settings.backup.provider.EBackupLocation;
import de.dreier.mytargets.features.training.input.EShowMode;
import de.dreier.mytargets.features.training.input.ETrainingScope;
import de.dreier.mytargets.features.training.input.SummaryConfiguration;
import de.dreier.mytargets.features.training.input.TargetView;
import de.dreier.mytargets.shared.analysis.aggregation.EAggregationStrategy;
import de.dreier.mytargets.shared.models.Dimension;
Expand Down Expand Up @@ -147,8 +148,8 @@ public void setDonated() {

@Test
public void setShowMode() {
SettingsManager.setShowMode(EShowMode.TRAINING);
assertThat(SettingsManager.getShowMode()).isEqualTo(EShowMode.TRAINING);
SettingsManager.setShowMode(ETrainingScope.TRAINING);
assertThat(SettingsManager.getShowMode()).isEqualTo(ETrainingScope.TRAINING);
}

@Test
Expand Down Expand Up @@ -281,4 +282,16 @@ public void setShouldShowIntroActivity() {
SettingsManager.setShouldShowIntroActivity(false);
assertThat(SettingsManager.shouldShowIntroActivity()).isEqualTo(false);
}

@Test
public void setInputSummaryConfiguration() {
SummaryConfiguration config = new SummaryConfiguration();
config.showEnd = false;
config.showRound = true;
config.showTraining = true;
config.showAverage = true;
config.averageScope = ETrainingScope.TRAINING;
SettingsManager.setInputSummaryConfiguration(config);
assertThat(SettingsManager.getInputSummaryConfiguration()).isEqualTo(config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.CoreMatchers.is;

public class MatcherUtils {
Expand Down Expand Up @@ -69,7 +70,23 @@ public void describeTo(Description description) {
};
}

public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) {
return new RecyclerViewMatcher(recyclerViewId);
public static RecyclerViewMatcher withRecyclerView(Matcher<View> recyclerViewMatcher) {
return new RecyclerViewMatcher(recyclerViewMatcher);
}

public static RecyclerViewMatcher withRecyclerView(int recyclerViewId) {
return new RecyclerViewMatcher(withId(recyclerViewId));
}

public static View getMatchingParent(View view, Matcher<View> matcher) {
if (view == null) {
return null;
}
if (matcher.matches(view)) {
return view;
} else if (view.getParent() != null && view.getParent() instanceof ViewGroup) {
return getMatchingParent((View) view.getParent(), matcher);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,53 @@
import org.hamcrest.TypeSafeMatcher;

public class RecyclerViewMatcher {
private final int recyclerViewId;
private final Matcher<View> recyclerViewMatcher;

public RecyclerViewMatcher(int recyclerViewId) {
this.recyclerViewId = recyclerViewId;
public RecyclerViewMatcher(Matcher<View> recyclerViewMatcher) {
this.recyclerViewMatcher = recyclerViewMatcher;
}

public Matcher<View> atPosition(final int position) {
return atPositionOnView(position, -1);
}

public Matcher<View> atPositionOnView(final int position, final int targetViewId) {

return new TypeSafeMatcher<View>() {
Resources resources = null;
View childView;

public void describeTo(Description description) {
String idDescription = Integer.toString(recyclerViewId);
if (resources != null) {
try {
idDescription = resources.getResourceName(recyclerViewId);
} catch (Resources.NotFoundException var4) {
idDescription = recyclerViewId + " (resource name not found)";
recyclerViewMatcher.describeTo(description);
description.appendText(" at position " + position);
if (targetViewId != -1) {
String idDescription = Integer.toString(targetViewId);
if (resources != null) {
try {
idDescription = resources.getResourceName(targetViewId);
} catch (Resources.NotFoundException var4) {
idDescription = targetViewId + " (resource name not found)";
}
}
description.appendText(" on view with id " + idDescription);
}

description.appendText("with id: " + idDescription);
}

public boolean matchesSafely(View view) {
resources = view.getResources();

RecyclerView recyclerView = (RecyclerView) MatcherUtils
.getParentViewById(view, recyclerViewId);
if (recyclerView == null || recyclerView.getId() != recyclerViewId) {
return false;
if (childView == null) {
View parent = MatcherUtils.getMatchingParent(view, recyclerViewMatcher);
if (parent == null || !(parent instanceof RecyclerView)) {
return false;
}
RecyclerView recyclerView = (RecyclerView) parent;
RecyclerView.ViewHolder viewHolder = recyclerView
.findViewHolderForAdapterPosition(position);
if (viewHolder == null) {
return false;
}
childView = viewHolder.itemView;
}
View childView = recyclerView
.findViewHolderForAdapterPosition(position).itemView;

if (targetViewId == -1) {
return view == childView;
Expand All @@ -73,6 +82,4 @@ public boolean matchesSafely(View view) {
}
};
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import android.content.Context;
import android.os.Parcelable;

import de.dreier.mytargets.utils.OnItemClickListener;
import de.dreier.mytargets.utils.multiselector.OnItemClickListener;

public abstract class ListFragmentBase<T> extends FragmentBase implements OnItemClickListener<T> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,37 @@
package de.dreier.mytargets.features.settings;

import de.dreier.mytargets.R;
import de.dreier.mytargets.features.training.input.SummaryConfiguration;

import static de.dreier.mytargets.features.settings.SettingsManager.KEY_INPUT_ARROW_DIAMETER_SCALE;
import static de.dreier.mytargets.features.settings.SettingsManager.KEY_INPUT_KEYBOARD_TYPE;
import static de.dreier.mytargets.features.settings.SettingsManager.KEY_INPUT_SUMMARY_AVERAGE_OF;
import static de.dreier.mytargets.features.settings.SettingsManager.KEY_INPUT_TARGET_ZOOM;
import static de.dreier.mytargets.features.training.input.TargetView.EKeyboardType.LEFT;

public class InputSettingsFragment extends SettingsFragmentBase {

@Override
protected void updateItemSummaries() {
setSummary(KEY_INPUT_SUMMARY_AVERAGE_OF, getAverageOf());
setSummary(KEY_INPUT_ARROW_DIAMETER_SCALE,
SettingsManager.getInputArrowDiameterScale() + "x");
setSummary(KEY_INPUT_TARGET_ZOOM, SettingsManager.getInputTargetZoom() + "x");
setSummary(KEY_INPUT_KEYBOARD_TYPE, SettingsManager.getInputKeyboardType() == LEFT
? getString(R.string.left_handed) : getString(R.string.right_handed));
}

private String getAverageOf() {
final SummaryConfiguration configuration = SettingsManager
.getInputSummaryConfiguration();
switch (configuration.averageScope) {
case END:
return getString(R.string.end);
case TRAINING:
return getString(R.string.training);
case ROUND:
default:
return getString(R.string.round);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import de.dreier.mytargets.app.ApplicationInstance;
import de.dreier.mytargets.features.settings.backup.EBackupInterval;
import de.dreier.mytargets.features.settings.backup.provider.EBackupLocation;
import de.dreier.mytargets.features.training.input.EShowMode;
import de.dreier.mytargets.features.training.input.ETrainingScope;
import de.dreier.mytargets.features.training.input.SummaryConfiguration;
import de.dreier.mytargets.features.training.input.TargetView.EKeyboardType;
import de.dreier.mytargets.shared.analysis.aggregation.EAggregationStrategy;
import de.dreier.mytargets.shared.models.Dimension;
Expand All @@ -48,10 +49,15 @@ public class SettingsManager {
public static final String KEY_PROFILE_LAST_NAME = "profile_last_name";
public static final String KEY_PROFILE_BIRTHDAY = "profile_birthday";
public static final String KEY_PROFILE_CLUB = "profile_club";
private static final String KEY_INPUT_SUMMARY_SHOW_END = "input_summary_show_end";
private static final String KEY_INPUT_SUMMARY_SHOW_ROUND = "input_summary_show_round";
private static final String KEY_INPUT_SUMMARY_SHOW_TRAINING = "input_summary_show_training";
private static final String KEY_INPUT_SUMMARY_SHOW_AVERAGE = "input_summary_show_average";
public static final String KEY_INPUT_SUMMARY_AVERAGE_OF = "input_summary_average_of";
public static final String KEY_INPUT_ARROW_DIAMETER_SCALE = "input_arrow_diameter_scale";
public static final String KEY_INPUT_TARGET_ZOOM = "input_target_zoom";
public static final String KEY_INPUT_KEYBOARD_TYPE = "input_keyboard_type";
public static final String KEY_FIRST_TRAINING_SHOWN = "first_training_shown";
private static final String KEY_FIRST_TRAINING_SHOWN = "first_training_shown";
private static final String KEY_BACKUP_INTERVAL = "backup_interval";
private static final String KEY_DONATED = "donated";
private static final String KEY_TIMER_VIBRATE = "timer_vibrate";
Expand Down Expand Up @@ -238,12 +244,12 @@ public static void setDonated(boolean donated) {
.apply();
}

public static EShowMode getShowMode() {
return EShowMode.valueOf(preferences
.getString(KEY_SHOW_MODE, EShowMode.END.toString()));
public static ETrainingScope getShowMode() {
return ETrainingScope.valueOf(preferences
.getString(KEY_SHOW_MODE, ETrainingScope.END.toString()));
}

public static void setShowMode(EShowMode showMode) {
public static void setShowMode(ETrainingScope showMode) {
preferences
.edit()
.putString(KEY_SHOW_MODE, showMode.toString())
Expand Down Expand Up @@ -503,4 +509,25 @@ public static void setShouldShowIntroActivity(boolean shouldShow) {
.putBoolean(KEY_INTRO_SHOWED, shouldShow)
.apply();
}

public static SummaryConfiguration getInputSummaryConfiguration() {
SummaryConfiguration config = new SummaryConfiguration();
config.showEnd = preferences.getBoolean(KEY_INPUT_SUMMARY_SHOW_END, true);
config.showRound = preferences.getBoolean(KEY_INPUT_SUMMARY_SHOW_ROUND, true);
config.showTraining = preferences.getBoolean(KEY_INPUT_SUMMARY_SHOW_TRAINING, false);
config.showAverage = preferences.getBoolean(KEY_INPUT_SUMMARY_SHOW_AVERAGE, true);
config.averageScope = ETrainingScope
.valueOf(preferences.getString(KEY_INPUT_SUMMARY_AVERAGE_OF, "ROUND"));
return config;
}

public static void setInputSummaryConfiguration(SummaryConfiguration configuration) {
preferences.edit()
.putBoolean(KEY_INPUT_SUMMARY_SHOW_END, configuration.showEnd)
.putBoolean(KEY_INPUT_SUMMARY_SHOW_ROUND, configuration.showRound)
.putBoolean(KEY_INPUT_SUMMARY_SHOW_TRAINING, configuration.showTraining)
.putBoolean(KEY_INPUT_SUMMARY_SHOW_AVERAGE, configuration.showAverage)
.putString(KEY_INPUT_SUMMARY_AVERAGE_OF, configuration.averageScope.name())
.apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ public boolean onPrepareOptionsMenu(Menu menu) {
filter.setIcon(showFilter ?
R.drawable.ic_clear_filter_white_24dp :
R.drawable.ic_filter_white_24dp);
filter.setVisible(rounds != null);
// only show filter if we have at least one category to filter by
boolean filterAvailable = binding.distanceTags.getTags().size() > 1
|| binding.bowTags.getTags().size() > 1
|| binding.arrowTags.getTags().size() > 1;
filter.setVisible(rounds != null && filterAvailable);
export.setVisible(rounds != null);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@

package de.dreier.mytargets.features.training.input;

import de.dreier.mytargets.R;

public enum EShowMode {
END(R.id.action_show_end),
ROUND(R.id.action_show_round),
TRAINING(R.id.action_show_training);

public final int actionItemId;

EShowMode(int actionItemId) {
this.actionItemId = actionItemId;
}
public enum ETrainingScope {
END, ROUND, TRAINING
}
Loading

0 comments on commit 77d30c8

Please sign in to comment.