Jetpack Compose mini course for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there have you ever felt like creating user interfaces for your Android app was a bit of a puzzle but say hello to Japan compose a superhero like toolkit that swoops in to save the day imagine you're an artist with a blank canvas ready to paint your UI Masterpiece but wait there is a catch the traditional way of Designing Android interfaces involve a lot of complex code like mixing colors without a proper palette it's like trying to build a sand castle with the square bricks not very intuitive right here's where Japan compose comes to the rescue it's like a magic one that transforms your UI creation process into a delightful and smooth experience it's all about making things easier and more fun instead of wrestling with the clunky XML layouts and wrestling with numerous view hierarchies deeper compose lets you build interfaces using a declarative and straightforward approach you describe how your UI should look like and behave in voila Japan compost takes care of the intricate coding behind the scenes so if you are ready to level up your app design and development game make sure to hit that like button and subscribe to our Channel and let's dive into the exciting world of Japan composed together so what is Japan compose so Japan compose is an Android modern toolkit for building native UI it simplifies and accelerates UI development on Android so this is just a modern toolkit which is designed lately so let's look why do we need jpa compose and why should we bother and create a new toolkit while we have an existing toolkit so let's look here first the existing toolkit has been around for a decade and since then devices have changed as well as used as expectation which exist in toolkit was designed for so hence a modern toolkit is required to accelerate and make building UI more easily and efficient so as you can see here we have two different images so this is around 2009 2010 and this is around 2000 to 2022 so as you can see here you notice the difference in design and also how things are going to be built and this is just showing us that user expectations and also the demand is changing so for this case we have to use a new modern toolkit that is going to help us to design this easily so don't get me wrong you can easily create this uis by using the existing toolkit but this will be a little bit difficult so getting a UI that is easily integrated with these things so for example complex animations and other things might be easier when we use a modern toolkit and this is what is All About Android debug compose now let's look at the declarative UI versus imperative UI so we have to ways of create getting a UI in Android and the first one is just the declarative and also the imperative so let's look at as the traditional way of creating the uis so as you can see here we have to Define an XML layout and for this case we can easily Define different views so for example here we have a button View and also a text View and for that case in order to interact with this layout we have to also Define an activity or fragment that is going to get access of these views and also interact with them so we have functions like for example find view I by ID which is going to give us the reference to this layout here and for that case all of these activities and XML layout have the state so we have to observe the state and also react to States when this changes so for example if a user clicks this button so we have to testify the ID then we have to access this method here the on click method and then fire up what we want to do and for this case we have to update the state so this is going to bring us one problem which is just the synchronization problem so we have to synchronize everything by ourselves and this can be a little difficult and a daunting task when we have a deep nested layout or a complex layout so for this case this is just really difficult okay now let's look at the declarative Way by using jpa compose so as you can see here we have two places so we have a state and also we have a UI so a state is completely decoupled from the UI so the UI is actually immutable meaning that we cannot change the UI when it is already rendered so for this case we have to react to State changes in order to change the UI so how are we going to change the UI if the UI is immutable so for this case we have to redraw the entire UI when a state changes so this seems like inefficient because we have to re-render the entire UIA for example only a text field changes so this is not the case with Japan compose so it only updates change the UI component so for example if we have the text field and the text field has changed then we are going to re-render only the build and we are going to see how this works in a background when we write the actual code okay now let's look at his sample code so we have here a jpa compose code that is written in kotlin so when you look this you can see it's just like a normal kotlin code which is actually the same but different so we have here a function that is just called the Japan compose and how to differentiate between a composable and a normal function is just by using this annotation here so when you annotate a function with at composable then we know this is going to be a composable function now when you look here at the right hand side we have here an image that is rendered from this piece of code here and basically here we have the background which is just going to be the system background and also here we have for example a card and also we have an image and a jpa compose text and these are basically arranged in a vertically sequence format so when we arrange things in Android we use layouts so for example this Arrangement here is just vertically sequenced and this is just used by a column and as you can see here we have written a column and this column actually contains two child and the first child is just an image which is just a JPEG compose logo and this is just inserted here by passing this image composable function and after that we have a text and this text here actually is what is going to be rendered here and also we have the animated visibility so for example here we have to state so the first state is collapsed and expanded so as you can see here we have defined this state expanded and this is wrapped inside a state of a mutable state of and by default is just false so by default this text here is not going to be viewable unless a user performs an action so for example when we click this here and change as you can see we have a clickable modifier that is going to tell hey column X be expanded and for this case our state is going to change from expanded to an for example to a collapse this state to an expanded State and this is what animated visibility is going to perform animations in order to reveal that text with just a simple line of code we have achieved a powerful UI by just writing for example three or five lines of code so this is just all about jpeg compose and we are going to see it in more detail if you are still confused don't worry just enjoy this piece of code for the first time then we are going to write this from basic up to profession okay so now we want to see how we can easily create a new project so for example if you have an existing project opened and you want to easily open a new project you can easily navigate here to files and then you can come here to New and call this new project here so we can easily create here a new project and for that case here we have this pop-up window where we can easily configure our application so as first we select here an empty compose activity and then we can just Define this application here so for example this we are just going to call it state okay now let's create a simple ordering application so this application is going to be basic and ugly looking and the purpose of this video is to teach you some basics of paper compose and how things actually work so right now let's delete everything which we have here because we are going to write everything from scratch for ourselves okay now let's delete this and leave everything else okay so the first thing here which we want to do is to deal with so let's create a new composable function so we can just write comp here in order to generate a composable function and here we are just going to call this order so inside this composable function here we want to create a row so a row is just something that is going to align items in a horizontal sequenced format so when we want to align to items horizontally then we can use a row so for example here let's write the row and basically here we can pass in different types of things so the first thing here is just the modifier so a modifier is just something that is going to modify the behavior of our application or of a specific function so for example here we can just pass in a modifier so we want this modifier to occupy the whole width so for this case we can just tell this modifier hey we want to hold the whole width of our application so for this case here we can just call build max width also we can just put this in a next line here so there are other things which you can easily insert here and we are going to see them okay so right now inside this we have a row scope so aeroscope here is a place where we can just put our composables which are going to be arranged in a horizontal format so the first thing here which we want to do is to create a text and this text here is going to tell us the orders which we have so for example let's create here a simple text okay so the first text this is just going to be shown there and we want to show the amount of avocados which we are going to have so we are going to see here how many avocados which we are going to be ordering so we are going to add this in a moment so right now here we want also to have a button so whenever we click that button we want to increment the number of avocados which we want to order so for here we want first to add a space between these two items so first here we can just create a simple preview function that is going to help us to preview what we are writing here so we can just reuse a shortcut of priv and basically here this is going to create a preview function and we want to call this for example priv or order item and we want to show the system UI so you can just call here show system UI and make it to be blue okay now let's come here inside the split screen and view okay so this is just the view of what we have written as you can see here we have the avocados by inserting a text here so what we want to also insert here is just the space so we want to insert here a space horizontally and then we have to put for example a an icon so basically here we can just create a spacer composable function which is just a a composable function that renders a space which is just a transparent View so let's create a TI modifier and basically we want to provide a size so we want to say for example we want this size to occupy a certain width and height so for Acacia we're going to make a size for example of a 16 DB so this is just going to be a white space that is going to differentiate between these two composables okay so another thing here which we want to add is a an icon so this icon we were going to use it to react when we are going to make changes so let's create here an icon and we can just add this icon with image Vector here so basically here we want to pass in the image Vector which is just going to be a a representation of that particular icon so for example here we can use icons and we want to use default and here basically we have to use this add and these are just the icons which are bundled inside jpeg compose so we can easily use them okay also here we have the content description so here we can just pass now because we are not just going to use this content description now let's keep this in a separate line okay now let's try to rebuild here and see the output okay so as you can see here we have printed an icon and also the text and arrange them in a horizontal format so right now here there is nothing which is going to change because we do not have any type of a state so we have a UI and as you can see here you are in Japan compose are immutable by default so we have to create a state and a state is going to help us to mutate this UI and change it to a certain type so let's speak about the state first and understand what is a state best understand what is a state so a state is any value that can change over time so this actually includes data from a database or a valuable in a class so for example a most recent message is going to be a state so it determines what is shown in the UI at any particular time so for example if we have a collapsed State then we are going to display a collapsed UI so if we have an expanded State then we have to display the expanded UI so actually a state is going to determine what is shown in the UI at that particular time so before we just write the code of a state we have to understand first events so we said a state is any value that changes over time but what changes that state so we have to understand this and basically events are input generated from inside or outside of an application so this can be for example a user interaction I.E typing in a text field so when we type in a text field actually we are going to change the state and actually this is just an event and basically we have to change the UI or we can have a network call and that Network call is going to return us a result and actually we are going to change a state from initial state to a new state and thus we have to change the UI so while the state of the app offers a description of what to display in the UI so events are just the mechanism through which the state changes so resulting in changes of UI so when we have the state again the state determines the UI and if events are going to be the mechanism in which a UI changes so these are going to be crucial when we are writing our code so we have to understand the difference between events and also state but a state is going to determine the UI and the events are going to trigger changes in this state okay so according to the definition is that a state is anything that changes over time so for that case here we can easily create a state so right now let's create here a variable so this variable we are going to call it for example quantity so we want to understand the quantity of the avocados so for example here we are initializing this to an initial State and this is just going to be zero show or display this quantity when the user is going to be ordering and we want to increment this value when a user is going to be adding more values so for this case here we can just use a string template and access our quantity variable and for that case here we are going to display it in the UI okay so how can we change this state so particularly we say that we are going to have events so these events are going to be called in order to change this so one thing is just clicking a button and here inside our UI we have designed a plus button which is going to be incrementing these values so for this case we can just capture that interaction and then change the state so for this case we have to modify here our icon in order to to to allow incrementing or clickability of this variable in order to change the state so we can just pass in here a modifier and create a modifier that is clickable so you can just call this modifier dot clickable so this is going to modify the behavior of this icon and making it clickable so for this case inside here now we have the clickable and we can just change this icon so whenever we click this we want to increment our state by one so for this case here we can just scroll our quantity and then we re initialize this to one so we are going to be incrementing this value which is going to be stored here by 1. so for that case we have our values which are going to be incremented okay so right now we can just navigate inside here our main activity so we want to pass in the order which is just the function which we have created and basically here we can just easily run this so I'm going to run this inside our application so that we can interact with it okay so as you can see here we have the numbers of our avocados and here we have another button which you can easily click in order to modify it now let's try to click here and as you can see nothing changes here so I'm going to explain why is not nothing is happening here okay so we have displayed here the order and also the quantity but when we click the button nothing changes here so this is happening because of one thing so a composable is going to change when the state is being observed so here this state here is not observable because the composable does not know if this is a state and actually here we have to observe it in order to actually increment this value so how can we do this so we can use for example the live data or we can use another observable mechanism which is easily observed by composable and this is just called the mutable state of so for example if we change here and add here for example a mutable State off so we can just call here the mutable state so we have this type here and we want to specify the type to be an INT and basically we have to change this here so here we have to specify a mutable immutable State off and basically here we can easily not pass for example you can pass in here zero and basically this is just going to be a mutable State off so we are getting here an error and we are going to fix this in a moment so this is just how the state actually works and this error here is just creating a state object during composition without using remember so I'm going to speak about remember here in order to give you a a context why so as you can see here if you run your application you are going to see that whenever we change the state this state is not changing because this mutable State off is just an observer and is observing this but whenever we change the state this state is going to be initialized back to zero or to a default State because there is no mechanism that is going to tell hey you have to remember this value so we have to use a remember in order remember function in order to store this value during composition because when this changes or when this value increments the composable compiler is going to call up this code here again and again because this we have not kept it in a remember function then this function is going to be initialized back to zero and that's why we are not going to get an increment of a variable so how can we fix this so basically here we can just press Ctrl X and we want to insert here a remember so we can just call and actually use this remember with a calculation inside here and basically we are going to open up a Lambda function and here we can easily pass in a mutable state so right now here we are getting an error because we cannot reinitialize this so in order to re-initialize this quantity we have to you to get that value so when you call Dot value here you're going to get actually that integer because we want to mutate a mutable State and mutable state does not know how to be mutated so for this case we can just call quantity dot value and here basically we can change back to Val because we are not going to be changing or initializing this to a new mutable State otherwise we are just changing this value which is wrapped inside the immutable State and that's why here we are using a quantity dot value and then re-initializing this one so right now let's try to run our application and see the changes we have made okay so as you can see here we're just getting a weird the output so because here we have used this quantity directly here and basically here we want to use the quantity.value so for this case let's cut call here.value in order to access that value which is just going to be an integer so now let's rerun again okay so here is just our output as you can see we have those avocados and right now the increment is just in zero so when we place this button here as you can see now we can just increment this because we are just changing the state again this is just an event which we are going to increment this value by changing the state and when we change the state that the state is going to change the UI and that's why we are handling here one and whenever we click this it's going to add this until to a point where we are not going to be clicking so this is how State and events actually works in Japan compose so compose is just a declarative UI framework so this is not surprised as you understand right now so instead of removing UI components or changing their visibility when State changes we describe how the UI is under specific conditions of that state so for example if that is state for example is just in a collapsed State then there are certain types of view which are not going to be a part of composable and we are going to see this when we write more code inside our applications so first we enter the composition again here we recompose zero or more times if there are changes made and whenever we quit this compostable for example navigating to another screen then we are going to leave the composition in this case we have an efficient way of just re-executing and changing the state so this actually help us in several things the first one is just avoid manual State update and also it is less error prone because it's just a manual State update it's not a manual State update the key idea here is if the UI is what the user sees then the UI state is what their app says they should see the state is going to determine what is shown in the UI so I'm stressing this in order you have to understand the state because deeper compose everything is actually a state okay so now we want to create another composable so this composable is going to get the username and again we can easily submit and show the order to the user so let's create here another so here we want to display items in a column format for the first time so in order to display that we can use a column so this is just a layout that is going to stack elements in a vertically sequenced format so now let's create here a column and this is just a column composable here okay so inside here we want to create a text and right now it's not just a normal text but we want to create a text field because we want to get the user input so you can just call this text field and this is going to give us two values the values and their own value changes so basically right now here we have to pass in a state because whenever we pass nothing here we are not going to actually change the state so in order to change this we have to create here instead so for example here we can create a variable and we want to call this name and we can easily initialize these two by using a remember so you know that this remember is going to help us to store the value in the composition and after we create here we have to pass in a mutable state of in order to tell hey you have to observe this value here and basically we are going to pass in an empty screen so that is going to be an initial State for the first time so we don't want to display anything here so basically here in a place of a value we have to pass in a name and here we have another Lambda function here this is just the on value change so when you press Ctrl q and right now because we're having an error we cannot see the documentation so first we can add here to Kali process and these are just a Lambda function so when you press also here you have to change this and here we are getting another error because we have used directly this name here and we can use name.value in order to get actually that value which is going to be of type of string which is evident when you look here and this is just the name dot value and here we have the on value change and when you press Ctrl Q you can see that this value parameter here is actually a string that returns a unit so this is just a Lambda function here which we can deal with it so how can we change this state here so let's put first this in a separate line and right now here we have a Lambda function and when you open up it you can see we have a reference which is just it and that is going to be a string so whenever a user is going to be typing here we're going to get that data so when you press here Ctrl Q you can see the Callback that is triggered when the input service updates the text so this is going to be triggered when the user is typing inside the keyboard so for this case we have that text and we can just call here the name dot value and then we can easily change it to another thing which we can pass in here to be it and that is how you can easily modify a state inside a text field now let's try to re-execute here our file so we cannot see these changes so let's create here another preview composable and basically call here our user data and here we have to add the add preview annotation so let's create here preview and basically here we want to show the steam UI and you can make this to be true again now we can just come here inside the split view and we can easily see the changes which we have made and you can click here build and refresh in order to refresh this finish to create here a preview so let's just preview here we have another preview so we have the order and here we have that preview function which is just going to be reviewing the the user data so basically now let's just navigate here and we can easily Zoom okay so as you can see this text field here is just empty and we have to tell the user what this is going to be so you want to tell them hey type your name here so right now let's just come here inside our text field and below this on value change here we can pass another parameter inside this text field which is just called the label so let's pass in here a label and basically this label is also a Lambda function so we can just deal with this label here okay so inside here let's create a text and now let's re-render again and see the outputs okay so as you can see here we have inserted the name so whenever user is going to click here then they know that they have to pass in their name okay so another thing which we want to add here is just a button which is going to help us to submit the order which we have taken so for our case here now let's create a let's navigate here inside our code So Below this text field here we want to create another button and this is just a button inside the composable function and it has a click parameter which we can easily use here to click it and again here inside this button we have something which is called the roscope so basically if we want to provide the icon or the text of this button here so for example for our case we want to pass in a text so we can just call this text and basically inside here we want to specify what this button is going to be named so for example we want to call this submit okay so we have here there on click method which is going to help us to submit so right now I'm not going to do anything but later I'm going to show you how you can change this so we want to add here a space between this text field and this button so we can just call here our spacer and we specify a size of 16 DP to separate between these two text build and the pattern so now let's review here the changes we have made so you can just click here in order to re-change this okay so as you can see here right now we have a name and also we have a submit button which you can easily click in order to submit so one thing which we want to add here is another state that is going to be able to display that name of this user so now let's jump inside our code so inside here our name we are going to create another state and this is State here okay so before we create this state as you can see here we have been using the name dot the name equals to this and here we have to use the dot value in order to access that variable so basically here we can just get easier by using the kotlin extension function that is just called a buy and this buy is going to give us access to this value directly which is wrapped inside the mutable state of so you will have to import the get and set values so first we can just import this yet and also we have to import the get value and the set values and for that case we have everything which we need so instead of using this name.value right now we are just getting error so we can just remove this name.value and also here name dot value we can just remove this and basically we are going to get the same changes but right now with more robust code and easily we can just read it so this is just a simple way so let's create here another and we can use here this delegate method by and basically here we can use now a remember and pass in here a mutable state of and right now we are going to pass in a mutable state of force in order to access this variable so we want to create here another text text that is going to be showing the name of this user here we can write an if statement so in order to hide the visibility we are just going to remove this inside the composition by using a control structure and basically we're just using here Portland functions or kotlin features inside this composable function so we can just write here an ifo statement and this if statement is going to help us to remove this inside the compose tree so basically here right now let's write for example and basically now let's create here a text so we can use this name which is just a state here so as you can see we have reused that state here easily by just calling this name here and displaying this name here and for that case we can easily show the name another thing which we want here is that when a user is going to click this button here in order to show the name so we have to just change that state into a state that so that we can easily re-enter this inside the composition so we can make this text field to intercomposition so how can we do this so we have to trigger an event inside here there on click so inside the on click method we can just call this state here show data and change it back to true so basically here we can just create a show data and re-initialize this to true and for this when we click this button then we are going to show it to true so also let's create another button and that button is going to help us to restore that state so we have to create these two buttons here and we can wrap them inside the row so for example let's press here Ctrl X in order to cut this button here so we are going to refactor this code so let's create here Arrow okay so this row here we can just put these two button and we can paste this and change the data here so instead of submit we can just call this reset and basically here instead of show data then we can just make this to be false and we want to add a space between these two buttons and pass in a modifier and make it for example to be 16 DP so right now we can easily we want to call this user data inside here our main activity so that we can just execute our file so inside here the main activity we want to display these two things inside the column so basically we want to display this order quantity or the quantity here inside the column so we can just press Ctrl X here and basically create a column and inside here we can just pass in order and we can create here a little spacer and basically provide a size modifier dot size and pass in here a size of 16 DP for example and again we have to call right now here we want to call our user data okay so right now we have called this inside our main activity we can easily re-execute and see the changes inside our application so now let's run again we increment this value also here we have this place where we can pass in the name so basically here now we can just click this and as you can see we have here a place where we can just type inside here so for example let me write here the names and we want to click submit here so as you can see here we have nothing here inside the composition but when we click submit here we are going to see this side and when we click this reset this is going to disappear and again when we click submit we are going to recreate this okay so one thing which I want to show you here is when this is going to have nothing so when we click here submit we're just showing nothing but we are making that text to intercomposition so we don't want to allow a user when they are not typing anything if there is nothing inside here the name then we have to disable these buttons here so how can we do this you can just control click inside this user data here so inside this user data here we want to disable the buttons so we have here our first button and basically inside here we can just create another another function here or another parameter which is just called the enabled so let's pass in here enabled and basically we want to put this in a separate line so that we can view them easily so we want to check if the name is going to be a blank so we can just call this name which is just particularly a string and call it dot blank and check if is not blank so basically here this if it's not blank basically it's going to return true and we have to enable the button and if this is not if this is going to be blank then it's going to return first and we can disable this button here so we can easily highlight this and Ctrl C we copy this and again here we can easily pass this inside here so we press Ctrl V in this reset button here and we want to put this in a separate line so basically here we have also changed inside this reset button by passing this enable and if it's not blank again we can just perform the same logic here now let's try to re-execute again and see the output this so as you can see here we have this submit and reset button when this is just going to be empty then they are just disabled so as you can see we cannot just click them but whenever we type here for example let's pass in the name and they are changing to this enabled State here and this is just directly the internal state of this button here and now we can just click submit and now let's assume here we delete this again they're just going to be in a disabled State and also let's for example type here an empty space and as you can see that just is still disabled until we type inside here a certain character then again we can just easily submit this and also we can reset and this is just how you can easily modify and create a state inside jpeg compose okay so I have shown you different ways of writing a state here so for example you have this and when you have a state that requires us for example here to perform this change and the on value change here because here we have for example when you press Ctrl Q you can see we have a Lambda function that requires us to perform by passing here a Lambda function which we can easily change it so there is another way which you can easily declare a state and that is actually more readable and and we can easily see here so for example let's look here we have been declaring this name and this name is used inside here text field so I'm going to show you another way so instead of passing here a name we want to pass in here the name and the on name change so you can just add here to Cali to parentheses and then we pass in here the name and we can pass it here on name change for example and here instead of using this delegate method by so we can just pass in here an equal sign and basically here right now we have the value and also we have the name and now instead here of changing this name directly here basically we can just remove this and when you navigate here you can see we have this unnamed change and when you press Ctrl Q you can see this on name change is actually a Lambda function which is just of type string that returns a unit and this is just a similar to what we want here inside our own value change which is just a string and that returns a unit so in this place we can entirely pass just the on name change and this will work as how it worked in the previous and instead here of using value we can just change this to Val and not reassign this name here and this is just going to make us our code more readable and also more robust okay so right now here we have this simple UI so we want when we type in here a name and we click submit here we're just getting a name so we want to get that the values of these avocados here a user has ordered then we want to display it inside this name here so we say for example Saeed has all that for example for avocados so how can we pass this value from this another composable to this composable here so when you look here inside our composables we have two types of composable so the first one is just the order composable and also we have the user composable so when you look at this all of them they contain State and this is just an internal state so when you look this it contains its own State and also here this user data contains its own State and basically when a composable is containing state that is using this we call it a state for composable and basically this is just called an internal state so an internal state is just not really good because these are making the composable not reusable and making them more complex to test them so for this case here we want to perform a thing that we can move this state to a place where we want to make this state stateless so we are going to speak about State hosting and what it means what is State hoisting so a composable that uses remember to store an object contains a Nintendo state so making the composable stateful as we have seen inside our code so this is you useful in situation where a caller does not need to control the state so composables with internal State tend to be less reusable and harder to test as we have stated earlier so State hosting in compose is a pattern of moving state to a composable cola to make a composable stateless and a composable that does not hold any state are called the state less composables and these are just good in order to make our code more reusable and readable okay so when we are going to be performing State hosting we have to consider something which is called unidirectional data flow and this we are going to be reusing it so much inside our code so you have to understand this so it's just the pattern where the state goes down and the events goes up so as you can see that is called the state hosting is how we implement this architecture in compose so when we move the state for example from the order and then we pass it to the main activity so thus we are just moving the state up and then we have to pass we have to pass in the events which are going to go down and then we react them so for example here we have the state which state is going to be to a call a composable which is for our cases just the main activity and then here we have the events which are just inside the the composables which have to rise up to the caller and for this case we have these changes so the state flows down and the events goes up so this is going to be clear when we write our code so let's say the importance of a state hosting so this is just a single source of Truth so by moving the state instead of the duplicating it we're ensuring there is only one third of Truth so this is going to help us avoid bugs so for example if we want to use this state again in another place we can actually get the same value and we should have only a single source of Truth also it is shareable so high state state can be shared with multiple composables as we have seen also another thing is interceptable so call us to the stateless composer book and decide to ignore or modify the events before changing the state so for this case we can easily intercept the the events and then we can change the state for example also this means we have decoupled our state from the UI so the state for a stateless composable function can be stored anywhere and for this case we can store it for example in a view model but we are going to see this in later videos how we can store State inside a View mode okay so now let's start with our order composable function and we want to host the state so the in order to host the state we have to move this state from this composable because now we have this composable is stateable so when we press here Ctrl X basically we are making this composable stateless and we are moving this to the composable color so for our case this is just our main activity so we are going to put it here inside this set content we have the state and we want this state to flow down to the order so how can we do this basically here we have to pass in the order so inside here we can just call the quantity okay so right now instead of passing here quantity.value we can easily just remove this dot value and here we can remove this dot value okay so we are getting here error we cannot change this quantity here because this is just immutable by default so how can we do this then now we have to pass in the event so the event now have to arise from this order function just going up to this color composable which is just the the set content here so we're going to call this inside our set content composable function so how can we do this then we have to create a callback by creating here a Lambda function so let's create here Lambda function and we can just call this on quantity change and basically here now we can just see pass in here what we want to pass so for example this is going to have a parameter which is just going to be of type int and this is just going to return a unit instead of changing this state inside here we're just going to invoke this compo this Lambda function inside this clickable here so we can just delete this and call here there on quantity change and basically we want to invoke it and if you don't want to invoke here you can just pass in these two parentheses and this is just going to be similarly to just calling dot invoke and basically right now here we can just pass in an integer which we want to increment so instead of passing here an integer we can just remove this because we are going to be incrementing this to the composable there and we don't have anything here so we can just remove this in here and make it nothing here so right now we are just executing this composable so we have no parameter and also return no parameter but we want to execute this inside here because we have this clickable modifier inside our icon so right now here we are going to get some errors when we go to our order so as you can see we have to pass in the quantity so the first thing we can just pass in here our quantity and the quantity here which is just defined inside our state okay so instead of using dot value we can just pass in here by and we can modify this to actually receive just an integer so right now we have pasta in here our quantity okay and because here this Lambda function is just the only parameter at the last so we can just use the trailing Lambda syntax here by opening two curly brushes instead of just passing here another parameter so for this case here we are going to now have a scope that is going to be executed whenever we click here so this is just the applicable a callback and basically inside here we want to modify the state or we can just call here our quantity and we want to re-initialize this by one so we can just re return from where we did the first time and instead of passing here Val we can just pass in VAR inside our quantity State variable so this is just how you can State hoist for the first time you can just remove these two Kali brushes here and remain with this same thing okay so we are just getting here another error so inside our preview order we have to also change this because inside here we have to pass in the quantity so for example for our case we can just pass in here zero for quantity and as in this um and empty Lambda function okay also we want to modify this user data we want to host the state to the composables so we can do the same so what we want to do here we can just easily use this n plus Ctrl X in order to and basically we can just rename also this so you can just press Ctrl X so we are going to do this a lot inside your programming life so I think it's just a good skill to understand so right now as you can see we have the show data and the onshore data which is just going to be the only show data change so we have to add here on show data change and now let's refactor here our function because we have to pass in here the name so for example here we can just pass in here the name okay now let's put this first in a separate parameter also we want to get that show data and basically here we have the name and the on name change which is just used inside our text field also we want to change here the button so inside here the button we want to use this show data and change the composable so we can just call here the onshore data to show data change and actually invoke this and pass in here the composable changes so we can just call this to be false so for example we are going to submit here so we have to pass in here true and also inside here this button here the show data and actually we can easily invoke by just creating these two parentheses and here we can just pass in false okay so here we are just getting an error so we have to pass in those parameters basically here we can just pass in an empty a Lambda function okay so when we finish here we can just navigate we are just getting another error which is just going to be here where we have called the user data inside our main activity so now we have to pass in here the state actually so basically we have to pass in here the name and as you can see here we can easily pass in this all of these variables here now let's put this in a separate line and also when you put your cursor here at the top and you press Alt Enter basically you can just easily add for example add names to call arguments so for this case we just add these color arguments inside here and this is going to help us to to read our code easily so this is how actually State hosting actually works so we have our state which are just here up and basically this state now are going to be passed down to these composables so for example here this composable actually is going to get the state here and then we have the event so the event here is just what we are going to execute so this icon here for example is going to be clicked and when this clickable modifier is going to be clicked we want to execute the on quantity change so this is just going to be an event so this callback here is going to be called inside our for example order here where we have this on quantity change and basically we have re-changed this value by using this event so when this event is going to occur and this event is actually when a user is going to click a button then we want to modify the state and for this case we have changed the quantity and implemented it by one so this is particularly what is called unidirectional data flow and state hosting in Japan compose and this is really powerful because we're going to see how we can create complex animation and also reuse the data so right now as you can see let's look here at the user data so we want to be able to display here the name and also display the order message so basically here now we can just add another parameter here so let's come here in the top and add here a comma and we want to get for example quantity and this is just going to be of type int and right now as you can see we are just reusing the state across different composables so instead of just passing name here inside this text field we can just press Ctrl X for the first time and then open up here these two quotation marks and we can say here you have ordered and basically we can just access the quantity so basically the user have ordered this quantity of avocados and right now we can just change this user data here and when you press Ctrl click this is going to be taking you to where it has been called for the first time we are just going here inside our main activity and as you can see we are just getting here an error and when we pass in the name the argument we can easily displace the calling of these parameters here so instead of java because as you can see inside here we have displayed this in the first parameter so actually here when we pass it to the last by passing in here the named argument so for example here we can pass in quantity and then we are not going to get any error and as you can see here we are just reusing this state across different type of composables and also we are just getting another error and this is just going to be arising here so basically here we can just add this named argument to pass in here the name and also here we can just pass in a quantity and then let's pass it to B10 by default here when we preview our user data so right now here nothing is going to change visually but we have already changed a lot of things and make our code more robust now let's try to rerun our application and see the changes so right now application has launched successfully okay so let's type inside here for example let's pass in the name and we want to increment the avocado we can just press this button here and actually increment it to 5 and right now let's click here submit and as you can see here we have that Saeed have all that five avocados and guess what the exciting Journey doesn't end here a comprehensive udemy course takes you on a deep dive into Japan compose earning over 20 hours of Hands-On content imagine mastering every aspect of Japan compose from the basics to the Advanced Techniques or while enjoying the convenience of online learning ready to become a jpac compose Pro and Lord today to gain the lifetime access to our in-depth course click the link below to subscribe and embark on this transformative Journey let's Empower your Android UI skills together
Info
Channel: HoodLab
Views: 1,037
Rating: undefined out of 5
Keywords:
Id: q56FbKsoo1Q
Channel Id: undefined
Length: 54min 31sec (3271 seconds)
Published: Sun Aug 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.