mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
EditHabitActivity: Make save button functional
This commit is contained in:
@@ -88,15 +88,21 @@ class FrequencyPickerDialog(var freqNumerator: Int,
|
|||||||
// NOP
|
// NOP
|
||||||
}
|
}
|
||||||
contentView.everyXDaysRadioButton.isChecked -> {
|
contentView.everyXDaysRadioButton.isChecked -> {
|
||||||
|
if (contentView.everyXDaysTextView.text.isNotEmpty()) {
|
||||||
denominator = Integer.parseInt(contentView.everyXDaysTextView.text.toString())
|
denominator = Integer.parseInt(contentView.everyXDaysTextView.text.toString())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
contentView.xTimesPerWeekRadioButton.isChecked -> {
|
contentView.xTimesPerWeekRadioButton.isChecked -> {
|
||||||
|
if (contentView.xTimesPerWeekTextView.text.isNotEmpty()) {
|
||||||
numerator = Integer.parseInt(contentView.xTimesPerWeekTextView.text.toString())
|
numerator = Integer.parseInt(contentView.xTimesPerWeekTextView.text.toString())
|
||||||
denominator = 7
|
denominator = 7
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else -> {
|
else -> {
|
||||||
|
if (contentView.xTimesPerMonthTextView.text.isNotEmpty()) {
|
||||||
numerator = Integer.parseInt(contentView.xTimesPerMonthTextView.text.toString())
|
numerator = Integer.parseInt(contentView.xTimesPerMonthTextView.text.toString())
|
||||||
denominator = 31
|
denominator = 30
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (numerator >= denominator || numerator < 1) {
|
if (numerator >= denominator || numerator < 1) {
|
||||||
@@ -138,7 +144,7 @@ class FrequencyPickerDialog(var freqNumerator: Int,
|
|||||||
contentView.xTimesPerMonthTextView.setText(freqNumerator.toString())
|
contentView.xTimesPerMonthTextView.setText(freqNumerator.toString())
|
||||||
focus(contentView.xTimesPerMonthTextView)
|
focus(contentView.xTimesPerMonthTextView)
|
||||||
} else {
|
} else {
|
||||||
Log.w("FrequencyPickerDialog", "Unknown frequency: " + freqNumerator + "/" + freqDenominator)
|
Log.w("FrequencyPickerDialog", "Unknown frequency: $freqNumerator/$freqDenominator")
|
||||||
contentView.everyDayRadioButton.isChecked = true
|
contentView.everyDayRadioButton.isChecked = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,39 +26,68 @@ import android.text.format.*
|
|||||||
import android.view.*
|
import android.view.*
|
||||||
import androidx.appcompat.app.*
|
import androidx.appcompat.app.*
|
||||||
import com.android.datetimepicker.time.*
|
import com.android.datetimepicker.time.*
|
||||||
|
import kotlinx.android.synthetic.main.activity_edit_habit.*
|
||||||
import org.isoron.androidbase.utils.*
|
import org.isoron.androidbase.utils.*
|
||||||
import org.isoron.uhabits.*
|
import org.isoron.uhabits.*
|
||||||
import org.isoron.uhabits.activities.*
|
import org.isoron.uhabits.activities.*
|
||||||
import org.isoron.uhabits.activities.common.dialogs.*
|
import org.isoron.uhabits.activities.common.dialogs.*
|
||||||
|
import org.isoron.uhabits.core.commands.*
|
||||||
import org.isoron.uhabits.core.models.*
|
import org.isoron.uhabits.core.models.*
|
||||||
import org.isoron.uhabits.core.preferences.*
|
|
||||||
import org.isoron.uhabits.databinding.*
|
import org.isoron.uhabits.databinding.*
|
||||||
import org.isoron.uhabits.preferences.*
|
|
||||||
import org.isoron.uhabits.utils.*
|
import org.isoron.uhabits.utils.*
|
||||||
|
|
||||||
class EditHabitActivity : AppCompatActivity() {
|
class EditHabitActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private lateinit var themeSwitcher: AndroidThemeSwitcher
|
private lateinit var themeSwitcher: AndroidThemeSwitcher
|
||||||
|
|
||||||
private lateinit var binding: ActivityEditHabitBinding
|
private lateinit var binding: ActivityEditHabitBinding
|
||||||
|
private lateinit var commandRunner: CommandRunner
|
||||||
|
|
||||||
|
var habitId = -1L
|
||||||
var paletteColor = 11
|
var paletteColor = 11
|
||||||
var androidColor = 0
|
var androidColor = 0
|
||||||
|
|
||||||
var freqNum = 1
|
var freqNum = 1
|
||||||
var freqDen = 1
|
var freqDen = 1
|
||||||
var reminderHour = -1
|
var reminderHour = -1
|
||||||
var reminderMin = -1
|
var reminderMin = -1
|
||||||
var reminderDays = WeekdayList.EVERY_DAY
|
var reminderDays: WeekdayList = WeekdayList.EVERY_DAY
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(state: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(state)
|
||||||
val prefs = Preferences(SharedPreferencesStorage(this))
|
|
||||||
themeSwitcher = AndroidThemeSwitcher(this, prefs)
|
val component = (application as HabitsApplication).component
|
||||||
|
themeSwitcher = AndroidThemeSwitcher(this, component.preferences)
|
||||||
themeSwitcher.apply()
|
themeSwitcher.apply()
|
||||||
|
|
||||||
binding = ActivityEditHabitBinding.inflate(layoutInflater)
|
binding = ActivityEditHabitBinding.inflate(layoutInflater)
|
||||||
setContentView(binding.root)
|
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()
|
updateColors()
|
||||||
|
|
||||||
setSupportActionBar(binding.toolbar)
|
setSupportActionBar(binding.toolbar)
|
||||||
@@ -120,8 +149,47 @@ class EditHabitActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
binding.buttonSave.setOnClickListener {
|
binding.buttonSave.setOnClickListener {
|
||||||
|
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()
|
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() {
|
private fun populateReminder() {
|
||||||
@@ -160,4 +228,18 @@ class EditHabitActivity : AppCompatActivity() {
|
|||||||
binding.toolbar.setBackgroundColor(androidColor)
|
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>) {
|
override fun showEditHabitsScreen(habits: List<Habit>) {
|
||||||
val dialog = editHabitDialogFactory.edit(habits[0])
|
val intent = intentFactory.startEditActivity(activity!!, habits[0])
|
||||||
activity.showDialog(dialog, "editNumericalHabit")
|
activity.startActivity(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun showFAQScreen() {
|
override fun showFAQScreen() {
|
||||||
|
|||||||
@@ -84,7 +84,13 @@ class IntentFactory
|
|||||||
fun codeContributors(context: Context) =
|
fun codeContributors(context: Context) =
|
||||||
buildViewIntent(context.getString(R.string.codeContributorsURL))
|
buildViewIntent(context.getString(R.string.codeContributorsURL))
|
||||||
|
|
||||||
fun startEditActivity(context: Context): Intent? {
|
fun startEditActivity(context: Context): Intent {
|
||||||
return Intent(context, EditHabitActivity::class.java)
|
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" />
|
android:text="@string/name" />
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
|
android:id="@+id/nameInput"
|
||||||
style="@style/FormInput"
|
style="@style/FormInput"
|
||||||
|
android:maxLines="1"
|
||||||
|
android:inputType="textCapSentences"
|
||||||
android:hint="@string/exercise_habit_name"
|
android:hint="@string/exercise_habit_name"
|
||||||
/>
|
/>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</FrameLayout>
|
</FrameLayout>
|
||||||
|
|
||||||
<!-- Habit Color -->
|
<!-- Habit Color -->
|
||||||
<FrameLayut
|
<FrameLayout
|
||||||
android:layout_width="80dp"
|
android:layout_width="80dp"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:clipChildren="false"
|
android:clipChildren="false"
|
||||||
@@ -106,7 +109,7 @@
|
|||||||
android:backgroundTint="#E23673" />
|
android:backgroundTint="#E23673" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</FrameLayut>
|
</FrameLayout>
|
||||||
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -128,7 +131,9 @@
|
|||||||
android:text="@string/question" />
|
android:text="@string/question" />
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
|
android:id="@+id/questionInput"
|
||||||
style="@style/FormInput"
|
style="@style/FormInput"
|
||||||
|
android:inputType="textCapSentences"
|
||||||
android:hint="@string/example_question_boolean"
|
android:hint="@string/example_question_boolean"
|
||||||
/>
|
/>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -203,6 +208,7 @@
|
|||||||
android:text="@string/notes" />
|
android:text="@string/notes" />
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
|
android:id="@+id/notesInput"
|
||||||
style="@style/FormInput"
|
style="@style/FormInput"
|
||||||
android:hint="@string/example_notes" />
|
android:hint="@string/example_notes" />
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|||||||
@@ -112,6 +112,7 @@ public class ListHabitsSelectionMenuBehavior
|
|||||||
public void onEditHabits()
|
public void onEditHabits()
|
||||||
{
|
{
|
||||||
screen.showEditHabitsScreen(adapter.getSelected());
|
screen.showEditHabitsScreen(adapter.getSelected());
|
||||||
|
adapter.clearSelection();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onUnarchiveHabits()
|
public void onUnarchiveHabits()
|
||||||
|
|||||||
Reference in New Issue
Block a user