EditHabitActivity: Make save button functional

pull/605/head
Alinson S. Xavier 5 years ago
parent 9ccb2b2737
commit 403ed8b250

@ -88,15 +88,21 @@ class FrequencyPickerDialog(var freqNumerator: Int,
// NOP
}
contentView.everyXDaysRadioButton.isChecked -> {
denominator = Integer.parseInt(contentView.everyXDaysTextView.text.toString())
if (contentView.everyXDaysTextView.text.isNotEmpty()) {
denominator = Integer.parseInt(contentView.everyXDaysTextView.text.toString())
}
}
contentView.xTimesPerWeekRadioButton.isChecked -> {
numerator = Integer.parseInt(contentView.xTimesPerWeekTextView.text.toString())
denominator = 7
if (contentView.xTimesPerWeekTextView.text.isNotEmpty()) {
numerator = Integer.parseInt(contentView.xTimesPerWeekTextView.text.toString())
denominator = 7
}
}
else -> {
numerator = Integer.parseInt(contentView.xTimesPerMonthTextView.text.toString())
denominator = 31
if (contentView.xTimesPerMonthTextView.text.isNotEmpty()) {
numerator = Integer.parseInt(contentView.xTimesPerMonthTextView.text.toString())
denominator = 30
}
}
}
if (numerator >= denominator || numerator < 1) {
@ -138,7 +144,7 @@ class FrequencyPickerDialog(var freqNumerator: Int,
contentView.xTimesPerMonthTextView.setText(freqNumerator.toString())
focus(contentView.xTimesPerMonthTextView)
} else {
Log.w("FrequencyPickerDialog", "Unknown frequency: " + freqNumerator + "/" + freqDenominator)
Log.w("FrequencyPickerDialog", "Unknown frequency: $freqNumerator/$freqDenominator")
contentView.everyDayRadioButton.isChecked = true
}
}

@ -26,39 +26,68 @@ import android.text.format.*
import android.view.*
import androidx.appcompat.app.*
import com.android.datetimepicker.time.*
import kotlinx.android.synthetic.main.activity_edit_habit.*
import org.isoron.androidbase.utils.*
import org.isoron.uhabits.*
import org.isoron.uhabits.activities.*
import org.isoron.uhabits.activities.common.dialogs.*
import org.isoron.uhabits.core.commands.*
import org.isoron.uhabits.core.models.*
import org.isoron.uhabits.core.preferences.*
import org.isoron.uhabits.databinding.*
import org.isoron.uhabits.preferences.*
import org.isoron.uhabits.utils.*
class EditHabitActivity : AppCompatActivity() {
private lateinit var themeSwitcher: AndroidThemeSwitcher
private lateinit var binding: ActivityEditHabitBinding
private lateinit var commandRunner: CommandRunner
var habitId = -1L
var paletteColor = 11
var androidColor = 0
var freqNum = 1
var freqDen = 1
var reminderHour = -1
var reminderMin = -1
var reminderDays = WeekdayList.EVERY_DAY
var reminderDays: WeekdayList = WeekdayList.EVERY_DAY
override fun onCreate(state: Bundle?) {
super.onCreate(state)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val prefs = Preferences(SharedPreferencesStorage(this))
themeSwitcher = AndroidThemeSwitcher(this, prefs)
val component = (application as HabitsApplication).component
themeSwitcher = AndroidThemeSwitcher(this, component.preferences)
themeSwitcher.apply()
binding = ActivityEditHabitBinding.inflate(layoutInflater)
setContentView(binding.root)
if (intent.hasExtra("habitId")) {
binding.toolbar.title = getString(R.string.edit_habit)
habitId = intent.getLongExtra("habitId", -1)
val habit = component.habitList.getById(habitId)!!
paletteColor = habit.color
freqNum = habit.frequency.numerator
freqDen = habit.frequency.denominator
if (habit.hasReminder()) {
reminderHour = habit.reminder.hour
reminderMin = habit.reminder.minute
reminderDays = habit.reminder.days
}
binding.nameInput.setText(habit.name)
binding.questionInput.setText(habit.question)
binding.notesInput.setText(habit.description)
}
if (state != null) {
habitId = state.getLong("habitId")
paletteColor = state.getInt("paletteColor")
freqNum = state.getInt("freqNum")
freqDen = state.getInt("freqDen")
reminderHour = state.getInt("reminderHour")
reminderMin = state.getInt("reminderMin")
reminderDays = WeekdayList(state.getInt("reminderDays"))
}
updateColors()
setSupportActionBar(binding.toolbar)
@ -120,8 +149,47 @@ class EditHabitActivity : AppCompatActivity() {
}
binding.buttonSave.setOnClickListener {
finish()
if(validate()) save()
}
}
private fun save() {
val component = (application as HabitsApplication).component
val habit = component.modelFactory.buildHabit()
var original: Habit? = null
if (habitId >= 0) {
original = component.habitList.getById(habitId)!!
habit.copyFrom(original)
}
habit.name = nameInput.text.trim().toString()
habit.question = questionInput.text.trim().toString()
habit.description = notesInput.text.trim().toString()
habit.color = paletteColor
if (reminderHour >= 0) {
habit.setReminder(Reminder(reminderHour, reminderMin, reminderDays))
}
habit.frequency = Frequency(freqNum, freqDen)
habit.unit = ""
habit.targetValue = 1.0
habit.type = Habit.YES_NO_HABIT
val command = if (habitId >= 0) {
component.editHabitCommandFactory.create(component.habitList, original, habit)
} else {
component.createHabitCommandFactory.create(component.habitList, habit)
}
component.commandRunner.execute(command, null)
finish()
}
private fun validate(): Boolean {
if (nameInput.text.isEmpty()) {
nameInput.error = getString(R.string.validation_name_should_not_be_blank)
return false
}
return true
}
private fun populateReminder() {
@ -160,4 +228,18 @@ class EditHabitActivity : AppCompatActivity() {
binding.toolbar.setBackgroundColor(androidColor)
}
}
override fun onSaveInstanceState(state: Bundle) {
super.onSaveInstanceState(state)
with(state) {
putLong("habitId", habitId)
putInt("paletteColor", paletteColor)
putInt("androidColor", androidColor)
putInt("freqNum", freqNum)
putInt("freqDen", freqDen)
putInt("reminderHour", reminderHour)
putInt("reminderMin", reminderMin)
putInt("reminderDays", reminderDays.toInteger())
}
}
}

