mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Refactor EditHabitDialog
This commit is contained in:
@@ -80,11 +80,11 @@ public class MainActivityActions
|
||||
onView(withId(R.id.buttonPickColor))
|
||||
.perform(click());
|
||||
pressBack();
|
||||
onView(withId(R.id.inputReminderTime))
|
||||
onView(withId(R.id.tvReminderTime))
|
||||
.perform(click());
|
||||
onView(withText("Done"))
|
||||
.perform(click());
|
||||
onView(withId(R.id.inputReminderDays))
|
||||
onView(withId(R.id.tvReminderDays))
|
||||
.perform(click());
|
||||
onView(withText("OK"))
|
||||
.perform(click());
|
||||
@@ -101,9 +101,9 @@ public class MainActivityActions
|
||||
|
||||
public static void typeHabitData(String name, String description, String num, String den)
|
||||
{
|
||||
onView(withId(R.id.input_name))
|
||||
onView(withId(R.id.tvName))
|
||||
.perform(replaceText(name));
|
||||
onView(withId(R.id.input_description))
|
||||
onView(withId(R.id.tvDescription))
|
||||
.perform(replaceText(description));
|
||||
|
||||
try
|
||||
@@ -119,9 +119,9 @@ public class MainActivityActions
|
||||
// ignored
|
||||
}
|
||||
|
||||
onView(withId(R.id.input_freq_num))
|
||||
onView(withId(R.id.tvFreqNum))
|
||||
.perform(replaceText(num));
|
||||
onView(withId(R.id.input_freq_den))
|
||||
onView(withId(R.id.tvFreqDen))
|
||||
.perform(replaceText(den));
|
||||
}
|
||||
|
||||
|
||||
@@ -178,7 +178,7 @@ public class MainTest
|
||||
typeHabitData("", "", "15", "7");
|
||||
|
||||
onView(withId(R.id.buttonSave)).perform(click());
|
||||
onView(withId(R.id.input_name)).check(matches(isDisplayed()));
|
||||
onView(withId(R.id.tvName)).check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
* 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.ui.habits.edit;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.app.AppCompatDialogFragment;
|
||||
import android.text.format.DateFormat;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.android.colorpicker.ColorPickerDialog;
|
||||
import com.android.colorpicker.ColorPickerSwatch;
|
||||
import com.android.datetimepicker.time.RadialPickerLayout;
|
||||
import com.android.datetimepicker.time.TimePickerDialog;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.utils.ColorUtils;
|
||||
import org.isoron.uhabits.utils.DateUtils;
|
||||
import org.isoron.uhabits.utils.Preferences;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
import butterknife.OnItemSelected;
|
||||
|
||||
public abstract class BaseDialogFragment extends AppCompatDialogFragment
|
||||
{
|
||||
protected Habit originalHabit;
|
||||
protected Habit modifiedHabit;
|
||||
protected Preferences prefs = Preferences.getInstance();
|
||||
protected BaseDialogHelper helper;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState)
|
||||
{
|
||||
View view = inflater.inflate(R.layout.edit_habit, container, false);
|
||||
helper = new BaseDialogHelper(this, view);
|
||||
ButterKnife.bind(this, view);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public void onSaveInstanceState(Bundle outState)
|
||||
{
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putInt("color", modifiedHabit.color);
|
||||
if(modifiedHabit.hasReminder())
|
||||
{
|
||||
outState.putInt("reminderMin", modifiedHabit.reminderMin);
|
||||
outState.putInt("reminderHour", modifiedHabit.reminderHour);
|
||||
outState.putInt("reminderDays", modifiedHabit.reminderDays);
|
||||
}
|
||||
}
|
||||
|
||||
protected void restoreSavedInstance(@Nullable Bundle bundle)
|
||||
{
|
||||
if(bundle == null) return;
|
||||
modifiedHabit.color = bundle.getInt("color", modifiedHabit.color);
|
||||
modifiedHabit.reminderMin = bundle.getInt("reminderMin", -1);
|
||||
modifiedHabit.reminderHour = bundle.getInt("reminderHour", -1);
|
||||
modifiedHabit.reminderDays = bundle.getInt("reminderDays", -1);
|
||||
if(modifiedHabit.reminderMin < 0) modifiedHabit.clearReminder();
|
||||
}
|
||||
|
||||
@OnClick(R.id.buttonDiscard)
|
||||
void onButtonDiscardClick()
|
||||
{
|
||||
dismiss();
|
||||
}
|
||||
|
||||
@OnClick(R.id.buttonSave)
|
||||
void onSaveButtonClick()
|
||||
{
|
||||
helper.parseFormIntoHabit(modifiedHabit);
|
||||
if (!helper.validate(modifiedHabit)) return;
|
||||
saveHabit();
|
||||
dismiss();
|
||||
}
|
||||
|
||||
@OnClick(R.id.tvReminderTime)
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
void onDateSpinnerClick()
|
||||
{
|
||||
int defaultHour = 8;
|
||||
int defaultMin = 0;
|
||||
|
||||
if (modifiedHabit.hasReminder())
|
||||
{
|
||||
defaultHour = modifiedHabit.reminderHour;
|
||||
defaultMin = modifiedHabit.reminderMin;
|
||||
}
|
||||
|
||||
showTimePicker(defaultHour, defaultMin);
|
||||
}
|
||||
|
||||
@OnClick(R.id.tvReminderDays)
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
void onWeekdayClick()
|
||||
{
|
||||
if(!modifiedHabit.hasReminder()) return;
|
||||
WeekdayPickerDialog dialog = new WeekdayPickerDialog();
|
||||
dialog.setListener(new OnWeekdaysPickedListener());
|
||||
dialog.setSelectedDays(DateUtils.unpackWeekdayList(modifiedHabit.reminderDays));
|
||||
dialog.show(getFragmentManager(), "weekdayPicker");
|
||||
}
|
||||
|
||||
@OnItemSelected(R.id.sFrequency)
|
||||
public void onFrequencySelected(int position)
|
||||
{
|
||||
if(position < 0 || position > 4) throw new IllegalArgumentException();
|
||||
int freqNums[] = { 1, 1, 2, 5, 3 };
|
||||
int freqDens[] = { 1, 7, 7, 7, 7 };
|
||||
modifiedHabit.freqNum = freqNums[position];
|
||||
modifiedHabit.freqDen = freqDens[position];
|
||||
helper.populateFrequencyFields(modifiedHabit);
|
||||
}
|
||||
|
||||
protected abstract void saveHabit();
|
||||
|
||||
@OnClick(R.id.buttonPickColor)
|
||||
void showColorPicker()
|
||||
{
|
||||
int androidColor = ColorUtils.getColor(getContext(), modifiedHabit.color);
|
||||
|
||||
ColorPickerDialog picker = ColorPickerDialog.newInstance(
|
||||
R.string.color_picker_default_title, ColorUtils.getPalette(getContext()),
|
||||
androidColor, 4, ColorPickerDialog.SIZE_SMALL);
|
||||
|
||||
picker.setOnColorSelectedListener(new OnColorSelectedListener());
|
||||
picker.show(getFragmentManager(), "picker");
|
||||
}
|
||||
|
||||
private void showTimePicker(int defaultHour, int defaultMin)
|
||||
{
|
||||
boolean is24HourMode = DateFormat.is24HourFormat(getContext());
|
||||
TimePickerDialog timePicker = TimePickerDialog.newInstance(new OnTimeSetListener(),
|
||||
defaultHour, defaultMin, is24HourMode);
|
||||
timePicker.show(getFragmentManager(), "timePicker");
|
||||
}
|
||||
|
||||
private class OnColorSelectedListener implements ColorPickerSwatch.OnColorSelectedListener
|
||||
{
|
||||
public void onColorSelected(int androidColor)
|
||||
{
|
||||
int paletteColor = ColorUtils.colorToPaletteIndex(getActivity(), androidColor);
|
||||
prefs.setDefaultHabitColor(paletteColor);
|
||||
modifiedHabit.color = paletteColor;
|
||||
helper.populateColor(paletteColor);
|
||||
}
|
||||
}
|
||||
|
||||
private class OnWeekdaysPickedListener implements WeekdayPickerDialog.OnWeekdaysPickedListener
|
||||
{
|
||||
@Override
|
||||
public void onWeekdaysPicked(boolean[] selectedDays)
|
||||
{
|
||||
if(isSelectionEmpty(selectedDays))
|
||||
Arrays.fill(selectedDays, true);
|
||||
|
||||
modifiedHabit.reminderDays = DateUtils.packWeekdayList(selectedDays);
|
||||
helper.populateReminderFields(modifiedHabit);
|
||||
}
|
||||
|
||||
private boolean isSelectionEmpty(boolean[] selectedDays)
|
||||
{
|
||||
for (boolean d : selectedDays) if (d) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private class OnTimeSetListener implements TimePickerDialog.OnTimeSetListener
|
||||
{
|
||||
@Override
|
||||
public void onTimeSet(RadialPickerLayout view, int hour, int minute)
|
||||
{
|
||||
modifiedHabit.reminderHour = hour;
|
||||
modifiedHabit.reminderMin = minute;
|
||||
modifiedHabit.reminderDays = DateUtils.ALL_WEEK_DAYS;
|
||||
helper.populateReminderFields(modifiedHabit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeCleared(RadialPickerLayout view)
|
||||
{
|
||||
modifiedHabit.clearReminder();
|
||||
helper.populateReminderFields(modifiedHabit);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* 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.ui.habits.edit;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.utils.ColorUtils;
|
||||
import org.isoron.uhabits.utils.DateUtils;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class BaseDialogHelper
|
||||
{
|
||||
private DialogFragment frag;
|
||||
@BindView(R.id.tvName) TextView tvName;
|
||||
@BindView(R.id.tvDescription) TextView tvDescription;
|
||||
@BindView(R.id.tvFreqNum) TextView tvFreqNum;
|
||||
@BindView(R.id.tvFreqDen) TextView tvFreqDen;
|
||||
@BindView(R.id.tvReminderTime) TextView tvReminderTime;
|
||||
@BindView(R.id.tvReminderDays) TextView tvReminderDays;
|
||||
@BindView(R.id.sFrequency) Spinner sFrequency;
|
||||
@BindView(R.id.llCustomFrequency) ViewGroup llCustomFrequency;
|
||||
@BindView(R.id.llReminderDays) ViewGroup llReminderDays;
|
||||
|
||||
public BaseDialogHelper(DialogFragment frag, View view)
|
||||
{
|
||||
this.frag = frag;
|
||||
ButterKnife.bind(this, view);
|
||||
}
|
||||
|
||||
protected void populateForm(final Habit habit)
|
||||
{
|
||||
if(habit.name != null) tvName.setText(habit.name);
|
||||
if(habit.description != null) tvDescription.setText(habit.description);
|
||||
|
||||
populateColor(habit.color);
|
||||
populateFrequencyFields(habit);
|
||||
populateReminderFields(habit);
|
||||
}
|
||||
|
||||
void populateColor(int paletteColor)
|
||||
{
|
||||
tvName.setTextColor(ColorUtils.getColor(frag.getContext(), paletteColor));
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
void populateReminderFields(Habit habit)
|
||||
{
|
||||
if (!habit.hasReminder())
|
||||
{
|
||||
tvReminderTime.setText(R.string.reminder_off);
|
||||
llReminderDays.setVisibility(View.GONE);
|
||||
return;
|
||||
}
|
||||
|
||||
String time = DateUtils.formatTime(frag.getContext(), habit.reminderHour, habit.reminderMin);
|
||||
tvReminderTime.setText(time);
|
||||
llReminderDays.setVisibility(View.VISIBLE);
|
||||
|
||||
boolean weekdays[] = DateUtils.unpackWeekdayList(habit.reminderDays);
|
||||
tvReminderDays.setText(DateUtils.formatWeekdayList(frag.getContext(), weekdays));
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
void populateFrequencyFields(Habit habit)
|
||||
{
|
||||
int quickSelectPosition = -1;
|
||||
|
||||
if(habit.freqNum.equals(habit.freqDen))
|
||||
quickSelectPosition = 0;
|
||||
|
||||
else if(habit.freqNum == 1 && habit.freqDen == 7)
|
||||
quickSelectPosition = 1;
|
||||
|
||||
else if(habit.freqNum == 2 && habit.freqDen == 7)
|
||||
quickSelectPosition = 2;
|
||||
|
||||
else if(habit.freqNum == 5 && habit.freqDen == 7)
|
||||
quickSelectPosition = 3;
|
||||
|
||||
if(quickSelectPosition >= 0) showSimplifiedFrequency(quickSelectPosition);
|
||||
else showCustomFrequency();
|
||||
|
||||
tvFreqNum.setText(habit.freqNum.toString());
|
||||
tvFreqDen.setText(habit.freqDen.toString());
|
||||
}
|
||||
|
||||
private void showCustomFrequency()
|
||||
{
|
||||
sFrequency.setVisibility(View.GONE);
|
||||
llCustomFrequency.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
private void showSimplifiedFrequency(int quickSelectPosition)
|
||||
{
|
||||
sFrequency.setVisibility(View.VISIBLE);
|
||||
sFrequency.setSelection(quickSelectPosition);
|
||||
llCustomFrequency.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
boolean validate(Habit habit)
|
||||
{
|
||||
Boolean valid = true;
|
||||
|
||||
if (habit.name.length() == 0)
|
||||
{
|
||||
tvName.setError(frag.getString(R.string.validation_name_should_not_be_blank));
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (habit.freqNum <= 0)
|
||||
{
|
||||
tvFreqNum.setError(frag.getString(R.string.validation_number_should_be_positive));
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (habit.freqNum > habit.freqDen)
|
||||
{
|
||||
tvFreqNum.setError(frag.getString(R.string.validation_at_most_one_rep_per_day));
|
||||
valid = false;
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
void parseFormIntoHabit(Habit habit)
|
||||
{
|
||||
habit.name = tvName.getText().toString().trim();
|
||||
habit.description = tvDescription.getText().toString().trim();
|
||||
String freqNum = tvFreqNum.getText().toString();
|
||||
String freqDen = tvFreqDen.getText().toString();
|
||||
if(!freqNum.isEmpty()) habit.freqNum = Integer.parseInt(freqNum);
|
||||
if(!freqDen.isEmpty()) habit.freqDen = Integer.parseInt(freqDen);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.ui.habits.edit;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.commands.Command;
|
||||
import org.isoron.uhabits.commands.CommandRunner;
|
||||
import org.isoron.uhabits.commands.CreateHabitCommand;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
|
||||
public class CreateHabitDialogFragment extends BaseDialogFragment
|
||||
{
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState)
|
||||
{
|
||||
View view = super.onCreateView(inflater, container, savedInstanceState);
|
||||
getDialog().setTitle(R.string.create_habit);
|
||||
initializeHabits();
|
||||
restoreSavedInstance(savedInstanceState);
|
||||
helper.populateForm(modifiedHabit);
|
||||
return view;
|
||||
}
|
||||
|
||||
private void initializeHabits()
|
||||
{
|
||||
modifiedHabit = new Habit();
|
||||
modifiedHabit.freqNum = 1;
|
||||
modifiedHabit.freqDen = 1;
|
||||
modifiedHabit.color = prefs.getDefaultHabitColor(modifiedHabit.color);
|
||||
}
|
||||
|
||||
protected void saveHabit()
|
||||
{
|
||||
Command command = new CreateHabitCommand(modifiedHabit);
|
||||
CommandRunner.getInstance().execute(command, null);
|
||||
}
|
||||
}
|
||||
@@ -19,428 +19,52 @@
|
||||
|
||||
package org.isoron.uhabits.ui.habits.edit;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v7.app.AppCompatDialogFragment;
|
||||
import android.text.format.DateFormat;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.android.colorpicker.ColorPickerDialog;
|
||||
import com.android.colorpicker.ColorPickerSwatch;
|
||||
import com.android.datetimepicker.time.RadialPickerLayout;
|
||||
import com.android.datetimepicker.time.TimePickerDialog;
|
||||
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.commands.Command;
|
||||
import org.isoron.uhabits.commands.CommandRunner;
|
||||
import org.isoron.uhabits.commands.CreateHabitCommand;
|
||||
import org.isoron.uhabits.commands.EditHabitCommand;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.utils.ColorUtils;
|
||||
import org.isoron.uhabits.utils.DateUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class EditHabitDialogFragment extends AppCompatDialogFragment
|
||||
implements OnClickListener, WeekdayPickerDialog.OnWeekdaysPickedListener,
|
||||
TimePickerDialog.OnTimeSetListener, Spinner.OnItemSelectedListener
|
||||
public class EditHabitDialogFragment extends BaseDialogFragment
|
||||
{
|
||||
private Integer mode;
|
||||
static final int EDIT_MODE = 0;
|
||||
static final int CREATE_MODE = 1;
|
||||
|
||||
private Habit originalHabit;
|
||||
private Habit modifiedHabit;
|
||||
|
||||
private TextView tvName;
|
||||
private TextView tvDescription;
|
||||
private TextView tvFreqNum;
|
||||
private TextView tvFreqDen;
|
||||
private TextView tvReminderTime;
|
||||
private TextView tvReminderDays;
|
||||
|
||||
private Spinner sFrequency;
|
||||
private ViewGroup llCustomFrequency;
|
||||
private ViewGroup llReminderDays;
|
||||
|
||||
private SharedPreferences prefs;
|
||||
private boolean is24HourMode;
|
||||
|
||||
public static EditHabitDialogFragment editSingleHabitFragment(long id)
|
||||
public static EditHabitDialogFragment newInstance(long habitId)
|
||||
{
|
||||
EditHabitDialogFragment frag = new EditHabitDialogFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("habitId", id);
|
||||
args.putInt("editMode", EDIT_MODE);
|
||||
args.putLong("habitId", habitId);
|
||||
frag.setArguments(args);
|
||||
return frag;
|
||||
}
|
||||
|
||||
public static EditHabitDialogFragment createHabitFragment()
|
||||
{
|
||||
EditHabitDialogFragment frag = new EditHabitDialogFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putInt("editMode", CREATE_MODE);
|
||||
frag.setArguments(args);
|
||||
return frag;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState)
|
||||
{
|
||||
View view = inflater.inflate(R.layout.edit_habit, container, false);
|
||||
tvName = (TextView) view.findViewById(R.id.input_name);
|
||||
tvDescription = (TextView) view.findViewById(R.id.input_description);
|
||||
tvFreqNum = (TextView) view.findViewById(R.id.input_freq_num);
|
||||
tvFreqDen = (TextView) view.findViewById(R.id.input_freq_den);
|
||||
tvReminderTime = (TextView) view.findViewById(R.id.inputReminderTime);
|
||||
tvReminderDays = (TextView) view.findViewById(R.id.inputReminderDays);
|
||||
|
||||
sFrequency = (Spinner) view.findViewById(R.id.sFrequency);
|
||||
llCustomFrequency = (ViewGroup) view.findViewById(R.id.llCustomFrequency);
|
||||
llReminderDays = (ViewGroup) view.findViewById(R.id.llReminderDays);
|
||||
|
||||
Button buttonSave = (Button) view.findViewById(R.id.buttonSave);
|
||||
Button buttonDiscard = (Button) view.findViewById(R.id.buttonDiscard);
|
||||
ImageButton buttonPickColor = (ImageButton) view.findViewById(R.id.buttonPickColor);
|
||||
|
||||
buttonSave.setOnClickListener(this);
|
||||
buttonDiscard.setOnClickListener(this);
|
||||
tvReminderTime.setOnClickListener(this);
|
||||
tvReminderDays.setOnClickListener(this);
|
||||
buttonPickColor.setOnClickListener(this);
|
||||
sFrequency.setOnItemSelectedListener(this);
|
||||
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||
|
||||
Bundle args = getArguments();
|
||||
mode = (Integer) args.get("editMode");
|
||||
|
||||
is24HourMode = DateFormat.is24HourFormat(getActivity());
|
||||
|
||||
if (mode == CREATE_MODE)
|
||||
{
|
||||
getDialog().setTitle(R.string.create_habit);
|
||||
modifiedHabit = new Habit();
|
||||
modifiedHabit.freqNum = 1;
|
||||
modifiedHabit.freqDen = 1;
|
||||
modifiedHabit.color = prefs.getInt("pref_default_habit_palette_color", modifiedHabit.color);
|
||||
}
|
||||
else if (mode == EDIT_MODE)
|
||||
{
|
||||
Long habitId = (Long) args.get("habitId");
|
||||
if(habitId == null) throw new IllegalArgumentException("habitId must be specified");
|
||||
|
||||
originalHabit = Habit.get(habitId);
|
||||
modifiedHabit = new Habit(originalHabit);
|
||||
|
||||
getDialog().setTitle(R.string.edit_habit);
|
||||
tvName.append(modifiedHabit.name);
|
||||
tvDescription.append(modifiedHabit.description);
|
||||
}
|
||||
|
||||
if(savedInstanceState != null)
|
||||
{
|
||||
modifiedHabit.color = savedInstanceState.getInt("color", modifiedHabit.color);
|
||||
modifiedHabit.reminderMin = savedInstanceState.getInt("reminderMin", -1);
|
||||
modifiedHabit.reminderHour = savedInstanceState.getInt("reminderHour", -1);
|
||||
modifiedHabit.reminderDays = savedInstanceState.getInt("reminderDays", -1);
|
||||
|
||||
if(modifiedHabit.reminderMin < 0)
|
||||
modifiedHabit.clearReminder();
|
||||
}
|
||||
|
||||
tvFreqNum.append(modifiedHabit.freqNum.toString());
|
||||
tvFreqDen.append(modifiedHabit.freqDen.toString());
|
||||
|
||||
changeColor(modifiedHabit.color);
|
||||
updateFrequency();
|
||||
updateReminder();
|
||||
|
||||
View view = super.onCreateView(inflater, container, savedInstanceState);
|
||||
getDialog().setTitle(R.string.edit_habit);
|
||||
initializeHabits();
|
||||
restoreSavedInstance(savedInstanceState);
|
||||
helper.populateForm(modifiedHabit);
|
||||
return view;
|
||||
}
|
||||
|
||||
private void changeColor(int paletteColor)
|
||||
private void initializeHabits()
|
||||
{
|
||||
modifiedHabit.color = paletteColor;
|
||||
tvName.setTextColor(ColorUtils.getColor(getActivity(), paletteColor));
|
||||
Long habitId = (Long) getArguments().get("habitId");
|
||||
if(habitId == null) throw new IllegalArgumentException("habitId must be specified");
|
||||
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
editor.putInt("pref_default_habit_palette_color", paletteColor);
|
||||
editor.apply();
|
||||
originalHabit = Habit.get(habitId);
|
||||
modifiedHabit = new Habit(originalHabit);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
private void updateReminder()
|
||||
protected void saveHabit()
|
||||
{
|
||||
if (modifiedHabit.hasReminder())
|
||||
{
|
||||
tvReminderTime.setText(DateUtils.formatTime(getActivity(), modifiedHabit.reminderHour,
|
||||
modifiedHabit.reminderMin));
|
||||
llReminderDays.setVisibility(View.VISIBLE);
|
||||
|
||||
boolean weekdays[] = DateUtils.unpackWeekdayList(modifiedHabit.reminderDays);
|
||||
tvReminderDays.setText(DateUtils.formatWeekdayList(getActivity(), weekdays));
|
||||
}
|
||||
else
|
||||
{
|
||||
tvReminderTime.setText(R.string.reminder_off);
|
||||
llReminderDays.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
switch(v.getId())
|
||||
{
|
||||
case R.id.inputReminderTime:
|
||||
onDateSpinnerClick();
|
||||
break;
|
||||
|
||||
case R.id.inputReminderDays:
|
||||
onWeekdayClick();
|
||||
break;
|
||||
|
||||
case R.id.buttonSave:
|
||||
onSaveButtonClick();
|
||||
break;
|
||||
|
||||
case R.id.buttonDiscard:
|
||||
dismiss();
|
||||
break;
|
||||
|
||||
case R.id.buttonPickColor:
|
||||
onColorButtonClick();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void onColorButtonClick()
|
||||
{
|
||||
int originalAndroidColor = ColorUtils.getColor(getActivity(), modifiedHabit.color);
|
||||
|
||||
ColorPickerDialog picker = ColorPickerDialog.newInstance(
|
||||
R.string.color_picker_default_title, ColorUtils.getPalette(getActivity()),
|
||||
originalAndroidColor, 4, ColorPickerDialog.SIZE_SMALL);
|
||||
|
||||
picker.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener()
|
||||
{
|
||||
public void onColorSelected(int androidColor)
|
||||
{
|
||||
int paletteColor = ColorUtils.colorToPaletteIndex(getActivity(), androidColor);
|
||||
changeColor(paletteColor);
|
||||
}
|
||||
});
|
||||
picker.show(getFragmentManager(), "picker");
|
||||
}
|
||||
|
||||
private void onSaveButtonClick()
|
||||
{
|
||||
modifiedHabit.name = tvName.getText().toString().trim();
|
||||
modifiedHabit.description = tvDescription.getText().toString().trim();
|
||||
String freqNum = tvFreqNum.getText().toString();
|
||||
String freqDen = tvFreqDen.getText().toString();
|
||||
if(!freqNum.isEmpty()) modifiedHabit.freqNum = Integer.parseInt(freqNum);
|
||||
if(!freqDen.isEmpty()) modifiedHabit.freqDen = Integer.parseInt(freqDen);
|
||||
|
||||
if (!validate()) return;
|
||||
|
||||
if (mode == EDIT_MODE)
|
||||
{
|
||||
Command command = new EditHabitCommand(originalHabit, modifiedHabit);
|
||||
CommandRunner.getInstance().execute(command, originalHabit.getId());
|
||||
}
|
||||
else if (mode == CREATE_MODE)
|
||||
{
|
||||
Command command = new CreateHabitCommand(modifiedHabit);
|
||||
CommandRunner.getInstance().execute(command, null);
|
||||
}
|
||||
|
||||
dismiss();
|
||||
}
|
||||
|
||||
private boolean validate()
|
||||
{
|
||||
Boolean valid = true;
|
||||
|
||||
if (modifiedHabit.name.length() == 0)
|
||||
{
|
||||
tvName.setError(getString(R.string.validation_name_should_not_be_blank));
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (modifiedHabit.freqNum <= 0)
|
||||
{
|
||||
tvFreqNum.setError(getString(R.string.validation_number_should_be_positive));
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (modifiedHabit.freqNum > modifiedHabit.freqDen)
|
||||
{
|
||||
tvFreqNum.setError(getString(R.string.validation_at_most_one_rep_per_day));
|
||||
valid = false;
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
private void onDateSpinnerClick()
|
||||
{
|
||||
int defaultHour = 8;
|
||||
int defaultMin = 0;
|
||||
|
||||
if (modifiedHabit.hasReminder())
|
||||
{
|
||||
defaultHour = modifiedHabit.reminderHour;
|
||||
defaultMin = modifiedHabit.reminderMin;
|
||||
}
|
||||
|
||||
TimePickerDialog timePicker =
|
||||
TimePickerDialog.newInstance(this, defaultHour, defaultMin, is24HourMode);
|
||||
timePicker.show(getFragmentManager(), "timePicker");
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
private void onWeekdayClick()
|
||||
{
|
||||
if(!modifiedHabit.hasReminder()) return;
|
||||
|
||||
WeekdayPickerDialog dialog = new WeekdayPickerDialog();
|
||||
dialog.setListener(this);
|
||||
dialog.setSelectedDays(DateUtils.unpackWeekdayList(modifiedHabit.reminderDays));
|
||||
dialog.show(getFragmentManager(), "weekdayPicker");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeSet(RadialPickerLayout view, int hour, int minute)
|
||||
{
|
||||
modifiedHabit.reminderHour = hour;
|
||||
modifiedHabit.reminderMin = minute;
|
||||
modifiedHabit.reminderDays = DateUtils.ALL_WEEK_DAYS;
|
||||
updateReminder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimeCleared(RadialPickerLayout view)
|
||||
{
|
||||
modifiedHabit.clearReminder();
|
||||
updateReminder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWeekdaysPicked(boolean[] selectedDays)
|
||||
{
|
||||
int count = 0;
|
||||
for(int i = 0; i < 7; i++)
|
||||
if(selectedDays[i]) count++;
|
||||
|
||||
if(count == 0) Arrays.fill(selectedDays, true);
|
||||
|
||||
modifiedHabit.reminderDays = DateUtils.packWeekdayList(selectedDays);
|
||||
updateReminder();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public void onSaveInstanceState(Bundle outState)
|
||||
{
|
||||
super.onSaveInstanceState(outState);
|
||||
|
||||
outState.putInt("color", modifiedHabit.color);
|
||||
|
||||
if(modifiedHabit.hasReminder())
|
||||
{
|
||||
outState.putInt("reminderMin", modifiedHabit.reminderMin);
|
||||
outState.putInt("reminderHour", modifiedHabit.reminderHour);
|
||||
outState.putInt("reminderDays", modifiedHabit.reminderDays);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
|
||||
{
|
||||
if(parent.getId() == R.id.sFrequency)
|
||||
{
|
||||
switch (position)
|
||||
{
|
||||
case 0:
|
||||
modifiedHabit.freqNum = 1;
|
||||
modifiedHabit.freqDen = 1;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
modifiedHabit.freqNum = 1;
|
||||
modifiedHabit.freqDen = 7;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
modifiedHabit.freqNum = 2;
|
||||
modifiedHabit.freqDen = 7;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
modifiedHabit.freqNum = 5;
|
||||
modifiedHabit.freqDen = 7;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
modifiedHabit.freqNum = 3;
|
||||
modifiedHabit.freqDen = 7;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
updateFrequency();
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
private void updateFrequency()
|
||||
{
|
||||
int quickSelectPosition = -1;
|
||||
|
||||
if(modifiedHabit.freqNum.equals(modifiedHabit.freqDen))
|
||||
quickSelectPosition = 0;
|
||||
|
||||
else if(modifiedHabit.freqNum == 1 && modifiedHabit.freqDen == 7)
|
||||
quickSelectPosition = 1;
|
||||
|
||||
else if(modifiedHabit.freqNum == 2 && modifiedHabit.freqDen == 7)
|
||||
quickSelectPosition = 2;
|
||||
|
||||
else if(modifiedHabit.freqNum == 5 && modifiedHabit.freqDen == 7)
|
||||
quickSelectPosition = 3;
|
||||
|
||||
if(quickSelectPosition >= 0)
|
||||
{
|
||||
sFrequency.setVisibility(View.VISIBLE);
|
||||
sFrequency.setSelection(quickSelectPosition);
|
||||
llCustomFrequency.setVisibility(View.GONE);
|
||||
tvFreqNum.setText(modifiedHabit.freqNum.toString());
|
||||
tvFreqDen.setText(modifiedHabit.freqDen.toString());
|
||||
}
|
||||
else
|
||||
{
|
||||
sFrequency.setVisibility(View.GONE);
|
||||
llCustomFrequency.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent)
|
||||
{
|
||||
|
||||
Command command = new EditHabitCommand(originalHabit, modifiedHabit);
|
||||
CommandRunner.getInstance().execute(command, originalHabit.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.isoron.uhabits.commands.DeleteHabitsCommand;
|
||||
import org.isoron.uhabits.commands.UnarchiveHabitsCommand;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.ui.BaseActivity;
|
||||
import org.isoron.uhabits.ui.habits.edit.BaseDialogFragment;
|
||||
import org.isoron.uhabits.ui.habits.edit.EditHabitDialogFragment;
|
||||
import org.isoron.uhabits.utils.ColorUtils;
|
||||
|
||||
@@ -205,8 +206,8 @@ public class HabitListSelectionCallback implements ActionMode.Callback
|
||||
|
||||
private void editHabit(Habit habit)
|
||||
{
|
||||
EditHabitDialogFragment
|
||||
frag = EditHabitDialogFragment.editSingleHabitFragment(habit.getId());
|
||||
BaseDialogFragment
|
||||
frag = EditHabitDialogFragment.newInstance(habit.getId());
|
||||
frag.show(activity.getSupportFragmentManager(), "editHabit");
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,8 @@ import org.isoron.uhabits.commands.ToggleRepetitionCommand;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.ui.BaseActivity;
|
||||
import org.isoron.uhabits.ui.HintManager;
|
||||
import org.isoron.uhabits.ui.habits.edit.EditHabitDialogFragment;
|
||||
import org.isoron.uhabits.ui.habits.edit.BaseDialogFragment;
|
||||
import org.isoron.uhabits.ui.habits.edit.CreateHabitDialogFragment;
|
||||
import org.isoron.uhabits.utils.InterfaceUtils;
|
||||
|
||||
import butterknife.BindView;
|
||||
@@ -141,7 +142,7 @@ public class ListHabitsFragment extends Fragment
|
||||
|
||||
private void showCreateHabitScreen()
|
||||
{
|
||||
EditHabitDialogFragment frag = EditHabitDialogFragment.createHabitFragment();
|
||||
BaseDialogFragment frag = new CreateHabitDialogFragment();
|
||||
frag.show(getFragmentManager(), "editHabit");
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.models.ModelObservable;
|
||||
import org.isoron.uhabits.tasks.BaseTask;
|
||||
import org.isoron.uhabits.ui.habits.edit.BaseDialogFragment;
|
||||
import org.isoron.uhabits.ui.habits.edit.EditHabitDialogFragment;
|
||||
import org.isoron.uhabits.ui.habits.edit.HistoryEditorDialog;
|
||||
import org.isoron.uhabits.utils.DateUtils;
|
||||
@@ -146,7 +147,7 @@ public class ShowHabitFragment extends Fragment implements ModelObservable.Liste
|
||||
{
|
||||
if(habit == null) return false;
|
||||
|
||||
EditHabitDialogFragment frag = EditHabitDialogFragment.editSingleHabitFragment(habit.getId());
|
||||
BaseDialogFragment frag = EditHabitDialogFragment.newInstance(habit.getId());
|
||||
frag.show(getFragmentManager(), "editHabit");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -82,4 +82,15 @@ public class Preferences
|
||||
{
|
||||
return prefs.getBoolean("pref_short_toggle", false);
|
||||
}
|
||||
|
||||
public Integer getDefaultHabitColor(int defaultColor)
|
||||
{
|
||||
return prefs.getInt("pref_default_habit_palette_color", defaultColor);
|
||||
}
|
||||
|
||||
public void setDefaultHabitColor(int color)
|
||||
{
|
||||
prefs.edit().putInt("pref_default_habit_palette_color", color).apply();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
style="@style/dialogForm"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
tools:context=".ui.habits.edit.EditHabitDialogFragment"
|
||||
tools:context=".ui.habits.edit.BaseDialogFragment"
|
||||
tools:ignore="MergeRootFrame">
|
||||
|
||||
<LinearLayout
|
||||
@@ -34,7 +34,7 @@
|
||||
style="@style/dialogFormRow">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/input_name"
|
||||
android:id="@+id/tvName"
|
||||
style="@style/dialogFormInput"
|
||||
android:hint="@string/name">
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
</LinearLayout>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/input_description"
|
||||
android:id="@+id/tvDescription"
|
||||
style="@style/dialogFormInputMultiline"
|
||||
android:hint="@string/description_hint"/>
|
||||
|
||||
@@ -78,7 +78,7 @@
|
||||
android:gravity="fill">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/input_freq_num"
|
||||
android:id="@+id/tvFreqNum"
|
||||
style="@style/dialogFormInputSmallNumber"/>
|
||||
|
||||
<TextView
|
||||
@@ -88,7 +88,7 @@
|
||||
android:gravity="center"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/input_freq_den"
|
||||
android:id="@+id/tvFreqDen"
|
||||
style="@style/dialogFormInputSmallNumber"/>
|
||||
|
||||
<TextView
|
||||
@@ -111,7 +111,7 @@
|
||||
android:text="@string/reminder"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/inputReminderTime"
|
||||
android:id="@+id/tvReminderTime"
|
||||
style="@style/dialogFormSpinner"/>
|
||||
</LinearLayout>
|
||||
|
||||
@@ -125,7 +125,7 @@
|
||||
android:text=""/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/inputReminderDays"
|
||||
android:id="@+id/tvReminderDays"
|
||||
style="@style/dialogFormSpinner"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
Reference in New Issue
Block a user