Dagger Hilt - Custom Qualifiers For Hilt in Android!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Salam friends and welcome back to another hair tutorial in this video I'm going to show you how you can use qualifiers in order to provide a specific variant of some of the dependencies you have let's get started so this is an example to show you something let's pretend we have a class called view model right in a lot of cases you want to eject dispatcher okay let's have our Constructor inject here and we want to inject a coutin dispatch right you can provide it like following now here the dispatcher you want to provide different dispatchers to different stuff for example we can create a module like following I'm going to put everything into one simple class here before that in health I'm using health 2.48 okay and I'm using it with KSP this is fine so here let's pretend I'm having a module and let's say I'm going to install it in the Singleton component like that and let's call it dispatchers module like following and here let's say I'm going to provide the iio dispatcher okay and here we can say that it's going to be coutin dispatcher and here you can do dispatchers do I okay this is how you can provide it this is how you can get it now what will happen is that in a lot of cases you won't just have only the iio dispatch maybe you want the unconfined dispatcher so here in order to have the unconfined dispatcher are going to do here unconfined dispatcher right now how do you tell that in this case I want this dispatcher and not this dispatcher well there is actual way to do it for example there is a name like following in this you can provide a name for this dependency you can call it I for example just a simple string you can of course create constant for that also and here let's call it un confined okay now you notice that we are not providing any of these because this will require what is the dispatcher you want and here you can use the name and again you need to pass which one you are providing like the following and here you are going to provide this one to this one okay okay this is a cool way to do it there is an actual better way because we are not depending on string you can create what we call annotation specific annotation these are called qualifiers right so here is how you do it first you create an annotation here we are going to create annotation for this one and for this one right how you can do it you can create an annotation like following annotation class and you can call it IO dispatcher okay this is The annotation are going to have and then you need to annotate it with qualifier okay and then we need something related to the retention of this annotation because we need this in the compile time we don't need it in the what we call it the right time so there is something called retention here retention and in this one there is something called annotation retention and you need to make it in the binary okay so this will be presented in the do class files okay just make sure to uh to present this whenever you are dealing with qualifiers because this one this annotation will be required for only comp compile time stuff okay so using this annotation binary this annotation won't be presented at run time via the reflection okay this is the main thing if you want it to be presented via the reflection you can use run time okay I think this is the default one the default so using reflection you can access what is The annotation being used and everything but we don't want it at the wrong time we want only to be presented at the classifi in the binary format that's why we use this one now what is the beauty is that you can get rid of this name and call it iio dispatcher like the following and here instead of using this name because sometimes you make typo you can use a constant strings but you can use it that way directly with the I dispatcher the only downside is that for everyone you have to do another thing for the unconfined for example you can get here for the unconfined andc confined dispatcher and tell it hey you can use and confined here but it reads really well here with The annotation it's much better like it's specific annotation for each dispatch so this is how you can provide specific dependencies for example you have multiple types of for example this sectin dispatcher and you can tell it in much readable way which dependencies you want okay that's pretty much it for this video actually I'm having full playlist about Heil and my YouTube channel you can check it thank you very much for watching this video to the end don't forget to subscribe to channel and always see you in the next videos for
Info
Channel: Charfaoui Younes
Views: 405
Rating: undefined out of 5
Keywords: dependency injection android, android dependency injection, hilt android, android hilt tutorial, android dagger hilt, dagger hilt, hilt kotlin android, hilt kotlin, hilt dependency injection, android dependency injection example, android hilt, android hilt version, android hilt viewmodel, hilt codelab, dagger hilt-android github, android hilt viewmodel factory, android hilt example, android hilt viewmodel scope, android hilt github, Dagger 2, Dagger, dependency injection
Id: UesX0H7QEmo
Channel Id: undefined
Length: 4min 44sec (284 seconds)
Published: Tue Apr 30 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.