Code Monkey home page Code Monkey logo

Comments (7)

vinaygaba avatar vinaygaba commented on May 14, 2024

I somehow completely missed noticing this issue! Thanks for reporting it, I'm going to take a look!

from showkase.

vinaygaba avatar vinaygaba commented on May 14, 2024

@peterfortuin I think this might be related to the difference between the compose versions used in the library and the one you are using. Can you try using the alpha02 version of Jetpack Compose and giving it a shot?

I recently just bumped up the library to the latest version - #112. I'll be doing a release in the next couple days so you should be able to use the latest library version soon. In the meanwhile, I'm curious to see if it works fine when you use Jetpack Compose v 1.0.0-alpha02

from showkase.

vinaygaba avatar vinaygaba commented on May 14, 2024

@peterfortuin Can you try the latest release of this library. I believe that will certainly fix your problem!

implementation "com.airbnb.android:showkase:1.0.0-alpha03"
kapt "com.airbnb.android:showkase-processor:1.0.0-alpha03"

from showkase.

peterfortuin avatar peterfortuin commented on May 14, 2024

If I downgrade to compose 1.0.0-alpha02 I have another problem. Maybe not related to this. I'm not sure if I can upgrade to the latest version of compose. Have to check.
To be continued.

from showkase.

vinaygaba avatar vinaygaba commented on May 14, 2024

@peterfortuin You don't need to downgrade anymore! You can use the latest version of compose as this library has been upgraded!

from showkase.

peterfortuin avatar peterfortuin commented on May 14, 2024

I have included showkase:1.0.0-alpha03 into my project with compose:1.0.0-alpha04 and that seems to work.

But now that I have it working, I'm not sure how useful it is for me.

Some things that I noticed:

  • The Components part directly crashes for me, because I use ViewModels on a lot of places inside @composable functions and those ViewModels have constructor parameters. Of course Showkase doesn't know what to do with those parameters of course.
    This is not only a problem with Showkase, but this is a general problem I have with the @Preview annotation of Compose.
  • Currently I have the colors of my application as Color Resources in xml and read them with colorResource function into my Theme and other places. As far I can see from the documentation I have to add the colors again to show up in Showkaze. But I prefer not to write down the colors in two places. (Changes are big that they will not match any more after some changes in the future). Is there no way to read them from the resources like this?
@ShowkaseColor(name = "Primary Color", group = "Material Design")
val primaryColor = colorResource(R.color.colorPrimary)
  • Same problem for the typography. I create those inside my AppTheme @composable function. I copy them from the MaterialTheme and change some values. I can't do that outside of the function and annotate that.

I hope you can do something with my feedback or maybe tell me how I can structure my application differently to be more compatible with Showkase.

from showkase.

vinaygaba avatar vinaygaba commented on May 14, 2024

Hey @peterfortuin,
I'm glad the original issue that you reported is fixed now. I can try to address some of the points you raised -

  1. Showkase (and Jetpack Compose) does have a way to provide parameters for preview. It does so using the @PreviewParameter annotation. I recently added support for it but I still have another PR to go, hence I have not mentioned it in the README just yet. This is how you can use it to provide parameters and use view models with Showkase. I'm not sure if @Preview in Android Studio allows using ViewModel, but Showkase certainly does! Here is what it would roughly look like in practice.
data class ViewModelParameters(
    val param1: String,
    val param2: Int
)


@ShowkaseComposable("Component with ViewModel")
@Composable
fun test(
    @PreviewParameter(provider = CustomPreviewProvider::class) viewModelParameters: ViewModelParameters
) {
    val factory = MyViewModelFactory(viewModelParameters.param1, viewModelParameters.param2)
    val viewModel: MyViewModel = viewModel(factory = factory)
    Text(viewModel.param1)
}

class MyViewModel(
    val param1: String,
    val param2: Int
): ViewModel() {}

class MyViewModelFactory(
    val param1: String,
    val param2: Int
): ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(MyViewModel::class.java)) {
            return MyViewModel(param1, param2) as T
        }
        throw IllegalArgumentException("Model class ${modelClass.name} not valid")
    }

}

class CustomPreviewProvider: PreviewParameterProvider<ViewModelParameters> {
    override val values: Sequence<ViewModelParameters>
        get() = sequenceOf(
            ViewModelParameters(param1 = "Param1", param2 = 5)
        )
}
  1. This is a reasonable request but there is no easy way to solve this as colorResource can only be used in the scope of a Composable function and the code that is generated doesn't have @composable scope. I can think of a few approaches to potentially support this but it's going to be a good amount of work. I might give this a shot at some point.

I'll close this ticket for now since the original problem is fixed but I'll create a separate issue (2) as a feature request. Tracking it here - #115

from showkase.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.