diff --git a/app/src/main/java/org/isoron/uhabits/commands/DeleteHabitsCommand.java b/app/src/main/java/org/isoron/uhabits/commands/DeleteHabitsCommand.java new file mode 100644 index 000000000..6dff1f59d --- /dev/null +++ b/app/src/main/java/org/isoron/uhabits/commands/DeleteHabitsCommand.java @@ -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 . + */ + +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 habits; + + public DeleteHabitsCommand(List 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; + } +} diff --git a/app/src/main/java/org/isoron/uhabits/fragments/ListHabitsFragment.java b/app/src/main/java/org/isoron/uhabits/fragments/ListHabitsFragment.java index 060c47f08..4eaddb660 100644 --- a/app/src/main/java/org/isoron/uhabits/fragments/ListHabitsFragment.java +++ b/app/src/main/java/org/isoron/uhabits/fragments/ListHabitsFragment.java @@ -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; diff --git a/app/src/main/java/org/isoron/uhabits/models/Habit.java b/app/src/main/java/org/isoron/uhabits/models/Habit.java index d2a40674b..a15672420 100644 --- a/app/src/main/java/org/isoron/uhabits/models/Habit.java +++ b/app/src/main/java/org/isoron/uhabits/models/Habit.java @@ -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) diff --git a/app/src/main/res/menu-v21/list_habits_context.xml b/app/src/main/res/menu-v21/list_habits_context.xml index 2fe0c4b77..1427d4ab5 100644 --- a/app/src/main/res/menu-v21/list_habits_context.xml +++ b/app/src/main/res/menu-v21/list_habits_context.xml @@ -21,4 +21,9 @@ android:title="@string/unarchive" android:icon="@drawable/ic_action_unarchive_dark"/> + + \ No newline at end of file diff --git a/app/src/main/res/menu/list_habits_context.xml b/app/src/main/res/menu/list_habits_context.xml index 734bd1faf..a65956d60 100644 --- a/app/src/main/res/menu/list_habits_context.xml +++ b/app/src/main/res/menu/list_habits_context.xml @@ -21,4 +21,7 @@ android:title="@string/unarchive" android:icon="@drawable/ic_action_unarchive_light"/> + \ No newline at end of file diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index d773eca2f..208ec6212 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -97,4 +97,7 @@ Dica Para mudar a ordem dos hábitos, aperte no nome do hábito, sustente e arraste. Para ver mais dias, coloque seu aparelho em modo paisagem. + Deletar hábitos + Hábitos restaurados. + Os hábitos escolhidos serão deletados permanentemente. Esta ação não pode ser desfeita. \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dac78f66e..d768ea08a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -12,7 +12,8 @@ Change color Habit created. - Habit deleted. + Habits deleted. + Habits restored. Nothing to undo. Nothing to redo. Habit changed. @@ -104,6 +105,8 @@ Did you know? To rearrange the entries, press-and-hold on the name of the habit, then drag it. You can see more days by putting your phone in landscape mode. + Delete Habits + The habits will be permanently deleted. This action cannot be undone. @string/hint_drag