Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
Expand All @@ -24,6 +28,7 @@
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.matches;
import static android.support.test.espresso.assertion.ViewAssertions.selectedDescendantsMatch;
import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
Expand All @@ -34,7 +39,8 @@
public class PlanTaskListFragmentTest {

private static final String PLAN_ID = "PLAN_ID";
private static final String TASK_NAME = "TASK_NAME";
private static final String TASK_NAME_1 = "TASK_NAME_1";
private static final String TASK_NAME_2 = "TASK_NAME_2";
private static final String PLAN_NAME = "PLAN NAME";

@ClassRule
Expand All @@ -56,7 +62,7 @@ public class PlanTaskListFragmentTest {
public void setUp() {
PlanTaskListFragment fragment = new PlanTaskListFragment();

taskTemplateRule.createTask(TASK_NAME);
taskTemplateRule.createTask(TASK_NAME_1);
long planId = planTemplateRule.createPlan(PLAN_NAME);

Bundle args = new Bundle();
Expand Down Expand Up @@ -97,6 +103,59 @@ public void whenAddingNewTaskToPlanExpectShowTaskOnList() {

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

@Test
public void whenStepIsRemovedExpectStepIsNotOnTheList() {
long planId = planTemplateRule.createPlanWithTasks(PLAN_NAME, TASK_NAME_1, TASK_NAME_2);
openPlanTaskListFragment(planId);

onView(withId(R.id.rv_create_plan_task_list)).perform(
RecyclerViewActions
.actionOnItemAtPosition(0, clickChildViewWithId(R.id.id_remove_plan_task)));

assertTaskDisplayed(0, TASK_NAME_2);
}

private void openPlanTaskListFragment(long planId) {
PlanTaskListFragment fragment = new PlanTaskListFragment();
Bundle args = new Bundle();
args.putLong(PLAN_ID, planId);
fragment.setArguments(args);

FragmentManager manager = activityRule.getActivity().getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.plan_container, fragment);
transaction.commit();
}

public static ViewAction clickChildViewWithId(final int id) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return null;
}

@Override
public String getDescription() {
return null;
}

@Override
public void perform(UiController uiController, View view) {
View v = view.findViewById(id);
v.performClick();
}
};
}

