LiveData deprecated? - Full Guide to StateFlow

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to a new video in this video i'm going to talk about the new state flow which is a replacement for life data that also requires a little bit knowledge about flows itself so if you don't know anything about flows then check out this video here first where i talked about those in general and just to give you an answer to this title no life data has not deprecated you can still use it it still works fine and stateflow is just a new way of doing that based on crew teams and you should just definitely check it out and see if you actually like it more as live data such as i do so i already set up a basic project here with view binding implemented we don't need to do that here in the video and also the activity main layout i will just show you how you can use state flows in combination with such a login screen so how we can actually use it to observe our ui state with a viewmodel and all that stuff i will show you here so i opened my emulator here that is what we're going to do we can log in here for example android and one two three four five six seven eight click on login a progress bar will show up and after two seconds or i think it's five seconds here in this example it says wrong credentials and if we enter the correct credentials click on login it will tell us successfully logged in so i'll show you how you can do this without livedata but with a stateflow so just make sure to visit this video's description and just get this initial project here with these dependencies for the view models function here architectural components and creatines of course flows are entirely based on curtins and of course just this layout here so what the heck is actually a state flow a state flow is something that was implemented in the latest crew teens version here so in this 1.3.9 i think maybe a little bit earlier but one of the latest versions definitely and it's basically live data that is based on curitins so it's not entirely the same as live data so one difference would be for example that a state flow needs to have an initial value live data does not need to have that so the advantage is that if we have an initial value then there is always a value inside of that state flow so it can't be null at any time and also a state flow will remain active when your app is in background um not just like livedata which will become inactive but with a little bit of setup here we can accomplish the same with state flow and also one big advantage here of state flow is that we can use these powerful flow operators that i talked a little bit about my video about flows but that is something that you just cannot do with live data so you can really transform the result before you observe it and well i know there are these live data transformations but with flows you can really do a little bit more so let's get into it and actually create our main view model here in which we will handle the business logic so the actual login process in which we will check if the password matches or not and from there we will emit so-called events to our main activity and here we can observe on those events check according to which event just happened and then just update our ui as we want so new kotlin class main view model select class here that will inherit from viewmodel of course and in here we will now create our state flow so usually when we use live data we had one immutable live data and one immutable live data that we exposed to our activity to our fragment and we will do the same with our state flow so there is immutable state flow we call that private var eval actually underscore log in ui state so this state flow represents the ui state of our login component and that is immutable state flow you can see that takes a value t and that is not nullable we need to pass that with live data we could simply leave that constructor empty but with stateflow we need to pass an initial value so let's choose that and here we actually also need to specify the type of element we actually emit here with the state flow and well which type do we actually want to emit here that will be a custom type so what we will do is we will create a sealed class here for all of the events that can happen here in the login process so class login ui state and if you don't know what a c class is that is similar to enum so we can actually define more class inside of that cl class and only those classes inside of this class are allowed to inherit from this class so we can basically only define ui states here in this block first of all we want to define a state or an event that we want to emit when we successfully log in so that will be an object because that doesn't need any parameters called success and that can now inherit from a login ui state that wouldn't work if we would take this object move it out of here you can see then we get an error because the success class inherits from log in ui state but it's outside of that sealed class so always use a c class for that that's just a good practice and let's actually define the next event we want to emit well when we log in and the credentials don't match then we want to emit an error event here and that will now be a data class error because that will take a val message here so that we are able to also emit a message with that event because there could be multiple different errors and each error has a different error message so we want to be able to pass that here in this class and that also inherits from the login ui state then we will have an object for loading so when our login is actually loading so that we can show the progress bar properly and we also want to define an empty state because we need to emit an initial state here as i said and if we would only have those three events here those are not real events that we want to emit initially so we don't want to initially emit a success date that we successfully logged in we don't want to emit an error because there was no error and we also don't want to emit loading because we're not loading yet we need to click on that button first so that's why i create such an empty state here that we can pass by default and in that empty state we will just do nothing in our main activity so what we can do now we specify that this is a mutable state flow of type log in ui state and the initial state is log in your istate empty and we also get a warning here because that belongs to the experimental crew teams api so we can press alt plus enter and add that annotation to our main view model so that's our immutable state flow here that we can actually change the value from now we will have the immutable state flow that we will expose to our main activity so log in your i state without an underscore that is a normal state flow of log in ui state and we set it to our login ui state with an underscore and then we can create our function to log in the user that takes a username and a password and we set it equal to vmodolscope.launch i will just use a curtin here that is not actually necessary here but i just want to simulate a little delay so that we can see the loading animation because we of course don't do a real network call here we rather just simulate it so the first thing when we click on login we want to set the logins ui state.value so very similar to the livedata to our loading state so we can simply now emit the loading value here and the moment we do this our main activity will be notified that we emitted this loading state and it can simply show the progress bar then here we will do a real network call we delay that routine for two seconds import delay and here we can do our validation if username is equal to let's say android and the password equal to top secret then we want to accept the password and username combination so we set login uistate.value to success and else we want to set the login ui state that value to error not this error to log in ui stated error and here we can now pass an error message so wrong credentials so in a real app you would have multiple if branches here so you could also check if the fields are actually empty then you could emit the error message oh your fields are empty and so on and so forth so for each different error that could occur you emit a different error state here so that's it for this main view model now we want to go back to main activity get a reference to that view model by view models like this and how do we now observe on these events that come from our viewmodel for that we now need to launch a creotin just to collect that flow so stateflow is just a normal flow that we can collect the values from and we want to do that in lifecycle scope of course that launch when started actually we don't want to use launch here if you just use launch here even though that is that that happens inside of lifecycle scope your flow will still collect even if your app is in background so it will still update the ui if your app is in background and that can lead to crashes you always want to use launch when started here so i mean always if you actually want to collect a state flow here so if you don't know this function it will just launch the screw routine when this activity's lifecycle is in the started state so what we want to do here we want to use our view model our login ui state and we want to collect that flow and here you can see we get these log in ui state events we define in our main view model we first want to get rid of this warning pressing alt plus enter and adding this experimental curtains api annotation here and now we can simply use a one expression check if that event we got is a success event for example then we're going to do this if it's an error from log in your i state we do this if it's loading we do this and else so in case it's empty we just don't want to do anything and we just pass unit here so on the success state let's simply show snackbar passbinding.root successfully logged in and length long like this and we want to hide our progress bar again because after logging in of course that login process is finished and when we get an error we also want to show snack bar but this time not with successfully logged in instead with our error message that we got from the viewmodel so we can use our event and now since kotlin has the smartcast feature we know this event here is an error event and then we can just access the message here and actually also hide the progress bar and when we get a loading event we just want to show the progress bar oops is equal to true so that is actually it so this collect function is just the equivalent to a live data's observed function and with the live data we often use this resource helper class so we could simply also have this one expression and check according to which event we actually emitted but i think this is just a little bit cleaner here it uses crew routines which i really like and it's just a cool um alternative to live data here so the last thing here that's missing is we actually need to call the login function from our viewmodel log in and we pass our binding et username text to string and et password text to stream and we actually don't want to do this adjusting on create instead in binding dot button login that's set on click listener when we click on login we want to call the login function from our viewmodel and then our viewmodel will first emit the loading status it will delay the creatine for two seconds and then check if the username and the password actually match if so it emits a success resource so actually success log in your i state and if not it will emit wrong credentials and back in our main activity we here then observe on these events so let's run our app and see if it's working let's try android and top secret one this will show the progress bar and wrong credentials and if we just use top secret as a password it shows the progress bar and successfully logged in so of course this is just um an example here with this login function you can just do this with everything you used live data before just to put your business logic into the view model and then observe on the new ui state in your activity of fragment so if you like this video hit the subscribe button hit the like button and also comment below if you will actually use this new state flow um i'd be really interested in that and yeah if you're looking for more advanced android courses then check out the first link in this video's description which will lead you to my website there you will find premium courses and with the code philip 15 you will actually get 15 off of all my courses so i'll see you in the next video have a nice day bye bye [Music] [Music] you
Info
Channel: Philipp Lackner
Views: 29,022
Rating: undefined out of 5
Keywords: android, tutorial, philip, philipp, filipp, filip, fillip, fillipp, phillipp, phillip, lackener, leckener, leckner, lackner, kotlin, mobile, android studio, stateflow, mutablestateflow, flow, coroutine, coroutines, livedata
Id: Qk2mIpE_riY
Channel Id: undefined
Length: 17min 2sec (1022 seconds)
Published: Wed Nov 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.