Merge "Scroll to top when opening more settings page" into main

This commit is contained in:
Haijie Hong
2025-03-19 06:16:02 -07:00
committed by Android (Google) Code Review
2 changed files with 19 additions and 1 deletions

View File

@@ -26,9 +26,13 @@ import android.os.Bundle
import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.core.graphics.drawable.toDrawable import androidx.core.graphics.drawable.toDrawable
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.compose.collectAsStateWithLifecycle
@@ -227,6 +231,7 @@ class DeviceDetailsFragmentFormatterImpl(
dashboardFragment.lifecycleScope.launch { dashboardFragment.lifecycleScope.launch {
if (isLoading) { if (isLoading) {
scrollToTop()
dashboardFragment.setLoading(false, false) dashboardFragment.setLoading(false, false)
isLoading = false isLoading = false
} }
@@ -495,6 +500,19 @@ class DeviceDetailsFragmentFormatterImpl(
} }
} }
private fun scrollToTop() {
// Temporary fix to make sure the screen is scroll to the top when rendering.
ComposePreference(context).apply {
order = -1
isEnabled = false
isSelectable = false
setContent { Spacer(modifier = Modifier.height(1.dp)) }
}.also {
dashboardFragment.preferenceScreen.addPreference(it)
dashboardFragment.scrollToPreference(it)
}
}
private fun getPreferenceKey(settingId: Int) = "DEVICE_SETTING_${settingId}" private fun getPreferenceKey(settingId: Int) = "DEVICE_SETTING_${settingId}"
private class SpotlightPreference(context: Context) : Preference(context) { private class SpotlightPreference(context: Context) : Preference(context) {

View File

@@ -404,7 +404,7 @@ class DeviceDetailsFragmentFormatterTest {
for (i in 0..<fragment.preferenceScreen.preferenceCount) { for (i in 0..<fragment.preferenceScreen.preferenceCount) {
prefs.add(fragment.preferenceScreen.getPreference(i)) prefs.add(fragment.preferenceScreen.getPreference(i))
} }
return prefs return prefs.filter { it.key != null }
} }
class TestFragment(context: Context) : DashboardFragment() { class TestFragment(context: Context) : DashboardFragment() {