feat: keep LocationScreen alive across navigation tabs with zero reload

This commit is contained in:
LZX committed 2026-10-20 20:29:31 +08:00
1 parent ee7b7d3666
commit 07abd416b6
2 files changed
+88 -32

No files matched your search

@@ -8,6 +8,8 @@ import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@@ -139,27 +141,65 @@ fun MainScreen(
) )
} }
) { innerPadding -> ) { innerPadding ->
AnimatedContent( Box(
targetState = currentTab, modifier = Modifier
transitionSpec = { .fillMaxSize()
fadeIn() togetherWith fadeOut() .padding(bottom = innerPadding.calculateBottomPadding())
}, ) {
label = "TabContentAnimation", // 1. 地图页面:一旦进入后始终常驻保活,切换 Tab 绝不销毁 MapView,零延迟、不重新加载
modifier = Modifier.padding(innerPadding) var hasLoadedLocation by rememberSaveable { mutableStateOf(false) }
) { targetTab -> if (currentTab == NavTab.LOCATION) {
when (targetTab) { hasLoadedLocation = true
NavTab.HOME -> HomeScreen( }
isModuleActive = isModuleActive,
isServiceEnabled = isServiceEnabled, if (hasLoadedLocation) {
onServiceEnabledChange = { enabled -> LocationScreen(
AppPreferences.updateServiceEnabled(enabled) modifier = Modifier.fillMaxSize(),
}, isVisible = (currentTab == NavTab.LOCATION)
onOpenBlacklist = { activeAppSelectionMode = AppMockMode.BLACKLIST },
onOpenWhitelist = { activeAppSelectionMode = AppMockMode.WHITELIST },
onCheckUpdate = performManualCheck
) )
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()
}
} }
} }
} }
@@ -157,7 +157,8 @@ val AMapVectorTileSource = object : OnlineTileSourceBase(
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun LocationScreen( fun LocationScreen(
modifier: Modifier = Modifier modifier: Modifier = Modifier,
isVisible: Boolean = true
) { ) {
val context = LocalContext.current val context = LocalContext.current
val keyboardController = LocalSoftwareKeyboardController.current val keyboardController = LocalSoftwareKeyboardController.current
@@ -338,20 +339,22 @@ fun LocationScreen(
} }
} }
// 核心二元状态机:根据虚拟定位总开关 (isServiceEnabled) 切换定位模式 // 核心二元状态机:根据虚拟定位总开关 (isServiceEnabled) 与页面可见性 (isVisible) 切换定位模式
LaunchedEffect(isServiceEnabled) { LaunchedEffect(isServiceEnabled, isVisible) {
val lm = context.getSystemService(Context.LOCATION_SERVICE) as? LocationManager ?: return@LaunchedEffect 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 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 val hasCoarse = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
if (isServiceEnabled) { if (isServiceEnabled || !isVisible) {
// ================= 【总开关开启】 ================= // 当总开关开启或页面处于不可见状态时,彻底注销物理真实定位监听,节省电量与硬件开销
// 1. 立刻彻底注销并移除所有的物理真实定位监听,不再接收任何真机硬件坐标
try { try {
lm.removeUpdates(realLocationListener) lm.removeUpdates(realLocationListener)
} catch (_: Throwable) {} } catch (_: Throwable) {}
}
// 2. 检查是否为首次开启且从未手动选过点:若无历史选点,主动取一次真实定位作为初始点 if (isServiceEnabled) {
// ================= 【总开关开启】 =================
// 检查是否为首次开启且从未手动选过点:若无历史选点,主动取一次真实定位作为初始点
if (!AppPreferences.hasUserSetLocation && (hasFine || hasCoarse)) { if (!AppPreferences.hasUserSetLocation && (hasFine || hasCoarse)) {
fetchRealLocationOnce(lm) { realWgsLat, realWgsLng -> fetchRealLocationOnce(lm) { realWgsLat, realWgsLng ->
val gcj = CoordinateConverter.wgs84ToGcj02(realWgsLat, realWgsLng) val gcj = CoordinateConverter.wgs84ToGcj02(realWgsLat, realWgsLng)
@@ -380,8 +383,8 @@ fun LocationScreen(
mapViewRef?.controller?.animateTo(p) mapViewRef?.controller?.animateTo(p)
mapViewRef?.invalidate() mapViewRef?.invalidate()
} }
} else { } else if (isVisible) {
// ================= 【总开关关闭】 ================= // ================= 【总开关关闭 & 页面可见】 =================
// 1. 关闭的一瞬间,立马主动获取一次真机真实坐标,拉回当前设备物理原位 // 1. 关闭的一瞬间,立马主动获取一次真机真实坐标,拉回当前设备物理原位
if (hasFine || hasCoarse) { if (hasFine || hasCoarse) {
fetchRealLocationOnce(lm) { realWgsLat, realWgsLng -> fetchRealLocationOnce(lm) { realWgsLat, realWgsLng ->
@@ -600,12 +603,24 @@ fun LocationScreen(
}, },
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
update = { mapView -> update = { mapView ->
currentMarker?.position = currentGeoPoint if (isVisible) {
mapView.invalidate() 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( Surface(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@@ -1559,6 +1574,7 @@ fun LocationScreen(
} }
} }
) )
}
} }
} }