StanfordSpezi / SpeziKt

Kotlin & Android Version of the Stanford Spezi Framework
http://spezi.health/SpeziKt/
MIT License
8 stars 1 forks source link

Add RouteParamProvider Component for JSON Decoding #60

Closed Basler182 closed 1 month ago

Basler182 commented 2 months ago

Problem

The current approach using extension functions for decoding JSON from SavedStateHandle involves repetitive code and adds complexity to our tests. We need to stub the JSON data in each test case individually, which is inefficient and error-prone.

Solution

@ViewModelScoped
class RouteParamProvider @Inject constructor(
   val savedStateHandle: SavedStateHandle,
) {
   inline fun <reified T> decode(key: String): T {
       val jsonString = savedStateHandle.get<String>(key) ?: throw IllegalArgumentException("Argument not found")
       return Json.decodeFromString(jsonString)
   }
}

Additional context

No response

Code of Conduct

eldcn commented 1 month ago

Closing this issue since we need the function to be inlined with a reified T parameter, however, mockk does not support stubbing of inline functions