Skip to content

Commit

Permalink
Add tags to field guide (WIP)
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Corry <[email protected]>
  • Loading branch information
kylecorry31 committed Jan 17, 2025
1 parent 1ed8a73 commit ea3ce5d
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ enum class FieldGuidePageTag(
Dangerous(402, FieldGuidePageTagType.HumanInteraction),
Crafting(403, FieldGuidePageTagType.HumanInteraction),
Medicinal(404, FieldGuidePageTagType.HumanInteraction),
}

fun List<FieldGuidePageTag>.getMostSpecific(): FieldGuidePageTag? {
val parents = map { it.parentId }.toSet()
return firstOrNull { it.id !in parents && it.parentId != null } ?: firstOrNull()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ package com.kylecorry.trail_sense.tools.field_guide.ui
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.isVisible
import com.google.android.flexbox.FlexboxLayout
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.fragments.BoundFragment
import com.kylecorry.andromeda.fragments.inBackground
import com.kylecorry.andromeda.views.badge.Badge
import com.kylecorry.luna.coroutines.CoroutineQueueRunner
import com.kylecorry.trail_sense.databinding.FragmentFieldGuidePageBinding
import com.kylecorry.trail_sense.shared.io.FileSubsystem
import com.kylecorry.trail_sense.shared.readableName
import com.kylecorry.trail_sense.tools.field_guide.domain.FieldGuidePage
import com.kylecorry.trail_sense.tools.field_guide.domain.FieldGuidePageTag
import com.kylecorry.trail_sense.tools.field_guide.domain.FieldGuidePageTagType
import com.kylecorry.trail_sense.tools.field_guide.domain.getMostSpecific
import com.kylecorry.trail_sense.tools.field_guide.infrastructure.FieldGuideRepo

class FieldGuidePageFragment : BoundFragment<FragmentFieldGuidePageBinding>() {
Expand Down Expand Up @@ -47,14 +56,73 @@ class FieldGuidePageFragment : BoundFragment<FragmentFieldGuidePageBinding>() {

effect2(page) {
binding.fieldGuidePageTitle.title.text = page?.name
val tags = page?.tags?.joinToString(", ") ?: ""
binding.notes.text = tags + "\n\n" + page?.notes
val tags = page?.tags ?: emptyList()
binding.notes.text = page?.notes
val image = page?.images?.firstOrNull()
binding.image.setImageDrawable(
image?.let { files.drawable(it) }
)

binding.fieldGuidePageTitle.subtitle.text = tags.getMostSpecific()?.readableName()

displayTags(
tags.filter { it.type == FieldGuidePageTagType.Location },
binding.locationsLabel,
binding.locations
)

displayTags(
tags.filter { it.type == FieldGuidePageTagType.Habitat },
binding.habitatsLabel,
binding.habitats
)

displayTags(
tags.filter { it.type == FieldGuidePageTagType.ActivityPattern },
binding.behaviorsLabel,
binding.behaviors
)

displayTags(
tags.filter { it.type == FieldGuidePageTagType.HumanInteraction },
binding.humanInteractionsLabel,
binding.humanInteractions
)
}
}

private fun displayTags(tags: List<FieldGuidePageTag>, label: TextView, layout: FlexboxLayout) {
if (tags.isEmpty()) {
label.isVisible = false
layout.isVisible = false
return
}

label.isVisible = true
layout.isVisible = true
layout.removeAllViews()

val badgeColor =
Resources.getAndroidColorAttr(requireContext(), android.R.attr.colorBackgroundFloating)
val foregroundColor = Resources.androidTextColorPrimary(requireContext())
val margin = Resources.dp(requireContext(), 8f).toInt()

for (tag in tags) {
val tagView = Badge(requireContext(), null).apply {
statusImage.isVisible = false
statusText.textSize = 12f
setStatusText(tag.readableName())
statusText.setTextColor(foregroundColor)
setBackgroundTint(badgeColor)
layoutParams = FlexboxLayout.LayoutParams(
FlexboxLayout.LayoutParams.WRAP_CONTENT,
FlexboxLayout.LayoutParams.WRAP_CONTENT
).apply {
setMargins(0, 0, margin, margin)
}
}
layout.addView(tagView)
}
}

}
95 changes: 91 additions & 4 deletions app/src/main/res/layout/fragment_field_guide_page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,111 @@
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingStart="16dp"
android:paddingEnd="16dp">
android:layout_weight="1">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="@dimen/default_bottom_margin">

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="250dp" />

<!--LOCATION TAGS-->
<TextView
android:id="@+id/locations_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/location"
android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall" />

<com.google.android.flexbox.FlexboxLayout
android:id="@+id/locations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:alignItems="center"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="flex_start" />

<!--HABITAT TAGS-->
<TextView
android:id="@+id/habitats_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Habitat"
android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall" />

<com.google.android.flexbox.FlexboxLayout
android:id="@+id/habitats"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:alignItems="center"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="flex_start" />

<!--BEHAVIOR TAGS-->
<TextView
android:id="@+id/behaviors_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Behavior"
android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall" />

<com.google.android.flexbox.FlexboxLayout
android:id="@+id/behaviors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:alignItems="center"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="flex_start" />

<!--HUMAN INTERACTION TAGS-->
<TextView
android:id="@+id/human_interactions_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Human interactions"
android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall" />

<com.google.android.flexbox.FlexboxLayout
android:id="@+id/human_interactions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:alignItems="center"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="flex_start" />

<!--NOTES-->
<TextView
android:id="@+id/notes_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/tool_notes_title"
android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall" />

<TextView
android:id="@+id/notes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textIsSelectable="true"
tools:text="Notes" />
</LinearLayout>
Expand Down

0 comments on commit ea3ce5d

Please sign in to comment.