mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-24 05:04:50 -04:00
Fixed: The LeftDrawer
automatically opens when we rotate the screen.
* In Compose, when the screen rotates and the screen width is above `600dp`, the `drawerState` is automatically set to `open`. Because of this, the issue was happening. * Now we are storing the `drawerState` and updating the `leftDrawerState` based on the previously saved value when the orientation changes. * Fixed: `LongMethod` lint error.
This commit is contained in:
parent
7915e6647d
commit
68470e44a1
@ -128,7 +128,6 @@ class KiwixMainActivity : CoreMainActivity() {
|
|||||||
private val storageDeviceList = arrayListOf<StorageDevice>()
|
private val storageDeviceList = arrayListOf<StorageDevice>()
|
||||||
private val pendingIntentFlow = MutableStateFlow<Intent?>(null)
|
private val pendingIntentFlow = MutableStateFlow<Intent?>(null)
|
||||||
|
|
||||||
@Suppress("InjectDispatcher")
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
cachedComponent.inject(this)
|
cachedComponent.inject(this)
|
||||||
@ -146,6 +145,8 @@ class KiwixMainActivity : CoreMainActivity() {
|
|||||||
KiwixDestination.Reader.route
|
KiwixDestination.Reader.route
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
RestoreDrawerStateOnOrientationChange()
|
||||||
|
PersistDrawerStateOnChange()
|
||||||
KiwixMainActivityScreen(
|
KiwixMainActivityScreen(
|
||||||
navController = navController,
|
navController = navController,
|
||||||
leftDrawerContent = leftDrawerMenu,
|
leftDrawerContent = leftDrawerMenu,
|
||||||
@ -160,8 +161,7 @@ class KiwixMainActivity : CoreMainActivity() {
|
|||||||
LaunchedEffect(navController) {
|
LaunchedEffect(navController) {
|
||||||
navController.addOnDestinationChangedListener(finishActionModeOnDestinationChange)
|
navController.addOnDestinationChangedListener(finishActionModeOnDestinationChange)
|
||||||
}
|
}
|
||||||
val lifecycleOwner = LocalLifecycleOwner.current
|
val lifecycle = LocalLifecycleOwner.current.lifecycle
|
||||||
val lifecycle = lifecycleOwner.lifecycle
|
|
||||||
LaunchedEffect(navController, pendingIntent) {
|
LaunchedEffect(navController, pendingIntent) {
|
||||||
snapshotFlow { pendingIntent }
|
snapshotFlow { pendingIntent }
|
||||||
.filterNotNull()
|
.filterNotNull()
|
||||||
@ -176,12 +176,17 @@ class KiwixMainActivity : CoreMainActivity() {
|
|||||||
}
|
}
|
||||||
DialogHost(alertDialogShower)
|
DialogHost(alertDialogShower)
|
||||||
}
|
}
|
||||||
lifecycleScope.launch {
|
runMigrations()
|
||||||
migrateInternalToPublicAppDirectory()
|
|
||||||
}
|
|
||||||
intent?.let {
|
intent?.let {
|
||||||
pendingIntentFlow.value = it
|
pendingIntentFlow.value = it
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("InjectDispatcher")
|
||||||
|
private fun runMigrations() {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
migrateInternalToPublicAppDirectory()
|
||||||
|
}
|
||||||
// run the migration on background thread to avoid any UI related issues.
|
// run the migration on background thread to avoid any UI related issues.
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
if (!sharedPreferenceUtil.prefIsTest) {
|
if (!sharedPreferenceUtil.prefIsTest) {
|
||||||
|
@ -28,9 +28,14 @@ import androidx.appcompat.app.ActionBarDrawerToggle
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.compose.material3.BottomAppBarScrollBehavior
|
import androidx.compose.material3.BottomAppBarScrollBehavior
|
||||||
import androidx.compose.material3.DrawerState
|
import androidx.compose.material3.DrawerState
|
||||||
|
import androidx.compose.material3.DrawerValue
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
import androidx.compose.runtime.mutableStateListOf
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.snapshotFlow
|
||||||
|
import androidx.compose.ui.platform.LocalConfiguration
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
@ -166,6 +171,16 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var downloadMonitor: DownloadMonitor
|
lateinit var downloadMonitor: DownloadMonitor
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages the visibility of the left drawer by tracking its state.
|
||||||
|
* In Compose, when the screen rotates and the screen width is above 600dp,
|
||||||
|
* the drawerState is automatically set to open. This causes unexpected behavior.
|
||||||
|
* To ensure a smooth user experience, we save the drawer state in a boolean so
|
||||||
|
* that it survives configuration changes and is not affected by Compose’s
|
||||||
|
* default implementation.
|
||||||
|
*/
|
||||||
|
private var wasLeftDrawerOpen = false
|
||||||
|
|
||||||
@Suppress("InjectDispatcher")
|
@Suppress("InjectDispatcher")
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
setTheme(R.style.KiwixTheme)
|
setTheme(R.style.KiwixTheme)
|
||||||
@ -199,6 +214,38 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
|
|||||||
leftDrawerMenu.addAll(leftNavigationDrawerMenuItems)
|
leftDrawerMenu.addAll(leftNavigationDrawerMenuItems)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restores the drawer state after an orientation change.
|
||||||
|
*
|
||||||
|
* In Compose, rotating the device (especially on large screens) can cause the drawer
|
||||||
|
* to be automatically opened by default. To provide a consistent user experience,
|
||||||
|
* this function syncs the drawer's state (open/closed) with the last known value
|
||||||
|
* stored in [wasLeftDrawerOpen].
|
||||||
|
*/
|
||||||
|
@Composable
|
||||||
|
fun RestoreDrawerStateOnOrientationChange() {
|
||||||
|
LaunchedEffect(LocalConfiguration.current.orientation) {
|
||||||
|
if (wasLeftDrawerOpen) {
|
||||||
|
openNavigationDrawer()
|
||||||
|
} else {
|
||||||
|
closeNavigationDrawer()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tracks the current drawer state and updates [wasLeftDrawerOpen] whenever the
|
||||||
|
* drawer is opened or closed. This ensures the drawer state is persisted across
|
||||||
|
* configuration changes (e.g., screen rotations) and can be restored later.
|
||||||
|
*/
|
||||||
|
@Composable
|
||||||
|
fun PersistDrawerStateOnChange() {
|
||||||
|
LaunchedEffect(leftDrawerState) {
|
||||||
|
snapshotFlow { leftDrawerState.currentValue }
|
||||||
|
.collect { wasLeftDrawerOpen = it == DrawerValue.Open }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Suppress("DEPRECATION")
|
@Suppress("DEPRECATION")
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
super.onActivityResult(requestCode, resultCode, data)
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
|
@ -77,6 +77,8 @@ class CustomMainActivity : CoreMainActivity() {
|
|||||||
navController = rememberNavController()
|
navController = rememberNavController()
|
||||||
leftDrawerState = rememberDrawerState(DrawerValue.Closed)
|
leftDrawerState = rememberDrawerState(DrawerValue.Closed)
|
||||||
uiCoroutineScope = rememberCoroutineScope()
|
uiCoroutineScope = rememberCoroutineScope()
|
||||||
|
RestoreDrawerStateOnOrientationChange()
|
||||||
|
PersistDrawerStateOnChange()
|
||||||
CustomMainActivityScreen(
|
CustomMainActivityScreen(
|
||||||
navController = navController,
|
navController = navController,
|
||||||
leftDrawerContent = leftDrawerMenu,
|
leftDrawerContent = leftDrawerMenu,
|
||||||
@ -96,9 +98,6 @@ class CustomMainActivity : CoreMainActivity() {
|
|||||||
.migrate()
|
.migrate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (savedInstanceState != null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getIconResId() = R.mipmap.ic_launcher
|
override fun getIconResId() = R.mipmap.ic_launcher
|
||||||
|
Loading…
x
Reference in New Issue
Block a user