Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigate forward not working when capture back-presses is false #12

Open
prawnguy opened this issue May 14, 2024 · 0 comments
Open

Navigate forward not working when capture back-presses is false #12

prawnguy opened this issue May 14, 2024 · 0 comments

Comments

@prawnguy
Copy link

The navigate forward functionality seems to be broken when capture back presses is disabled.
This is my composable:

@Composable
fun BrowserScreen(
    navController: NavController,
    initialUrl: String,
) {
    val viewModel = hiltViewModel<BrowserViewModel, BrowserViewModel.BrowserViewModelFactory> { factory ->
        factory.create(navController,initialUrl)
    }
    val state by viewModel.state.collectAsState()
    val onEvent = viewModel::onEvent

    val webViewNavigator = rememberWebViewNavigator()
    val webViewState = rememberWebViewState(state.activeUrl)
    val accompanistWebViewClient = remember {
        CustomWebViewClient(
            onActiveUrlChanged = {
                onEvent(BrowserEvent.ActiveUrlChanged(it))
            })
    }
    val focusManager = LocalFocusManager.current

//    BackHandler(enabled = true) {
//        webViewNavigator.navigateBack()
//    }

    Scaffold(
        topBar = {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.End,
                modifier = Modifier
                    .background(MaterialTheme.colorScheme.background)
                    .padding(paddingValues = PaddingValues(horizontal = 5.dp, vertical = 3.dp))
            ) {
                AppBarSearchField(
                    value = state.urlBar,
                    onValueChange = {
                        onEvent(BrowserEvent.UrlBarModified(it))
                    },
                    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
                    keyboardActions = KeyboardActions(
                        onSearch = {
                            webViewNavigator.loadUrl(state.urlBar)
                            focusManager.clearFocus()
                        }
                    ),
                    modifier = Modifier
                        .widthIn(min = 300.dp)
                        .fillMaxWidth(0.85f)
                )
                Spacer(Modifier.weight(1f))
                IconButton(
                    onClick = {
                        val pageTitle = webViewState.pageTitle
                        val pageIcon = webViewState.pageIcon
                        if (pageTitle != null && pageIcon != null){
                            if(state.bookmarked){
                                onEvent(BrowserEvent.RemoveBookmark)
                            } else {
                                onEvent(BrowserEvent.AddBookmark(pageTitle,pageIcon))
                            }
                        }
                    }
                ) {
                    val iconId = if (state.bookmarked) {R.drawable.baseline_bookmark_remove_24} else {R.drawable.baseline_bookmark_add_24}
                    Icon(painter = painterResource(id = iconId), contentDescription = "Add to bookmarks", modifier = Modifier.size(30.dp))
                }

            }
        },
        bottomBar = {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.End,
                modifier = Modifier
                    .background(MaterialTheme.colorScheme.background)
                    .padding(paddingValues = PaddingValues(horizontal = 5.dp, vertical = 3.dp))
            ) {
                IconButton(
                    onClick = {
                        webViewNavigator.navigateBack()
                    }
                ) {
                    Icon(painter = painterResource(id = R.drawable.baseline_arrow_back_ios_24), contentDescription = "Navigate backward", modifier = Modifier.size(30.dp))
                }
                Spacer(Modifier.weight(0.1f))
                IconButton(
                    onClick = {
                        webViewNavigator.navigateForward()
                    }
                ) {
                    Icon(painter = painterResource(id = R.drawable.baseline_arrow_forward_ios_24), contentDescription = "Navigate forward", modifier = Modifier.size(30.dp))
                }
                Spacer(Modifier.weight(1f))
                IconButton(
                    onClick = {
                        onEvent(BrowserEvent.ShowExitConfirmationPrompt)
                    }
                ) {
                    Icon(painter = painterResource(id = R.drawable.baseline_exit_to_app_24), contentDescription = "Exit browser", modifier = Modifier.size(40.dp))
                }
            }
        }
    ) {
            if (state.showExitConfirmationPrompt) {
                ConfirmationDialog(
                    onDismissRequest = { onEvent(BrowserEvent.DismissExitConfirmationPrompt) },
                    onConfirmation = { onEvent(BrowserEvent.ExitBrowser) },
                    dialogTitle = "Exit browser",
                    dialogText = "Are you sure you want to exit the browser?",
                    confirmText = "Exit",
                    dismissText = "Cancel"
                )
            }
            WebView(
                state = webViewState,
                navigator = webViewNavigator,
                client = accompanistWebViewClient,
                captureBackPresses = true,
                onCreated = { webView ->
                    webView.settings.javaScriptEnabled = true
                    webView.setOnCreateContextMenuListener(View.OnCreateContextMenuListener { menu, v, menuInfo ->
                        val result = (v as WebView).getHitTestResult()

                        val type = result.getType()

                        // Confirm type is an image
                        if (type == HitTestResult.IMAGE_TYPE || type == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                            val imageUrl = result.getExtra()
                            println(imageUrl)
                        }
                    })
                },
                modifier = Modifier
                    .fillMaxSize()
                    .padding(it)
            )
        }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant