feat: keep LocationScreen alive across navigation tabs with zero reload
This commit is contained in:
1 parent
ee7b7d3666
commit
07abd416b6
2 files changed
+77
-21
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.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,16 +141,38 @@ fun MainScreen(
|
||||
)
|
||||
}
|
||||
) { innerPadding ->
|
||||
AnimatedContent(
|
||||
targetState = currentTab,
|
||||
transitionSpec = {
|
||||
fadeIn() togetherWith fadeOut()
|
||||
},
|
||||
label = "TabContentAnimation",
|
||||
modifier = Modifier.padding(innerPadding)
|
||||
) { targetTab ->
|
||||
when (targetTab) {
|
||||
NavTab.HOME -> HomeScreen(
|
||||
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)
|
||||
)
|
||||
}
|
||||
|
||||
// 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 ->
|
||||
@@ -158,8 +182,24 @@ fun MainScreen(
|
||||
onOpenWhitelist = { activeAppSelectionMode = AppMockMode.WHITELIST },
|
||||
onCheckUpdate = performManualCheck
|
||||
)
|
||||
NavTab.LOCATION -> LocationScreen()
|
||||
NavTab.SETTINGS -> SettingsScreen()
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
@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,11 +603,23 @@ fun LocationScreen(
|
||||
},
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
update = { mapView ->
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
if (isVisible) {
|
||||
// ------------------ 2. 顶部未激活时的浮动搜索胶囊 ------------------
|
||||
Surface(
|
||||
modifier = Modifier
|
||||
@@ -1561,6 +1576,7 @@ fun LocationScreen(
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制文本到系统剪贴板工具函数
|
||||
|
||||
Reference in new issue
Block a user