mirror of https://github.com/iSoron/uhabits.git
parent
5653651c0d
commit
7d2e8573f8
@ -0,0 +1,3 @@
|
||||
alter table Habits add column target_type integer not null default 0;
|
||||
alter table Habits add column target_value real not null default 0;
|
||||
alter table Habits add column unit text not null default "";
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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());
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.activities.habits.edit;
|
||||
|
||||
import android.os.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.models.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
public class 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
/*
|
||||
* 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.models.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import butterknife.*;
|
||||
|
||||
public class NumericalHabitDialogHelper
|
||||
{
|
||||
private DialogFragment frag;
|
||||
|
||||
@BindView(R.id.tvName)
|
||||
TextView tvName;
|
||||
|
||||
@BindView(R.id.tvDescription)
|
||||
TextView tvDescription;
|
||||
|
||||
@BindView(R.id.tvUnit)
|
||||
TextView 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.getText().toString().trim());
|
||||
habit.setTargetType(tvTargetType.getSelectedItemPosition());
|
||||
habit.setTargetValue(
|
||||
Double.parseDouble(tvTargetValue.getText().toString()));
|
||||
habit.setUnit(tvUnit.getText().toString().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.setText(habit.getDescription());
|
||||
|
||||
if(!habit.getUnit().isEmpty())
|
||||
tvUnit.setText(habit.getUnit());
|
||||
|
||||
tvTargetType.setSelection(habit.getTargetType());
|
||||
tvTargetValue.setText(String.format("%.0f", 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;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue