Never ask again is now a checkbox in the confirmation dialog

Next task text on confirmation dialog change
New text when there are no tasks in a list
Automatically open TaskList Activity when  no task lists exist
Confirmation on task list delete
Code cleanup (new interfaces...)
This commit is contained in:
2016-01-08 17:53:53 -05:00
parent 85c73eacb9
commit 31d9b26afc
9 changed files with 159 additions and 103 deletions

View File

@@ -63,17 +63,23 @@ public class MainActivity extends AppCompatActivity implements TasksFragment.Tas
mSectionsPagerAdapter.notifyDataSetChanged();
taskListDataAccess.close();
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
if (taskLists.size() == 0) {
Intent intent = new Intent(this, TaskListActivity.class);
startActivity(intent);
}
else {
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
// Hide or show new task floating button
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
if (taskLists.size() == 0) fab.hide();
else fab.show();
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
// Hide or show new task floating button
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.show();
}
}
@Override

View File

@@ -1,6 +1,5 @@
package com.wismna.geoffroy.donext.adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
@@ -11,7 +10,6 @@ import android.widget.TextView;
import com.wismna.geoffroy.donext.R;
import com.wismna.geoffroy.donext.dao.TaskList;
import com.wismna.geoffroy.donext.database.TaskListDataAccess;
import com.wismna.geoffroy.donext.helpers.TaskListTouchHelper;
import java.util.Collections;
@@ -25,16 +23,16 @@ public class TaskListRecyclerViewAdapter extends RecyclerView.Adapter<TaskListRe
implements TaskListTouchHelper.TaskListTouchHelperAdapter {
public interface TaskListRecyclerViewAdapterListener {
void notifyOnDeleteButtonClicked();
void onNameChangeFocus(TaskList taskList);
void onClickDeleteButton(int position, long id);
void onItemMove(long fromTaskId, long toTaskId, int fromPosition, int toPosition);
}
private final List<TaskList> mValues;
private Context mContext;
private TaskListRecyclerViewAdapterListener mListener;
public TaskListRecyclerViewAdapter(List<TaskList> items, Context context, TaskListRecyclerViewAdapterListener listener) {
public TaskListRecyclerViewAdapter(List<TaskList> items, TaskListRecyclerViewAdapterListener listener) {
mValues = items;
mContext = context;
mListener = listener;
}
@@ -61,17 +59,13 @@ public class TaskListRecyclerViewAdapter extends RecyclerView.Adapter<TaskListRe
if (!hasFocus && !holder.mItem.getName().matches(name)) {
holder.mItem.setName(name);
TaskListDataAccess taskListDataAccess = new TaskListDataAccess(mContext);
taskListDataAccess.open();
update(holder.mItem, position);
taskListDataAccess.updateName(holder.mItem.getId(), holder.mItem.getName());
taskListDataAccess.close();
mListener.onNameChangeFocus(holder.mItem);
}
}
});
// TODO: add confirmation dialog
// Handle click on delete button
holder.mTaskDeleteButton.setOnClickListener(new View.OnClickListener() {
@Override
@@ -79,16 +73,8 @@ public class TaskListRecyclerViewAdapter extends RecyclerView.Adapter<TaskListRe
// Disable the OnFocusChanged listener as it is pointless now
holder.mTaskNameView.setOnFocusChangeListener(null);
TaskListDataAccess taskListDataAccess = new TaskListDataAccess(mContext);
taskListDataAccess.open();
taskListDataAccess.deleteTaskList(holder.mItem.getId());
remove(position);
taskListDataAccess.close();
// Notify parent fragment that a task list was deleted
mListener.notifyOnDeleteButtonClicked();
//remove(position);
mListener.onClickDeleteButton(position, holder.mItem.getId());
}
});
}
@@ -104,8 +90,8 @@ public class TaskListRecyclerViewAdapter extends RecyclerView.Adapter<TaskListRe
}
public void remove(int position) {
mValues.remove(position);
notifyItemRemoved(position);
mValues.remove(position);
}
public void update(TaskList item, int position) {
@@ -115,8 +101,6 @@ public class TaskListRecyclerViewAdapter extends RecyclerView.Adapter<TaskListRe
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
TaskListDataAccess taskListDataAccess = new TaskListDataAccess(mContext);
taskListDataAccess.open();
long fromTaskId = mValues.get(fromPosition).getId();
long toTaskId = mValues.get(toPosition).getId();
@@ -131,13 +115,8 @@ public class TaskListRecyclerViewAdapter extends RecyclerView.Adapter<TaskListRe
Collections.swap(mValues, i, i - 1);
}
}
taskListDataAccess.updateOrder(fromTaskId, toPosition);
taskListDataAccess.updateOrder(toTaskId, fromPosition);
// Update the adapter on the fly
mListener.onItemMove(fromTaskId, toTaskId, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
taskListDataAccess.close();
return true;
}

