diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/field_guide/infrastructure/FieldGuideRepo.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/field_guide/infrastructure/FieldGuideRepo.kt index 7d68da6d2..9433eb267 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/field_guide/infrastructure/FieldGuideRepo.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/field_guide/infrastructure/FieldGuideRepo.kt @@ -17,28 +17,28 @@ class FieldGuideRepo private constructor(private val context: Context) { BuiltInFieldGuide.getFieldGuide(context) + saved } - suspend fun getPage(id: Long): FieldGuidePage? { - return if (id < 0) { + suspend fun getPage(id: Long): FieldGuidePage? = onIO { + if (id < 0) { BuiltInFieldGuide.getFieldGuidePage(context, id) } else { dao.getPage(id)?.toFieldGuidePage() } } - suspend fun delete(page: FieldGuidePage) { + suspend fun delete(page: FieldGuidePage) = onIO { if (page.isReadOnly) { - return + return@onIO } page.images.forEach { files.delete(it) } dao.delete(FieldGuidePageEntity.fromFieldGuidePage(page)) } - suspend fun add(page: FieldGuidePage): Long { + suspend fun add(page: FieldGuidePage): Long = onIO { if (page.isReadOnly) { - return -1 + return@onIO -1 } val entity = FieldGuidePageEntity.fromFieldGuidePage(page) - return dao.upsert(entity) + dao.upsert(entity) } companion object { diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/field_guide/ui/CreateFieldGuidePageFragment.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/field_guide/ui/CreateFieldGuidePageFragment.kt new file mode 100644 index 000000000..79456fb24 --- /dev/null +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/field_guide/ui/CreateFieldGuidePageFragment.kt @@ -0,0 +1,58 @@ +package com.kylecorry.trail_sense.tools.field_guide.ui + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.kylecorry.andromeda.fragments.BoundFragment +import com.kylecorry.andromeda.fragments.inBackground +import com.kylecorry.trail_sense.databinding.FragmentCreateFieldGuidePageBinding +import com.kylecorry.trail_sense.shared.CustomUiUtils +import com.kylecorry.trail_sense.shared.withId +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.infrastructure.FieldGuideRepo + +class CreateFieldGuidePageFragment : BoundFragment() { + + private var existingPage by state(null) + private var tags by state>(emptyList()) + private val repo by lazy { FieldGuideRepo.getInstance(requireContext()) } + + override fun generateBinding( + layoutInflater: LayoutInflater, + container: ViewGroup? + ): FragmentCreateFieldGuidePageBinding { + return FragmentCreateFieldGuidePageBinding.inflate(layoutInflater, container, false) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + arguments?.let { + val pageId = it.getLong(ARG_PAGE_ID, 0L) + if (pageId != 0L) { + inBackground { + existingPage = repo.getPage(pageId) + } + } + + val tag = it.getLong(ARG_CLASSIFICATION_ID, 0L) + .takeIf { id -> id != 0L } + ?.let { id -> FieldGuidePageTag.entries.withId(id) } + + if (tag != null) { + tags += listOf(tag) + } + } + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + CustomUiUtils.setButtonState(binding.createFieldGuidePageTitle.rightButton, true) + } + + companion object { + private const val ARG_PAGE_ID = "page_id" + private const val ARG_CLASSIFICATION_ID = "classification_id" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/field_guide/ui/FieldGuideFragment.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/field_guide/ui/FieldGuideFragment.kt index 5f9aa7545..3e97d3b61 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/field_guide/ui/FieldGuideFragment.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/field_guide/ui/FieldGuideFragment.kt @@ -9,7 +9,6 @@ import android.view.ViewGroup import android.widget.ImageView import androidx.core.os.bundleOf import androidx.navigation.fragment.findNavController -import com.kylecorry.andromeda.alerts.dialog import com.kylecorry.andromeda.core.coroutines.BackgroundMinimumState import com.kylecorry.andromeda.core.coroutines.onIO import com.kylecorry.andromeda.core.system.Resources @@ -23,7 +22,6 @@ import com.kylecorry.trail_sense.R import com.kylecorry.trail_sense.databinding.FragmentFieldGuideBinding import com.kylecorry.trail_sense.shared.io.DeleteTempFilesCommand import com.kylecorry.trail_sense.shared.io.FileSubsystem -import com.kylecorry.trail_sense.shared.views.Views 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.infrastructure.FieldGuideRepo @@ -64,6 +62,14 @@ class FieldGuideFragment : BoundFragment() { } } } + + binding.addBtn.setOnClickListener { + findNavController().navigate( + R.id.createFieldGuidePageFragment, bundleOf( + "classification_id" to (tagFilter?.id ?: 0L) + ) + ) + } } override fun onUpdate() { diff --git a/app/src/main/res/layout/fragment_create_field_guide_page.xml b/app/src/main/res/layout/fragment_create_field_guide_page.xml new file mode 100644 index 000000000..752bbabaa --- /dev/null +++ b/app/src/main/res/layout/fragment_create_field_guide_page.xml @@ -0,0 +1,22 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_field_guide.xml b/app/src/main/res/layout/fragment_field_guide.xml index 44de37187..cb4517777 100644 --- a/app/src/main/res/layout/fragment_field_guide.xml +++ b/app/src/main/res/layout/fragment_field_guide.xml @@ -1,17 +1,33 @@ - + android:layout_height="match_parent"> + android:padding="16dp" + app:layout_constraintTop_toTopOf="parent" /> - \ No newline at end of file + android:layout_height="0dp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintTop_toBottomOf="@id/search" /> + + + \ No newline at end of file diff --git a/app/src/main/res/navigation/nav_graph.xml b/app/src/main/res/navigation/nav_graph.xml index 614cb723d..34f062406 100644 --- a/app/src/main/res/navigation/nav_graph.xml +++ b/app/src/main/res/navigation/nav_graph.xml @@ -125,6 +125,22 @@ android:defaultValue="0L" app:argType="long" /> + + + + + + +