Skip to content

Commit

Permalink
Added delete button to statistics screen
Browse files Browse the repository at this point in the history
  • Loading branch information
derebaba committed Nov 2, 2017
1 parent 7613e6e commit 1edd76a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.isoron.uhabits.activities.habits.edit.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.ui.screens.habits.show.*;
import org.isoron.uhabits.core.ui.callbacks.OnConfirmedCallback;

import javax.inject.*;

Expand All @@ -45,6 +46,9 @@ public class ShowHabitScreen extends BaseScreen
@NonNull
private final EditHabitDialogFactory editHabitDialogFactory;

@NonNull
private final ConfirmDeleteDialogFactory confirmDeleteDialogFactory;

private final Lazy<ShowHabitBehavior> behavior;

@Inject
Expand All @@ -53,10 +57,12 @@ public ShowHabitScreen(@NonNull BaseActivity activity,
@NonNull ShowHabitRootView view,
@NonNull ShowHabitsMenu menu,
@NonNull
EditHabitDialogFactory editHabitDialogFactory,
EditHabitDialogFactory editHabitDialogFactory,
@NonNull ConfirmDeleteDialogFactory confirmDeleteDialogFactory,
@NonNull Lazy<ShowHabitBehavior> behavior)
{
super(activity);
this.confirmDeleteDialogFactory = confirmDeleteDialogFactory;
setMenu(menu);
setRootView(view);

Expand Down Expand Up @@ -116,6 +122,18 @@ public void showMessage(ShowHabitMenuBehavior.Message m)
{
case COULD_NOT_EXPORT:
showMessage(R.string.could_not_export);
case HABIT_DELETED:
showMessage(R.string.delete_habits_message);
}
}

@Override
public void showDeleteConfirmationScreen(OnConfirmedCallback callback) {
activity.showDialog(confirmDeleteDialogFactory.create(callback));
}

@Override
public void endActivity() {
activity.finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public boolean onItemSelected(@NonNull MenuItem item)
behavior.get().onExportCSV();
return true;

case R.id.action_delete:
behavior.get().onDeleteHabit();
return true;

default:
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions uhabits-android/src/main/res/menu/show_habit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
android:title="@string/export"
app:showAsAction="never"/>

<item
android:id="@+id/action_delete"
android:title="@string/delete"
app:showAsAction="never"/>

<item
android:id="@+id/action_edit_habit"
android:icon="?iconEdit"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@

import android.support.annotation.*;

import org.isoron.uhabits.core.commands.CommandRunner;
import org.isoron.uhabits.core.commands.DeleteHabitsCommand;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.ui.callbacks.OnConfirmedCallback;

import java.io.*;
import java.util.*;
Expand All @@ -45,18 +48,23 @@ public class ShowHabitMenuBehavior
@NonNull
private System system;

@NonNull
private CommandRunner commandRunner;

@Inject
public ShowHabitMenuBehavior(@NonNull HabitList habitList,
@NonNull Habit habit,
@NonNull TaskRunner taskRunner,
@NonNull Screen screen,
@NonNull System system)
@NonNull System system,
@NonNull CommandRunner commandRunner)
{
this.habitList = habitList;
this.habit = habit;
this.taskRunner = taskRunner;
this.screen = screen;
this.system = system;
this.commandRunner = commandRunner;
}

public void onEditHabit()
Expand All @@ -77,9 +85,21 @@ public void onExportCSV()
}));
}

public void onDeleteHabit()
{
List<Habit> selected = Collections.singletonList(habit);

screen.showDeleteConfirmationScreen(() -> {
commandRunner.execute(new DeleteHabitsCommand(habitList, selected),
null);
screen.showMessage(Message.HABIT_DELETED);
screen.endActivity();
});
}

public enum Message
{
COULD_NOT_EXPORT
COULD_NOT_EXPORT, HABIT_DELETED
}

public interface Screen
Expand All @@ -89,6 +109,11 @@ public interface Screen
void showMessage(Message m);

void showSendFileScreen(String filename);

void showDeleteConfirmationScreen(
@NonNull OnConfirmedCallback callback);

void endActivity();
}

public interface System
Expand Down

0 comments on commit 1edd76a

Please sign in to comment.