Remove unwanted scroll behavior on the FantasyMap when popBackStack.

This commit is contained in:
Thomas Andres Gomez 2023-11-10 16:15:57 +01:00
parent a6e86dd331
commit 50b0eb31d4
3 changed files with 22 additions and 24 deletions

View file

@ -170,24 +170,6 @@ fun FantasyMap(
} }
} }
@Composable
@Stable
fun rememberFantasyMapState(
initialScale: Float = 1f,
initialOffset: Offset = Offset.Zero,
minScale: Float = 1f,
maxScale: Float = 5f,
): FantasyMapState {
return remember {
FantasyMapState(
initialScale = initialScale,
initialOffset = initialOffset,
minScale = minScale,
maxScale = maxScale,
)
}
}
@Stable @Stable
class FantasyMapState( class FantasyMapState(
initialScale: Float = 1f, initialScale: Float = 1f,

View file

@ -93,7 +93,6 @@ fun LocationDetail(
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val scroll = rememberScrollState() val scroll = rememberScrollState()
val fantasy = rememberFantasyMapState()
val snapBehavior = rememberSnapConnection(scrollState = scroll) val snapBehavior = rememberSnapConnection(scrollState = scroll)
val ok = stringResource(id = android.R.string.ok) val ok = stringResource(id = android.R.string.ok)
@ -107,7 +106,7 @@ fun LocationDetail(
.fillMaxSize() .fillMaxSize()
.nestedScroll(connection = snapBehavior), .nestedScroll(connection = snapBehavior),
scrollState = scroll, scrollState = scroll,
fantasyMapState = fantasy, fantasyMapState = viewModel.fantasyMapState,
item = viewModel.location, item = viewModel.location,
selectedIndex = viewModel.selectedMarquee, selectedIndex = viewModel.selectedMarquee,
mapHighlight = mapHighlight, mapHighlight = mapHighlight,
@ -145,13 +144,14 @@ fun LocationDetail(
} else { } else {
snackJob.value?.cancel() snackJob.value?.cancel()
} }
fantasy.toggleFreeHand(it) viewModel.fantasyMapState.toggleFreeHand(it)
}, },
onCenter = { onCenter = {
fantasy.scale(scale = 1f) viewModel.fantasyMapState.scale(scale = 1f)
fantasy.pan(offset = Offset.Zero) viewModel.fantasyMapState.pan(offset = Offset.Zero)
}, },
onZoomIn = { onZoomIn = {
val fantasy = viewModel.fantasyMapState
val newScale = fantasy.scale + 1 val newScale = fantasy.scale + 1
if (newScale <= fantasy.maxScale) { if (newScale <= fantasy.maxScale) {
val oldScale = fantasy.scale val oldScale = fantasy.scale
@ -166,6 +166,7 @@ fun LocationDetail(
} }
}, },
onZoomOut = { onZoomOut = {
val fantasy = viewModel.fantasyMapState
val newScale = fantasy.scale - 1 val newScale = fantasy.scale - 1
if (newScale >= fantasy.minScale) { if (newScale >= fantasy.minScale) {
val oldScale = fantasy.scale val oldScale = fantasy.scale
@ -419,7 +420,14 @@ private fun LocationPreview() {
LocationContent( LocationContent(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
scrollState = rememberScrollState(), scrollState = rememberScrollState(),
fantasyMapState = rememberFantasyMapState(), fantasyMapState = remember {
FantasyMapState(
initialScale = 1f,
initialOffset = Offset.Zero,
minScale = 1f,
maxScale = 5f,
)
},
item = remember { item = remember {
mutableStateOf( mutableStateOf(
LocationDetailUio( LocationDetailUio(

View file

@ -6,6 +6,7 @@ import android.content.ClipboardManager
import android.content.Context.CLIPBOARD_SERVICE import android.content.Context.CLIPBOARD_SERVICE
import androidx.compose.runtime.State import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Offset
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.SavedStateHandle
@ -32,6 +33,13 @@ class LocationDetailViewModel @Inject constructor(
private val _selectedMarquee = mutableStateOf<Int?>(null) private val _selectedMarquee = mutableStateOf<Int?>(null)
val selectedMarquee: State<Int?> get() = _selectedMarquee val selectedMarquee: State<Int?> get() = _selectedMarquee
val fantasyMapState = FantasyMapState(
initialScale = 1f,
initialOffset = Offset.Zero,
minScale = 1f,
maxScale = 5f,
)
init { init {
val argument = savedStateHandle.locationDetailArgument val argument = savedStateHandle.locationDetailArgument
val source = repository.find(id = argument.id) val source = repository.find(id = argument.id)