Implement habit deletion

This commit is contained in:
2016-02-24 04:46:18 -05:00
parent dcaff3d1b8
commit b0ccf3464f
7 changed files with 112 additions and 3 deletions

View File

@@ -0,0 +1,56 @@
/* Copyright (C) 2016 Alinson Santos Xavier
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.commands;
import org.isoron.helpers.Command;
import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
import java.util.List;
public class DeleteHabitsCommand extends Command
{
private List<Habit> habits;
public DeleteHabitsCommand(List<Habit> habits)
{
this.habits = habits;
}
@Override
public void execute()
{
for(Habit h : habits)
h.cascadeDelete();
}
@Override
public void undo()
{
}
public Integer getExecuteStringId()
{
return R.string.toast_habit_deleted;
}
public Integer getUndoStringId()
{
return R.string.toast_habit_restored;
}
}

View File

@@ -19,16 +19,16 @@ package org.isoron.uhabits.fragments;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
@@ -65,6 +65,7 @@ import org.isoron.helpers.ReplayableActivity;
import org.isoron.uhabits.R;
import org.isoron.uhabits.commands.ArchiveHabitsCommand;
import org.isoron.uhabits.commands.ChangeHabitColorCommand;
import org.isoron.uhabits.commands.DeleteHabitsCommand;
import org.isoron.uhabits.commands.UnarchiveHabitsCommand;
import org.isoron.uhabits.helpers.ReminderHelper;
import org.isoron.uhabits.loaders.HabitListLoader;
@@ -178,6 +179,23 @@ public class ListHabitsFragment extends Fragment
});
picker.show(getFragmentManager(), "picker");
}
case R.id.action_delete:
{
new AlertDialog.Builder(activity)
.setTitle(R.string.delete_habits)
.setMessage(R.string.delete_habits_message)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
executeCommand(new DeleteHabitsCommand(selectedHabits), null);
mode.finish();
}
}).setNegativeButton(android.R.string.no, null)
.show();
}
}
return false;

View File

@@ -215,6 +215,27 @@ public class Habit extends Model
return (count > 0);
}
public void cascadeDelete()
{
Long id = getId();
ActiveAndroid.beginTransaction();
try
{
new Delete().from(Checkmark.class).where("habit = ?", id).execute();
new Delete().from(Repetition.class).where("habit = ?", id).execute();
new Delete().from(Score.class).where("habit = ?", id).execute();
new Delete().from(Streak.class).where("habit = ?", id).execute();
delete();
ActiveAndroid.setTransactionSuccessful();
}
finally
{
ActiveAndroid.endTransaction();
}
}
public void deleteReps(long timestamp)
{
new Delete().from(Repetition.class)