Created apropriate classes to implement a reset confirm dialog

Created factory classes for the ConfirmResetDialog following the design patern of the ConfirmDeleteDialog class
pull/592/head
Febon 5 years ago
parent 74e44e9843
commit 9aa610271c

@ -0,0 +1,61 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker 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.
*
* Loop Habit Tracker 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.activities.common.dialogs;
import android.content.*;
import androidx.annotation.NonNull;
import androidx.appcompat.app.*;
import com.google.auto.factory.*;
import org.isoron.androidbase.activities.*;
import org.isoron.uhabits.R;
import org.isoron.uhabits.core.ui.callbacks.*;
import butterknife.*;
/**
* Dialog that asks the user confirmation before executing a delete operation.
*/
@AutoFactory(allowSubclasses = true)
public class ConfirmResetDialog extends AlertDialog
{
@BindString(R.string.reset_habits_message)
protected String question;
@BindString(android.R.string.yes)
protected String yes;
@BindString(android.R.string.no)
protected String no;
protected ConfirmResetDialog(@Provided @ActivityContext Context context,
@NonNull OnConfirmedCallback callback)
{
super(context);
ButterKnife.bind(this);
setTitle(R.string.reset_habits);
setMessage(question);
setButton(BUTTON_POSITIVE, yes, (dialog, which) -> callback.onConfirmed());
setButton(BUTTON_NEGATIVE, no, (dialog, which) -> {});
}
}

@ -60,12 +60,12 @@ class ListHabitsScreen
private val commandRunner: CommandRunner, private val commandRunner: CommandRunner,
private val intentFactory: IntentFactory, private val intentFactory: IntentFactory,
private val themeSwitcher: ThemeSwitcher, private val themeSwitcher: ThemeSwitcher,
private val preferences: Preferences,
private val adapter: HabitCardListAdapter, private val adapter: HabitCardListAdapter,
private val taskRunner: TaskRunner, private val taskRunner: TaskRunner,
private val exportDBFactory: ExportDBTaskFactory, private val exportDBFactory: ExportDBTaskFactory,
private val importTaskFactory: ImportDataTaskFactory, private val importTaskFactory: ImportDataTaskFactory,
private val confirmDeleteDialogFactory: ConfirmDeleteDialogFactory, private val confirmDeleteDialogFactory: ConfirmDeleteDialogFactory,
private val confirmResetDialogFactory: ConfirmResetDialogFactory,
private val colorPickerFactory: ColorPickerDialogFactory, private val colorPickerFactory: ColorPickerDialogFactory,
private val editHabitDialogFactory: EditHabitDialogFactory, private val editHabitDialogFactory: EditHabitDialogFactory,
private val numberPickerFactory: NumberPickerFactory, private val numberPickerFactory: NumberPickerFactory,
@ -120,14 +120,13 @@ class ListHabitsScreen
} }
private fun onSettingsResult(resultCode: Int) { private fun onSettingsResult(resultCode: Int) {
//val liist = habitList.getFiltered(HabitMatcher(true,true,true))
when (resultCode) { when (resultCode) {
RESULT_IMPORT_DATA -> showImportScreen() RESULT_IMPORT_DATA -> showImportScreen()
RESULT_EXPORT_CSV -> behavior.get().onExportCSV() RESULT_EXPORT_CSV -> behavior.get().onExportCSV()
RESULT_EXPORT_DB -> onExportDB() RESULT_EXPORT_DB -> onExportDB()
RESULT_BUG_REPORT -> behavior.get().onSendBugReport() RESULT_BUG_REPORT -> behavior.get().onSendBugReport()
RESULT_REPAIR_DB -> behavior.get().onRepairDB() RESULT_REPAIR_DB -> behavior.get().onRepairDB()
RESET_HABITS -> /*ResetHabitsCommand(liist, liist.toList()).execute()*/ behavior.get().onResetHabits() RESET_HABITS -> behavior.get().onResetHabits()
} }
} }
@ -156,6 +155,10 @@ class ListHabitsScreen
activity.showDialog(confirmDeleteDialogFactory.create(callback)) activity.showDialog(confirmDeleteDialogFactory.create(callback))
} }
override fun showResetConfirmationScreen(callback: OnConfirmedCallback) {
activity.showDialog(confirmResetDialogFactory.create(callback))
}
override fun showEditHabitsScreen(habits: List<Habit>) { override fun showEditHabitsScreen(habits: List<Habit>) {
val dialog = editHabitDialogFactory.edit(habits[0]) val dialog = editHabitDialogFactory.edit(habits[0])
activity.showDialog(dialog, "editNumericalHabit") activity.showDialog(dialog, "editNumericalHabit")

@ -270,7 +270,16 @@ public class HabitCardListAdapter
for (Habit h : habits) for (Habit h : habits)
cache.remove(h.getId()); cache.remove(h.getId());
} }
@Override
public void performReset(List<Habit> habits)
{
for (Habit h : habits)
while(h.getRepetitions().getTotalCount() != 0) {
Repetition rep = h.getRepetitions().getOldest();
h.getRepetitions().toggle(rep.getTimestamp());
h.getRepetitions().remove(rep);
}
}
/** /**
* Changes the order of habits on the adapter. * Changes the order of habits on the adapter.
* <p> * <p>

@ -106,6 +106,8 @@
<string name="hint_landscape">You can see more days by putting your phone in landscape mode.</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">Delete Habits</string>
<string name="delete_habits_message">The habits will be permanently deleted. This action cannot be undone.</string> <string name="delete_habits_message">The habits will be permanently deleted. This action cannot be undone.</string>
<string name="reset_habits">Reset Habits</string>
<string name="reset_habits_message">The habits will be permanently reset. This action cannot be undone.</string>
<string name="habit_not_found">Habit deleted / not found</string> <string name="habit_not_found">Habit deleted / not found</string>
<string name="weekends">Weekends</string> <string name="weekends">Weekends</string>
<string name="any_weekday">Monday to Friday</string> <string name="any_weekday">Monday to Friday</string>

@ -111,12 +111,14 @@ public class ListHabitsSelectionMenuBehavior
public void onResetHabits() public void onResetHabits()
{ {
//ResetHabitsCommand command = new ResetHabitsCommand(habitList, adapter.getSelected());
//command.execute();
List<Habit> selected = adapter.getSelected(); List<Habit> selected = adapter.getSelected();
commandRunner.execute(new ResetHabitsCommand(habitList, selected), screen.showResetConfirmationScreen(() ->
null); {
adapter.clearSelection(); adapter.performReset(selected);
commandRunner.execute(new ResetHabitsCommand(habitList, selected),
null);
adapter.clearSelection();
});
} }
public void onEditHabits() public void onEditHabits()
@ -138,6 +140,8 @@ public class ListHabitsSelectionMenuBehavior
List<Habit> getSelected(); List<Habit> getSelected();
void performRemove(List<Habit> selected); void performRemove(List<Habit> selected);
void performReset(List<Habit> selected);
} }
public interface Screen public interface Screen
@ -148,6 +152,9 @@ public class ListHabitsSelectionMenuBehavior
void showDeleteConfirmationScreen( void showDeleteConfirmationScreen(
@NonNull OnConfirmedCallback callback); @NonNull OnConfirmedCallback callback);
void showResetConfirmationScreen(
@NonNull OnConfirmedCallback callback);
void showEditHabitsScreen(@NonNull List<Habit> selected); void showEditHabitsScreen(@NonNull List<Habit> selected);
} }
} }

Loading…
Cancel
Save