mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Implement habit deletion
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -21,4 +21,9 @@
|
||||
android:title="@string/unarchive"
|
||||
android:icon="@drawable/ic_action_unarchive_dark"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/action_delete"
|
||||
android:title="@string/delete"
|
||||
android:showAsAction="never" />
|
||||
|
||||
</menu>
|
||||
@@ -21,4 +21,7 @@
|
||||
android:title="@string/unarchive"
|
||||
android:icon="@drawable/ic_action_unarchive_light"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/action_delete"
|
||||
android:title="@string/delete" />
|
||||
</menu>
|
||||
@@ -97,4 +97,7 @@
|
||||
<string name="hint_title">Dica</string>
|
||||
<string name="hint_drag">Para mudar a ordem dos hábitos, aperte no nome do hábito, sustente e arraste.</string>
|
||||
<string name="hint_landscape">Para ver mais dias, coloque seu aparelho em modo paisagem.</string>
|
||||
<string name="delete_habits">Deletar hábitos</string>
|
||||
<string name="toast_habit_restored">Hábitos restaurados.</string>
|
||||
<string name="delete_habits_message">Os hábitos escolhidos serão deletados permanentemente. Esta ação não pode ser desfeita.</string>
|
||||
</resources>
|
||||
@@ -12,7 +12,8 @@
|
||||
<string name="color_picker_default_title">Change color</string>
|
||||
|
||||
<string name="toast_habit_created">Habit created.</string>
|
||||
<string name="toast_habit_deleted">Habit deleted.</string>
|
||||
<string name="toast_habit_deleted">Habits deleted.</string>
|
||||
<string name="toast_habit_restored">Habits restored.</string>
|
||||
<string name="toast_nothing_to_undo">Nothing to undo.</string>
|
||||
<string name="toast_nothing_to_redo">Nothing to redo.</string>
|
||||
<string name="toast_habit_changed">Habit changed.</string>
|
||||
@@ -104,6 +105,8 @@
|
||||
<string name="hint_title">Did you know?</string>
|
||||
<string name="hint_drag">To rearrange the entries, press-and-hold on the name of the habit, then drag it.</string>
|
||||
<string name="hint_landscape">You can see more days by putting your phone in landscape mode.</string>
|
||||
<string name="delete_habits">Delete Habits</string>
|
||||
<string name="delete_habits_message">The habits will be permanently deleted. This action cannot be undone.</string>
|
||||
|
||||
<string-array name="hints">
|
||||
<item>@string/hint_drag</item>
|
||||
|
||||
Reference in New Issue
Block a user