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.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,16 +141,38 @@ 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(
|
}
|
||||||
|
|
||||||
|
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,
|
isModuleActive = isModuleActive,
|
||||||
isServiceEnabled = isServiceEnabled,
|
isServiceEnabled = isServiceEnabled,
|
||||||
onServiceEnabledChange = { enabled ->
|
onServiceEnabledChange = { enabled ->
|
||||||
@@ -158,8 +182,24 @@ fun MainScreen(
|
|||||||
onOpenWhitelist = { activeAppSelectionMode = AppMockMode.WHITELIST },
|
onOpenWhitelist = { activeAppSelectionMode = AppMockMode.WHITELIST },
|
||||||
onCheckUpdate = performManualCheck
|
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)
|
@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,11 +603,23 @@ fun LocationScreen(
|
|||||||
},
|
},
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = Modifier.fillMaxSize(),
|
||||||
update = { mapView ->
|
update = { mapView ->
|
||||||
|
if (isVisible) {
|
||||||
|
if (mapView.visibility != android.view.View.VISIBLE) {
|
||||||
|
mapView.visibility = android.view.View.VISIBLE
|
||||||
|
mapView.onResume()
|
||||||
|
}
|
||||||
currentMarker?.position = currentGeoPoint
|
currentMarker?.position = currentGeoPoint
|
||||||
mapView.invalidate()
|
mapView.invalidate()
|
||||||
|
} else {
|
||||||
|
if (mapView.visibility != android.view.View.GONE) {
|
||||||
|
mapView.onPause()
|
||||||
|
mapView.visibility = android.view.View.GONE
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (isVisible) {
|
||||||
// ------------------ 2. 顶部未激活时的浮动搜索胶囊 ------------------
|
// ------------------ 2. 顶部未激活时的浮动搜索胶囊 ------------------
|
||||||
Surface(
|
Surface(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@@ -1560,6 +1575,7 @@ fun LocationScreen(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user