Skip to content

Commit

Permalink
Fix: Retain scroll position for privacy policy screen after orientati…
Browse files Browse the repository at this point in the history
…on change
  • Loading branch information
TanishMoral11 committed Dec 22, 2024
1 parent bc0483d commit 37cd0a8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class PoliciesFragment : InjectableFragment() {
POLICIES_FRAGMENT_POLICY_PAGE_ARGUMENT_PROTO,
PoliciesFragmentArguments.getDefaultInstance()
)
return policiesFragmentPresenter.handleCreateView(inflater, container, policies)
return policiesFragmentPresenter.handleCreateView(inflater, container, policies, savedInstanceState)
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
policiesFragmentPresenter.handleSaveInstanceState(outState)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.oppia.android.app.policies

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ScrollView
import androidx.appcompat.app.AppCompatActivity
import org.oppia.android.R
import org.oppia.android.app.fragment.FragmentScope
Expand All @@ -22,22 +24,39 @@ class PoliciesFragmentPresenter @Inject constructor(
private val resourceHandler: AppLanguageResourceHandler
) : HtmlParser.PolicyOppiaTagActionListener {

private lateinit var binding: PoliciesFragmentBinding
private var scrollPosition = 0

/** Handles onCreate() method of the [PoliciesFragment]. */
fun handleCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
policiesFragmentArguments: PoliciesFragmentArguments
policiesFragmentArguments: PoliciesFragmentArguments,
savedInstanceState: Bundle?
): View {
val binding = PoliciesFragmentBinding.inflate(
binding = PoliciesFragmentBinding.inflate(
inflater,
container,
/* attachToRoot= */ false
)

savedInstanceState?.let {
scrollPosition = it.getInt(KEY_SCROLL_Y, 0)
}

setUpContentForTextViews(policiesFragmentArguments.policyPage, binding)

(binding.root as ScrollView).viewTreeObserver.addOnGlobalLayoutListener {
binding.root.scrollTo(0, scrollPosition)
}

return binding.root
}

fun handleSaveInstanceState(outState: Bundle) {
outState.putInt(KEY_SCROLL_Y, (binding.root as ScrollView).scrollY)
}

private fun setUpContentForTextViews(
policyPage: PolicyPage,
binding: PoliciesFragmentBinding
Expand Down Expand Up @@ -86,4 +105,8 @@ class PoliciesFragmentPresenter @Inject constructor(
(activity as RouteToPoliciesListener).onRouteToPolicies(PolicyPage.TERMS_OF_SERVICE)
}
}

companion object {
private const val KEY_SCROLL_Y = "policies_scroll_y"
}
}

0 comments on commit 37cd0a8

Please sign in to comment.