View File

@@ -7,16 +7,20 @@ import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import com.wismna.geoffroy.donext.R;
public class ConfirmDialogFragment extends DialogFragment {
public interface ConfirmDialogListener {
void onConfirmDialogPositiveClick(DialogFragment dialog);
void onConfirmDialogNeutralClick(DialogFragment dialog);
void onConfirmDialogCancel(int position);
void onConfirmDialogClick(DialogFragment dialog, ButtonEvent event);
}
public enum ButtonEvent{
YES,
NO
}
private ConfirmDialogListener confirmDialogListener;
private String message;
@@ -32,18 +36,20 @@ public class ConfirmDialogFragment extends DialogFragment {
super.onCancel(dialog);
// Allows refreshing the first item of the adapter
Bundle args = getArguments();
confirmDialogListener.onConfirmDialogCancel(args.getInt("ItemPosition"));
confirmDialogListener.onConfirmDialogClick(this, ButtonEvent.NO);
}
@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(message)
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_task_confirmation, null);
builder.setView(view).setMessage(message)
.setPositiveButton(R.string.task_confirmation_yes_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
confirmDialogListener.onConfirmDialogPositiveClick(ConfirmDialogFragment.this);
confirmDialogListener.onConfirmDialogClick(ConfirmDialogFragment.this, ButtonEvent.YES);
}
})
.setNegativeButton(R.string.task_confirmation_no_button, new DialogInterface.OnClickListener() {
@@ -51,10 +57,6 @@ public class ConfirmDialogFragment extends DialogFragment {
// User cancelled the dialog
ConfirmDialogFragment.this.getDialog().cancel();
}
}).setNeutralButton(R.string.task_confirmation_never_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
confirmDialogListener.onConfirmDialogNeutralClick(ConfirmDialogFragment.this);
}
})
.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override

View File

@@ -5,6 +5,7 @@ import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
@@ -27,7 +28,9 @@ import java.util.List;
/**
* A fragment representing a list of Items.
*/
public class TaskListsFragment extends Fragment implements TaskListRecyclerViewAdapter.TaskListRecyclerViewAdapterListener {
public class TaskListsFragment extends Fragment implements
TaskListRecyclerViewAdapter.TaskListRecyclerViewAdapterListener,
ConfirmDialogFragment.ConfirmDialogListener {
private TaskListRecyclerViewAdapter taskListRecyclerViewAdapter;
private TaskListDataAccess taskListDataAccess;
private View mView;
@@ -64,7 +67,6 @@ public class TaskListsFragment extends Fragment implements TaskListRecyclerViewA
taskListDataAccess.open();
TaskList taskList = taskListDataAccess.createTaskList(text, position);
taskListDataAccess.close();
taskListRecyclerViewAdapter.add(taskList, position);
editText.setText("");
@@ -75,6 +77,18 @@ public class TaskListsFragment extends Fragment implements TaskListRecyclerViewA
return mView;
}
@Override
public void onPause() {
super.onPause();
taskListDataAccess.close();
}
@Override
public void onResume() {
super.onResume();
taskListDataAccess.open();
}
private void toggleVisibleCreateNewTaskListLayout(View view) {
LinearLayout layout = (LinearLayout) view.findViewById(R.id.new_task_list_layout);
int taskListCount = taskListRecyclerViewAdapter.getItemCount();
@@ -86,7 +100,37 @@ public class TaskListsFragment extends Fragment implements TaskListRecyclerViewA
}
@Override
public void notifyOnDeleteButtonClicked() {
public void onNameChangeFocus(TaskList taskList) {
taskListDataAccess.updateName(taskList.getId(), taskList.getName());
}
@Override
public void onClickDeleteButton(int position, long id) {
String title = getResources().getString(R.string.task_list_confirmation_delete);
ConfirmDialogFragment confirmDialogFragment =
ConfirmDialogFragment.newInstance(title, this);
Bundle args = new Bundle();
args.putInt("ItemPosition", position);
args.putLong("ItemId", id);
confirmDialogFragment.setArguments(args);
confirmDialogFragment.show(getFragmentManager(), title);
}
@Override
public void onItemMove(long fromTaskId, long toTaskId, int fromPosition, int toPosition) {
taskListDataAccess.updateOrder(fromTaskId, toPosition);
taskListDataAccess.updateOrder(toTaskId, fromPosition);
}
@Override
public void onConfirmDialogClick(DialogFragment dialog, ConfirmDialogFragment.ButtonEvent event) {
if (event == ConfirmDialogFragment.ButtonEvent.NO) return;
Bundle args = dialog.getArguments();
int position = args.getInt("ItemPosition");
long id = args.getLong("ItemId");
taskListRecyclerViewAdapter.remove(position);
taskListDataAccess.deleteTaskList(id);
toggleVisibleCreateNewTaskListLayout(mView);
}
@@ -104,7 +148,7 @@ public class TaskListsFragment extends Fragment implements TaskListRecyclerViewA
protected void onPostExecute(List<TaskList> taskLists) {
super.onPostExecute(taskLists);
taskListRecyclerViewAdapter =
new TaskListRecyclerViewAdapter(taskLists, getContext(), TaskListsFragment.this);
new TaskListRecyclerViewAdapter(taskLists, TaskListsFragment.this);
// Set the adapter
Context context = mView.getContext();

View File

@@ -16,6 +16,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
@@ -96,8 +97,6 @@ public class TasksFragment extends Fragment implements
// Set total cycles
UpdateCycleCount();
taskDataAccess.close();
// Set ItemTouch helper in RecyclerView to handle swipe move on elements
ItemTouchHelper.Callback callback = new TaskTouchHelper(this);
ItemTouchHelper helper = new ItemTouchHelper(callback);
@@ -141,21 +140,36 @@ public class TasksFragment extends Fragment implements
return view;
}
@Override
public void onPause() {
super.onPause();
taskDataAccess.close();
}
@Override
public void onResume() {
super.onResume();
taskDataAccess.open();
}
private void UpdateCycleCount() {
int totalCycles = taskDataAccess.getTotalCycles(taskListId);
if (totalCycles == 0) return;
TextView totalCyclesView = (TextView) view.findViewById(R.id.total_task_cycles);
totalCyclesView.setText(String.valueOf(taskDataAccess.getTotalCycles(taskListId) + " cycles"));
totalCyclesView.setText(String.valueOf(totalCycles + " cycles"));
}
private void UpdateTaskCount() {
int totalTasks = taskRecyclerViewAdapter.getItemCount();
TextView totalTasksView = (TextView) view.findViewById(R.id.total_task_count);
totalTasksView.setText(String.valueOf(totalTasks + " tasks"));
if (totalTasks == 0) totalTasksView.setText(getResources().getText(R.string.task_no_tasks));
else totalTasksView.setText(String.valueOf(totalTasks + " tasks"));
}
private void UpdateRemainingTaskCount() {
TextView remainingTasksView = (TextView) view.findViewById(R.id.remaining_task_count);
NoScrollingLayoutManager layoutManager = (NoScrollingLayoutManager) recyclerView.getLayoutManager();
int remainingTaskCount = taskRecyclerViewAdapter.getItemCount() - layoutManager.findLastCompletelyVisibleItemPosition() - 1;
int remainingTaskCount = taskRecyclerViewAdapter.getItemCount() - layoutManager.findLastVisibleItemPosition() - 1;
if (remainingTaskCount == 0)
remainingTasksView.setText("");
else
@@ -219,7 +233,6 @@ public class TasksFragment extends Fragment implements
// When clicked on undo, do not write to DB
if (event == DISMISS_EVENT_ACTION) return;
taskDataAccess.open();
// Commit the changes to DB
switch (direction)
{
@@ -237,7 +250,6 @@ public class TasksFragment extends Fragment implements
}
UpdateCycleCount();
taskDataAccess.close();
UpdateTaskCount();
UpdateRemainingTaskCount();
@@ -246,43 +258,38 @@ public class TasksFragment extends Fragment implements
}
@Override
public void onConfirmDialogPositiveClick(DialogFragment dialog) {
public void onConfirmDialogClick(DialogFragment dialog, ConfirmDialogFragment.ButtonEvent event) {
Bundle args = dialog.getArguments();
int itemPosition = args.getInt("ItemPosition");
int direction = args.getInt("Direction");
PerformTaskAction(itemPosition, direction);
}
// Handle never ask again checkbox
CheckBox neverAskAgainCheckBox = (CheckBox) dialog.getDialog().findViewById(R.id.task_confirmation_never);
if (neverAskAgainCheckBox.isChecked()) {
@Override
public void onConfirmDialogNeutralClick(DialogFragment dialog) {
Bundle args = dialog.getArguments();
int itemPosition = args.getInt("ItemPosition");
int direction = args.getInt("Direction");
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = sharedPref.edit();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = sharedPref.edit();
// Set system settings
switch (direction)
{
case ItemTouchHelper.LEFT:
editor.putBoolean("pref_conf_done", false);
break;
case ItemTouchHelper.RIGHT:
editor.putBoolean("pref_conf_next", false);
break;
case -1:
editor.putBoolean("pref_conf_del", false);
break;
// Set system settings
switch (direction) {
case ItemTouchHelper.LEFT:
editor.putBoolean("pref_conf_done", false);
break;
case ItemTouchHelper.RIGHT:
editor.putBoolean("pref_conf_next", false);
break;
case -1:
editor.putBoolean("pref_conf_del", false);
break;
}
editor.apply();
}
if (event == ConfirmDialogFragment.ButtonEvent.YES) {
PerformTaskAction(itemPosition, direction);
}
else if(event == ConfirmDialogFragment.ButtonEvent.NO) {
taskRecyclerViewAdapter.notifyItemChanged(itemPosition);
}
editor.apply();
PerformTaskAction(itemPosition, direction);
}
@Override
public void onConfirmDialogCancel(int position) {
taskRecyclerViewAdapter.notifyItemChanged(position);
}
@Override
@@ -302,15 +309,12 @@ public class TasksFragment extends Fragment implements
TaskList taskList = (TaskList) listSpinner.getSelectedItem();
// Add the task to the database
taskDataAccess.open();
Task newTask = taskDataAccess.createOrUpdateTask(id,
nameText.getText().toString(),
descText.getText().toString(),
priorityRadio.getText().toString(),
taskList.getId());
taskDataAccess.close();
Bundle args = dialog.getArguments();
// Should never happen because we will have to be on this tab to open the dialog
@@ -373,12 +377,12 @@ public class TasksFragment extends Fragment implements
{
// Mark item as Done
case ItemTouchHelper.LEFT:
title = "Mark task as done?";
title = getResources().getString(R.string.task_confirmation_done_text);
showDialog = sharedPref.getBoolean("pref_conf_done", true);
break;
// Increase task cycle count
case ItemTouchHelper.RIGHT:
title = "Go to next task?";
title = getResources().getString(R.string.task_confirmation_next_text);
showDialog = sharedPref.getBoolean("pref_conf_next", true);
break;
}

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/darker_gray" android:state_pressed="false" android:state_selected="true" />
<item android:drawable="@android:color/white" android:state_selected="false" />
</selector>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/task_confirmation_never"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:text="@string/task_confirmation_never_button" />
</LinearLayout>

View File

@@ -3,7 +3,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent"
android:background="@drawable/task_select" >
<TextView
android:id="@+id/total_task_cycles"
android:layout_width="100dp"
@@ -12,10 +13,9 @@
android:layout_alignParentStart="true"/>
<TextView
android:id="@+id/total_task_count"
android:layout_width="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
android:layout_centerHorizontal="true" />
<android.support.v7.widget.RecyclerView
android:id="@+id/task_list_view"
android:name="com.wismna.geoffroy.donext.activities.TaskFragment"

View File

@@ -12,6 +12,8 @@
<string name="task_list_new_list_hint">New list name</string>
<string name="task_list_new_list_create">Create</string>
<string name="task_list_delete">Delete</string>
<string name="task_list_confirmation_delete">Delete task list?</string>
<!-- Strings related to new task dialog -->
<string name="new_task_list">List</string>
@@ -27,11 +29,11 @@
<!-- Strings related to task details activity -->
<string name="task_details_activity_title">Details</string>
<string name="task_no_tasks">Yeah! No more tasks!</string>
<!-- Strings related to the confirmation dialog -->
<string name="task_confirmation_done_text">Mark task as Done?</string>
<string name="task_confirmation_next_text">Go to next task?</string>
<string name="task_confirmation_next_text">NEXT, do this one later?</string>
<string name="task_confirmation_delete_text">Delete this task?</string>
<string name="task_confirmation_done_button">Done</string>
<string name="task_confirmation_next_button">Next</string>