Skip to content

Commit

Permalink
#315 remove selected task from plan (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanBan authored and malsolec committed Aug 3, 2018
1 parent 23ef7e5 commit dc88215
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.rule.ActivityTestRule;
Expand All @@ -14,15 +15,18 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import database.repository.PlanTemplateRepository;
import pg.autyzm.friendly_plans.R;
import pg.autyzm.friendly_plans.manager_app.view.plan_create.PlanCreateActivity;
import pg.autyzm.friendly_plans.manager_app.view.plan_create_task_list.PlanTaskListFragment;
import pg.autyzm.friendly_plans.resource.DaoSessionResource;
import pg.autyzm.friendly_plans.resource.PlanTemplateRule;
import pg.autyzm.friendly_plans.resource.TaskTemplateRule;
import pg.autyzm.friendly_plans.view_actions.ViewClicker;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
Expand All @@ -36,6 +40,7 @@ public class PlanTaskListFragmentTest {
private static final String PLAN_ID = "PLAN_ID";
private static final String TASK_NAME = "TASK_NAME";
private static final String PLAN_NAME = "PLAN NAME";
private static final String DELETE_TEST_TASK = "DELETE TEST TASK";

@ClassRule
public static DaoSessionResource daoSessionResource = new DaoSessionResource();
Expand All @@ -54,10 +59,15 @@ public class PlanTaskListFragmentTest {

@Before
public void setUp() {
PlanTaskListFragment fragment = new PlanTaskListFragment();
Context context = activityRule.getActivity().getApplicationContext();
PlanTemplateRepository planTemplateRepository = new PlanTemplateRepository(daoSessionResource.getSession(context));

taskTemplateRule.createTask(TASK_NAME);
long taskId = taskTemplateRule.createTask(DELETE_TEST_TASK);
long planId = planTemplateRule.createPlan(PLAN_NAME);
planTemplateRepository.setTasksWithThisPlan(planId, taskId);
taskTemplateRule.createTask(TASK_NAME);

PlanTaskListFragment fragment = new PlanTaskListFragment();

Bundle args = new Bundle();
args.putLong(PLAN_ID, planId);
Expand Down Expand Up @@ -95,8 +105,27 @@ public void whenAddingNewTaskToPlanExpectShowTaskOnList() {
onView(withId(R.id.id_btn_add_tasks_to_plan))
.perform(click());

recyclerView = (RecyclerView) activityRule.getActivity().findViewById(R.id.rv_create_plan_task_list);
lastPosition = recyclerView.getAdapter().getItemCount() - 1;

onView(withRecyclerView(R.id.rv_create_plan_task_list)
.atPosition(0))
.atPosition(lastPosition))
.check(matches(hasDescendant(withText(TASK_NAME))));
}

@Test
public void whenRemoveIconClickedExpectTaskToNoLongerBeInThisPlan(){
final int testedTaskPosition = 0;

onView(withId(R.id.rv_create_plan_task_list))
.perform(RecyclerViewActions
.actionOnItemAtPosition(testedTaskPosition,
new ViewClicker(R.id.id_remove_task)));

onView(withId(R.id.rv_create_plan_task_list)).perform(
RecyclerViewActions.scrollToPosition(testedTaskPosition));

onView(withRecyclerView(R.id.rv_create_plan_task_list)
.atPosition(testedTaskPosition)).check(doesNotExist());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package database.repository;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import database.entities.DaoSession;
Expand Down Expand Up @@ -30,7 +33,6 @@ public void update(Long planId, String name) {
daoSession.getPlanTemplateDao().update(planTemplate);
}


public void setTasksWithThisPlan(Long planId, Long taskId) {
PlanTaskTemplate planTaskTemplate = new PlanTaskTemplate();
planTaskTemplate.setTaskTemplateId(taskId);
Expand All @@ -42,6 +44,19 @@ public void setTasksWithThisPlan(Long planId, Long taskId) {
get(planId).resetTasksWithThisPlan();
}

public void deleteTaskFromThisPlan(Long planId, Long taskId){
PlanTaskTemplateDao planTaskTemplateDao = daoSession.getPlanTaskTemplateDao();
QueryBuilder<PlanTaskTemplate> queryBuilder = planTaskTemplateDao.queryBuilder();

PlanTaskTemplate planTaskTemplate = queryBuilder.where(
PlanTaskTemplateDao.Properties.PlanTemplateId.eq(planId),
PlanTaskTemplateDao.Properties.TaskTemplateId.eq(taskId))
.uniqueOrThrow();

planTaskTemplateDao.delete(planTaskTemplate);
}


public PlanTemplate get(Long id) {
return daoSession.getPlanTemplateDao().load(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public void onRemoveTaskClick(int position){
}
};



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public void onTaskItemClick(int position) {

@Override
public void onRemoveTaskClick(int position){
/*Item remove TODO*/
planTemplateRepository.deleteTaskFromThisPlan(
planId,
taskListAdapter.getTaskItem(position).getId());
taskListAdapter.removeListItem(position);
}
};

Expand Down
6 changes: 3 additions & 3 deletions Friendly-plans/app/src/main/res/layout/item_task.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:orientation="vertical">

<ImageButton
android:id="@+id/id_remove_task"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="@drawable/ic_delete_black_24dp" />
android:layout_height="wrap_content"
android:src="@drawable/ic_delete_black_24dp" />
</LinearLayout>

</LinearLayout>

0 comments on commit dc88215

Please sign in to comment.