Unify habit creation dialogs

pull/157/merge
Alinson S. Xavier 9 years ago
parent 7de69c1c10
commit 103c0b57f8

@ -1,56 +0,0 @@
/*
* 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 com.google.auto.factory.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.*;
@AutoFactory(allowSubclasses = true)
public class CreateNumericalHabitDialog extends NumericalHabitDialog
{
@Override
protected int getTitle()
{
return R.string.create_habit;
}
@Override
protected void initializeHabits()
{
modifiedHabit = modelFactory.buildHabit();
modifiedHabit.setFrequency(Frequency.DAILY);
modifiedHabit.setColor(
prefs.getDefaultHabitColor(modifiedHabit.getColor()));
modifiedHabit.setType(Habit.NUMBER_HABIT);
modifiedHabit.setTargetValue(100);
}
@Override
protected void saveHabit()
{
Command command = appComponent
.getCreateHabitCommandFactory()
.create(habitList, modifiedHabit);
commandRunner.execute(command, null);
}
}

@ -39,10 +39,14 @@ import org.isoron.uhabits.preferences.*;
import butterknife.*; import butterknife.*;
public class BooleanHabitDialog extends AppCompatDialogFragment import static android.view.View.*;
public class EditHabitDialog extends AppCompatDialogFragment
{ {
public static final String BUNDLE_HABIT_ID = "habitId"; public static final String BUNDLE_HABIT_ID = "habitId";
public static final String BUNDLE_HABIT_TYPE = "habitType";
protected Habit originalHabit; protected Habit originalHabit;
protected Preferences prefs; protected Preferences prefs;
@ -64,6 +68,9 @@ public class BooleanHabitDialog extends AppCompatDialogFragment
@BindView(R.id.frequencyPanel) @BindView(R.id.frequencyPanel)
FrequencyPanel frequencyPanel; FrequencyPanel frequencyPanel;
@BindView(R.id.targetPanel)
TargetPanel targetPanel;
private ColorPickerDialogFactory colorPickerDialogFactory; private ColorPickerDialogFactory colorPickerDialogFactory;
@Override @Override
@ -88,7 +95,7 @@ public class BooleanHabitDialog extends AppCompatDialogFragment
Bundle savedInstanceState) Bundle savedInstanceState)
{ {
View view; View view;
view = inflater.inflate(R.layout.edit_boolean_habit, container, false); view = inflater.inflate(R.layout.edit_habit, container, false);
initDependencies(); initDependencies();
ButterKnife.bind(this, view); ButterKnife.bind(this, view);
@ -124,6 +131,11 @@ public class BooleanHabitDialog extends AppCompatDialogFragment
} }
} }
private int getTypeFromArguments()
{
return getArguments().getInt(BUNDLE_HABIT_TYPE);
}
private void initDependencies() private void initDependencies()
{ {
Context appContext = getContext().getApplicationContext(); Context appContext = getContext().getApplicationContext();
@ -145,8 +157,11 @@ public class BooleanHabitDialog extends AppCompatDialogFragment
@OnClick(R.id.buttonSave) @OnClick(R.id.buttonSave)
void onSaveButtonClick() void onSaveButtonClick()
{ {
int type = getTypeFromArguments();
if (!namePanel.validate()) return; if (!namePanel.validate()) return;
if (!frequencyPanel.validate()) return; if (type == Habit.YES_NO_HABIT && !frequencyPanel.validate()) return;
if (type == Habit.NUMBER_HABIT && !targetPanel.validate()) return;
Habit habit = modelFactory.buildHabit(); Habit habit = modelFactory.buildHabit();
habit.setName(namePanel.getName()); habit.setName(namePanel.getName());
@ -154,6 +169,9 @@ public class BooleanHabitDialog extends AppCompatDialogFragment
habit.setColor(namePanel.getColor()); habit.setColor(namePanel.getColor());
habit.setReminder(reminderPanel.getReminder()); habit.setReminder(reminderPanel.getReminder());
habit.setFrequency(frequencyPanel.getFrequency()); habit.setFrequency(frequencyPanel.getFrequency());
habit.setUnit(targetPanel.getUnit());
habit.setTargetValue(targetPanel.getTargetValue());
habit.setType(type);
saveHabit(habit); saveHabit(habit);
dismiss(); dismiss();
@ -179,11 +197,17 @@ public class BooleanHabitDialog extends AppCompatDialogFragment
Habit habit = modelFactory.buildHabit(); Habit habit = modelFactory.buildHabit();
habit.setFrequency(Frequency.DAILY); habit.setFrequency(Frequency.DAILY);
habit.setColor(prefs.getDefaultHabitColor(habit.getColor())); habit.setColor(prefs.getDefaultHabitColor(habit.getColor()));
habit.setType(getTypeFromArguments());
if (originalHabit != null) habit.copyFrom(originalHabit); if (originalHabit != null) habit.copyFrom(originalHabit);
if (habit.isNumerical()) frequencyPanel.setVisibility(GONE);
else targetPanel.setVisibility(GONE);
namePanel.populateFrom(habit); namePanel.populateFrom(habit);
frequencyPanel.setFrequency(habit.getFrequency()); frequencyPanel.setFrequency(habit.getFrequency());
targetPanel.setTargetValue(habit.getTargetValue());
targetPanel.setUnit(habit.getUnit());
if (habit.hasReminder()) reminderPanel.setReminder(habit.getReminder()); if (habit.hasReminder()) reminderPanel.setReminder(habit.getReminder());
} }

@ -26,28 +26,42 @@ import org.isoron.uhabits.models.*;
import javax.inject.*; import javax.inject.*;
import static org.isoron.uhabits.activities.habits.edit.BooleanHabitDialog.*; import static org.isoron.uhabits.activities.habits.edit.EditHabitDialog.*;
public class BooleanHabitDialogFactory public class EditHabitDialogFactory
{ {
@Inject @Inject
public BooleanHabitDialogFactory() public EditHabitDialogFactory()
{ {
} }
public BooleanHabitDialog create() public EditHabitDialog createBoolean()
{ {
return new BooleanHabitDialog(); EditHabitDialog dialog = new EditHabitDialog();
Bundle args = new Bundle();
args.putInt(BUNDLE_HABIT_TYPE, Habit.YES_NO_HABIT);
dialog.setArguments(args);
return dialog;
}
public EditHabitDialog createNumerical()
{
EditHabitDialog dialog = new EditHabitDialog();
Bundle args = new Bundle();
args.putInt(BUNDLE_HABIT_TYPE, Habit.NUMBER_HABIT);
dialog.setArguments(args);
return dialog;
} }
public BooleanHabitDialog edit(@NonNull Habit habit) public EditHabitDialog edit(@NonNull Habit habit)
{ {
if (habit.getId() == null) if (habit.getId() == null)
throw new IllegalArgumentException("habit not saved"); throw new IllegalArgumentException("habit not saved");
BooleanHabitDialog dialog = new BooleanHabitDialog(); EditHabitDialog dialog = new EditHabitDialog();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong(BUNDLE_HABIT_ID, habit.getId()); args.putLong(BUNDLE_HABIT_ID, habit.getId());
args.putInt(BUNDLE_HABIT_TYPE, habit.getType());
dialog.setArguments(args); dialog.setArguments(args);
return dialog; return dialog;
} }

@ -1,55 +0,0 @@
/*
* 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 org.isoron.uhabits.*;
import org.isoron.uhabits.commands.*;
public class EditNumericalHabitDialog extends NumericalHabitDialog
{
@Override
protected int getTitle()
{
return R.string.edit_habit;
}
@Override
protected void initializeHabits()
{
Long habitId = (Long) getArguments().get("habitId");
if (habitId == null)
throw new IllegalArgumentException("habitId must be specified");
originalHabit = habitList.getById(habitId);
if (originalHabit == null)
throw new IllegalStateException("habit is null");
modifiedHabit = modelFactory.buildHabit();
modifiedHabit.copyFrom(originalHabit);
}
@Override
protected void saveHabit()
{
Command command = appComponent.getEditHabitCommandFactory().
create(habitList, originalHabit, modifiedHabit);
commandRunner.execute(command, originalHabit.getId());
}
}

@ -1,47 +0,0 @@
/*
* 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 EditNumericalHabitDialogFactory
{
@Inject
public EditNumericalHabitDialogFactory()
{
}
public EditNumericalHabitDialog create(@NonNull Habit habit)
{
if (habit.getId() == null)
throw new IllegalArgumentException("habit not saved");
EditNumericalHabitDialog dialog = new EditNumericalHabitDialog();
Bundle args = new Bundle();
args.putLong("habitId", habit.getId());
dialog.setArguments(args);
return dialog;
}
}

@ -1,158 +0,0 @@
/*
* 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 android.support.v7.app.*;
import android.view.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.R;
import org.isoron.uhabits.activities.*;
import org.isoron.uhabits.activities.common.dialogs.*;
import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.preferences.*;
import butterknife.*;
public abstract class NumericalHabitDialog extends AppCompatDialogFragment
{
@Nullable
protected Habit originalHabit;
@Nullable
protected Habit modifiedHabit;
protected Preferences prefs;
protected CommandRunner commandRunner;
protected HabitList habitList;
protected AppComponent appComponent;
protected ModelFactory modelFactory;
private ColorPickerDialogFactory colorPickerDialogFactory;
private NumericalHabitDialogHelper helper;
@Override
public int getTheme()
{
return R.style.DialogWithTitle;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
BaseActivity activity = (BaseActivity) getActivity();
ActivityComponent component = activity.getComponent();
colorPickerDialogFactory = component.getColorPickerDialogFactory();
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View view =
inflater.inflate(R.layout.edit_numerical_habit, container, false);
HabitsApplication app =
(HabitsApplication) getContext().getApplicationContext();
appComponent = app.getComponent();
prefs = appComponent.getPreferences();
habitList = appComponent.getHabitList();
commandRunner = appComponent.getCommandRunner();
modelFactory = appComponent.getModelFactory();
ButterKnife.bind(this, view);
helper = new NumericalHabitDialogHelper(this, view);
getDialog().setTitle(getTitle());
initializeHabits();
restoreSavedInstance(savedInstanceState);
helper.populateForm(modifiedHabit);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putInt("color", modifiedHabit.getColor());
}
protected abstract int getTitle();
protected abstract void initializeHabits();
protected void restoreSavedInstance(@Nullable Bundle bundle)
{
if (bundle == null) return;
if (modifiedHabit == null) return;
int color = bundle.getInt("color", modifiedHabit.getColor());
modifiedHabit.setColor(color);
modifiedHabit.setReminder(null);
}
protected abstract void saveHabit();
@OnClick(R.id.buttonDiscard)
void onButtonDiscardClick()
{
dismiss();
}
@OnClick(R.id.buttonSave)
void onSaveButtonClick()
{
helper.parseForm(modifiedHabit);
if (!helper.validate(modifiedHabit)) return;
saveHabit();
dismiss();
}
@OnClick(R.id.buttonPickColor)
void showColorPicker()
{
if (modifiedHabit == null) return;
int color = modifiedHabit.getColor();
ColorPickerDialog picker = colorPickerDialogFactory.create(color);
picker.setListener(c ->
{
prefs.setDefaultHabitColor(c);
modifiedHabit.setColor(c);
helper.populateColor(c);
});
picker.show(getFragmentManager(), "picker");
}
}

@ -1,121 +0,0 @@
/*
* 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.icu.text.*;
import android.support.v4.app.*;
import android.view.*;
import android.widget.*;
import org.isoron.uhabits.R;
import org.isoron.uhabits.activities.habits.edit.views.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.utils.*;
import butterknife.*;
public class NumericalHabitDialogHelper
{
private DialogFragment frag;
private DecimalFormat valueFormatter = new DecimalFormat("#.##");
@BindView(R.id.tvName)
TextView tvName;
@BindView(R.id.tvDescription)
ExampleEditText tvDescription;
@BindView(R.id.tvUnit)
ExampleEditText tvUnit;
@BindView(R.id.tvTargetCount)
TextView tvTargetValue;
@BindView(R.id.tvTargetType)
Spinner tvTargetType;
public NumericalHabitDialogHelper(DialogFragment frag, View view)
{
this.frag = frag;
ButterKnife.bind(this, view);
}
public void parseForm(Habit habit)
{
habit.setName(tvName.getText().toString().trim());
habit.setDescription(tvDescription.getRealText().trim());
habit.setTargetType(tvTargetType.getSelectedItemPosition());
habit.setTargetValue(
Double.parseDouble(tvTargetValue.getText().toString()));
habit.setUnit(tvUnit.getRealText().trim());
}
public void populateColor(int paletteColor)
{
tvName.setTextColor(
ColorUtils.getColor(frag.getContext(), paletteColor));
}
public void populateForm(final Habit habit)
{
tvName.setText(habit.getName());
if(!habit.getDescription().isEmpty())
tvDescription.setRealText(habit.getDescription());
if(!habit.getUnit().isEmpty())
tvUnit.setRealText(habit.getUnit());
tvTargetType.setSelection(habit.getTargetType());
tvTargetValue.setText(valueFormatter.format(habit.getTargetValue()));
populateColor(habit.getColor());
}
public boolean validate(Habit habit)
{
Boolean valid = true;
if (!validateName(habit)) valid = false;
if (!validateTargetValue()) valid = false;
return valid;
}
private boolean validateTargetValue()
{
double value = Double.parseDouble(tvTargetValue.getText().toString());
if(value <= 0)
{
tvTargetValue.setError(frag.getString(R.string.validation_number_should_be_positive));
return false;
}
return true;
}
private Boolean validateName(Habit habit)
{
if (habit.getName().isEmpty())
{
tvName.setError(frag.getString(R.string.validation_name_should_not_be_blank));
return false;
}
return true;
}
}

@ -0,0 +1,90 @@
/*
* 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.views;
import android.content.*;
import android.content.res.*;
import android.icu.text.*;
import android.support.annotation.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import org.isoron.uhabits.R;
import butterknife.*;
public class TargetPanel extends FrameLayout
{
private DecimalFormat valueFormatter = new DecimalFormat("#.##");
@BindView(R.id.tvUnit)
ExampleEditText tvUnit;
@BindView(R.id.tvTargetCount)
TextView tvTargetValue;
public TargetPanel(@NonNull Context context, @Nullable AttributeSet attrs)
{
super(context, attrs);
View view = inflate(context, R.layout.edit_habit_target, null);
ButterKnife.bind(this, view);
addView(view);
}
public double getTargetValue()
{
String sValue = tvTargetValue.getText().toString();
return Double.parseDouble(sValue);
}
public void setTargetValue(double targetValue)
{
tvTargetValue.setText(valueFormatter.format(targetValue));
}
public String getUnit()
{
return tvUnit.getRealText();
}
public void setUnit(String unit)
{
tvUnit.setRealText(unit);
}
public boolean validate()
{
Resources res = getResources();
String sValue = tvTargetValue.getText().toString();
double value = Double.parseDouble(sValue);
if (value <= 0)
{
tvTargetValue.setError(
res.getString(R.string.validation_number_should_be_positive));
return false;
}
return true;
}
}

@ -89,17 +89,11 @@ public class ListHabitsScreen extends BaseScreen
private final ColorPickerDialogFactory colorPickerFactory; private final ColorPickerDialogFactory colorPickerFactory;
@NonNull @NonNull
private final BooleanHabitDialogFactory booleanHabitDialogFactory; private final EditHabitDialogFactory editHabitDialogFactory;
@NonNull @NonNull
private final ThemeSwitcher themeSwitcher; private final ThemeSwitcher themeSwitcher;
@NonNull
private CreateNumericalHabitDialogFactory createNumericalHabitDialogFactory;
@NonNull
private EditNumericalHabitDialogFactory editNumericalHabitDialogFactory;
@Inject @Inject
public ListHabitsScreen(@NonNull BaseActivity activity, public ListHabitsScreen(@NonNull BaseActivity activity,
@NonNull CommandRunner commandRunner, @NonNull CommandRunner commandRunner,
@ -110,18 +104,14 @@ public class ListHabitsScreen extends BaseScreen
@NonNull ConfirmDeleteDialogFactory confirmDeleteDialogFactory, @NonNull ConfirmDeleteDialogFactory confirmDeleteDialogFactory,
@NonNull FilePickerDialogFactory filePickerDialogFactory, @NonNull FilePickerDialogFactory filePickerDialogFactory,
@NonNull ColorPickerDialogFactory colorPickerFactory, @NonNull ColorPickerDialogFactory colorPickerFactory,
@NonNull BooleanHabitDialogFactory booleanHabitDialogFactory, @NonNull EditHabitDialogFactory editHabitDialogFactory)
@NonNull EditNumericalHabitDialogFactory editNumericalHabitDialogFactory,
@NonNull CreateNumericalHabitDialogFactory createNumericalHabitDialogFactory)
{ {
super(activity); super(activity);
setRootView(rootView); setRootView(rootView);
this.colorPickerFactory = colorPickerFactory; this.colorPickerFactory = colorPickerFactory;
this.commandRunner = commandRunner; this.commandRunner = commandRunner;
this.confirmDeleteDialogFactory = confirmDeleteDialogFactory; this.confirmDeleteDialogFactory = confirmDeleteDialogFactory;
this.createNumericalHabitDialogFactory = createNumericalHabitDialogFactory; this.editHabitDialogFactory = editHabitDialogFactory;
this.booleanHabitDialogFactory = booleanHabitDialogFactory;
this.editNumericalHabitDialogFactory = editNumericalHabitDialogFactory;
this.dirFinder = dirFinder; this.dirFinder = dirFinder;
this.filePickerDialogFactory = filePickerDialogFactory; this.filePickerDialogFactory = filePickerDialogFactory;
this.intentFactory = intentFactory; this.intentFactory = intentFactory;
@ -196,15 +186,15 @@ public class ListHabitsScreen extends BaseScreen
private void showCreateNumericalHabitScreen() private void showCreateNumericalHabitScreen()
{ {
CreateNumericalHabitDialog dialog; EditHabitDialog dialog;
dialog = createNumericalHabitDialogFactory.create(); dialog = editHabitDialogFactory.createNumerical();
activity.showDialog(dialog, "editHabit"); activity.showDialog(dialog, "editHabit");
} }
public void showCreateBooleanHabitScreen() public void showCreateBooleanHabitScreen()
{ {
BooleanHabitDialog dialog; EditHabitDialog dialog;
dialog = booleanHabitDialogFactory.create(); dialog = editHabitDialogFactory.createBoolean();
activity.showDialog(dialog, "editHabit"); activity.showDialog(dialog, "editHabit");
} }
@ -215,19 +205,10 @@ public class ListHabitsScreen extends BaseScreen
public void showEditHabitScreen(Habit habit) public void showEditHabitScreen(Habit habit)
{ {
if(habit.isNumerical()) EditHabitDialog dialog;
{ dialog = editHabitDialogFactory.edit(habit);
EditNumericalHabitDialog dialog;
dialog = editNumericalHabitDialogFactory.create(habit);
activity.showDialog(dialog, "editNumericalHabit"); activity.showDialog(dialog, "editNumericalHabit");
} }
else
{
BooleanHabitDialog dialog;
dialog = booleanHabitDialogFactory.edit(habit);
activity.showDialog(dialog, "editHabit");
}
}
public void showFAQScreen() public void showFAQScreen()
{ {

@ -38,19 +38,18 @@ public class ShowHabitScreen extends BaseScreen
private ShowHabitController controller; private ShowHabitController controller;
@NonNull @NonNull
private final BooleanHabitDialogFactory booleanHabitDialogFactory; private final EditHabitDialogFactory editHabitDialogFactory;
@Inject @Inject
public ShowHabitScreen(@NonNull BaseActivity activity, public ShowHabitScreen(@NonNull BaseActivity activity,
@NonNull Habit habit, @NonNull Habit habit,
@NonNull ShowHabitRootView view, @NonNull ShowHabitRootView view,
@NonNull @NonNull
BooleanHabitDialogFactory EditHabitDialogFactory editHabitDialogFactory)
booleanHabitDialogFactory)
{ {
super(activity); super(activity);
setRootView(view); setRootView(view);
this.booleanHabitDialogFactory = booleanHabitDialogFactory; this.editHabitDialogFactory = editHabitDialogFactory;
this.habit = habit; this.habit = habit;
} }
@ -74,7 +73,7 @@ public class ShowHabitScreen extends BaseScreen
public void showEditHabitDialog() public void showEditHabitDialog()
{ {
activity.showDialog( activity.showDialog(
booleanHabitDialogFactory.edit(habit), editHabitDialogFactory.edit(habit),
"editHabit"); "editHabit");
} }

@ -104,7 +104,7 @@ public class Habit
this.name = ""; this.name = "";
this.description = ""; this.description = "";
this.targetType = AT_LEAST; this.targetType = AT_LEAST;
this.targetValue = 1; this.targetValue = 100;
this.unit = ""; this.unit = "";
checkmarks = factory.buildCheckmarkList(this); checkmarks = factory.buildCheckmarkList(this);

@ -22,7 +22,7 @@
style="@style/dialogForm" style="@style/dialogForm"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.habits.edit.BooleanHabitDialog" tools:context=".activities.habits.edit.EditHabitDialog"
tools:ignore="MergeRootFrame"> tools:ignore="MergeRootFrame">
<LinearLayout <LinearLayout
@ -39,6 +39,11 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"/>
<org.isoron.uhabits.activities.habits.edit.views.TargetPanel
android:id="@+id/targetPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<org.isoron.uhabits.activities.habits.edit.views.ReminderPanel <org.isoron.uhabits.activities.habits.edit.views.ReminderPanel
android:id="@+id/reminderPanel" android:id="@+id/reminderPanel"
android:layout_width="match_parent" android:layout_width="match_parent"

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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/>.
-->
<LinearLayout style="@style/dialogFormRow"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://isoron.org/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
style="@style/dialogFormLabel"
android:text="@string/target"/>
<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
<EditText
android:id="@+id/tvTargetCount"
style="@style/dialogFormInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/count"
android:inputType="numberDecimal"
android:text="@string/default_count"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
<org.isoron.uhabits.activities.habits.edit.views.ExampleEditText
android:id="@+id/tvUnit"
style="@style/dialogFormInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/unit"
android:inputType="text"
app:example="@string/example_units"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>

@ -1,137 +0,0 @@
<!--
~ 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/>.
-->
<LinearLayout
android:id="@+id/container"
style="@style/dialogForm"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.habits.edit.BooleanHabitDialog"
tools:ignore="MergeRootFrame"
xmlns:app="http://isoron.org/android">
<LinearLayout
android:id="@+id/formPanel"
style="@style/dialogFormPanel">
<LinearLayout
style="@style/dialogFormRow">
<android.support.design.widget.TextInputLayout
android:id="@+id/tilName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6">
<EditText
android:id="@+id/tvName"
style="@style/dialogFormInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name">
<requestFocus/>
</EditText>
</android.support.design.widget.TextInputLayout>
<ImageButton
android:id="@+id/buttonPickColor"
style="@style/dialogFormInputColor"
android:layout_weight="1"
android:contentDescription="@string/color_picker_default_title"
android:src="?dialogIconChangeColor"/>
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<org.isoron.uhabits.activities.habits.edit.views.ExampleEditText
android:id="@+id/tvDescription"
style="@style/dialogFormInputMultiline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Question"
app:example="e.g. How many steps did you walk today?"/>
</android.support.design.widget.TextInputLayout>
<LinearLayout style="@style/dialogFormRow">
<Spinner
android:id="@+id/tvTargetType"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:entries="@array/targetValues"/>
<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
<EditText
android:id="@+id/tvTargetCount"
style="@style/dialogFormInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/count"
android:inputType="numberDecimal"
android:text="@string/default_count"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
<org.isoron.uhabits.activities.habits.edit.views.ExampleEditText
android:id="@+id/tvUnit"
style="@style/dialogFormInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/unit"
app:example="e.g. steps"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end">
<Button
android:id="@+id/buttonDiscard"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="@string/discard"/>
<Button
android:id="@+id/buttonSave"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="@string/save"/>
</LinearLayout>
</LinearLayout>

@ -211,6 +211,8 @@
<string name="count">Count</string> <string name="count">Count</string>
<string name="validation_show_not_be_blank">This field should not be blank</string> <string name="validation_show_not_be_blank">This field should not be blank</string>
<string name="example_question_numerical">e.g. How many steps did you walk today?</string> <string name="example_question_numerical">e.g. How many steps did you walk today?</string>
<string name="example_units">e.g. steps</string>
<string name="example_question_boolean">e.g. Did you exercise today?</string> <string name="example_question_boolean">e.g. Did you exercise today?</string>
<string name="question">Question</string> <string name="question">Question</string>
<string name="target">Target</string>
</resources> </resources>

@ -71,16 +71,12 @@ public class ListHabitsScreenTest extends BaseUnitTest
private ColorPickerDialogFactory colorPickerDialogFactory; private ColorPickerDialogFactory colorPickerDialogFactory;
private BooleanHabitDialogFactory dialogFactory; private EditHabitDialogFactory dialogFactory;
private ThemeSwitcher themeSwitcher; private ThemeSwitcher themeSwitcher;
private ListHabitsScreen baseScreen; private ListHabitsScreen baseScreen;
private CreateNumericalHabitDialogFactory createNumericalHabitDialogFactory;
private EditNumericalHabitDialogFactory editNumericalHabitDialogFactory;
@Before @Before
@Override @Override
public void setUp() public void setUp()
@ -96,17 +92,11 @@ public class ListHabitsScreenTest extends BaseUnitTest
confirmDeleteDialogFactory = mock(ConfirmDeleteDialogFactory.class); confirmDeleteDialogFactory = mock(ConfirmDeleteDialogFactory.class);
filePickerDialogFactory = mock(FilePickerDialogFactory.class); filePickerDialogFactory = mock(FilePickerDialogFactory.class);
colorPickerDialogFactory = mock(ColorPickerDialogFactory.class); colorPickerDialogFactory = mock(ColorPickerDialogFactory.class);
dialogFactory = mock(BooleanHabitDialogFactory.class); dialogFactory = mock(EditHabitDialogFactory.class);
editNumericalHabitDialogFactory =
mock(EditNumericalHabitDialogFactory.class);
createNumericalHabitDialogFactory =
mock(CreateNumericalHabitDialogFactory.class);
screen = spy(new ListHabitsScreen(activity, commandRunner, dirFinder, screen = spy(new ListHabitsScreen(activity, commandRunner, dirFinder,
rootView, intentFactory, themeSwitcher, confirmDeleteDialogFactory, rootView, intentFactory, themeSwitcher, confirmDeleteDialogFactory,
filePickerDialogFactory, colorPickerDialogFactory, dialogFactory, filePickerDialogFactory, colorPickerDialogFactory, dialogFactory));
editNumericalHabitDialogFactory,
createNumericalHabitDialogFactory));
doNothing().when(screen).showMessage(anyInt()); doNothing().when(screen).showMessage(anyInt());
@ -218,7 +208,7 @@ public class ListHabitsScreenTest extends BaseUnitTest
@Test @Test
public void testShowEditHabitScreen() public void testShowEditHabitScreen()
{ {
BooleanHabitDialog dialog = mock(BooleanHabitDialog.class); EditHabitDialog dialog = mock(EditHabitDialog.class);
when(dialogFactory.edit(habit)).thenReturn(dialog); when(dialogFactory.edit(habit)).thenReturn(dialog);
screen.showEditHabitScreen(habit); screen.showEditHabitScreen(habit);

Loading…
Cancel
Save