Skip to content

Commit

Permalink
autyzm-pg#298: resolve task
Browse files Browse the repository at this point in the history
  • Loading branch information
paulamoller committed Oct 16, 2018
1 parent 1166d3d commit e55cb5d
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
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.scrollToPosition;
import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
Expand Down Expand Up @@ -152,8 +153,9 @@ public void whenChildIsRemovedExpectChildIsNotOnTheList() {
onView(withId(R.id.rv_child_list)).perform(scrollToPosition(testedChildPosition));
onView(withRecyclerView(R.id.rv_child_list)
.atPosition(testedChildPosition))
.check(matches(not(hasDescendant(withText(EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME
+ testedChildPosition)))));
.check(matches(
not(hasDescendant(withText(EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME
+ testedChildPosition)))));
}

@Test
Expand All @@ -171,4 +173,42 @@ public void whenChildRemoveIconIsClickedButNoConfirmationGivenExpectChildIsOnThe
.check(matches(hasDescendant(withText(EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME
+ 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;
final int secondTestedChildPosition = 6;
closeSoftKeyboard();
onView(withId(R.id.rv_child_list))
.perform(RecyclerViewActions
.actionOnItemAtPosition(firstTestedChildPosition, 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 + firstTestedChildPosition));

onView(withId(R.id.button_addRemoveChild)).perform(scrollTo()).perform(click());
closeSoftKeyboard();
onView(withId(R.id.rv_child_list))
.perform(RecyclerViewActions
.actionOnItemAtPosition(secondTestedChildPosition, 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 + secondTestedChildPosition));
}

}
39 changes: 22 additions & 17 deletions Friendly-plans/app/src/main/java/database/entities/Child.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,29 @@ public class Child {

private String displayMode;

private boolean isActive;

@ToMany(referencedJoinProperty = "childId")
private List<ChildPlan> childPlans;

/**
* Used to resolve relations
*/
/** Used to resolve relations */
@Generated(hash = 2040040024)
private transient DaoSession daoSession;

/**
* Used for active entity operations.
*/
/** Used for active entity operations. */
@Generated(hash = 1911343815)
private transient ChildDao myDao;

@Generated(hash = 1302308560)
@Generated(hash = 87265832)
public Child(Long id, String name, String surname, String fontSize, String pictureSize,
String displayMode) {
String displayMode, boolean isActive) {
this.id = id;
this.name = name;
this.surname = surname;
this.fontSize = fontSize;
this.pictureSize = pictureSize;
this.displayMode = displayMode;
this.isActive = isActive;
}

@Generated(hash = 891984724)
Expand Down Expand Up @@ -79,6 +78,14 @@ public void setSurname(String surname) {
this.surname = surname;
}

public boolean getIsActive() {
return this.isActive;
}

public void setIsActive(boolean isActive) {
this.isActive = isActive;
}

public String getFontSize() {
return this.fontSize;
}
Expand Down Expand Up @@ -125,17 +132,15 @@ public List<ChildPlan> getChildPlans() {
return childPlans;
}

/**
* Resets a to-many relationship, making the next get call to query for a fresh result.
*/
/** Resets a to-many relationship, making the next get call to query for a fresh result. */
@Generated(hash = 398907355)
public synchronized void resetChildPlans() {
childPlans = null;
}

/**
* Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}. Entity must
* attached to an entity context.
* Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
* Entity must attached to an entity context.
*/
@Generated(hash = 128553479)
public void delete() {
Expand All @@ -146,8 +151,8 @@ public void delete() {
}

/**
* Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}. Entity must
* attached to an entity context.
* Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
* Entity must attached to an entity context.
*/
@Generated(hash = 1942392019)
public void refresh() {
Expand All @@ -158,8 +163,8 @@ public void refresh() {
}

/**
* Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}. Entity must
* attached to an entity context.
* Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
* Entity must attached to an entity context.
*/
@Generated(hash = 713229351)
public void update() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ public long create(String firstName, String lastName) {
Child childTemplate = new Child();
childTemplate.setName(firstName);
childTemplate.setSurname(lastName);
childTemplate.setIsActive(false);
return daoSession.getChildDao().insert(childTemplate);
}

public void setIsActive(Child child, boolean isActive) {
child.setIsActive(isActive);
daoSession.getChildDao().update(child);
}

public List<Child> getBySurname(String surname) {
return daoSession.getChildDao()
.queryBuilder()
Expand All @@ -39,6 +45,13 @@ public void delete(Long id) {
daoSession.getChildDao().deleteByKey(id);
}

public List<Child> getByIsActive() {
return daoSession.getChildDao()
.queryBuilder()
.where(Properties.IsActive.eq(true))
.list();
}

public void deleteAll() {
daoSession.getChildDao().deleteAll();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package pg.autyzm.friendly_plans.manager_app.view.child_list;

import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
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;
import pg.autyzm.friendly_plans.databinding.ActivityChildListBinding;
import pg.autyzm.friendly_plans.manager_app.view.main_screen.MainActivity;
import pg.autyzm.friendly_plans.notifications.DialogUserNotifier;
import pg.autyzm.friendly_plans.notifications.ToastUserNotifier;

Expand All @@ -26,6 +32,9 @@ public class ChildListActivity extends AppCompatActivity implements ChildListAct

ChildListData childData;

private ChildRecyclerViewAdapter childListAdapter;
private Integer selectedChildPosition;

ChildRecyclerViewAdapter.ChildItemClickListener childItemClickListener =
new ChildRecyclerViewAdapter.ChildItemClickListener() {

Expand All @@ -37,15 +46,17 @@ public void onRemoveChildClick(final long childId) {
getResources().getString(R.string.child_removal_confirmation_message)
);
dialog.setPositiveButton(
getResources().getString(R.string.child_removal_confirmation_positive_button),
getResources()
.getString(R.string.child_removal_confirmation_positive_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
removeChild(childId);
dialog.dismiss();
}
});
dialog.setNegativeButton(
getResources().getString(R.string.child_removal_confirmation_negative_button),
getResources()
.getString(R.string.child_removal_confirmation_negative_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Expand All @@ -56,7 +67,8 @@ public void onClick(DialogInterface dialog, int which) {

@Override
public void onChildItemClick(int position) {
//todo
childListAdapter.setSelectedChildPosition(position);
selectedChildPosition = position;
}
};

Expand All @@ -81,13 +93,21 @@ private void setUpViews() {

childData = new ChildListData(initialFirstName, initialLastName);
binding.setChildListData(childData);
ChildRecyclerViewAdapter childListAdapter;
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_child_list);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
childListAdapter = new ChildRecyclerViewAdapter(childItemClickListener);
recyclerView.setAdapter(childListAdapter);
childListAdapter.setChildItems(childRepository.getAll());

Button setActiveChildButton = (Button) findViewById(R.id.id_set_active_child);
setActiveChildButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setActiveChild();
Intent intent = new Intent(ChildListActivity.this, MainActivity.class);
startActivity(intent);
}
});
}

@Override
Expand All @@ -107,12 +127,24 @@ private Long addChild(String firstName, String lastName) {
}
}

private void removeChild(long itemId){
childRepository.delete(itemId);
setUpViews();
toastUserNotifier.displayNotifications(
R.string.child_removed_message,
getApplicationContext());
private void removeChild(long itemId) {
childRepository.delete(itemId);
setUpViews();
toastUserNotifier.displayNotifications(
R.string.child_removed_message,
getApplicationContext());
}

private void setActiveChild() {
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
@@ -1,5 +1,6 @@
package pg.autyzm.friendly_plans.manager_app.view.child_list;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -16,6 +17,7 @@ public class ChildRecyclerViewAdapter extends

private List<Child> childItemList;
private ChildItemClickListener childItemClickListener;
private Integer selectedChildPosition;

ChildRecyclerViewAdapter(ChildItemClickListener childItemClickListener) {
this.childItemClickListener = childItemClickListener;
Expand All @@ -34,7 +36,8 @@ 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);
return new ChildRecyclerViewAdapter.ChildListViewHolder(view, childItemClickListener,
childItemList);
}

@Override
Expand All @@ -43,26 +46,48 @@ 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)) {
holder.itemView.setBackgroundColor(Color.parseColor("#cccccc"));
} else {
holder.itemView.setBackgroundColor(Color.TRANSPARENT);
}
}
}

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

@Override
public int getItemCount() {
return childItemList != null && childItemList.size() != 0 ? childItemList.size() : 0;
}

Child getChild(int position) {
return childItemList.get(position);
}


void setChildItems(List<Child> childItemList) {
this.childItemList = childItemList;
notifyDataSetChanged();
}

void setSelectedChildPosition(int selectedChildPosition) {
this.selectedChildPosition = selectedChildPosition;
notifyDataSetChanged();
}

static class ChildListViewHolder extends RecyclerView.ViewHolder {

TextView childName;
ImageButton removeButton;
ChildItemClickListener childItemClickListener;
List<Child> childItemList;
ChildListViewHolder(View itemView, ChildItemClickListener childItemClickListener, List<Child> childItemList) {

ChildListViewHolder(View itemView, ChildItemClickListener childItemClickListener,
List<Child> childItemList) {
super(itemView);
this.childName = (TextView) itemView
.findViewById(R.id.id_tv_child_name);
Expand Down
Loading

0 comments on commit e55cb5d

Please sign in to comment.