not fairly Serverless chat app with Jetpack Compose and Huawei Cellular Providers — Half 2 | by Francesco Stranieri | Nov, 2022 will lid the most recent and most present steering roughly the world. go browsing slowly consequently you comprehend skillfully and accurately. will deposit your information precisely and reliably
Good day pals! Now it is time for the chat circulatethe place we’ve got two object sorts associated messages: input_messages Y complete_messages.
- Input_messages it’s the kind of object that we create simply to ship messages within the chat.
- Complete_messages is the fusion between ‘customers’ Y ‘input_messages’ and we use it to show messages within the chat.
So mainly we’re sending input_messages to the server and we’re exhibiting complete_messages within the app
To obtain new knowledge, we’re all the time listening complete_messages The article kind modifications in Cloud DB, so each time there’s a new change, we get the brand new knowledge within the utility.
Similar to we did within the Cloud Perform for the person knowledge, to entry the Cloud DB ObjectTypes from the applying, we simply have to initialize AGConnectCloudDB passing the context and a fundamental configuration to get a AGConnectInstance object.
That object is crucial for making a cloud database occasion.
Then we simply have to go all the article kind data to our cloud database occasion, which is a perform that’s already included within the ObjectTypeInfoHelperObjectTypeInfoHelper class that we downloaded from the developer console.
if (DBZone == null)
val agcConnectOptions = AGConnectOptionsBuilder()
val agConnectInstance = AGConnectInstance.buildInstance(agcConnectOptions)
this.DBInstance = AGConnectCloudDB.getInstance(
Lastly we’ve got to open the Cloud database zone the place we wish to work
After we open the Cloud database zone we have to specify work, whether or not to handle synchronization with ONLY native CACHE both with the CLOUD and what sort of property entry.
Configuring’persistenceEnabled‘ we wish a replica of the Cloud DB knowledge in native. Calling ‘openCloudDBZone2‘ we entry the Cloud DB Zone, so we go the setting we configure earlier than after which whether it is allowed to create the world if it doesn’t exist already.
personal enjoyable openCloudZone()
val mConfig = CloudDBZoneConfig(
persistenceEnabled = true
Now we will lastly ship our first message!
We simply create a input_messages object after which invoke ‘executeUpsert‘ with that object.
We’ll set a void’ID‘, which is the first key, as a result of we wish a randomly generated worth set by the backend.
Concentrate that on this step we’re not displaying the message that we despatched, as a result of the input_messages object is just for sending to the server.
enjoyable sendMessage(textual content: String)
val message = Message().apply
this.id = ""
this.textual content = textual content
this.user_id = userID
this.kind = ObjectTypeInfoHelper.MESSAGE_TYPE_STANDARD
personal enjoyable sendMessageOnCloud(message: Message)
val upsertTask = this.DBZone!!.executeUpsert(message)
upsertTask.addOnSuccessListener cloudDBZoneResult ->
So he input_messages the article has been saved in Cloud DB.
To handle the backend half, we arrange a CLOUD DATABASE TRIGGER on that ObjectType that has been fired because of the eventType ‘onUpsert‘. Will begin the cloud perform ‘create-full-message‘ who will handle the complete_messages ObjectType creation primarily based on the person knowledge of the person sending the message and the message simply saved in Cloud DB, producing a random ID.
As soon as the full_messages object has been saved, the listening utility full message modifications you can be notified and obtain the brand new knowledge.
This time we’ve got the info of the Cloud Database Set off with the ‘onUpsert‘ occasion kind.
Since we have to merge the input_messages knowledge together with customers knowledge associated to the person who writes the message, we are going to use the user_id worth included within the message knowledge to run a question to retrieve the person knowledge.
const cloudDBZoneQuery = clouddb.CloudDBZoneQuery
.equalTo("id", user_id);const resp = await this.cloudDBZoneClient
GO BACK TO THE APP AGAIN
Now that we’ve got saved the complete_messages knowledge and subsequently there’s a new change to that object kind, the applying that was listening to the modifications might be notified.
However configure the listener?
We simply run a questionon this case primarily based on the ‘write‘ worth that’s associated to the kind of messages we wish to present within the chat. Then with the perform ‘subscribeSnapshot‘ we’re telling Cloud DB to maintain listening for modifications primarily based on that question, and on this case we’re specifying to hear solely on the CLOUD aspect, so we’re not contemplating native storage/CACHE.
val question = CloudDBZoneQuery.the place(FullMessage::class.java)
val queryTask = this.DBZone!!.executeQuery(queryTask.addOnSuccessListener snapshot ->
So we’ve got a OnSnapshotListener returning a CloudDBZone snapshot consequently, which is mainly a Cursor containing the up to date knowledge. Since we have to set an inventory within the Compose LazyColumnwe have to parse it, so we use a Actual time knowledge noticed as compose standing to replace the UI on change.
var messages: MutableLiveData<Record<FullMessage>> = MutableLiveData()
personal set...personal val mSnapshotListener = OnSnapshotListener<FullMessage> cloudDBZoneSnapshot, err ->
Log.w(TAG, "onSnapshot: $err.message")
...personal enjoyable processQueryResult(snapshot:CloudDBZoneSnapshot<FullMessage>)
val messagesList = snapshot.toList()
messages.postValue(messagesList.sortedBy it.date_ins )
Bonus tip: to investigate the CloudDBZone snapshot a MutableListwe create a Kotlin prolonged perform referred to as ‘To record()‘ which simply finds the cursor and creates a MutableList.
personal enjoyable <T : CloudDBZoneObject> CloudDBZoneSnapshot<T>.toList() = run
val cursor = this.snapshotObjects
val record = mutableListOf<T>()
catch (e: AGConnectCloudDBException)
benefit from the speak
As mentioned above, MutableLiveData noticed as a Compose State will replace the UI primarily based on the LazyColumn by way of the info replace.
...val messagesValue by cloudDBViewModel.messages.observeAsState()...LazyColumn(…)
Edit / Delete messages (EXTRA)
There are two further options in chat: Edit Y Take away messages
- Edit a message – you simply have to name an upsert of the message utilizing the identical message main key worth, the IDit will replace your knowledge with out creating a brand new prevalence.
- Delete a message: It’s primarily based on the Cloud DB perform ‘executeDelete‘ passing the complete_messages object that we wish to take away from the chat.
In each instances, edit and delete, the info in complete_messages will change and they’re going to notify the OnSnapshotListener we’ve got beforehand created with the brand new knowledge, so it’s strictly tied to the chat circulate we noticed earlier than.
enjoyable editMessage(textual content: String, fullMessage: FullMessage)
val message = Message().apply
this.id = fullMessage.id
this.textual content = textual content
this.user_id = fullMessage.user_id
this.kind = fullMessage.kind
enjoyable deleteMessage(message: FullMessage)
val deleteTask = this.DBZone!!.executeDelete(message)
I hope the article roughly Serverless chat app with Jetpack Compose and Huawei Cellular Providers — Half 2 | by Francesco Stranieri | Nov, 2022 provides notion to you and is helpful for accumulation to your information
Serverless chat app with Jetpack Compose and Huawei Mobile Services — Part 2 | by Francesco Stranieri | Nov, 2022