Skip to content

Commit

Permalink
autyzm-pg#298: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
paulamoller committed Oct 20, 2018
1 parent c286016 commit 9c553a2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition;
import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.not;
Expand Down Expand Up @@ -174,19 +175,6 @@ public void whenChildRemoveIconIsClickedButNoConfirmationGivenExpectChildIsOnThe
+ testedChildPosition))));
}

@Test
public void whenChildIsClickedAndSelectedExpectDBChildStatusChangeToActive() {
final int testedChildPosition = 5;
closeSoftKeyboard();
onView(withId(R.id.rv_child_list))
.perform(RecyclerViewActions.actionOnItemAtPosition(testedChildPosition, click()));
onView(withId(R.id.id_set_active_child)).perform(click());

assertThat(childRepository.getByIsActive().size(), is(1));
assertThat(childRepository.getByIsActive().get(0).getSurname(),
is(EXPECTED_LAST_NAME + testedChildPosition));
}

@Test
public void whenOtherChildIsSelectedActiveExpectPreviousActiveChildNoLongerActiveInDB() {
final int firstTestedChildPosition = 5;
Expand All @@ -211,4 +199,14 @@ public void whenOtherChildIsSelectedActiveExpectPreviousActiveChildNoLongerActiv
is(EXPECTED_LAST_NAME + secondTestedChildPosition));
}

@Test
public void whenChildIsSelectedExpectButtonIsEnabled() {
final int testedChildPosition = 5;
closeSoftKeyboard();

onView(withId(R.id.id_set_active_child)).check(matches(not(isEnabled())));
onView(withId(R.id.rv_child_list))
.perform(RecyclerViewActions.actionOnItemAtPosition(testedChildPosition, click()));
onView(withId(R.id.id_set_active_child)).check(matches(isEnabled()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public void setIsActive(Child child, boolean isActive) {
daoSession.getChildDao().update(child);
}

public void setAllInactive (){
List<Child> children = daoSession.getChildDao().loadAll();
for (Child child : children){
child.setIsActive(false);
}
daoSession.getChildDao().updateInTx(children);
};

public List<Child> getBySurname(String surname) {
return daoSession.getChildDao()
.queryBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.widget.Button;
import database.entities.Child;
import database.repository.ChildRepository;
import java.util.List;
import javax.inject.Inject;
import pg.autyzm.friendly_plans.App;
import pg.autyzm.friendly_plans.R;
Expand All @@ -34,6 +33,7 @@ public class ChildListActivity extends AppCompatActivity implements ChildListAct

private ChildRecyclerViewAdapter childListAdapter;
private Integer selectedChildPosition;
private Button setActiveChildButton;

ChildRecyclerViewAdapter.ChildItemClickListener childItemClickListener =
new ChildRecyclerViewAdapter.ChildItemClickListener() {
Expand Down Expand Up @@ -68,6 +68,7 @@ public void onClick(DialogInterface dialog, int which) {
@Override
public void onChildItemClick(int position) {
childListAdapter.setSelectedChildPosition(position);
setActiveChildButton.setEnabled(true);
selectedChildPosition = position;
}
};
Expand Down Expand Up @@ -100,10 +101,13 @@ private void setUpViews() {
recyclerView.setAdapter(childListAdapter);
childListAdapter.setChildItems(childRepository.getAll());

Button setActiveChildButton = (Button) findViewById(R.id.id_set_active_child);
setActiveChildButton = (Button) findViewById(R.id.id_set_active_child);
setActiveChildButton.setEnabled(childRepository.getByIsActive().size() > 0);
setActiveChildButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setActiveChild();
if (selectedChildPosition != null) {
setActiveChild();
}
Intent intent = new Intent(ChildListActivity.this, MainActivity.class);
startActivity(intent);
}
Expand Down Expand Up @@ -136,15 +140,9 @@ private void removeChild(long itemId) {
}

private void setActiveChild() {
childRepository.setAllInactive();
Child selectedChild = childListAdapter.getChild(selectedChildPosition);
childRepository.setIsActive(selectedChild, true);

List<Child> childList = childRepository.getAll();
for (Child child : childList) {
if (child.getId() != selectedChild.getId()) {
childRepository.setIsActive(child, false);
}
}
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public ChildRecyclerViewAdapter.ChildListViewHolder onCreateViewHolder(ViewGroup
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_child, parent, false);

return new ChildRecyclerViewAdapter.ChildListViewHolder(view, childItemClickListener,
childItemList);
}
Expand All @@ -46,15 +47,15 @@ public void onBindViewHolder(ChildRecyclerViewAdapter.ChildListViewHolder holder
if (childItemList != null && !childItemList.isEmpty()) {
Child childItem = childItemList.get(position);
holder.childName.setText(childItem.getName() + " " + childItem.getSurname());
if (isPositonActive(position)) {
if (isPositionActive(position)) {
holder.itemView.setBackgroundColor(Color.parseColor("#cccccc"));
} else {
holder.itemView.setBackgroundColor(Color.TRANSPARENT);
}
}
}

public boolean isPositonActive(int position) {
public boolean isPositionActive(int position) {
return (selectedChildPosition != null && selectedChildPosition == position) || (
selectedChildPosition == null && childItemList.get(position).getIsActive());
}
Expand Down

0 comments on commit 9c553a2

Please sign in to comment.