‫The second step is to move to the repository and write the code that allow us to get the chat groups
‫available from Firebase Realtime Database.
‫So for that, I'll create a mutable live data list of chat group and I'll name it as Mutable Live data.
‫Mutable live data is a class provided by Android Architecture components that is used to hold and observe
‫data changes.
‫It's immutable because you can't change the data it holds over time.
‫And list of chat group specifies the type of data that mutable live data will hold.
‫In this case, it's generic type and it's a list of chat group objects.
‫I'll name it as chat group mutable live data.
‫I'll create Firebase database instance database database reference equals to reference.
‫Then I need to initialize these variables in the constructor.
‫So alt plus insert to create constructor and select none I needed an empty constructor.
‫And here I need to initialize the chat group.
‫Mutable live data equals to mutable live data.
‫Firebase database equals to Firebase database dot get instance and reference equals to database dot
‫get reference move down.
‫These are the codes for the authentication.
‫So I'll mark it as authentication.
‫And here this is for getting the chat groups available from Firebase Realtime Database I'll create a
‫getter for this mutable live data called chat group alt plus insert to create a getter for chat group
‫mutable live data.
‫And inside this getter I need to write the code to fetch all the data from Firebase into RecyclerView
‫that displays all the groups available for chatting.
‫I want from you to focus with.
‫The first step is to create a list.
‫List of chat group.
‫This is the type that it is passed among the mutable live data.
‫Generic type I name it as group groups.
‫List equals to new array list.
‫I need to fetch the data from Firebase and add them to the mutable live data and this groups list.
‫So for that I need to create database reference dot add value event listener, new value event listener.
‫And I'll override two methods on data change and on cancel.
‫A value event listener is used to listen for changes in data at a specific database reference in Firebase.
‫So this is the reference to a specific location.
‫In the Firebase Realtime database.
‫You'll attach a value event listener to this reference to listen for changes in the data located at
‫that path and add value.
‫Event listener.
‫This is a method provided by Firebase Realtime Database that allows you to attach a listener to a database
‫reference.
‫This listener will be triggered when data add that reference changes.
‫The method takes an instance of value event listener as a parameter and we have a new value event listener.
‫This is an anonymous inner class or an anonymous class that implements the value event listener interface.
‫Inside the curly braces, you should override two methods the Onchange on data change and on canceled
‫methods inside anonymous class to define the behavior when data changes, or when an error occurs during
‫the retrieval of data from Firebase real time database in on data change method, you can access and
‫process the data received from the specified database reference and the on canceled method.
‫You can handle any errors that might occur during the data retrieval group list.
‫This is mandatory to prevent any duplication of received data from Firebase.
‫Then I need to start by using the for each loop data snapshot.
‫And we talked about the snapshot and the data snapshots in the previous videos.
‫But don't worry I'll make a quick recap.
‫Snapshots dot get children.
‫I need to get the children.
‫A data snapshot represents a specific location in the Firebase database and can contain data or child
‫data.
‫Get children method is a method of data snapshot class that returns an iterable of all the immediate
‫children of the current snapshot.
‫The for each loop iterates through each child of snapshot inside the loop.
‫Data snapshot represents each child data one by.
‫One.
‫Then I'll create an object from a chat group and name it as group equals to new chat group.
‫And here I'll pass data snapshot dot get key, then add this group to the groups list array list.
‫So data snapshot is the instance of data snapshot class representing a specific location in your Firebase
‫database and dot get key method is a method provided by data snapshot class.
‫When you called on the data snapshot object, it returns the key associated with the data at the location
‫in the database.
‫So I'm getting the keys associated with that data snapshot at this specific position.
‫Think about the keys as nodes in Firebase.
‫Then we need to post the value to the chat group.
‫Immutable live data.
‫So chat group mutable live data dot post value groups list I need to add this group list to the mutable
‫live data.
‫I need to return the chat group mutable live data.