Check out a free preview of the full Introduction to Kotlin and Android Development course:
The "Managing Data" Lesson is part of the full, Introduction to Kotlin and Android Development course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano walks through creating a DataManager class that will be responsible for managing the application's data. Adding and removing products from the cart will be managed by the created class.

Get Unlimited Access Now

Transcript from the "Managing Data" Lesson

[00:00:00]
>> So now that we have the menu and we have the dependencies, it's time to request the data using these libraries, okay? I'm not going to get too deep into how every of those libraries work, okay? But we need to do some things. So first, what we need to do is to create first the container for our data.

[00:00:27] We need some kind of object that we're going to call the DataManager, okay? So because we have data model, but this is just playing a structure for the data. We need someone that will hold the data in a list, so then we can render it, okay? So that is going to be our DataManager.

[00:00:52] So I'm going to right-click, create a new Kotlin file. In this case, I'm going to create a class. So let's go out with our friend class, okay? So it's a DataManager. And what we need is at least two things. The menu that is going to be a list of categories.

[00:01:13] And we need the cart that is going to be a list of item in cart, okay? That's what we need at this point. But instead, I can just create an empty, remember how to create a list? List of, and actually to create an empty one, we can do this, list of.

[00:01:37] But I wanna show you something. I want us to think about something. Every time the menu changes or every time the cart changes, we may need to update the UI, right? So so far, we know only one way to update the UI, and that's immutable state. Does it make sense?

[00:02:00] So that's why we can actually convert this into a mutable state with this index. We say by instead of equals mutableStateOf, and then the empty list. We probably need to import mutable state. So using this, you're actually saying mutableStateOf listOf, I think it's fine. Okay, so the DataManager has the menu on the cart as mutable state of empty list.

[00:02:40] We start with an empty list. And right now, what I wanna add is some couple of functions to actually work with the cart such as remove item. So I'm gonna remove a product. I wanna add something to the cart, cartAdd that also receives a product. And maybe a clear because I wanna emphasize on something.

[00:03:01] This will be actually pretty simple I know for add. We are going to add one by one. So first, we need to check if the product is already in the cart, because if it's already in the cart, we need to increase the quantity, okay, makes sense? And we shall remove it completely from the cart.

[00:03:18] But I have just created list, not mutable list, okay? Does it make sense? So maybe you're saying, just go and change it with a mutable list. But that's not gonna work, because with the state, everything should be immutable. What it means, if I change this to mutable, actually the chatbot compose framework will never detect, will never realize when you add something to the list or remove something to the list.

[00:03:47] So what we need to do is every time we change the list, we need to create a new list with one more item or one less item. So for example, clear or cartClear, call this cartClear, is actually the simplest one, should actually take the cart and create a new list.

[00:04:07] So I'm not taking the current list and clearing the contents. This is not gonna work, okay? I need to create a new one, a new reference. And when we do that, then the state mechanism, the state engine will detect a change and will trigger an update if it's needed in part of the composition.

[00:04:29] Does it make sense? So then cartAdd will just, for example, something that I can do is first chain, check if the current product is already there or not. So it's a simple algorithm. I can also use functional programming if you want like the product that I'm receiving. Let's do this.

[00:04:49] I can take the cart and use forEach functional programming. So I will say if the id==it, what is it here? The argument of the forEach, that is, the item in the cart. I can get a little help here saying that it's the item in the cart, okay, makes sense?

[00:05:12] So if it's this one, then I'm going to increase from it the quantity. By the way, I think that we have in our data model another var here and here. We need to have var and var, okay, makes sense? So going back here, now I can increase the quantity and say that it was found.

[00:05:39] And if it wasn't found, I need to create, I need to add one item, okay? But I cannot add it because it's a list, not immutable list. So what I need to do is create a copy. We haven't seen how to create a copy yet in Kotlin. So this is the first time we are doing that.

[00:06:05] So I'm going to say there is a new list of and I'm going to use a *cart.toTypeArray, like so, okay? And actually, what I need to do here is I need to add also a list of that. So a copy of the current cart plus another item in the cart that has the product that you asked me to add and one as quantity, okay?

[00:06:40] So this is how you clone a list. It creates a copy. So this is a new list, a mutable list of the current list plus one more item. Does it make sense?
>> Just the syntax is kinda weird, like [INAUDIBLE]
>> Yeah, it's weird because we haven't used a star.

[00:06:56] We don't use the star here, but yeah, that's how you make a copy. And cartRemove, it's something similar, but the problem is that here, we need to create a new, an auxiliary list to remove it from there, okay? So we need to convert this into a mutable list.

[00:07:16] We can convert a list into a mutable one. And then we are going to remove, there are many ways to do this, but we can remove all the products whose id is equal to the product id you wanna remove. So now I have created another list that is mutable.

[00:07:36] And now what I need to do is on our cart, I need to take a copy of this one, like so. Does it make sense? By the way, if you are used to state in React or even the Redux mechanism, the Flux design pattern, you typically need to do this as well.

[00:07:59] I mean, you cannot add items to the array. You need to create a new array every time. So something similar is here, okay, makes sense? So that's our DataManager for now.