Change implementation to use ids instead of uuids

This commit is contained in:
Dharanish
2024-07-11 23:32:14 +02:00
parent 6abea29736
commit 0de7c7f8bd
22 changed files with 200 additions and 209 deletions

View File

@@ -47,7 +47,6 @@ import org.isoron.uhabits.core.commands.CreateHabitCommand
import org.isoron.uhabits.core.commands.EditHabitCommand
import org.isoron.uhabits.core.commands.RefreshParentGroupCommand
import org.isoron.uhabits.core.models.Frequency
import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.HabitGroup
import org.isoron.uhabits.core.models.HabitType
import org.isoron.uhabits.core.models.NumericalHabitType
@@ -76,7 +75,7 @@ class EditHabitActivity : AppCompatActivity() {
private lateinit var binding: ActivityEditHabitBinding
private lateinit var commandRunner: CommandRunner
var habitUUID: String? = null
var habitId = -1L
lateinit var habitType: HabitType
var parentGroup: HabitGroup? = null
var unit = ""
@@ -99,20 +98,20 @@ class EditHabitActivity : AppCompatActivity() {
binding = ActivityEditHabitBinding.inflate(layoutInflater)
setContentView(binding.root)
if (intent.hasExtra("parentGroupUUID")) {
val parentGroupUUID = intent.getStringExtra("parentGroupUUID")
parentGroup = component.habitGroupList.getByUUID(parentGroupUUID)
if (intent.hasExtra("groupId")) {
val groupId = intent.getLongExtra("groupId", -1L)
parentGroup = component.habitGroupList.getById(groupId)
}
if (intent.hasExtra("habitUUID")) {
if (intent.hasExtra("habitId")) {
binding.toolbar.title = getString(R.string.edit_habit)
habitUUID = intent.getStringExtra("habitUUID")!!
habitId = intent.getLongExtra("habitId", -1L)
val habitList = if (parentGroup != null) {
parentGroup!!.habitList
} else {
component.habitList
}
val habit = habitList.getByUUID(habitUUID)!!
val habit = habitList.getById(habitId)!!
habitType = habit.type
color = habit.color
freqNum = habit.frequency.numerator
@@ -133,7 +132,7 @@ class EditHabitActivity : AppCompatActivity() {
}
if (state != null) {
habitUUID = state.getString("habitUUID")
habitId = state.getLong("habitId")
habitType = HabitType.fromInt(state.getInt("habitType"))
color = PaletteColor(state.getInt("paletteColor"))
freqNum = state.getInt("freqNum")
@@ -279,9 +278,8 @@ class EditHabitActivity : AppCompatActivity() {
component.habitList
}
var original: Habit? = null
if (habitUUID != null) {
original = habitList.getByUUID(habitUUID)!!
if (habitId > 0) {
val original = habitList.getById(habitId)!!
habit.copyFrom(original)
}
@@ -303,13 +301,13 @@ class EditHabitActivity : AppCompatActivity() {
}
habit.type = habitType
habit.parent = parentGroup
habit.parentID = parentGroup?.id
habit.parentUUID = parentGroup?.uuid
habit.groupId = parentGroup?.id
habit.groupUUID = parentGroup?.uuid
val command = if (habitUUID != null) {
val command = if (habitId > 0) {
EditHabitCommand(
habitList,
habitUUID!!,
habitId,
habit
)
} else {
@@ -321,7 +319,7 @@ class EditHabitActivity : AppCompatActivity() {
}
component.commandRunner.run(command)
if (habit.parentID != null) {
if (habit.groupId != null) {
val habitGroupList = component.habitGroupList
val refreshCommand = RefreshParentGroupCommand(habit, habitGroupList)
component.commandRunner.run(refreshCommand)
@@ -395,7 +393,7 @@ class EditHabitActivity : AppCompatActivity() {
override fun onSaveInstanceState(state: Bundle) {
super.onSaveInstanceState(state)
with(state) {
putString("habitUUID", habitUUID)
putLong("habitId", habitId)
putInt("habitType", habitType.value)
putInt("paletteColor", color.paletteIndex)
putInt("androidColor", androidColor)

View File

@@ -29,7 +29,7 @@ import org.isoron.uhabits.core.models.HabitType
import org.isoron.uhabits.databinding.SelectHabitTypeBinding
import org.isoron.uhabits.intents.IntentFactory
class HabitTypeDialog(val parentUUID: String? = null) : AppCompatDialogFragment() {
class HabitTypeDialog(val groupId: Long? = null) : AppCompatDialogFragment() {
override fun getTheme() = R.style.Translucent
override fun onCreateView(
@@ -40,13 +40,13 @@ class HabitTypeDialog(val parentUUID: String? = null) : AppCompatDialogFragment(
val binding = SelectHabitTypeBinding.inflate(inflater, container, false)
binding.buttonYesNo.setOnClickListener {
val intent = IntentFactory().startEditActivity(requireActivity(), HabitType.YES_NO.value, parentUUID)
val intent = IntentFactory().startEditActivity(requireActivity(), HabitType.YES_NO.value, groupId)
startActivity(intent)
dismiss()
}
binding.buttonMeasurable.setOnClickListener {
val intent = IntentFactory().startEditActivity(requireActivity(), HabitType.NUMERICAL.value, parentUUID)
val intent = IntentFactory().startEditActivity(requireActivity(), HabitType.NUMERICAL.value, groupId)
startActivity(intent)
dismiss()
}
@@ -57,7 +57,7 @@ class HabitTypeDialog(val parentUUID: String? = null) : AppCompatDialogFragment(
dismiss()
}
if (parentUUID != null) {
if (groupId != null) {
binding.buttonHabitGroup.visibility = View.GONE
}

View File

@@ -166,8 +166,8 @@ class ListHabitsScreen
activity.startActivity(intent)
}
override fun showSelectHabitTypeDialog(parentUUID: String?) {
val dialog = HabitTypeDialog(parentUUID)
override fun showSelectHabitTypeDialog(groupId: Long?) {
val dialog = HabitTypeDialog(groupId)
dialog.show(activity.supportFragmentManager, "habitType")
}

View File

@@ -28,7 +28,7 @@ class AddButtonView(
}
override fun onClick(v: View) {
(context as ListHabitsActivity).component.listHabitsMenu.behavior.onCreateHabit(habitGroup!!.uuid)
(context as ListHabitsActivity).component.listHabitsMenu.behavior.onCreateHabit(habitGroup!!.id)
}
override fun onDraw(canvas: Canvas) {

View File

@@ -34,7 +34,6 @@ import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsSelectionMenuBeh
import org.isoron.uhabits.core.utils.MidnightTimer
import org.isoron.uhabits.inject.ActivityScope
import java.util.LinkedList
import java.util.UUID
import javax.inject.Inject
/**
@@ -75,10 +74,6 @@ class HabitCardListAdapter @Inject constructor(
return cache.hasNoHabitGroup()
}
fun hasNoSubHabits(): Boolean {
return cache.hasNoSubHabits()
}
/**
* Sets all items as not selected.
*/
@@ -122,22 +117,7 @@ class HabitCardListAdapter @Inject constructor(
}
override fun getItemId(position: Int): Long {
val uuidString = cache.getUUIDByPosition(position)
return if (uuidString != null) {
val formattedUUIDString = formatUUID(uuidString)
val uuid = UUID.fromString(formattedUUIDString)
uuid.mostSignificantBits and Long.MAX_VALUE
} else {
-1
}
}
private fun formatUUID(uuidString: String): String {
return uuidString.substring(0, 8) + "-" +
uuidString.substring(8, 12) + "-" +
uuidString.substring(12, 16) + "-" +
uuidString.substring(16, 20) + "-" +
uuidString.substring(20, 32)
return cache.getIdByPosition(position)!!
}
/**
@@ -165,14 +145,14 @@ class HabitCardListAdapter @Inject constructor(
if (listView == null) return
val habit = cache.getHabitByPosition(position)
if (habit != null) {
val score = cache.getScore(habit.uuid!!)
val checkmarks = cache.getCheckmarks(habit.uuid!!)
val notes = cache.getNotes(habit.uuid!!)
val score = cache.getScore(habit.id!!)
val checkmarks = cache.getCheckmarks(habit.id!!)
val notes = cache.getNotes(habit.id!!)
val selected = selectedHabits.contains(habit)
listView!!.bindCardView(holder, habit, score, checkmarks, notes, selected)
} else {
val habitGroup = cache.getHabitGroupByPosition(position)
val score = cache.getScore(habitGroup!!.uuid!!)
val score = cache.getScore(habitGroup!!.id!!)
val selected = selectedHabitGroups.contains(habitGroup)
listView!!.bindGroupCardView(holder, habitGroup, score, selected)
}
@@ -253,11 +233,11 @@ class HabitCardListAdapter @Inject constructor(
* @param selected list of habits to be removed
*/
override fun performRemove(selected: List<Habit>) {
for (habit in selected) cache.remove(habit.uuid!!)
for (habit in selected) cache.remove(habit.id!!)
}
override fun performRemoveHabitGroup(selected: List<HabitGroup>) {
for (hgr in selected) cache.remove(hgr.uuid!!)
for (hgr in selected) cache.remove(hgr.id!!)
}
/**

View File

@@ -74,14 +74,14 @@ class ShowHabitActivity : AppCompatActivity(), CommandRunner.Listener {
val appComponent = (applicationContext as HabitsApplication).component
val habitGroupList = appComponent.habitGroupList
val parentUUID = intent.getStringExtra("parentUUID")
val habitList = if (parentUUID == null) {
appComponent.habitList
val groupId = intent.getLongExtra("groupId", -1L)
val habitList = if (groupId > 0) {
habitGroupList.getById(groupId)!!.habitList
} else {
habitGroupList.getByUUID(parentUUID)!!.habitList
appComponent.habitList
}
val uuid = intent.getStringExtra("habitUUID")!!
habit = habitList.getByUUID(uuid)!!
val id = intent.getLongExtra("habitId", -1L)
habit = habitList.getById(id)!!
preferences = appComponent.preferences
commandRunner = appComponent.commandRunner
widgetUpdater = appComponent.widgetUpdater

View File

@@ -65,8 +65,8 @@ class IntentFactory
fun startShowHabitActivity(context: Context, habit: Habit): Intent {
val intent = Intent(context, ShowHabitActivity::class.java)
intent.putExtra("habitUUID", habit.uuid)
intent.putExtra("parentUUID", habit.parentUUID)
intent.putExtra("habitId", habit.id)
intent.putExtra("groupId", habit.groupId)
return intent
}
@@ -100,17 +100,17 @@ class IntentFactory
fun startEditActivity(context: Context, habit: Habit): Intent {
val intent = startEditActivity(context)
intent.putExtra("habitUUID", habit.uuid)
intent.putExtra("habitId", habit.id)
intent.putExtra("habitType", habit.type)
intent.putExtra("parentGroupUUID", habit.parentUUID)
intent.putExtra("groupId", habit.groupId)
return intent
}
fun startEditActivity(context: Context, habitType: Int, parentUUID: String?): Intent {
fun startEditActivity(context: Context, habitType: Int, groupId: Long?): Intent {
val intent = startEditActivity(context)
intent.putExtra("habitType", habitType)
if (parentUUID != null) {
intent.putExtra("parentGroupUUID", parentUUID)
if (groupId != null) {
intent.putExtra("groupId", groupId)
}
return intent
}

View File

@@ -101,7 +101,13 @@ class ReminderController @Inject constructor(
}
fun onDismiss(habitGroup: HabitGroup) {
notificationTray.cancel(habitGroup)
if (preferences.shouldMakeNotificationsSticky()) {
// This is a workaround to keep sticky notifications non-dismissible in Android 14+.
// If the notification is dismissed, we immediately reshow it.
notificationTray.reshow(habitGroup)
} else {
notificationTray.cancel(habitGroup)
}
}
private fun showSnoozeDelayPicker(habit: Habit, context: Context) {