Reputation: 1277
In my Android project, I’m using a DeepLinkGenerator class to centralize all DeepLink URIs for navigating between fragments. The DeepLinks are also defined in navgraph.xml. My goal is to ensure that the DeepLinks in the DeepLinkGenerator class and the ones defined in navgraph.xml stay synchronized.
Problem:
I already have a DeepLinkGenerator class that generates DeepLinks for navigation in Kotlin.
My DeepLinks are also defined in navgraph.xml, and sometimes they include arguments.
If I update the DeepLink in one place (e.g., in the XML), I want to be alerted to update the corresponding DeepLink in the DeepLinkGenerator class.
What I Need:
I’m looking for a way to ensure that if a DeepLink is added or modified in navgraph.xml, the Kotlin DeepLinkGenerator class is updated accordingly.
I want to catch these discrepancies at compile time, ideally raising a warning or error if the DeepLinks don’t match.
Existing Setup:
DeepLinkGenerator:
object DeepLinkGenerator {
private const val BASE_URI = "myapp://"
fun getFragment1DeepLink(arg: String): Uri {
return Uri.parse("$BASE_URI/fragment1").buildUpon()
.appendQueryParameter("arg", arg)
.build()
}
fun getFragment2DeepLink(): Uri {
return Uri.parse("$BASE_URI/fragment2")
}
}
< deepLink android:id="@+id/deepLinkFragment1" app:uri="myapp://fragment1?arg={arg}" />
My Questions:
How can I implement a compile-time validation to ensure DeepLinks defined in navgraph.xml are in sync with the URIs generated by the DeepLinkGenerator?
Is annotation processing the best approach for this, or is there an alternative way to ensure synchronization between the XML and Kotlin DeepLink generation?
do we have any already implemented best practice for this purpose?
Any advice or examples on how to achieve this would be greatly appreciated!
Upvotes: 0
Views: 28