Creates a new task in dialog

Edit lists activity now shows number of tasks
Deleting a task list deletes all related tasks
This commit is contained in:
2015-11-27 17:57:25 -05:00
parent 6b4cf52404
commit 9063420eb4
12 changed files with 348 additions and 60 deletions

View File

@@ -1,6 +1,9 @@
package com.wismna.geoffroy.donext.activities;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
@@ -14,17 +17,25 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import com.wismna.geoffroy.donext.R;
import com.wismna.geoffroy.donext.adapters.TaskCursorAdapter;
import com.wismna.geoffroy.donext.dao.TaskList;
import com.wismna.geoffroy.donext.database.TasksDataAccess;
import com.wismna.geoffroy.donext.database.TaskDataAccess;
import com.wismna.geoffroy.donext.database.TaskListDataAccess;
import com.wismna.geoffroy.donext.fragments.NewTaskFragment;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements NewTaskFragment.NewTaskListener {
protected TaskDataAccess taskDataAccess;
protected TaskCursorAdapter adapter;
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
@@ -40,7 +51,7 @@ public class MainActivity extends AppCompatActivity {
*/
private ViewPager mViewPager;
private TasksDataAccess dataAccess;
private TaskListDataAccess taskListDataAccess;
private List<TaskList> taskLists;
@Override
@@ -55,11 +66,15 @@ public class MainActivity extends AppCompatActivity {
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Access database to retrieve Tabs
dataAccess = new TasksDataAccess(this);
dataAccess.open();
// Access database to retrieve tasks
taskDataAccess = new TaskDataAccess(this);
taskDataAccess.open();
taskLists = dataAccess.getAllTaskLists();
// Access database to retrieve Tabs
taskListDataAccess = new TaskListDataAccess(this);
taskListDataAccess.open();
taskLists = taskListDataAccess.getAllTaskLists();
mSectionsPagerAdapter.notifyDataSetChanged();
// Set up the ViewPager with the sections adapter.
@@ -70,6 +85,68 @@ public class MainActivity extends AppCompatActivity {
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
taskDataAccess.open();
super.onResume();
}
@Override
protected void onPause() {
taskDataAccess.close();
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
taskDataAccess.close();
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
Dialog dialogView = dialog.getDialog();
Spinner listSpinner = (Spinner) dialogView.findViewById(R.id.new_task_list);
EditText nameText = (EditText) dialogView.findViewById(R.id.new_task_name);
EditText descText = (EditText) dialogView.findViewById(R.id.new_task_description);
RadioGroup priorityGroup = (RadioGroup) dialogView.findViewById(R.id.new_task_priority);
RadioButton priorityRadio = (RadioButton) dialogView.findViewById(priorityGroup.getCheckedRadioButtonId());
Cursor cursor = taskDataAccess.createTask(
nameText.getText().toString(),
descText.getText().toString(),
priorityRadio.getText().toString(),
((TaskList) listSpinner.getSelectedItem()).getId());
// TODO: uncomment after successfully creating adapter
//adapter.changeCursor(cursor);
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
}
/** Called when the user clicks the Settings button */
public void openSettings(MenuItem menuItem) {
Intent intent = new Intent(this, SettingsActivity.class);
@@ -94,28 +171,6 @@ public class MainActivity extends AppCompatActivity {
newTaskFragment.show(manager, "Create new task");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
@@ -149,6 +204,12 @@ public class MainActivity extends AppCompatActivity {
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
// TODO: implement task list
/*ListView listView = (ListView) rootView.findViewById(android.R.id.list);
adapter = new TaskCursorAdapter(
rootView.getContext(), R.layout.item_task, taskDataAccess.getAllTasksCursor(), 0);
listView.setAdapter(adapter);*/
return rootView;
}
}
@@ -170,12 +231,6 @@ public class MainActivity extends AppCompatActivity {
return PlaceholderFragment.newInstance(position + 1);
}
/*@Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}*/
@Override
public int getCount() {
if (taskLists != null) {

View File

@@ -12,10 +12,10 @@ import android.widget.RelativeLayout;
import com.wismna.geoffroy.donext.R;
import com.wismna.geoffroy.donext.adapters.TaskListCursorAdapter;
import com.wismna.geoffroy.donext.database.TasksDataAccess;
import com.wismna.geoffroy.donext.database.TaskListDataAccess;
public class TaskListActivity extends AppCompatActivity {
private TasksDataAccess dataAccess;
private TaskListDataAccess dataAccess;
private TaskListCursorAdapter adapter;
private ListView listView;
@@ -25,7 +25,7 @@ public class TaskListActivity extends AppCompatActivity {
setContentView(R.layout.activity_task_list);
listView = (ListView) findViewById(android.R.id.list);
dataAccess = new TasksDataAccess(this);
dataAccess = new TaskListDataAccess(this);
dataAccess.open();
adapter = new TaskListCursorAdapter(
@@ -66,6 +66,11 @@ public class TaskListActivity extends AppCompatActivity {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
dataAccess.close();
}
private void updateCreateButtonEnabled() {
//Button createButton = (Button) findViewById(R.id.new_task_list_button);
//EditText editText = (EditText) findViewById(R.id.new_task_list_name);
@@ -80,9 +85,4 @@ public class TaskListActivity extends AppCompatActivity {
else layout.setVisibility(View.VISIBLE);
}
@Override
protected void onDestroy() {
super.onDestroy();
dataAccess.close();
}
}

View File

@@ -0,0 +1,45 @@
package com.wismna.geoffroy.donext.adapters;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;
import com.wismna.geoffroy.donext.R;
import com.wismna.geoffroy.donext.database.DatabaseHelper;
/**
* Created by geoffroy on 15-11-27.
*/
public class TaskCursorAdapter extends ResourceCursorAdapter {
public TaskCursorAdapter(Context context, int layout, Cursor cursor, int flags) {
super(context, layout, cursor, flags);
}
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.item_task, parent, false);
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView taskListName = (TextView) view.findViewById(R.id.task_name);
// Extract properties from cursor
String name = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.TASKS_COLUMN_NAME));
// Populate fields with extracted properties
taskListName.setText(name);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
}

View File

@@ -35,8 +35,7 @@ public class TaskListCursorAdapter extends ResourceCursorAdapter {
TextView taskListName = (TextView) view.findViewById(R.id.task_list_name);
// Extract properties from cursor
String name = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.TASKLIST_COLUMN_NAME));
// TODO: Update the count when tasks are implemented
int count = 0;
long count = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseHelper.TASKLIST_COLUMN_TASK_COUNT));
// Populate fields with extracted properties
taskListCount.setText(String.valueOf(count));
taskListName.setText(name);

View File

@@ -7,11 +7,25 @@ public class Task {
private long id;
private String name;
private String description;
private int priority;
private int cycle;
private boolean deleted;
private int done;
private int deleted;
private long taskList;
private String taskListName;
public enum TaskPriority {
LOW(0),
NORMAL(1),
HIGH(2);
private int value;
private TaskPriority(int value) {
this.value = value;
}
}
public long getId() {
return id;
}
@@ -36,6 +50,14 @@ public class Task {
this.description = description;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public int getCycle() {
return cycle;
}
@@ -44,11 +66,19 @@ public class Task {
this.cycle = cycle;
}
public boolean isDeleted() {
return deleted;
public boolean isDone() {
return done != 0;
}
public void setDeleted(boolean deleted) {
public void setDone(int done) {
this.done = done;
}
public boolean isDeleted() {
return deleted != 0;
}
public void setDeleted(int deleted) {
this.deleted = deleted;
}

View File

@@ -6,6 +6,7 @@ package com.wismna.geoffroy.donext.dao;
public class TaskList {
private long id;
private String name;
private long taskCount;
public long getId() {
return id;
@@ -23,9 +24,18 @@ public class TaskList {
this.name = comment;
}
public long getTaskCount() {
return taskCount;
}
public void setTaskCount(long taskCount) {
this.taskCount = taskCount;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return name;
}
}

View File

@@ -14,6 +14,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
public static final String TASKLIST_TABLE_NAME = "tasklist";
public static final String TASKLIST_COLUMN_NAME = "name";
public static final String TASKLIST_COLUMN_TASK_COUNT = "taskcount";
private static final String TASKLIST_TABLE_CREATE =
"CREATE TABLE " + TASKLIST_TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
@@ -32,10 +33,10 @@ public class DatabaseHelper extends SQLiteOpenHelper {
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TASKS_COLUMN_NAME + " TEXT NOT NULL, " +
TASKS_COLUMN_DESC + " TEXT, " +
TASKS_COLUMN_CYCLE + " INTEGER NOT NULL, " +
TASKS_COLUMN_DONE + " INTEGER, " +
TASKS_COLUMN_DELETED + " INTEGER, " +
TASKS_COLUMN_PRIORITY + " INTEGER, " +
TASKS_COLUMN_CYCLE + " INTEGER DEFAULT 0, " +
TASKS_COLUMN_DONE + " INTEGER DEFAULT 0, " +
TASKS_COLUMN_DELETED + " INTEGER DEFAULT 0, " +
TASKS_COLUMN_LIST + " INTEGER NOT NULL, " +
"FOREIGN KEY(" + TASKS_COLUMN_LIST + ") REFERENCES " +
TASKLIST_TABLE_NAME + "(" + COLUMN_ID + ")" +

View File

@@ -0,0 +1,95 @@
package com.wismna.geoffroy.donext.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.wismna.geoffroy.donext.R;
import com.wismna.geoffroy.donext.dao.Task;
import java.util.ArrayList;
import java.util.List;
/**
* Created by geoffroy on 15-11-27.
*/
public class TaskDataAccess {
private SQLiteDatabase database;
private DatabaseHelper dbHelper;
private String[] taskColumns = {
DatabaseHelper.COLUMN_ID, DatabaseHelper.TASKS_COLUMN_NAME,
DatabaseHelper.TASKS_COLUMN_DESC, DatabaseHelper.TASKS_COLUMN_PRIORITY,
DatabaseHelper.TASKS_COLUMN_CYCLE, DatabaseHelper.TASKS_COLUMN_DONE,
DatabaseHelper.TASKS_COLUMN_DELETED, DatabaseHelper.TASKS_COLUMN_LIST};
private List<String> priorities = new ArrayList<>();
public TaskDataAccess(Context context) {
dbHelper = new DatabaseHelper(context);
priorities.add(context.getString(R.string.new_task_priority_low));
priorities.add(context.getString(R.string.new_task_priority_normal));
priorities.add(context.getString(R.string.new_task_priority_high));
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Cursor createTask(String name, String description, String priority, long taskList) {
ContentValues values = new ContentValues();
values.put(DatabaseHelper.TASKS_COLUMN_NAME, name);
values.put(DatabaseHelper.TASKS_COLUMN_DESC, description);
values.put(DatabaseHelper.TASKS_COLUMN_PRIORITY, priorities.indexOf(priority));
values.put(DatabaseHelper.TASKS_COLUMN_LIST, taskList);
database.insert(DatabaseHelper.TASKS_TABLE_NAME, null, values);
return getAllTasksCursor();
}
public Cursor deleteTask(Cursor taskCursor) {
Task task = cursorToTask(taskCursor);
long id = task.getId();
//System.out.println("Task deleted with id: " + id);
database.delete(DatabaseHelper.TASKS_TABLE_NAME, DatabaseHelper.COLUMN_ID
+ " = " + id, null);
return getAllTasksCursor();
}
public List<Task> getAllTasks() {
List<Task> tasks = new ArrayList<>();
Cursor cursor = getAllTasksCursor();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Task task = cursorToTask(cursor);
tasks.add(task);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return tasks;
}
public Cursor getAllTasksCursor() {
return database.query(DatabaseHelper.TASKS_TABLE_NAME,
taskColumns, null, null, null, null, null);
}
private Task cursorToTask(Cursor cursor) {
Task task = new Task();
task.setId(cursor.getLong(0));
task.setName(cursor.getString(1));
task.setDescription(cursor.getString(2));
task.setPriority(cursor.getInt(3));
task.setCycle(cursor.getInt(4));
task.setDone(cursor.getInt(5));
task.setDeleted(cursor.getInt(6));
task.setTaskList(cursor.getLong(7));
return task;
}
}

View File

@@ -14,13 +14,13 @@ import java.util.List;
/**
* Created by geoffroy on 15-11-25.
*/
public class TasksDataAccess {
public class TaskListDataAccess {
// Database fields
private SQLiteDatabase database;
private DatabaseHelper dbHelper;
private String[] taskListColumns = {DatabaseHelper.COLUMN_ID, DatabaseHelper.TASKLIST_COLUMN_NAME};
public TasksDataAccess(Context context) {
public TaskListDataAccess(Context context) {
dbHelper = new DatabaseHelper(context);
}
@@ -63,7 +63,9 @@ public class TasksDataAccess {
public Cursor deleteTaskList(Cursor taskListCursor) {
TaskList taskList = cursorToTaskList(taskListCursor);
long id = taskList.getId();
System.out.println("Comment deleted with id: " + id);
System.out.println("Task list deleted with id: " + id);
database.delete(DatabaseHelper.TASKS_TABLE_NAME, DatabaseHelper.TASKS_COLUMN_LIST
+ " = " + id, null);
database.delete(DatabaseHelper.TASKLIST_TABLE_NAME, DatabaseHelper.COLUMN_ID
+ " = " + id, null);
return getAllTaskListsCursor();
@@ -86,8 +88,15 @@ public class TasksDataAccess {
}
public Cursor getAllTaskListsCursor() {
return database.query(DatabaseHelper.TASKLIST_TABLE_NAME,
taskListColumns, null, null, null, null, null);
//return database.query(DatabaseHelper.TASKLIST_TABLE_NAME,
// taskListColumns, null, null, null, null, null);
return database.rawQuery("SELECT *," +
" (SELECT COUNT(*) " +
" FROM " + DatabaseHelper.TASKS_TABLE_NAME +
" WHERE " + DatabaseHelper.TASKS_TABLE_NAME + "." + DatabaseHelper.TASKS_COLUMN_LIST + " = " +
DatabaseHelper.TASKLIST_TABLE_NAME + "._id) AS " + DatabaseHelper.TASKLIST_COLUMN_TASK_COUNT +
" FROM " + DatabaseHelper.TASKLIST_TABLE_NAME,
null);
}
private TaskList cursorToTaskList(Cursor cursor) {

View File

@@ -1,5 +1,6 @@
package com.wismna.geoffroy.donext.fragments;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
@@ -12,7 +13,8 @@ import android.widget.Spinner;
import com.wismna.geoffroy.donext.R;
import com.wismna.geoffroy.donext.dao.TaskList;
import com.wismna.geoffroy.donext.database.TasksDataAccess;
import com.wismna.geoffroy.donext.database.TaskDataAccess;
import com.wismna.geoffroy.donext.database.TaskListDataAccess;
import java.util.List;
@@ -21,6 +23,33 @@ import java.util.List;
*/
public class NewTaskFragment extends DialogFragment {
/** The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface NewTaskListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
private TaskDataAccess taskDataAccess;
// Use this instance of the interface to deliver action events
private NewTaskListener mListener;
/** Override the Fragment.onAttach() method to instantiate the NoticeDialogListener */
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NewTaskListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NewTaskListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
@@ -35,17 +64,21 @@ public class NewTaskFragment extends DialogFragment {
.setPositiveButton(R.string.new_task_save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO: create the task in DB
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(NewTaskFragment.this);
}
})
.setNegativeButton(R.string.new_task_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
//mListener.onDialogNegativeClick(NoticeDialogFragment.this);
// Canceled creation, nothing to do
NewTaskFragment.this.getDialog().cancel();
}
});
// Access database to retrieve task lists
TasksDataAccess dataAccess = new TasksDataAccess(getActivity());
TaskListDataAccess dataAccess = new TaskListDataAccess(getActivity());
dataAccess.open();
// Populate spinner with task lists

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/task_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

View File

@@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
tools:context=".activities.MainActivity">
<TextView
@@ -40,6 +41,7 @@
android:text="@string/new_task_priority"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="@+id/new_task_priority"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton