hotwired / turbo-android

Android framework for making Turbo native apps
MIT License
424 stars 50 forks source link

[Feature request] Custom properties filled via pattern capture groups #247

Closed svantepolk closed 7 months ago

svantepolk commented 1 year ago

We have some situations where the native app needs a rails ID to do something native through an API, but the pages are web.

Currently, we rely on specially-marked hidden elements which the app looks for on page visit, but I think it could make things a bit cleaner and more flexible if there was a way to add capture groups to patterns and then convert those capture groups into properties.

I think this would make it easier to enhance web pages with native functionality.

eg:

{
  "patterns": [ "/posts/(\\d+)/comments/new" ],
  "properties": {
    "context": "modal",
    "uri": "turbo://fragment/comment_on_post",
    "post_id": "$1"
  }
}

Would Basecamp be open to such a change? I'd be happy to make a patch.

jayohms commented 1 year ago

Really interesting idea! At 37signals, we add extension functions in the app to extract whatever IDs we may need for native functionality:

// Extension function
fun String.bucketId(): Long? = find("/buckets/([0-9]+)")?.toLongOrNull()

private fun String.find(patternRegex: String): String? {
    return Regex(patternRegex, RegexOption.IGNORE_CASE).find(this)?.groups?.get(1)?.value
}

// Use
val bucketId = location.bucketId()

This generally works well b/c urls may come from an API response as well as a web destination location, so we can have shared logic. However, your approach means that ID captures could change if urls change, which is quite nice.

My main concern is having to be extremely careful with regex rules when you have multiple path patterns. Would the simple approach I outlined above work for your needs? Or am I missing something?

joemasilotti commented 1 year ago

Side note, but having more insight into how 37signals builds Turbo Native apps would be hugely appreciated. I'd love to know more about this stuff! A lot of times it feels like we are in the dark on best practices or even how to do something. And there's a ton of knowledge built up that could be shared. This issue response is a perfect example of something I never even though of but makes perfect sense when seeing it on paper.

jayohms commented 7 months ago

Thinking about this more, I think using a custom regex within your app or using a Strada component to notify the app about specific page data is the way to go. We don't have any plans to add capture groups to the path config at this time.