mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-14 04:58:52 -06:00
Implement edits sub habits
This commit is contained in:
@@ -75,7 +75,7 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
private lateinit var binding: ActivityEditHabitBinding
|
||||
private lateinit var commandRunner: CommandRunner
|
||||
|
||||
var habitId = -1L
|
||||
var habitUUID: String? = null
|
||||
lateinit var habitType: HabitType
|
||||
var parentGroup: HabitGroup? = null
|
||||
var unit = ""
|
||||
@@ -98,10 +98,20 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
binding = ActivityEditHabitBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
if (intent.hasExtra("habitId")) {
|
||||
if (intent.hasExtra("parentGroupUUID")) {
|
||||
val parentGroupUUID = intent.getStringExtra("parentGroupUUID")
|
||||
parentGroup = component.habitGroupList.getByUUID(parentGroupUUID)
|
||||
}
|
||||
|
||||
if (intent.hasExtra("habitUUID")) {
|
||||
binding.toolbar.title = getString(R.string.edit_habit)
|
||||
habitId = intent.getLongExtra("habitId", -1)
|
||||
val habit = component.habitList.getById(habitId)!!
|
||||
habitUUID = intent.getStringExtra("habitUUID")!!
|
||||
val habitList = if (parentGroup != null) {
|
||||
parentGroup!!.habitList
|
||||
} else {
|
||||
component.habitList
|
||||
}
|
||||
val habit = habitList.getByUUID(habitUUID)!!
|
||||
habitType = habit.type
|
||||
color = habit.color
|
||||
freqNum = habit.frequency.numerator
|
||||
@@ -112,7 +122,6 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
reminderMin = it.minute
|
||||
reminderDays = it.days
|
||||
}
|
||||
parentGroup = habit.parent
|
||||
binding.nameInput.setText(habit.name)
|
||||
binding.questionInput.setText(habit.question)
|
||||
binding.notesInput.setText(habit.description)
|
||||
@@ -120,14 +129,10 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
binding.targetInput.setText(habit.targetValue.toString())
|
||||
} else {
|
||||
habitType = HabitType.fromInt(intent.getIntExtra("habitType", HabitType.YES_NO.value))
|
||||
if (intent.hasExtra("parentGroupUUID")) {
|
||||
val parentGroupUUID = intent.getStringExtra("parentGroupUUID")!!
|
||||
parentGroup = component.habitGroupList.getByUUID(parentGroupUUID)
|
||||
}
|
||||
}
|
||||
|
||||
if (state != null) {
|
||||
habitId = state.getLong("habitId")
|
||||
habitUUID = state.getString("habitUUID")
|
||||
habitType = HabitType.fromInt(state.getInt("habitType"))
|
||||
color = PaletteColor(state.getInt("paletteColor"))
|
||||
freqNum = state.getInt("freqNum")
|
||||
@@ -274,8 +279,8 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
var original: Habit? = null
|
||||
if (habitId >= 0) {
|
||||
original = habitList.getById(habitId)!!
|
||||
if (habitUUID != null) {
|
||||
original = habitList.getByUUID(habitUUID)!!
|
||||
habit.copyFrom(original)
|
||||
}
|
||||
|
||||
@@ -300,10 +305,10 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
habit.parentID = parentGroup?.id
|
||||
habit.parentUUID = parentGroup?.uuid
|
||||
|
||||
val command = if (habitId >= 0) {
|
||||
val command = if (habitUUID != null) {
|
||||
EditHabitCommand(
|
||||
habitList,
|
||||
habitId,
|
||||
habitUUID!!,
|
||||
habit
|
||||
)
|
||||
} else {
|
||||
@@ -382,7 +387,7 @@ class EditHabitActivity : AppCompatActivity() {
|
||||
override fun onSaveInstanceState(state: Bundle) {
|
||||
super.onSaveInstanceState(state)
|
||||
with(state) {
|
||||
putLong("habitId", habitId)
|
||||
putString("habitUUID", habitUUID)
|
||||
putInt("habitType", habitType.value)
|
||||
putInt("paletteColor", color.paletteIndex)
|
||||
putInt("androidColor", androidColor)
|
||||
|
||||
@@ -100,8 +100,9 @@ class IntentFactory
|
||||
|
||||
fun startEditActivity(context: Context, habit: Habit): Intent {
|
||||
val intent = startEditActivity(context)
|
||||
intent.putExtra("habitId", habit.id)
|
||||
intent.putExtra("habitUUID", habit.uuid)
|
||||
intent.putExtra("habitType", habit.type)
|
||||
intent.putExtra("parentGroupUUID", habit.parentUUID)
|
||||
return intent
|
||||
}
|
||||
|
||||
|
||||
@@ -24,11 +24,11 @@ import org.isoron.uhabits.core.models.HabitNotFoundException
|
||||
|
||||
data class EditHabitCommand(
|
||||
val habitList: HabitList,
|
||||
val habitId: Long,
|
||||
val habitUUID: String,
|
||||
val modified: Habit
|
||||
) : Command {
|
||||
override fun run() {
|
||||
val habit = habitList.getById(habitId) ?: throw HabitNotFoundException()
|
||||
val habit = habitList.getByUUID(habitUUID) ?: throw HabitNotFoundException()
|
||||
habit.copyFrom(modified)
|
||||
habitList.update(habit)
|
||||
habit.observable.notifyListeners()
|
||||
|
||||
@@ -89,7 +89,7 @@ class LoopDBImporter
|
||||
val modified = modelFactory.buildHabit()
|
||||
habitRecord.id = habit.id
|
||||
habitRecord.copyTo(modified)
|
||||
EditHabitCommand(habitList, habit.id!!, modified).run()
|
||||
EditHabitCommand(habitList, habit.uuid!!, modified).run()
|
||||
}
|
||||
|
||||
// Reload saved version of the habit
|
||||
|
||||
Reference in New Issue
Block a user