@ -147,8 +147,8 @@ class ListHabitsScreen
}
override fun showEditHabitsScreen(habits: List<Habit>) {
val dialog = editHabitDialogFactory.edit(habits[0])
activity.showDialog(dialog, "editNumericalHabit")
val intent = intentFactory.startEditActivity(activity!!, habits[0])
activity.startActivity(intent)
}
override fun showFAQScreen() {

@ -84,7 +84,13 @@ class IntentFactory
fun codeContributors(context: Context) =
buildViewIntent(context.getString(R.string.codeContributorsURL))
fun startEditActivity(context: Context): Intent? {
fun startEditActivity(context: Context): Intent {
return Intent(context, EditHabitActivity::class.java)
}
fun startEditActivity(context: Context, habit: Habit): Intent {
val intent = startEditActivity(context)
intent.putExtra("habitId", habit.id)
return intent
}
}

@ -74,14 +74,17 @@
android:text="@string/name" />
<EditText
android:id="@+id/nameInput"
style="@style/FormInput"
android:maxLines="1"
android:inputType="textCapSentences"
android:hint="@string/exercise_habit_name"
/>
</LinearLayout>
</FrameLayout>
<!-- Habit Color -->
<FrameLayut
<FrameLayout
android:layout_width="80dp"
android:layout_height="match_parent"
android:clipChildren="false"
@ -106,7 +109,7 @@
android:backgroundTint="#E23673" />
</LinearLayout>
</FrameLayut>
</FrameLayout>
</LinearLayout>
@ -128,7 +131,9 @@
android:text="@string/question" />
<EditText
android:id="@+id/questionInput"
style="@style/FormInput"
android:inputType="textCapSentences"
android:hint="@string/example_question_boolean"
/>
</LinearLayout>
@ -203,6 +208,7 @@
android:text="@string/notes" />
<EditText
android:id="@+id/notesInput"
style="@style/FormInput"
android:hint="@string/example_notes" />
</LinearLayout>

@ -112,6 +112,7 @@ public class ListHabitsSelectionMenuBehavior
public void onEditHabits()
{
screen.showEditHabitsScreen(adapter.getSelected());
adapter.clearSelection();
}
public void onUnarchiveHabits()

Loading…
Cancel
Save