mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Remove DialogFactory
This commit is contained in:
@@ -29,5 +29,5 @@ import dagger.*;
|
||||
dependencies = { AppComponent.class })
|
||||
public interface ActivityComponent
|
||||
{
|
||||
DialogFactory getDialogFactory();
|
||||
ColorPickerDialogFactory getColorPickerDialogFactory();
|
||||
}
|
||||
|
||||
@@ -19,9 +19,6 @@
|
||||
|
||||
package org.isoron.uhabits.activities.common.dialogs;
|
||||
|
||||
import android.content.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
/**
|
||||
@@ -29,20 +26,6 @@ import org.isoron.uhabits.utils.*;
|
||||
*/
|
||||
public class ColorPickerDialog extends com.android.colorpicker.ColorPickerDialog
|
||||
{
|
||||
public static ColorPickerDialog newInstance(Context context,
|
||||
int paletteColor)
|
||||
{
|
||||
ColorPickerDialog dialog = new ColorPickerDialog();
|
||||
StyledResources res = new StyledResources(context);
|
||||
|
||||
int color = ColorUtils.getColor(context, paletteColor);
|
||||
|
||||
dialog.initialize(R.string.color_picker_default_title, res.getPalette(),
|
||||
color, 4, com.android.colorpicker.ColorPickerDialog.SIZE_SMALL);
|
||||
|
||||
return dialog;
|
||||
}
|
||||
|
||||
public void setListener(OnColorSelectedListener listener)
|
||||
{
|
||||
super.setOnColorSelectedListener(c -> {
|
||||
|
||||
@@ -20,33 +20,33 @@
|
||||
package org.isoron.uhabits.activities.common.dialogs;
|
||||
|
||||
import android.content.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.activities.*;
|
||||
import org.isoron.uhabits.activities.habits.edit.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
public class DialogFactory
|
||||
@ActivityScope
|
||||
public class ColorPickerDialogFactory
|
||||
{
|
||||
private final Context context;
|
||||
|
||||
@Inject
|
||||
public DialogFactory(@ActivityContext Context context)
|
||||
public ColorPickerDialogFactory(@ActivityContext Context context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public ColorPickerDialog buildColorPicker(int paletteColor)
|
||||
public ColorPickerDialog create(int paletteColor)
|
||||
{
|
||||
return ColorPickerDialog.newInstance(context, paletteColor);
|
||||
}
|
||||
ColorPickerDialog dialog = new ColorPickerDialog();
|
||||
StyledResources res = new StyledResources(context);
|
||||
int color = ColorUtils.getColor(context, paletteColor);
|
||||
|
||||
@NonNull
|
||||
public EditHabitDialog buildEditHabitDialog(Habit habit)
|
||||
{
|
||||
return EditHabitDialog.newInstance(habit);
|
||||
dialog.initialize(R.string.color_picker_default_title, res.getPalette(),
|
||||
color, 4, com.android.colorpicker.ColorPickerDialog.SIZE_SMALL);
|
||||
|
||||
return dialog;
|
||||
}
|
||||
}
|
||||
@@ -55,12 +55,22 @@ public abstract class BaseDialog extends AppCompatDialogFragment
|
||||
|
||||
protected HabitList habitList;
|
||||
|
||||
private DialogFactory dialogFactory;
|
||||
|
||||
protected AppComponent component;
|
||||
protected AppComponent appComponent;
|
||||
|
||||
protected ModelFactory modelFactory;
|
||||
|
||||
private ColorPickerDialogFactory colorPickerDialogFactory;
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState)
|
||||
{
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
||||
BaseActivity activity = (BaseActivity) getActivity();
|
||||
colorPickerDialogFactory =
|
||||
activity.getComponent().getColorPickerDialogFactory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater,
|
||||
ViewGroup container,
|
||||
@@ -68,12 +78,13 @@ public abstract class BaseDialog extends AppCompatDialogFragment
|
||||
{
|
||||
View view = inflater.inflate(R.layout.edit_habit, container, false);
|
||||
|
||||
HabitsApplication app = (HabitsApplication) getContext().getApplicationContext();
|
||||
component = app.getComponent();
|
||||
prefs = component.getPreferences();
|
||||
habitList = component.getHabitList();
|
||||
commandRunner = component.getCommandRunner();
|
||||
modelFactory = component.getModelFactory();
|
||||
HabitsApplication app =
|
||||
(HabitsApplication) getContext().getApplicationContext();
|
||||
|
||||
prefs = app.getComponent().getPreferences();
|
||||
habitList = app.getComponent().getHabitList();
|
||||
commandRunner = app.getComponent().getCommandRunner();
|
||||
modelFactory = app.getComponent().getModelFactory();
|
||||
|
||||
ButterKnife.bind(this, view);
|
||||
|
||||
@@ -85,16 +96,6 @@ public abstract class BaseDialog extends AppCompatDialogFragment
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState)
|
||||
{
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
||||
BaseActivity baseActivity = (BaseActivity) getActivity();
|
||||
ActivityComponent component = baseActivity.getComponent();
|
||||
dialogFactory = component.getDialogFactory();
|
||||
}
|
||||
|
||||
@OnItemSelected(R.id.sFrequency)
|
||||
public void onFrequencySelected(int position)
|
||||
{
|
||||
@@ -196,7 +197,7 @@ public abstract class BaseDialog extends AppCompatDialogFragment
|
||||
void showColorPicker()
|
||||
{
|
||||
int color = modifiedHabit.getColor();
|
||||
ColorPickerDialog picker = dialogFactory.buildColorPicker(color);
|
||||
ColorPickerDialog picker = colorPickerDialogFactory.create(color);
|
||||
|
||||
picker.setListener(c -> {
|
||||
prefs.setDefaultHabitColor(c);
|
||||
|
||||
@@ -46,7 +46,7 @@ public class CreateHabitDialog extends BaseDialog
|
||||
@Override
|
||||
protected void saveHabit()
|
||||
{
|
||||
Command command = component
|
||||
Command command = appComponent
|
||||
.getCreateHabitCommandFactory()
|
||||
.create(habitList, modifiedHabit);
|
||||
commandRunner.execute(command, null);
|
||||
|
||||
@@ -19,26 +19,11 @@
|
||||
|
||||
package org.isoron.uhabits.activities.habits.edit;
|
||||
|
||||
import android.os.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
|
||||
public class EditHabitDialog extends BaseDialog
|
||||
{
|
||||
public static EditHabitDialog newInstance(Habit habit)
|
||||
{
|
||||
if (habit.getId() == null)
|
||||
throw new IllegalArgumentException("habit not saved");
|
||||
|
||||
EditHabitDialog frag = new EditHabitDialog();
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("habitId", habit.getId());
|
||||
frag.setArguments(args);
|
||||
return frag;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getTitle()
|
||||
{
|
||||
@@ -60,7 +45,7 @@ public class EditHabitDialog extends BaseDialog
|
||||
@Override
|
||||
protected void saveHabit()
|
||||
{
|
||||
Command command = component.getEditHabitCommandFactory().
|
||||
Command command = appComponent.getEditHabitCommandFactory().
|
||||
create(habitList, originalHabit, modifiedHabit);
|
||||
commandRunner.execute(command, originalHabit.getId());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.habits.edit;
|
||||
|
||||
import android.os.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.models.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
public class EditHabitDialogFactory
|
||||
{
|
||||
@Inject
|
||||
public EditHabitDialogFactory()
|
||||
{
|
||||
}
|
||||
|
||||
public EditHabitDialog create(@NonNull Habit habit)
|
||||
{
|
||||
if (habit.getId() == null)
|
||||
throw new IllegalArgumentException("habit not saved");
|
||||
|
||||
EditHabitDialog dialog = new EditHabitDialog();
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("habitId", habit.getId());
|
||||
dialog.setArguments(args);
|
||||
return dialog;
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ import dagger.*;
|
||||
@ActivityScope
|
||||
@Component(modules = { ActivityModule.class },
|
||||
dependencies = { AppComponent.class })
|
||||
public interface ListHabitsComponent
|
||||
public interface ListHabitsComponent extends ActivityComponent
|
||||
{
|
||||
CheckmarkButtonControllerFactory getCheckmarkButtonControllerFactory();
|
||||
|
||||
|
||||
@@ -24,14 +24,14 @@ import android.os.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.intents.*;
|
||||
import org.isoron.uhabits.io.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.activities.*;
|
||||
import org.isoron.uhabits.activities.common.dialogs.*;
|
||||
import org.isoron.uhabits.activities.common.dialogs.ColorPickerDialog.*;
|
||||
import org.isoron.uhabits.activities.habits.edit.*;
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.intents.*;
|
||||
import org.isoron.uhabits.io.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import java.io.*;
|
||||
@@ -53,9 +53,6 @@ public class ListHabitsScreen extends BaseScreen
|
||||
@Nullable
|
||||
private ListHabitsController controller;
|
||||
|
||||
@NonNull
|
||||
private final DialogFactory dialogFactory;
|
||||
|
||||
@NonNull
|
||||
private final IntentFactory intentFactory;
|
||||
|
||||
@@ -74,23 +71,31 @@ public class ListHabitsScreen extends BaseScreen
|
||||
@NonNull
|
||||
private final FilePickerDialogFactory filePickerDialogFactory;
|
||||
|
||||
@NonNull
|
||||
private final ColorPickerDialogFactory colorPickerFactory;
|
||||
|
||||
@NonNull
|
||||
private EditHabitDialogFactory editHabitDialogFactory;
|
||||
|
||||
@Inject
|
||||
public ListHabitsScreen(@NonNull BaseActivity activity,
|
||||
@NonNull CommandRunner commandRunner,
|
||||
@NonNull DirFinder dirFinder,
|
||||
@NonNull DialogFactory dialogFactory,
|
||||
@NonNull ListHabitsRootView rootView,
|
||||
@NonNull IntentFactory intentFactory,
|
||||
@NonNull ConfirmDeleteDialogFactory confirmDeleteDialogFactory,
|
||||
@NonNull CreateHabitDialogFactory createHabitDialogFactory,
|
||||
@NonNull FilePickerDialogFactory filePickerDialogFactory)
|
||||
@NonNull FilePickerDialogFactory filePickerDialogFactory,
|
||||
@NonNull ColorPickerDialogFactory colorPickerFactory,
|
||||
@NonNull EditHabitDialogFactory editHabitDialogFactory)
|
||||
{
|
||||
super(activity);
|
||||
setRootView(rootView);
|
||||
this.editHabitDialogFactory = editHabitDialogFactory;
|
||||
this.colorPickerFactory = colorPickerFactory;
|
||||
this.commandRunner = commandRunner;
|
||||
this.confirmDeleteDialogFactory = confirmDeleteDialogFactory;
|
||||
this.createHabitDialogFactory = createHabitDialogFactory;
|
||||
this.dialogFactory = dialogFactory;
|
||||
this.dirFinder = dirFinder;
|
||||
this.filePickerDialogFactory = filePickerDialogFactory;
|
||||
this.intentFactory = intentFactory;
|
||||
@@ -160,8 +165,7 @@ public class ListHabitsScreen extends BaseScreen
|
||||
public void showColorPicker(@NonNull Habit habit,
|
||||
@NonNull OnColorSelectedListener callback)
|
||||
{
|
||||
ColorPickerDialog picker =
|
||||
dialogFactory.buildColorPicker(habit.getColor());
|
||||
ColorPickerDialog picker = colorPickerFactory.create(habit.getColor());
|
||||
picker.setListener(callback);
|
||||
activity.showDialog(picker, "picker");
|
||||
}
|
||||
@@ -178,7 +182,7 @@ public class ListHabitsScreen extends BaseScreen
|
||||
|
||||
public void showEditHabitScreen(Habit habit)
|
||||
{
|
||||
EditHabitDialog dialog = dialogFactory.buildEditHabitDialog(habit);
|
||||
EditHabitDialog dialog = editHabitDialogFactory.create(habit);
|
||||
activity.showDialog(dialog, "editHabit");
|
||||
}
|
||||
|
||||
|
||||
@@ -34,21 +34,22 @@ public class ShowHabitScreen extends BaseScreen
|
||||
@NonNull
|
||||
private final Habit habit;
|
||||
|
||||
private DialogFactory dialogFactory;
|
||||
|
||||
@Nullable
|
||||
private ShowHabitController controller;
|
||||
|
||||
@NonNull
|
||||
private final EditHabitDialogFactory editHabitDialogFactory;
|
||||
|
||||
@Inject
|
||||
public ShowHabitScreen(@NonNull BaseActivity activity,
|
||||
@NonNull Habit habit,
|
||||
@NonNull ShowHabitRootView view,
|
||||
@NonNull DialogFactory dialogFactory)
|
||||
@NonNull EditHabitDialogFactory editHabitDialogFactory)
|
||||
{
|
||||
super(activity);
|
||||
setRootView(view);
|
||||
this.editHabitDialogFactory = editHabitDialogFactory;
|
||||
this.habit = habit;
|
||||
this.dialogFactory = dialogFactory;
|
||||
}
|
||||
|
||||
public void setController(@NonNull ShowHabitController controller)
|
||||
@@ -70,7 +71,7 @@ public class ShowHabitScreen extends BaseScreen
|
||||
|
||||
public void showEditHabitDialog()
|
||||
{
|
||||
EditHabitDialog dialog = dialogFactory.buildEditHabitDialog(habit);
|
||||
EditHabitDialog dialog = editHabitDialogFactory.create(habit);
|
||||
activity.showDialog(dialog, "editHabit");
|
||||
}
|
||||
|
||||
|
||||
@@ -24,14 +24,14 @@ import android.content.*;
|
||||
import android.support.v7.app.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.intents.*;
|
||||
import org.isoron.uhabits.io.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.activities.*;
|
||||
import org.isoron.uhabits.activities.common.dialogs.*;
|
||||
import org.isoron.uhabits.activities.common.dialogs.ColorPickerDialog.*;
|
||||
import org.isoron.uhabits.activities.habits.edit.*;
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.intents.*;
|
||||
import org.isoron.uhabits.io.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.*;
|
||||
import org.junit.runners.*;
|
||||
@@ -66,12 +66,14 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
|
||||
private IntentFactory intentFactory;
|
||||
|
||||
private DialogFactory dialogFactory;
|
||||
|
||||
private DirFinder dirFinder;
|
||||
|
||||
private CommandRunner commandRunner;
|
||||
|
||||
private ColorPickerDialogFactory colorPickerDialogFactory;
|
||||
|
||||
private EditHabitDialogFactory editHabitDialogFactory;
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp()
|
||||
@@ -81,16 +83,19 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
activity = mock(BaseActivity.class);
|
||||
commandRunner = mock(CommandRunner.class);
|
||||
dirFinder = mock(DirFinder.class);
|
||||
dialogFactory = mock(DialogFactory.class);
|
||||
rootView = mock(ListHabitsRootView.class);
|
||||
intentFactory = mock(IntentFactory.class);
|
||||
confirmDeleteDialogFactory = mock(ConfirmDeleteDialogFactory.class);
|
||||
createHabitDialogFactory = mock(CreateHabitDialogFactory.class);
|
||||
filePickerDialogFactory = mock(FilePickerDialogFactory.class);
|
||||
colorPickerDialogFactory = mock(ColorPickerDialogFactory.class);
|
||||
editHabitDialogFactory = mock(EditHabitDialogFactory.class);
|
||||
|
||||
screen = new ListHabitsScreen(activity, commandRunner, dirFinder,
|
||||
dialogFactory, rootView, intentFactory, confirmDeleteDialogFactory,
|
||||
createHabitDialogFactory, filePickerDialogFactory);
|
||||
screen =
|
||||
new ListHabitsScreen(activity, commandRunner, dirFinder, rootView,
|
||||
intentFactory, confirmDeleteDialogFactory,
|
||||
createHabitDialogFactory, filePickerDialogFactory,
|
||||
colorPickerDialogFactory, editHabitDialogFactory);
|
||||
|
||||
controller = mock(ListHabitsController.class);
|
||||
screen.setController(controller);
|
||||
@@ -151,7 +156,7 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
{
|
||||
habit.setColor(999);
|
||||
ColorPickerDialog picker = mock(ColorPickerDialog.class);
|
||||
when(dialogFactory.buildColorPicker(999)).thenReturn(picker);
|
||||
when(colorPickerDialogFactory.create(999)).thenReturn(picker);
|
||||
OnColorSelectedListener callback = mock(OnColorSelectedListener.class);
|
||||
|
||||
screen.showColorPicker(habit, callback);
|
||||
@@ -178,7 +183,7 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
public void testShowEditHabitScreen()
|
||||
{
|
||||
EditHabitDialog dialog = mock(EditHabitDialog.class);
|
||||
when(dialogFactory.buildEditHabitDialog(habit)).thenReturn(dialog);
|
||||
when(editHabitDialogFactory.create(habit)).thenReturn(dialog);
|
||||
|
||||
screen.showEditHabitScreen(habit);
|
||||
verify(activity).showDialog(eq(dialog), any());
|
||||
|
||||
Reference in New Issue
Block a user