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