Implement listing sub habits

This commit is contained in:
Dharanish
2024-07-05 15:12:20 +02:00
parent 0a1cdd45cb
commit 35c9a1a0ab
10 changed files with 435 additions and 178 deletions

View File

@@ -18,6 +18,7 @@
*/
package org.isoron.uhabits.activities.habits.list.views
import android.annotation.SuppressLint
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.Adapter
import org.isoron.uhabits.activities.habits.list.MAX_CHECKMARK_COUNT
@@ -74,9 +75,14 @@ class HabitCardListAdapter @Inject constructor(
return cache.hasNoHabitGroup()
}
fun hasNoSubHabits(): Boolean {
return cache.hasNoSubHabits()
}
/**
* Sets all items as not selected.
*/
@SuppressLint("NotifyDataSetChanged")
override fun clearSelection() {
selectedHabits.clear()
selectedHabitGroups.clear()
@@ -116,7 +122,7 @@ class HabitCardListAdapter @Inject constructor(
}
override fun getItemId(position: Int): Long {
val uuidString = getItemUUID(position)
val uuidString = cache.getUUIDByPosition(position)
return if (uuidString != null) {
val formattedUUIDString = formatUUID(uuidString)
val uuid = UUID.fromString(formattedUUIDString)
@@ -126,18 +132,6 @@ class HabitCardListAdapter @Inject constructor(
}
}
fun getItemUUID(position: Int): String? {
val h = cache.getHabitByPosition(position)
val hgr = cache.getHabitGroupByPosition(position)
return if (h != null) {
h.uuid!!
} else if (hgr != null) {
hgr.uuid!!
} else {
null
}
}
private fun formatUUID(uuidString: String): String {
return uuidString.substring(0, 8) + "-" +
uuidString.substring(8, 12) + "-" +
@@ -207,7 +201,7 @@ class HabitCardListAdapter @Inject constructor(
// function to override getItemViewType and return the type of the view. The view can either be a HabitCardView or a HabitGroupCardView
override fun getItemViewType(position: Int): Int {
return if (position < cache.habitCount) {
return if (cache.getHabitByPosition(position) != null) {
0
} else {
1
@@ -322,6 +316,7 @@ class HabitCardListAdapter @Inject constructor(
*
* @param position position of the item to be toggled
*/
@SuppressLint("NotifyDataSetChanged")
fun toggleSelection(position: Int) {
val h = cache.getHabitByPosition(position)
val hgr = cache.getHabitGroupByPosition(position)

View File

@@ -136,11 +136,10 @@ class HabitCardView(
init {
scoreRing = RingView(context).apply {
val thickness = dp(3f)
val rightMargin = dp(8f).toInt()
val margin = dp(8f).toInt()
val ringSize = dp(15f).toInt()
val leftMargin = if (habit?.isSubHabit() == true) dp(30f).toInt() else dp(8f).toInt()
layoutParams = LinearLayout.LayoutParams(ringSize, ringSize).apply {
setMargins(leftMargin, 0, rightMargin, 0)
setMargins(margin, 0, margin, 0)
gravity = Gravity.CENTER
}
setThickness(thickness)
@@ -268,6 +267,16 @@ class HabitCardView(
}
scoreRing.apply {
setColor(c)
if (h.isSubHabit()) {
val rightMargin = dp(8f).toInt()
val ringSize = dp(15f).toInt()
val leftMargin =
if (habit?.isSubHabit() == true) dp(30f).toInt() else dp(8f).toInt()
layoutParams = LinearLayout.LayoutParams(ringSize, ringSize).apply {
setMargins(leftMargin, 0, rightMargin, 0)
gravity = Gravity.CENTER
}
}
}
checkmarkPanel.apply {
color = c