Remove unwanted scroll behavior on the FantasyMap when popBackStack.
This commit is contained in:
		
							parent
							
								
									a6e86dd331
								
							
						
					
					
						commit
						50b0eb31d4
					
				
					 3 changed files with 22 additions and 24 deletions
				
			
		| 
						 | 
				
			
			@ -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
 | 
			
		||||
class FantasyMapState(
 | 
			
		||||
    initialScale: Float = 1f,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -93,7 +93,6 @@ fun LocationDetail(
 | 
			
		|||
 | 
			
		||||
    val scope = rememberCoroutineScope()
 | 
			
		||||
    val scroll = rememberScrollState()
 | 
			
		||||
    val fantasy = rememberFantasyMapState()
 | 
			
		||||
    val snapBehavior = rememberSnapConnection(scrollState = scroll)
 | 
			
		||||
 | 
			
		||||
    val ok = stringResource(id = android.R.string.ok)
 | 
			
		||||
| 
						 | 
				
			
			@ -107,7 +106,7 @@ fun LocationDetail(
 | 
			
		|||
                .fillMaxSize()
 | 
			
		||||
                .nestedScroll(connection = snapBehavior),
 | 
			
		||||
            scrollState = scroll,
 | 
			
		||||
            fantasyMapState = fantasy,
 | 
			
		||||
            fantasyMapState = viewModel.fantasyMapState,
 | 
			
		||||
            item = viewModel.location,
 | 
			
		||||
            selectedIndex = viewModel.selectedMarquee,
 | 
			
		||||
            mapHighlight = mapHighlight,
 | 
			
		||||
| 
						 | 
				
			
			@ -145,13 +144,14 @@ fun LocationDetail(
 | 
			
		|||
                } else {
 | 
			
		||||
                    snackJob.value?.cancel()
 | 
			
		||||
                }
 | 
			
		||||
                fantasy.toggleFreeHand(it)
 | 
			
		||||
                viewModel.fantasyMapState.toggleFreeHand(it)
 | 
			
		||||
            },
 | 
			
		||||
            onCenter = {
 | 
			
		||||
                fantasy.scale(scale = 1f)
 | 
			
		||||
                fantasy.pan(offset = Offset.Zero)
 | 
			
		||||
                viewModel.fantasyMapState.scale(scale = 1f)
 | 
			
		||||
                viewModel.fantasyMapState.pan(offset = Offset.Zero)
 | 
			
		||||
            },
 | 
			
		||||
            onZoomIn = {
 | 
			
		||||
                val fantasy = viewModel.fantasyMapState
 | 
			
		||||
                val newScale = fantasy.scale + 1
 | 
			
		||||
                if (newScale <= fantasy.maxScale) {
 | 
			
		||||
                    val oldScale = fantasy.scale
 | 
			
		||||
| 
						 | 
				
			
			@ -166,6 +166,7 @@ fun LocationDetail(
 | 
			
		|||
                }
 | 
			
		||||
            },
 | 
			
		||||
            onZoomOut = {
 | 
			
		||||
                val fantasy = viewModel.fantasyMapState
 | 
			
		||||
                val newScale = fantasy.scale - 1
 | 
			
		||||
                if (newScale >= fantasy.minScale) {
 | 
			
		||||
                    val oldScale = fantasy.scale
 | 
			
		||||
| 
						 | 
				
			
			@ -419,7 +420,14 @@ private fun LocationPreview() {
 | 
			
		|||
            LocationContent(
 | 
			
		||||
                modifier = Modifier.fillMaxSize(),
 | 
			
		||||
                scrollState = rememberScrollState(),
 | 
			
		||||
                fantasyMapState = rememberFantasyMapState(),
 | 
			
		||||
                fantasyMapState = remember {
 | 
			
		||||
                    FantasyMapState(
 | 
			
		||||
                        initialScale = 1f,
 | 
			
		||||
                        initialOffset = Offset.Zero,
 | 
			
		||||
                        minScale = 1f,
 | 
			
		||||
                        maxScale = 5f,
 | 
			
		||||
                    )
 | 
			
		||||
                },
 | 
			
		||||
                item = remember {
 | 
			
		||||
                    mutableStateOf(
 | 
			
		||||
                        LocationDetailUio(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,6 +6,7 @@ import android.content.ClipboardManager
 | 
			
		|||
import android.content.Context.CLIPBOARD_SERVICE
 | 
			
		||||
import androidx.compose.runtime.State
 | 
			
		||||
import androidx.compose.runtime.mutableStateOf
 | 
			
		||||
import androidx.compose.runtime.remember
 | 
			
		||||
import androidx.compose.ui.geometry.Offset
 | 
			
		||||
import androidx.lifecycle.AndroidViewModel
 | 
			
		||||
import androidx.lifecycle.SavedStateHandle
 | 
			
		||||
| 
						 | 
				
			
			@ -32,6 +33,13 @@ class LocationDetailViewModel @Inject constructor(
 | 
			
		|||
    private val _selectedMarquee = mutableStateOf<Int?>(null)
 | 
			
		||||
    val selectedMarquee: State<Int?> get() = _selectedMarquee
 | 
			
		||||
 | 
			
		||||
    val fantasyMapState = FantasyMapState(
 | 
			
		||||
        initialScale = 1f,
 | 
			
		||||
        initialOffset = Offset.Zero,
 | 
			
		||||
        minScale = 1f,
 | 
			
		||||
        maxScale = 5f,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    init {
 | 
			
		||||
        val argument = savedStateHandle.locationDetailArgument
 | 
			
		||||
        val source = repository.find(id = argument.id)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue