From 07abd416b6dd379ba11433aa4f42110c671ea779 Mon Sep 17 00:00:00 2001 From: LZX <1820455493@qq.com> Date: Tue, 20 Oct 2026 20:29:31 +0800 Subject: [PATCH] feat: keep LocationScreen alive across navigation tabs with zero reload --- .../lzxnone/openloc/ui/screens/MainScreen.kt | 80 ++++++++++++++----- .../ui/screens/location/LocationScreen.kt | 40 +++++++--- 2 files changed, 88 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/lzxnone/openloc/ui/screens/MainScreen.kt b/app/src/main/java/com/lzxnone/openloc/ui/screens/MainScreen.kt index be3bdef..def340e 100644 --- a/app/src/main/java/com/lzxnone/openloc/ui/screens/MainScreen.kt +++ b/app/src/main/java/com/lzxnone/openloc/ui/screens/MainScreen.kt @@ -8,6 +8,8 @@ import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.material3.Scaffold +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -139,27 +141,65 @@ fun MainScreen( ) } ) { innerPadding -> - AnimatedContent( - targetState = currentTab, - transitionSpec = { - fadeIn() togetherWith fadeOut() - }, - label = "TabContentAnimation", - modifier = Modifier.padding(innerPadding) - ) { targetTab -> - when (targetTab) { - NavTab.HOME -> HomeScreen( - isModuleActive = isModuleActive, - isServiceEnabled = isServiceEnabled, - onServiceEnabledChange = { enabled -> - AppPreferences.updateServiceEnabled(enabled) - }, - onOpenBlacklist = { activeAppSelectionMode = AppMockMode.BLACKLIST }, - onOpenWhitelist = { activeAppSelectionMode = AppMockMode.WHITELIST }, - onCheckUpdate = performManualCheck + Box( + modifier = Modifier + .fillMaxSize() + .padding(bottom = innerPadding.calculateBottomPadding()) + ) { + // 1. 地图页面:一旦进入后始终常驻保活,切换 Tab 绝不销毁 MapView,零延迟、不重新加载 + var hasLoadedLocation by rememberSaveable { mutableStateOf(false) } + if (currentTab == NavTab.LOCATION) { + hasLoadedLocation = true + } + + if (hasLoadedLocation) { + LocationScreen( + modifier = Modifier.fillMaxSize(), + isVisible = (currentTab == NavTab.LOCATION) ) - NavTab.LOCATION -> LocationScreen() - NavTab.SETTINGS -> SettingsScreen() + } + + // 2. 首页 (当前 Tab 为 HOME 时展示) + AnimatedVisibility( + visible = currentTab == NavTab.HOME, + enter = fadeIn(), + exit = fadeOut(), + modifier = Modifier.fillMaxSize() + ) { + Surface( + modifier = Modifier + .fillMaxSize() + .padding(top = innerPadding.calculateTopPadding()), + color = MaterialTheme.colorScheme.background + ) { + HomeScreen( + isModuleActive = isModuleActive, + isServiceEnabled = isServiceEnabled, + onServiceEnabledChange = { enabled -> + AppPreferences.updateServiceEnabled(enabled) + }, + onOpenBlacklist = { activeAppSelectionMode = AppMockMode.BLACKLIST }, + onOpenWhitelist = { activeAppSelectionMode = AppMockMode.WHITELIST }, + onCheckUpdate = performManualCheck + ) + } + } + + // 3. 设置页 (当前 Tab 为 SETTINGS 时展示) + AnimatedVisibility( + visible = currentTab == NavTab.SETTINGS, + enter = fadeIn(), + exit = fadeOut(), + modifier = Modifier.fillMaxSize() + ) { + Surface( + modifier = Modifier + .fillMaxSize() + .padding(top = innerPadding.calculateTopPadding()), + color = MaterialTheme.colorScheme.background + ) { + SettingsScreen() + } } } } diff --git a/app/src/main/java/com/lzxnone/openloc/ui/screens/location/LocationScreen.kt b/app/src/main/java/com/lzxnone/openloc/ui/screens/location/LocationScreen.kt index 82e54ad..fb00ed9 100644 --- a/app/src/main/java/com/lzxnone/openloc/ui/screens/location/LocationScreen.kt +++ b/app/src/main/java/com/lzxnone/openloc/ui/screens/location/LocationScreen.kt @@ -157,7 +157,8 @@ val AMapVectorTileSource = object : OnlineTileSourceBase( @OptIn(ExperimentalMaterial3Api::class) @Composable fun LocationScreen( - modifier: Modifier = Modifier + modifier: Modifier = Modifier, + isVisible: Boolean = true ) { val context = LocalContext.current val keyboardController = LocalSoftwareKeyboardController.current @@ -338,20 +339,22 @@ fun LocationScreen( } } - // 核心二元状态机:根据虚拟定位总开关 (isServiceEnabled) 切换定位模式 - LaunchedEffect(isServiceEnabled) { + // 核心二元状态机:根据虚拟定位总开关 (isServiceEnabled) 与页面可见性 (isVisible) 切换定位模式 + LaunchedEffect(isServiceEnabled, isVisible) { val lm = context.getSystemService(Context.LOCATION_SERVICE) as? LocationManager ?: return@LaunchedEffect val hasFine = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED val hasCoarse = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED - if (isServiceEnabled) { - // ================= 【总开关开启】 ================= - // 1. 立刻彻底注销并移除所有的物理真实定位监听,不再接收任何真机硬件坐标 + if (isServiceEnabled || !isVisible) { + // 当总开关开启或页面处于不可见状态时,彻底注销物理真实定位监听,节省电量与硬件开销 try { lm.removeUpdates(realLocationListener) } catch (_: Throwable) {} + } - // 2. 检查是否为首次开启且从未手动选过点:若无历史选点,主动取一次真实定位作为初始点 + if (isServiceEnabled) { + // ================= 【总开关开启】 ================= + // 检查是否为首次开启且从未手动选过点:若无历史选点,主动取一次真实定位作为初始点 if (!AppPreferences.hasUserSetLocation && (hasFine || hasCoarse)) { fetchRealLocationOnce(lm) { realWgsLat, realWgsLng -> val gcj = CoordinateConverter.wgs84ToGcj02(realWgsLat, realWgsLng) @@ -380,8 +383,8 @@ fun LocationScreen( mapViewRef?.controller?.animateTo(p) mapViewRef?.invalidate() } - } else { - // ================= 【总开关关闭】 ================= + } else if (isVisible) { + // ================= 【总开关关闭 & 页面可见】 ================= // 1. 关闭的一瞬间,立马主动获取一次真机真实坐标,拉回当前设备物理原位 if (hasFine || hasCoarse) { fetchRealLocationOnce(lm) { realWgsLat, realWgsLng -> @@ -600,12 +603,24 @@ fun LocationScreen( }, modifier = Modifier.fillMaxSize(), update = { mapView -> - currentMarker?.position = currentGeoPoint - mapView.invalidate() + if (isVisible) { + if (mapView.visibility != android.view.View.VISIBLE) { + mapView.visibility = android.view.View.VISIBLE + mapView.onResume() + } + currentMarker?.position = currentGeoPoint + mapView.invalidate() + } else { + if (mapView.visibility != android.view.View.GONE) { + mapView.onPause() + mapView.visibility = android.view.View.GONE + } + } } ) - // ------------------ 2. 顶部未激活时的浮动搜索胶囊 ------------------ + if (isVisible) { + // ------------------ 2. 顶部未激活时的浮动搜索胶囊 ------------------ Surface( modifier = Modifier .fillMaxWidth() @@ -1559,6 +1574,7 @@ fun LocationScreen( } } ) + } } }