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

Location access via JavaScript #28

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Location access via JavaScript #28

wants to merge 8 commits into from

Conversation

joemasilotti
Copy link
Member

@joemasilotti joemasilotti commented Apr 5, 2024

This PR configures the web view and WebFragment to access the user's location via JavaScript.

The first time the user's location is accessed the following appears:

Xnapper-2024-04-05-11 13 05

If accepted, WebFragment reloads the page. When the location is accessed a second time the following appears:

Xnapper-2024-04-05-11 13 25

If the permissions in the manifest file are commented out the following appears when accessing the user's location:

Xnapper-2024-04-05-11 13 15

Note that this only works against HTTPS websites, so you will need to tunnel via ngrok for Chrome to expose the user's location.

The following Stimulus controller was used to test the location access:

// public/javascript/controllers/location_controller.js

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  request(event) {
    event.preventDefault()

    navigator.geolocation.getCurrentPosition(
      this.success.bind(this),
      this.failure.bind(this),
      {enableHighAccuracy: true}
    )
  }

  success(position) {
    const {latitude, longitude} = position.coords
    alert(`${latitude}, ${longitude}`)
  }

  failure(error) {
    alert(`Could not get your location: ${error.message}.`)
  }
}

Comment on lines 50 to 70
override fun createWebChromeClient(): TurboWebChromeClient {
return object : TurboWebChromeClient(session) {
override fun onGeolocationPermissionsShowPrompt(
origin: String?,
callback: GeolocationPermissions.Callback?
) {
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
permissionLauncher.launch(
Manifest.permission.ACCESS_FINE_LOCATION
)
} else {
callback?.invoke(origin, true, false)
}
}
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would live in TurboWebChromeClient. But I wasn't sure how to get a reference for the permission launcher.

private val permissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) refresh()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an ideal world this would refresh and re-request the user's location. But I don't think that is possible with a web view.

jayohms and others added 7 commits April 25, 2024 13:51
* main:
  Present better looking Material dialogs for WebView alert and confirm dialogs
  Allow camera capture by default if the accept type explicitly allows images
* main: (63 commits)
  Update to the latest gradle plugin
  Include Turbo Native Android in the user agent substring for backwards compatibilty with existing apps
  Change Turbo Native to Hotwire Native in user agent.
  Update HotwireNavigation.kt
  Remove old docs
  Update README.md
  Revert "Add repositories to each build script"
  Add repositories to each build script
  Disable the gradle configuration cache due to incompatibility with the signing plugin
  Add the signing plugin
  Add Sonatype (Maven Central) publishing support
  Readme updates
  Add publishing build script for the navigation-fragments module
  Remove Strada references
  Update demo site url
  Update android plugin dependencies
  Update CI workflow actions
  Update logo filename
  Update raster launcher icon
  Update vector launcher icon
  ...

# Conflicts:
#	demo/src/main/kotlin/dev/hotwire/demo/features/web/WebFragment.kt
… the HotwireWebChromeClient if the app has declared location permission(s) are declared in the app manifest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants