Skip to content

Commit

Permalink
Remove DialogFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Jul 31, 2016
1 parent 5aa9114 commit 3d4ae21
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
dependencies = { AppComponent.class })
public interface ActivityComponent
{
DialogFactory getDialogFactory();
ColorPickerDialogFactory getColorPickerDialogFactory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,13 @@

package org.isoron.uhabits.activities.common.dialogs;

import android.content.*;

import org.isoron.uhabits.*;
import org.isoron.uhabits.utils.*;

/**
* Dialog that allows the user to choose a color.
*/
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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,36 @@ 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,
Bundle savedInstanceState)
{
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);

Expand All @@ -85,16 +96,6 @@ public View onCreateView(LayoutInflater inflater,
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)
{
Expand Down Expand Up @@ -196,7 +197,7 @@ void onWeekdayClick()
void showColorPicker()
{
int color = modifiedHabit.getColor();
ColorPickerDialog picker = dialogFactory.buildColorPicker(color);
ColorPickerDialog picker = colorPickerDialogFactory.create(color);

picker.setListener(c -> {
prefs.setDefaultHabitColor(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected void initializeHabits()
@Override
protected void saveHabit()
{
Command command = component
Command command = appComponent
.getCreateHabitCommandFactory()
.create(habitList, modifiedHabit);
commandRunner.execute(command, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -60,7 +45,7 @@ protected void initializeHabits()
@Override
protected void saveHabit()
{
Command command = component.getEditHabitCommandFactory().
Command command = appComponent.getEditHabitCommandFactory().
create(habitList, originalHabit, modifiedHabit);
commandRunner.execute(command, originalHabit.getId());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <[email protected]>
*
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@ActivityScope
@Component(modules = { ActivityModule.class },
dependencies = { AppComponent.class })
public interface ListHabitsComponent
public interface ListHabitsComponent extends ActivityComponent
{
CheckmarkButtonControllerFactory getCheckmarkButtonControllerFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
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.*;
Expand All @@ -53,9 +53,6 @@ public class ListHabitsScreen extends BaseScreen
@Nullable
private ListHabitsController controller;

@NonNull
private final DialogFactory dialogFactory;

@NonNull
private final IntentFactory intentFactory;

Expand All @@ -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;
Expand Down Expand Up @@ -160,8 +165,7 @@ public void showAboutScreen()
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");
}
Expand All @@ -178,7 +182,7 @@ public void showDeleteConfirmationScreen(ConfirmDeleteDialog.Callback callback)

public void showEditHabitScreen(Habit habit)
{
EditHabitDialog dialog = dialogFactory.buildEditHabitDialog(habit);
EditHabitDialog dialog = editHabitDialogFactory.create(habit);
activity.showDialog(dialog, "editHabit");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -70,7 +71,7 @@ public void reattachDialogs()

public void showEditHabitDialog()
{
EditHabitDialog dialog = dialogFactory.buildEditHabitDialog(habit);
EditHabitDialog dialog = editHabitDialogFactory.create(habit);
activity.showDialog(dialog, "editHabit");
}

Expand Down
Loading

0 comments on commit 3d4ae21

Please sign in to comment.