|
|
|
@ -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())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|