Skip to content

Commit

Permalink
Add name and notes field to field guide creation
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Corry <[email protected]>
  • Loading branch information
kylecorry31 committed Jan 30, 2025
1 parent a95e15f commit 1ea0844
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.kylecorry.trail_sense.shared.withId

data class FieldGuidePage(
override val id: Long,
val name: String,
val name: String = "",
val images: List<String> = emptyList(),
val directTags: List<FieldGuidePageTag> = emptyList(),
val notes: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import androidx.navigation.fragment.findNavController
import com.kylecorry.andromeda.fragments.BoundFragment
import com.kylecorry.andromeda.fragments.inBackground
import com.kylecorry.luna.coroutines.onMain
import com.kylecorry.trail_sense.databinding.FragmentCreateFieldGuidePageBinding
import com.kylecorry.trail_sense.shared.CustomUiUtils
import com.kylecorry.trail_sense.shared.withId
Expand All @@ -15,9 +18,9 @@ import com.kylecorry.trail_sense.tools.field_guide.infrastructure.FieldGuideRepo

class CreateFieldGuidePageFragment : BoundFragment<FragmentCreateFieldGuidePageBinding>() {

private var existingPage by state<FieldGuidePage?>(null)
private var tags by state<List<FieldGuidePageTag>>(emptyList())
private var originalPage by state(FieldGuidePage(0))
private val repo by lazy { FieldGuideRepo.getInstance(requireContext()) }
private var page by state(originalPage)

override fun generateBinding(
layoutInflater: LayoutInflater,
Expand All @@ -32,25 +35,64 @@ class CreateFieldGuidePageFragment : BoundFragment<FragmentCreateFieldGuidePageB
val pageId = it.getLong(ARG_PAGE_ID, 0L)
if (pageId != 0L) {
inBackground {
existingPage = repo.getPage(pageId)
repo.getPage(pageId)?.let {
originalPage = it
page = it
}
}
}

val tag = it.getLong(ARG_CLASSIFICATION_ID, 0L)
.takeIf { id -> id != 0L }
?.let { id -> FieldGuidePageTag.entries.withId(id) }
val tag = FieldGuidePageTag.entries.withId(it.getLong(ARG_CLASSIFICATION_ID, 0L))

if (tag != null) {
tags += listOf(tag)
page = page.copy(directTags = page.directTags + tag)
}
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
CustomUiUtils.setButtonState(binding.createFieldGuidePageTitle.rightButton, true)
binding.createFieldGuidePageTitle.rightButton.setOnClickListener {
save()
}

// Fields
binding.name.addTextChangedListener {
page = page.copy(name = it.toString())
}

binding.notes.addTextChangedListener {
page = page.copy(notes = it.toString())
}

// TODO: Add dirty checking
}

override fun onUpdate() {
super.onUpdate()

// Original content
effect2(originalPage) {
binding.name.setText(originalPage.name)
binding.notes.setText(originalPage.notes)
}

effect2(page.tags) {
// TODO: Update the tags holder
}
}

private fun save() {
inBackground {
repo.add(page)
onMain {
findNavController().navigateUp()
}
}
}


companion object {
private const val ARG_PAGE_ID = "page_id"
private const val ARG_CLASSIFICATION_ID = "classification_id"
Expand Down
51 changes: 46 additions & 5 deletions app/src/main/res/layout/fragment_create_field_guide_page.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
Expand All @@ -10,13 +9,55 @@
android:id="@+id/create_field_guide_page_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:rightButtonIcon="@drawable/ic_add"
app:rightButtonIcon="@drawable/ic_check"
app:showSubtitle="false"
tools:title="Test Name" />
app:title="@string/create_page" />

<ScrollView
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
android:layout_weight="1">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:orientation="vertical">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/name_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNone"
android:inputType="text" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.card.MaterialCardView
style="@style/Widget.Material3.CardView.Outlined"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="16dp"
android:backgroundTint="@android:color/transparent">

<com.kylecorry.trail_sense.shared.views.Notepad
android:id="@+id/notes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/tool_notes_title"
android:imeOptions="actionNone"
android:lineSpacingMultiplier="1.5"
android:padding="16dp"
android:scrollbars="vertical" />
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1594,4 +1594,5 @@
<string name="full_service">Full service</string>
<string name="emergency_calls_only">Emergency calls only</string>
<string name="cell_tower">Cell tower</string>
<string name="create_page">Create page</string>
</resources>

0 comments on commit 1ea0844

Please sign in to comment.