Skip to content

Commit

Permalink
OutlineListFragment: validate node id argument
Browse files Browse the repository at this point in the history
  • Loading branch information
inthewaves committed Jan 14, 2025
1 parent 55b0885 commit 8483f8d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentContainerView
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import app.grapheneos.pdfviewer.PdfViewer
import app.grapheneos.pdfviewer.R
import app.grapheneos.pdfviewer.applySystemBarMargins
import app.grapheneos.pdfviewer.databinding.OutlineFragmentBinding
Expand All @@ -26,6 +26,7 @@ class OutlineFragment : Fragment() {
private lateinit var topBar: MaterialToolbar
private lateinit var listContainer: FragmentContainerView

private val activityViewModel by activityViewModels<PdfViewModel>()
private val viewModel by viewModels<OutlineViewModel>()

private fun dismissOutlineFragment(pageNumber: Int? = null) {
Expand Down Expand Up @@ -84,7 +85,6 @@ class OutlineFragment : Fragment() {

val docTitle = arguments?.getString(ARG_DOC_TITLE_KEY, "") ?: ""

val activityViewModel = (requireActivity() as PdfViewer).viewModel
activityViewModel.requestOutlineIfNotAvailable()
activityViewModel.outline.observe(viewLifecycleOwner) { outlineState ->
if (outlineState is PdfViewModel.OutlineStatus.Loaded) {
Expand Down Expand Up @@ -122,6 +122,7 @@ class OutlineFragment : Fragment() {
is OutlineViewModel.Action.ViewChildren -> {
val parent = action.parent
val fragment = OutlineListFragment.makeInstance(parent.id)
// could be replaced with androidx.navigation
if (childFragmentManager.findFragmentByTag(parent.id.toString()) == null) {
listContainer.visibility = View.VISIBLE
childFragmentManager.beginTransaction()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.grapheneos.pdfviewer.outline

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -18,8 +19,8 @@ import com.google.android.material.divider.MaterialDividerItemDecoration
class OutlineListViewModel : ViewModel() {
val outlineContents = MutableLiveData<List<OutlineNode>?>(null)

fun updateOutlineContents(contents: List<OutlineNode>) {
if (outlineContents.value == null) {
fun updateOutlineContents(contents: List<OutlineNode>?) {
if (contents != null && outlineContents.value == null) {
outlineContents.value = contents
}
}
Expand Down Expand Up @@ -48,9 +49,18 @@ class OutlineListFragment : Fragment() {
list.addItemDecoration(dividerItemDecoration)
list.layoutManager = layoutManager

val parentNodeId = arguments?.getInt(ARG_OUTLINE_ID, -2) ?: -2

outlineViewModel.currentChild.observe(viewLifecycleOwner) { child ->
if (child != null) {
viewModel.updateOutlineContents(child.children)
val contents = if (child?.id == parentNodeId) {
child.children
} else {
outlineViewModel.findNodeOrNull(parentNodeId)?.children
}
viewModel.updateOutlineContents(contents)
if (contents == null) {
Log.d(TAG, "unable to find child with id $parentNodeId")
outlineViewModel.submitAction(OutlineViewModel.Action.Back)
}
}

Expand All @@ -70,6 +80,7 @@ class OutlineListFragment : Fragment() {
}

companion object {
private const val TAG = "OutlineListFragment"
private const val ARG_OUTLINE_ID = "outlinenodeid"

fun makeInstance(outlineId: Int) = OutlineListFragment().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class OutlineViewModel : ViewModel() {

val currentChild = MutableLiveData<OutlineNode?>(null)

fun findNodeOrNull(id: Int): OutlineNode? = outlineStack.find { it.id == id }

fun getSubtitleString() = outlineStack.lastOrNull()?.title?.trim() ?: docTitle

private var docTitle = ""
Expand Down

0 comments on commit 8483f8d

Please sign in to comment.