Viktor Vostrikov
Viktor Vostrikov

Reputation: 1522

Can the app provide Jetpack Compose view for the SDK to recreate?

We are planning to redesign some layouts in the SDK. Right now we use XML and allow the client's app to override the layouts, by them simply copying our XML files and applying customization, without modifying the IDs of the elements. Now, I am not sure how would it be possible to do something similar with Jetpack compose.

I am looking the following approach from our IOS SDK to be implemented in the Android:

  1. We create interfaces of our UIViews (Like Language selection View, which has SearchView, two TextViews and a button).
  2. Then we expose this interface and create a static method, which accepts concrete implementations of the views.
  3. Then our SDK checks if the client app actually did modifications and if he did, we inflate the supplied views.

Would it be possible with Jetpack Compose? I would assume you can also expose interfaces and then client apps provide the layout structure. However, we would not like to let the client app to handle the state. We would like for them to use Jetpack compose only for the layout display.

Upvotes: 0

Views: 365

Answers (1)

Phil Dukhov
Phil Dukhov

Reputation: 87804

You can mark your view lambda arguments with @Composable, so those can be used as view builders:

@Composable
fun SdkView(
    title: @Composable () -> Unit,
    content: @Composable (SomeState) -> Unit,
) {
    // here you can use rememberSaveable or view model instead to get more reliable state storage 
    val state = remember { SomeState() }
    Column {
        title()
        content(state)
    }
}

@Composable
fun ClientView(
) {
    SdkView(
        title = {
            Text("Hello")
        },
        content = { state ->
            // build view depending on state
        }
    )
}

Upvotes: 1

Related Questions