mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Simplify BooleanHabitDialog subclasses and factories
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
|
||||
package org.isoron.uhabits.activities.habits.edit;
|
||||
|
||||
import android.content.*;
|
||||
import android.os.*;
|
||||
import android.support.annotation.*;
|
||||
import android.support.v7.app.*;
|
||||
@@ -38,24 +39,31 @@ import org.isoron.uhabits.preferences.*;
|
||||
|
||||
import butterknife.*;
|
||||
|
||||
public abstract class BooleanHabitDialog extends AppCompatDialogFragment
|
||||
public class BooleanHabitDialog extends AppCompatDialogFragment
|
||||
{
|
||||
public static final String BUNDLE_HABIT_ID = "habitId";
|
||||
|
||||
protected Habit originalHabit;
|
||||
|
||||
protected Habit modifiedHabit;
|
||||
|
||||
protected BooleanHabitDialogHelper helper;
|
||||
|
||||
protected Preferences prefs;
|
||||
|
||||
protected CommandRunner commandRunner;
|
||||
|
||||
protected HabitList habitList;
|
||||
|
||||
protected AppComponent appComponent;
|
||||
protected AppComponent component;
|
||||
|
||||
protected ModelFactory modelFactory;
|
||||
|
||||
@BindView(R.id.namePanel)
|
||||
NameDescriptionPanel namePanel;
|
||||
|
||||
@BindView(R.id.reminderPanel)
|
||||
ReminderPanel reminderPanel;
|
||||
|
||||
@BindView(R.id.frequencyPanel)
|
||||
FrequencyPanel frequencyPanel;
|
||||
|
||||
private ColorPickerDialogFactory colorPickerDialogFactory;
|
||||
|
||||
@Override
|
||||
@@ -79,76 +87,54 @@ public abstract class BooleanHabitDialog extends AppCompatDialogFragment
|
||||
ViewGroup container,
|
||||
Bundle savedInstanceState)
|
||||
{
|
||||
View view =
|
||||
inflater.inflate(R.layout.edit_boolean_habit, container, false);
|
||||
|
||||
HabitsApplication app =
|
||||
(HabitsApplication) getContext().getApplicationContext();
|
||||
|
||||
appComponent = app.getComponent();
|
||||
prefs = appComponent.getPreferences();
|
||||
habitList = appComponent.getHabitList();
|
||||
commandRunner = appComponent.getCommandRunner();
|
||||
modelFactory = appComponent.getModelFactory();
|
||||
View view;
|
||||
view = inflater.inflate(R.layout.edit_boolean_habit, container, false);
|
||||
|
||||
initDependencies();
|
||||
ButterKnife.bind(this, view);
|
||||
|
||||
helper = new BooleanHabitDialogHelper(this, view);
|
||||
getDialog().setTitle(getTitle());
|
||||
initializeHabits();
|
||||
restoreSavedInstance(savedInstanceState);
|
||||
helper.populateForm(modifiedHabit);
|
||||
originalHabit = parseHabitFromArguments();
|
||||
|
||||
helper.frequencyPanel.setFrequency(modifiedHabit.getFrequency());
|
||||
|
||||
if (modifiedHabit.hasReminder())
|
||||
helper.reminderPanel.setReminder(modifiedHabit.getReminder());
|
||||
|
||||
helper.reminderPanel.setController(new ReminderPanel.Controller()
|
||||
{
|
||||
@Override
|
||||
public void onTimeClicked(int currentHour, int currentMin)
|
||||
{
|
||||
TimePickerDialog timePicker;
|
||||
boolean is24HourMode = DateFormat.is24HourFormat(getContext());
|
||||
timePicker = TimePickerDialog.newInstance(helper.reminderPanel,
|
||||
currentHour, currentMin, is24HourMode);
|
||||
timePicker.show(getFragmentManager(), "timePicker");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWeekdayClicked(WeekdayList currentDays)
|
||||
{
|
||||
WeekdayPickerDialog dialog = new WeekdayPickerDialog();
|
||||
dialog.setListener(helper.reminderPanel);
|
||||
dialog.setSelectedDays(currentDays);
|
||||
dialog.show(getFragmentManager(), "weekdayPicker");
|
||||
}
|
||||
});
|
||||
populateForm();
|
||||
setupReminderController();
|
||||
setupNameController();
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public void onSaveInstanceState(Bundle outState)
|
||||
protected int getTitle()
|
||||
{
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putInt("color", modifiedHabit.getColor());
|
||||
if (originalHabit == null) return R.string.edit_habit;
|
||||
else return R.string.create_habit;
|
||||
}
|
||||
|
||||
protected abstract int getTitle();
|
||||
|
||||
protected abstract void initializeHabits();
|
||||
|
||||
protected void restoreSavedInstance(@Nullable Bundle bundle)
|
||||
protected void saveHabit(@NonNull Habit habit)
|
||||
{
|
||||
if (bundle == null) return;
|
||||
modifiedHabit.setColor(
|
||||
bundle.getInt("color", modifiedHabit.getColor()));
|
||||
if (originalHabit == null)
|
||||
{
|
||||
commandRunner.execute(component
|
||||
.getCreateHabitCommandFactory()
|
||||
.create(habitList, habit), null);
|
||||
}
|
||||
else
|
||||
{
|
||||
commandRunner.execute(component.getEditHabitCommandFactory().
|
||||
create(habitList, originalHabit, habit), originalHabit.getId());
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void saveHabit();
|
||||
private void initDependencies()
|
||||
{
|
||||
Context appContext = getContext().getApplicationContext();
|
||||
HabitsApplication app = (HabitsApplication) appContext;
|
||||
|
||||
component = app.getComponent();
|
||||
prefs = component.getPreferences();
|
||||
habitList = component.getHabitList();
|
||||
commandRunner = component.getCommandRunner();
|
||||
modelFactory = component.getModelFactory();
|
||||
}
|
||||
|
||||
@OnClick(R.id.buttonDiscard)
|
||||
void onButtonDiscardClick()
|
||||
@@ -159,30 +145,92 @@ public abstract class BooleanHabitDialog extends AppCompatDialogFragment
|
||||
@OnClick(R.id.buttonSave)
|
||||
void onSaveButtonClick()
|
||||
{
|
||||
helper.parseFormIntoHabit(modifiedHabit);
|
||||
modifiedHabit.setReminder(helper.reminderPanel.getReminder());
|
||||
modifiedHabit.setFrequency(helper.frequencyPanel.getFrequency());
|
||||
if (!namePanel.validate()) return;
|
||||
if (!frequencyPanel.validate()) return;
|
||||
|
||||
if (!helper.frequencyPanel.validate()) return;
|
||||
if (!helper.validate(modifiedHabit)) return;
|
||||
Habit habit = modelFactory.buildHabit();
|
||||
habit.setName(namePanel.getName());
|
||||
habit.setDescription(namePanel.getDescription());
|
||||
habit.setColor(namePanel.getColor());
|
||||
habit.setReminder(reminderPanel.getReminder());
|
||||
habit.setFrequency(frequencyPanel.getFrequency());
|
||||
|
||||
saveHabit();
|
||||
saveHabit(habit);
|
||||
dismiss();
|
||||
}
|
||||
|
||||
@OnClick(R.id.buttonPickColor)
|
||||
void showColorPicker()
|
||||
@Nullable
|
||||
private Habit parseHabitFromArguments()
|
||||
{
|
||||
int color = modifiedHabit.getColor();
|
||||
ColorPickerDialog picker = colorPickerDialogFactory.create(color);
|
||||
Bundle arguments = getArguments();
|
||||
if (arguments == null) return null;
|
||||
|
||||
Long id = (Long) arguments.get(BUNDLE_HABIT_ID);
|
||||
if (id == null) return null;
|
||||
|
||||
Habit habit = habitList.getById(id);
|
||||
if (habit == null) throw new IllegalStateException();
|
||||
|
||||
return habit;
|
||||
}
|
||||
|
||||
private void populateForm()
|
||||
{
|
||||
Habit habit = modelFactory.buildHabit();
|
||||
habit.setFrequency(Frequency.DAILY);
|
||||
habit.setColor(prefs.getDefaultHabitColor(habit.getColor()));
|
||||
|
||||
if (originalHabit != null) habit.copyFrom(originalHabit);
|
||||
|
||||
namePanel.populateFrom(habit);
|
||||
frequencyPanel.setFrequency(habit.getFrequency());
|
||||
if (habit.hasReminder()) reminderPanel.setReminder(habit.getReminder());
|
||||
}
|
||||
|
||||
private void setupNameController()
|
||||
{
|
||||
namePanel.setController(new NameDescriptionPanel.Controller()
|
||||
{
|
||||
@Override
|
||||
public void onColorPickerClicked(int previousColor)
|
||||
{
|
||||
ColorPickerDialog picker =
|
||||
colorPickerDialogFactory.create(previousColor);
|
||||
|
||||
picker.setListener(c ->
|
||||
{
|
||||
prefs.setDefaultHabitColor(c);
|
||||
modifiedHabit.setColor(c);
|
||||
helper.populateColor(c);
|
||||
namePanel.setColor(c);
|
||||
});
|
||||
|
||||
picker.show(getFragmentManager(), "picker");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setupReminderController()
|
||||
{
|
||||
reminderPanel.setController(new ReminderPanel.Controller()
|
||||
{
|
||||
@Override
|
||||
public void onTimeClicked(int currentHour, int currentMin)
|
||||
{
|
||||
TimePickerDialog timePicker;
|
||||
boolean is24HourMode = DateFormat.is24HourFormat(getContext());
|
||||
timePicker =
|
||||
TimePickerDialog.newInstance(reminderPanel, currentHour,
|
||||
currentMin, is24HourMode);
|
||||
timePicker.show(getFragmentManager(), "timePicker");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWeekdayClicked(WeekdayList currentDays)
|
||||
{
|
||||
WeekdayPickerDialog dialog = new WeekdayPickerDialog();
|
||||
dialog.setListener(reminderPanel);
|
||||
dialog.setSelectedDays(currentDays);
|
||||
dialog.show(getFragmentManager(), "weekdayPicker");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,21 +26,28 @@ import org.isoron.uhabits.models.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
public class EditBooleanHabitDialogFactory
|
||||
import static org.isoron.uhabits.activities.habits.edit.BooleanHabitDialog.*;
|
||||
|
||||
public class BooleanHabitDialogFactory
|
||||
{
|
||||
@Inject
|
||||
public EditBooleanHabitDialogFactory()
|
||||
public BooleanHabitDialogFactory()
|
||||
{
|
||||
}
|
||||
|
||||
public EditBooleanHabitDialog create(@NonNull Habit habit)
|
||||
public BooleanHabitDialog create()
|
||||
{
|
||||
return new BooleanHabitDialog();
|
||||
}
|
||||
|
||||
public BooleanHabitDialog edit(@NonNull Habit habit)
|
||||
{
|
||||
if (habit.getId() == null)
|
||||
throw new IllegalArgumentException("habit not saved");
|
||||
|
||||
EditBooleanHabitDialog dialog = new EditBooleanHabitDialog();
|
||||
BooleanHabitDialog dialog = new BooleanHabitDialog();
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("habitId", habit.getId());
|
||||
args.putLong(BUNDLE_HABIT_ID, habit.getId());
|
||||
dialog.setArguments(args);
|
||||
return dialog;
|
||||
}
|
||||
@@ -1,86 +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.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 BooleanHabitDialogHelper
|
||||
{
|
||||
private DialogFragment frag;
|
||||
|
||||
@BindView(R.id.tvName)
|
||||
TextView tvName;
|
||||
|
||||
@BindView(R.id.tvDescription)
|
||||
TextView tvDescription;
|
||||
|
||||
@BindView(R.id.reminderPanel)
|
||||
ReminderPanel reminderPanel;
|
||||
|
||||
@BindView(R.id.frequencyPanel)
|
||||
FrequencyPanel frequencyPanel;
|
||||
|
||||
public BooleanHabitDialogHelper(DialogFragment frag, View view)
|
||||
{
|
||||
this.frag = frag;
|
||||
ButterKnife.bind(this, view);
|
||||
}
|
||||
|
||||
protected void populateForm(final Habit habit)
|
||||
{
|
||||
tvName.setText(habit.getName());
|
||||
tvDescription.setText(habit.getDescription());
|
||||
populateColor(habit.getColor());
|
||||
}
|
||||
|
||||
void parseFormIntoHabit(Habit habit)
|
||||
{
|
||||
habit.setName(tvName.getText().toString().trim());
|
||||
habit.setDescription(tvDescription.getText().toString().trim());
|
||||
}
|
||||
|
||||
void populateColor(int paletteColor)
|
||||
{
|
||||
tvName.setTextColor(
|
||||
ColorUtils.getColor(frag.getContext(), paletteColor));
|
||||
}
|
||||
|
||||
boolean validate(Habit habit)
|
||||
{
|
||||
Boolean valid = true;
|
||||
|
||||
if (habit.getName().isEmpty())
|
||||
{
|
||||
tvName.setError(frag.getString(R.string.validation_name_should_not_be_blank));
|
||||
valid = false;
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
}
|
||||
@@ -1,53 +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 CreateBooleanHabitDialog extends BooleanHabitDialog
|
||||
{
|
||||
@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()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void saveHabit()
|
||||
{
|
||||
Command command = appComponent
|
||||
.getCreateHabitCommandFactory()
|
||||
.create(habitList, modifiedHabit);
|
||||
commandRunner.execute(command, null);
|
||||
}
|
||||
}
|
||||
@@ -1,52 +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 EditBooleanHabitDialog extends BooleanHabitDialog
|
||||
{
|
||||
@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);
|
||||
modifiedHabit = modelFactory.buildHabit();
|
||||
modifiedHabit.copyFrom(originalHabit);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void saveHabit()
|
||||
{
|
||||
Command command = appComponent.getEditHabitCommandFactory().
|
||||
create(habitList, originalHabit, modifiedHabit);
|
||||
commandRunner.execute(command, originalHabit.getId());
|
||||
}
|
||||
}
|
||||
@@ -60,8 +60,6 @@ public class NumericalHabitDialogHelper
|
||||
|
||||
public void parseForm(Habit habit)
|
||||
{
|
||||
tvUnit.clearFocus();
|
||||
tvDescription.clearFocus();
|
||||
habit.setName(tvName.getText().toString().trim());
|
||||
habit.setDescription(tvDescription.getRealText().trim());
|
||||
habit.setTargetType(tvTargetType.getSelectedItemPosition());
|
||||
|
||||
@@ -64,7 +64,8 @@ public class ExampleEditText extends EditText
|
||||
|
||||
public String getRealText()
|
||||
{
|
||||
return realText;
|
||||
if(hasFocus()) return getText().toString();
|
||||
else return realText;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* 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.os.*;
|
||||
import android.support.annotation.*;
|
||||
import android.util.*;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.activities.common.views.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import butterknife.*;
|
||||
|
||||
|
||||
public class NameDescriptionPanel extends FrameLayout
|
||||
{
|
||||
@BindView(R.id.tvName)
|
||||
EditText tvName;
|
||||
|
||||
@BindView(R.id.tvDescription)
|
||||
ExampleEditText tvDescription;
|
||||
|
||||
private int color;
|
||||
|
||||
@NonNull
|
||||
private Controller controller;
|
||||
|
||||
public NameDescriptionPanel(@NonNull Context context,
|
||||
@Nullable AttributeSet attrs)
|
||||
{
|
||||
super(context, attrs);
|
||||
|
||||
View view = inflate(context, R.layout.edit_habit_name, null);
|
||||
ButterKnife.bind(this, view);
|
||||
addView(view);
|
||||
|
||||
controller = new Controller() {};
|
||||
}
|
||||
|
||||
public int getColor()
|
||||
{
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(int color)
|
||||
{
|
||||
this.color = color;
|
||||
tvName.setTextColor(ColorUtils.getColor(getContext(), color));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getDescription()
|
||||
{
|
||||
return tvDescription.getRealText().trim();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getName()
|
||||
{
|
||||
return tvName.getText().toString().trim();
|
||||
}
|
||||
|
||||
public void populateFrom(@NonNull Habit habit)
|
||||
{
|
||||
Resources res = getResources();
|
||||
|
||||
if(habit.isNumerical())
|
||||
tvDescription.setExample(res.getString(R.string.example_question_numerical));
|
||||
else
|
||||
tvDescription.setExample(res.getString(R.string.example_question_boolean));
|
||||
|
||||
setColor(habit.getColor());
|
||||
tvName.setText(habit.getName());
|
||||
tvDescription.setRealText(habit.getDescription());
|
||||
}
|
||||
|
||||
public boolean validate()
|
||||
{
|
||||
Resources res = getResources();
|
||||
|
||||
if (getName().isEmpty())
|
||||
{
|
||||
tvName.setError(
|
||||
res.getString(R.string.validation_name_should_not_be_blank));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestoreInstanceState(Parcelable state)
|
||||
{
|
||||
BundleSavedState bss = (BundleSavedState) state;
|
||||
setColor(bss.bundle.getInt("color"));
|
||||
super.onRestoreInstanceState(bss.getSuperState());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Parcelable onSaveInstanceState()
|
||||
{
|
||||
Parcelable superState = super.onSaveInstanceState();
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt("color", color);
|
||||
return new BundleSavedState(superState, bundle);
|
||||
}
|
||||
|
||||
@OnClick(R.id.buttonPickColor)
|
||||
void showColorPicker()
|
||||
{
|
||||
controller.onColorPickerClicked(color);
|
||||
}
|
||||
|
||||
public void setController(@NonNull Controller controller)
|
||||
{
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
public interface Controller
|
||||
{
|
||||
/**
|
||||
* Called when the user has clicked the widget to select a new
|
||||
* color for the habit.
|
||||
*
|
||||
* @param previousColor the color previously selected
|
||||
*/
|
||||
default void onColorPickerClicked(int previousColor) {}
|
||||
}
|
||||
}
|
||||
@@ -82,10 +82,6 @@ public class ListHabitsScreen extends BaseScreen
|
||||
@NonNull
|
||||
private final ConfirmDeleteDialogFactory confirmDeleteDialogFactory;
|
||||
|
||||
@NonNull
|
||||
private final CreateBooleanHabitDialogFactory
|
||||
createBooleanHabitDialogFactory;
|
||||
|
||||
@NonNull
|
||||
private final FilePickerDialogFactory filePickerDialogFactory;
|
||||
|
||||
@@ -93,7 +89,7 @@ public class ListHabitsScreen extends BaseScreen
|
||||
private final ColorPickerDialogFactory colorPickerFactory;
|
||||
|
||||
@NonNull
|
||||
private final EditBooleanHabitDialogFactory editBooleanHabitDialogFactory;
|
||||
private final BooleanHabitDialogFactory booleanHabitDialogFactory;
|
||||
|
||||
@NonNull
|
||||
private final ThemeSwitcher themeSwitcher;
|
||||
@@ -112,10 +108,9 @@ public class ListHabitsScreen extends BaseScreen
|
||||
@NonNull IntentFactory intentFactory,
|
||||
@NonNull ThemeSwitcher themeSwitcher,
|
||||
@NonNull ConfirmDeleteDialogFactory confirmDeleteDialogFactory,
|
||||
@NonNull CreateBooleanHabitDialogFactory createBooleanHabitDialogFactory,
|
||||
@NonNull FilePickerDialogFactory filePickerDialogFactory,
|
||||
@NonNull ColorPickerDialogFactory colorPickerFactory,
|
||||
@NonNull EditBooleanHabitDialogFactory editBooleanHabitDialogFactory,
|
||||
@NonNull BooleanHabitDialogFactory booleanHabitDialogFactory,
|
||||
@NonNull EditNumericalHabitDialogFactory editNumericalHabitDialogFactory,
|
||||
@NonNull CreateNumericalHabitDialogFactory createNumericalHabitDialogFactory)
|
||||
{
|
||||
@@ -125,8 +120,7 @@ public class ListHabitsScreen extends BaseScreen
|
||||
this.commandRunner = commandRunner;
|
||||
this.confirmDeleteDialogFactory = confirmDeleteDialogFactory;
|
||||
this.createNumericalHabitDialogFactory = createNumericalHabitDialogFactory;
|
||||
this.createBooleanHabitDialogFactory = createBooleanHabitDialogFactory;
|
||||
this.editBooleanHabitDialogFactory = editBooleanHabitDialogFactory;
|
||||
this.booleanHabitDialogFactory = booleanHabitDialogFactory;
|
||||
this.editNumericalHabitDialogFactory = editNumericalHabitDialogFactory;
|
||||
this.dirFinder = dirFinder;
|
||||
this.filePickerDialogFactory = filePickerDialogFactory;
|
||||
@@ -209,8 +203,8 @@ public class ListHabitsScreen extends BaseScreen
|
||||
|
||||
public void showCreateBooleanHabitScreen()
|
||||
{
|
||||
CreateBooleanHabitDialog dialog;
|
||||
dialog = createBooleanHabitDialogFactory.create();
|
||||
BooleanHabitDialog dialog;
|
||||
dialog = booleanHabitDialogFactory.create();
|
||||
activity.showDialog(dialog, "editHabit");
|
||||
}
|
||||
|
||||
@@ -229,8 +223,8 @@ public class ListHabitsScreen extends BaseScreen
|
||||
}
|
||||
else
|
||||
{
|
||||
EditBooleanHabitDialog dialog;
|
||||
dialog = editBooleanHabitDialogFactory.create(habit);
|
||||
BooleanHabitDialog dialog;
|
||||
dialog = booleanHabitDialogFactory.edit(habit);
|
||||
activity.showDialog(dialog, "editHabit");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,18 +38,19 @@ public class ShowHabitScreen extends BaseScreen
|
||||
private ShowHabitController controller;
|
||||
|
||||
@NonNull
|
||||
private final EditBooleanHabitDialogFactory editBooleanHabitDialogFactory;
|
||||
private final BooleanHabitDialogFactory booleanHabitDialogFactory;
|
||||
|
||||
@Inject
|
||||
public ShowHabitScreen(@NonNull BaseActivity activity,
|
||||
@NonNull Habit habit,
|
||||
@NonNull ShowHabitRootView view,
|
||||
@NonNull
|
||||
EditBooleanHabitDialogFactory editBooleanHabitDialogFactory)
|
||||
BooleanHabitDialogFactory
|
||||
booleanHabitDialogFactory)
|
||||
{
|
||||
super(activity);
|
||||
setRootView(view);
|
||||
this.editBooleanHabitDialogFactory = editBooleanHabitDialogFactory;
|
||||
this.booleanHabitDialogFactory = booleanHabitDialogFactory;
|
||||
this.habit = habit;
|
||||
}
|
||||
|
||||
@@ -72,8 +73,9 @@ public class ShowHabitScreen extends BaseScreen
|
||||
|
||||
public void showEditHabitDialog()
|
||||
{
|
||||
EditBooleanHabitDialog dialog = editBooleanHabitDialogFactory.create(habit);
|
||||
activity.showDialog(dialog, "editHabit");
|
||||
activity.showDialog(
|
||||
booleanHabitDialogFactory.edit(habit),
|
||||
"editHabit");
|
||||
}
|
||||
|
||||
public void showEditHistoryDialog()
|
||||
|
||||
@@ -29,29 +29,10 @@
|
||||
android:id="@+id/formPanel"
|
||||
style="@style/dialogFormPanel">
|
||||
|
||||
<LinearLayout
|
||||
<org.isoron.uhabits.activities.habits.edit.views.NameDescriptionPanel
|
||||
android:id="@+id/namePanel"
|
||||
style="@style/dialogFormRow">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/tvName"
|
||||
style="@style/dialogFormInput"
|
||||
android:hint="@string/name">
|
||||
|
||||
<requestFocus/>
|
||||
</EditText>
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/buttonPickColor"
|
||||
style="@style/dialogFormInputColor"
|
||||
android:contentDescription="@string/color_picker_default_title"
|
||||
android:src="?dialogIconChangeColor"/>
|
||||
</LinearLayout>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/tvDescription"
|
||||
style="@style/dialogFormInputMultiline"
|
||||
android:hint="@string/description_hint"/>
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.edit.views.FrequencyPanel
|
||||
android:id="@+id/frequencyPanel"
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
|
||||
70
app/src/main/res/layout/edit_habit_name.xml
Normal file
70
app/src/main/res/layout/edit_habit_name.xml
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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
|
||||
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="vertical">
|
||||
|
||||
<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="@string/question"
|
||||
app:example="@string/example_question_numerical"/>
|
||||
</android.support.design.widget.TextInputLayout>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -19,12 +19,13 @@
|
||||
-->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
style="@style/dialogFormRow">
|
||||
style="@style/dialogFormRow"
|
||||
android:layout_marginTop="12dp">
|
||||
|
||||
<TextView
|
||||
style="@style/dialogFormLabel"
|
||||
@@ -37,7 +38,8 @@
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/llReminderDays"
|
||||
style="@style/dialogFormRow">
|
||||
style="@style/dialogFormRow"
|
||||
android:layout_marginTop="12dp">
|
||||
|
||||
<TextView
|
||||
style="@style/dialogFormLabel"
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
-->
|
||||
|
||||
<resources>
|
||||
|
||||
<string name="app_name">Loop Habit Tracker</string>
|
||||
<string name="main_activity_title">Habits</string>
|
||||
<string name="action_settings">Settings</string>
|
||||
@@ -211,4 +210,7 @@
|
||||
<string name="unit">Unit</string>
|
||||
<string name="count">Count</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_boolean">e.g. Did you exercise today?</string>
|
||||
<string name="question">Question</string>
|
||||
</resources>
|
||||
@@ -61,8 +61,6 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
|
||||
private ConfirmDeleteDialogFactory confirmDeleteDialogFactory;
|
||||
|
||||
private CreateBooleanHabitDialogFactory createHabitDialogFactory;
|
||||
|
||||
private FilePickerDialogFactory filePickerDialogFactory;
|
||||
|
||||
private IntentFactory intentFactory;
|
||||
@@ -73,7 +71,7 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
|
||||
private ColorPickerDialogFactory colorPickerDialogFactory;
|
||||
|
||||
private EditBooleanHabitDialogFactory editHabitDialogFactory;
|
||||
private BooleanHabitDialogFactory dialogFactory;
|
||||
|
||||
private ThemeSwitcher themeSwitcher;
|
||||
|
||||
@@ -96,10 +94,9 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
intentFactory = mock(IntentFactory.class);
|
||||
themeSwitcher = mock(ThemeSwitcher.class);
|
||||
confirmDeleteDialogFactory = mock(ConfirmDeleteDialogFactory.class);
|
||||
createHabitDialogFactory = mock(CreateBooleanHabitDialogFactory.class);
|
||||
filePickerDialogFactory = mock(FilePickerDialogFactory.class);
|
||||
colorPickerDialogFactory = mock(ColorPickerDialogFactory.class);
|
||||
editHabitDialogFactory = mock(EditBooleanHabitDialogFactory.class);
|
||||
dialogFactory = mock(BooleanHabitDialogFactory.class);
|
||||
editNumericalHabitDialogFactory =
|
||||
mock(EditNumericalHabitDialogFactory.class);
|
||||
createNumericalHabitDialogFactory =
|
||||
@@ -107,8 +104,7 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
|
||||
screen = spy(new ListHabitsScreen(activity, commandRunner, dirFinder,
|
||||
rootView, intentFactory, themeSwitcher, confirmDeleteDialogFactory,
|
||||
createHabitDialogFactory, filePickerDialogFactory,
|
||||
colorPickerDialogFactory, editHabitDialogFactory,
|
||||
filePickerDialogFactory, colorPickerDialogFactory, dialogFactory,
|
||||
editNumericalHabitDialogFactory,
|
||||
createNumericalHabitDialogFactory));
|
||||
|
||||
@@ -222,8 +218,8 @@ public class ListHabitsScreenTest extends BaseUnitTest
|
||||
@Test
|
||||
public void testShowEditHabitScreen()
|
||||
{
|
||||
EditBooleanHabitDialog dialog = mock(EditBooleanHabitDialog.class);
|
||||
when(editHabitDialogFactory.create(habit)).thenReturn(dialog);
|
||||
BooleanHabitDialog dialog = mock(BooleanHabitDialog.class);
|
||||
when(dialogFactory.edit(habit)).thenReturn(dialog);
|
||||
|
||||
screen.showEditHabitScreen(habit);
|
||||
verify(activity).showDialog(eq(dialog), any());
|
||||
|
||||
Reference in New Issue
Block a user