private void assertTaskDisplayed(int position, String taskName) {
onView(withRecyclerView(R.id.rv_create_plan_task_list)
.atPosition(position))
.check(selectedDescendantsMatch(withId(R.id.id_tv_task_name),
withText(taskName)));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public long createPlan(String planName) {
return planId;
}

public long createPlanWithTasksSteps(String planName, String taskName1, String taskName2) {
public long createPlanWithTasks(String planName, String taskName1, String taskName2) {
Context context = activityRule.getActivity().getApplicationContext();
planTemplateRepository = new PlanTemplateRepository(daoSessionResource.getSession(context));
taskTemplateRepository = new TaskTemplateRepository(daoSessionResource.getSession(context));
Expand All @@ -50,8 +50,8 @@ public long createPlanWithTasksSteps(String planName, String taskName1, String t
long taksId2 = taskTemplateRepository.create(taskName2, 2, null, null);
taskIds.add(taksId1);
taskIds.add(taksId2);
planTemplateRepository.setTasksWithThisPlan(planId, taksId1);
planTemplateRepository.setTasksWithThisPlan(planId, taksId2);
planTemplateRepository.setTaskWithPlan(planId, taksId1);
planTemplateRepository.setTaskWithPlan(planId, taksId2);

return planId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,41 @@ public void update(Long planId, String name) {
daoSession.getPlanTemplateDao().update(planTemplate);
}

public void setTaskWithPlan(Long planId, Long taskId) {
List<PlanTaskTemplate> planTaskList = get(planId).getPlanTaskTemplates();
int order = 0;
if(planTaskList != null){
for(PlanTaskTemplate planTaskTemplate : planTaskList){
if(planTaskTemplate.getOrder() > order) {
order = planTaskTemplate.getOrder();
}
}
}
setTaskWithPlan(planId, taskId, order);
}

public void setTasksWithThisPlan(Long planId, Long taskId) {
public void setTaskWithPlan(Long planId, Long taskId, int order) {
PlanTaskTemplate planTaskTemplate = new PlanTaskTemplate();
planTaskTemplate.setTaskTemplateId(taskId);
planTaskTemplate.setPlanTemplateId(planId);
planTaskTemplate.setOrder(order);

PlanTaskTemplateDao targetDao = daoSession.getPlanTaskTemplateDao();
targetDao.insert(planTaskTemplate);

get(planId).resetTasksWithThisPlan();
get(planId).resetPlanTaskTemplates();
}

public void removeTaskFromPlan(Long planId, Long planTaskId) {
PlanTemplate planTemplate = daoSession.getPlanTemplateDao().load(planId);
List<PlanTaskTemplate> planTaskTemplatesList = planTemplate.getPlanTaskTemplates();
PlanTaskTemplateDao targetDao = daoSession.getPlanTaskTemplateDao();
for(PlanTaskTemplate planTaskTemplate : planTaskTemplatesList){
if(planTaskTemplate.getId() == planTaskId){
targetDao.delete(planTaskTemplate);
}
}
planTemplate.resetPlanTaskTemplates();
}

public PlanTemplate get(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,16 @@ public class AddTasksToPlanFragment extends Fragment implements AddTasksToPlanEv
new TaskRecyclerViewAdapter.TaskItemClickListener() {
@Override
public void onTaskItemClick(int position) {
planTemplateRepository.setTasksWithThisPlan(planId, taskListAdapter.getTaskItem(position).getId());
planTemplateRepository.setTaskWithPlan(planId, taskListAdapter.getTaskItem(position).getId());
taskListAdapter.removeListItem(position);
}

@Override
public void onRemoveTaskClick(int position){
/*Item remove TODO*/
/* Intentionally empty. Remove button is hidden */
}
};



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package pg.autyzm.friendly_plans.manager_app.view.plan_create_task_list;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import database.entities.PlanTaskTemplate;
import database.entities.TaskTemplate;
import pg.autyzm.friendly_plans.R;
import pg.autyzm.friendly_plans.asset.AssetsHelper;
import pg.autyzm.friendly_plans.item_touch_helper.ItemTouchHelperAdapter;

public class PlanCreateTaskListRecyclerViewAdapter extends
RecyclerView.Adapter<PlanCreateTaskListRecyclerViewAdapter.TaskListViewHolder> implements ItemTouchHelperAdapter {

private static final int ICON_PLACEHOLDER_PICTURE_ID = R.drawable.ic_placeholder;
private static final int ICON_PLACEHOLDER_SOUND_ID = R.drawable.ic_playing_sound;
private static final int ICON_PLACEHOLDER_TIME_ID = R.drawable.ic_placeholder_time;
private static List<PlanTaskTemplate> planTaskItemList = new ArrayList<>();
private TaskItemClickListener taskItemClickListener;
private AssetsHelper assetsHelper;

PlanCreateTaskListRecyclerViewAdapter(TaskItemClickListener taskItemClickListener) {
this.taskItemClickListener = taskItemClickListener;
}

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same code is in steps list. It would be good to extract it so some common component like. ItemOrderUpdater with method swapOrder(list, from, to)

for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(planTaskItemList, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(planTaskItemList, i, i - 1);
}
}
notifyItemMoved(fromPosition, toPosition);
return true;
}

@Override
public void onItemDismiss(int position) {
taskItemClickListener.onRemoveTaskClick(planTaskItemList.get(position).getId());
notifyItemRemoved(position);
}

@Override
public void onItemReleased() {
taskItemClickListener.onMoveItem();
}

interface TaskItemClickListener {

void onRemoveTaskClick(long itemId);

void onMoveItem();
}

@Override
public TaskListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
assetsHelper = new AssetsHelper(parent.getContext());
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_plan_task, parent, false);
return new TaskListViewHolder(view, taskItemClickListener);
}

@Override
public void onBindViewHolder(TaskListViewHolder parent, int position) {
if (planTaskItemList != null && planTaskItemList.size() != 0) {
PlanTaskTemplate planTaskTemplate = planTaskItemList.get(position);
TaskTemplate taskTemplate = planTaskTemplate.getTaskTemplate();
parent.taskName.setText(taskTemplate.getName());
setPicture(parent, taskTemplate);
setSound(parent, taskTemplate);
setDurationTime(parent, taskTemplate);
}
}

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

public PlanTaskTemplate getPlanTaskTemplate(int id){
return planTaskItemList.get(id);
}

void setTaskItems(List<PlanTaskTemplate> planTaskItemList) {
this.planTaskItemList = planTaskItemList;
notifyDataSetChanged();
}

private void setDurationTime(TaskListViewHolder holder, TaskTemplate taskItem) {
if (taskItem.getDurationTime() != null && taskItem.getDurationTime() != 0) {
holder.taskDurationIcon.setImageResource(ICON_PLACEHOLDER_TIME_ID);
holder.taskDurationTime.setText(String.valueOf(taskItem.getDurationTime()));
holder.taskDurationIcon.setVisibility(View.VISIBLE);
} else {
holder.taskDurationTime.setText("");
holder.taskDurationIcon.setVisibility(View.INVISIBLE);
}
}

private void setSound(TaskListViewHolder holder, TaskTemplate taskItem) {
if (taskItem.getSound() != null && !taskItem.getSound().getFilename().isEmpty()) {
holder.taskSoundIcon.setImageResource(ICON_PLACEHOLDER_SOUND_ID);
holder.taskSoundIcon.setVisibility(View.VISIBLE);
} else {
holder.taskSoundIcon.setVisibility(View.GONE);
}
}

private void setPicture(TaskListViewHolder holder, TaskTemplate taskItem) {
if (taskItem.getPicture() != null && !taskItem.getPicture().getFilename().isEmpty()) {
String picturePath = assetsHelper.getFileFullPath(taskItem.getPicture());
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
holder.taskPicture.setImageBitmap(bitmap);
} else {
holder.taskPicture.setImageResource(ICON_PLACEHOLDER_PICTURE_ID);
}
}

static class TaskListViewHolder extends RecyclerView.ViewHolder {

TextView taskName;
ImageButton removeButton;
ImageView taskPicture;
ImageView taskSoundIcon;
ImageView taskDurationIcon;
TextView taskDurationTime;

TaskListViewHolder(View itemView, final TaskItemClickListener taskItemClickListener) {
super(itemView);
this.taskName = (TextView) itemView
.findViewById(R.id.id_tv_task_name);
this.taskPicture = (ImageView) itemView
.findViewById(R.id.id_iv_task_picture);
this.taskSoundIcon = (ImageView) itemView
.findViewById(R.id.id_iv_task_sound_icon);
this.taskDurationIcon = (ImageView) itemView
.findViewById(R.id.id_iv_task_duration_icon);
this.taskDurationTime = (TextView) itemView
.findViewById(R.id.id_tv_task_duration_time);
this.removeButton = (ImageButton) itemView
.findViewById(R.id.id_remove_plan_task);
this.removeButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
long id = planTaskItemList.get(getAdapterPosition()).getId();
taskItemClickListener.onRemoveTaskClick(id);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
public interface PlanTaskListEvents {

void eventAddTasksToPlan(View view);

void eventSaveAndFinish(View view);
}
Loading