The Most Basic Way to Develop an Android Application to Add Friends with Cloud DB

ibrahimertanylmz
Huawei Developers
Published in
6 min readMar 8, 2023

Introduction

Hi everyone đź‘‹

This article is based on add friend feature of Photography App. Photography App is an application that allows you tagging and editing photos, showing the location that photo was taken, adding friends, sharing files and photos with your friends. I will try to explain add friend feature with 3 main titles as getting data from cloud, sending friend request and sending response to a friend request. You can reach out the codelab from here. Let’s get started!

Contents

Integration and Initialization of Cloud DB

Getting Datas From the Cloud and Listing for Adding Friends

Sending Friend Request

Sending Response to Friend Request

Conclusion

References

Integration & Initialization of Cloud DB

Before using the Cloud DB service, you need to enable it. You can follow the steps from Enabling the Service. Then you need to create object types on the AppGallery Connect console and export the object type file in the Java format for Android application development. You can follow the steps on Adding and Exporting Object Types to add UserRelationship object. Add the fields and change pendingFirstSecond, pendingSecondFirst and areFriends types as Boolean. Select firstSecondUID as primary key as the table below and export the JAVA file.

UserRelationship object table

Next, create a CloudDb Zone by following the steps on Adding a Cloud DB Zone. Then, add the exported JAVA file to the local development environment. If the file already exists, replace it with the exported one. To ensure successful integration, do not modify the exported JAVA file.

JAVA file model path

After adding the object type file, you can develop your app using Cloud DB. Before that, you need to initialize AGConnectCloudDB, and create a Cloud DB zone and an object type. You can check the sample code under each of the following steps:

1. Initialize Cloud DB in your application class. For this, create an initialize method in PhotoApplication.kt to initialize AGConnectCloudDB.

2. Create RoutePolicy, Instance, and ObjectTypes for Cloud DB. In this case a CloudDBModule used with singleton principle on our project.

3. Create the Cloud DB zone configuration object, and open the Cloud DB zone.

Also you can see the writing data to Cloud DB from the documentation.

Getting Datas From the Cloud and Listing for Adding Friends

In this section, first the user and userRelationship status must be shown on app’s add friend screen. So at the beginning we should get the all user list from cloudDb and exclude our friends from that list so we can send friend request to the users that not added as friends.

There may be different needs in various scenarios to view data that have been recorded, for example, the list of registered users. The executeQuery method can be used to implement this requirement.

1. Define parameters to save user and userRelation responses in CloudRepository.kt as follows. StateFlow is used to receive live updates from Cloud Db.

2. Inside CloudDbRepository.kt file in create a function and Define Query and Policy parameters and in a successful result case process query result as snapshot.

3. Process query result using cursor and get user response as a list.

4. Same operations should be done for userRelations with getPendingRequests and processUserRelationQueryResult functions inside CloudRepository.kt so we can reach the pending requests with processing the response of userRelations data.

5. getUsers method is collecting userResponse and then collecting userRelationResponse to handle users excluding our friends and to send them possible friend requests.

6. In this state, first remove the current user from the userList. Then we check if the user is added as current user’s friend from userRelationList. If so remove the user from the list because it’s already added as friend. If the areFriends value is true, then the user is current user’s friend. Then update the uiState for the final userList.

  • Here is the final user list example on our application:
Userlist Photography App

7. For the next step pending requests should be shown in the add friend page. Current user could either accept or decline friend requests. The data should be collected from userRelationResponse and we should handle userRelationList to show pending requests.

8. In this step, while handling pendingRequests firstly secondUserId must be current user id so we can find out that friend request has been sent to current user. The first user will be the user that sender of the friend request in this case. Then add user with userId and userName to uiModel. Lastly, update uiState with the currentRequestList.

  • Here is the final result example for pending requests on our application:
Pending Requests Photography App

Sending Friend Request

1. In case of sending a friend request an item should be added to userRelations field on Cloud Db and to do that, we need the id’s and name’s of both users and pendingFirstSecond value from first to second should be true for this condition. Then we can handle the save status.

2. Here save status could be handled as a result and uiState updates are possible in success case and also errorState could be added to uiState as an error message.

  • On our application, when we select user or multiple users from the userlist and click add friend button; a userRelation between that users will be generated and pending value will be true while areFriends value was false until the other user accept or decline our request.
Send Friend Request Photography App

Sending Response to Friend Request

1. Current user should be able to accepting or declining a pending request. In case of accepting or declining a request we need to send a response including areFriends value true or false. If the request is accepted areFriends value will be true, else it will be false.

2. To send a friend request response the field of areFriends value for the correct id’s of userRelations table should be updated. areFriends value has to be true or false due to response of user. Then we can update the userRelation on Cloud and handle userRelation save status.

Conclusion

The article explained add friend feature from the Photography App using Cloud DB. This guide includes “Integration and Initialization of Cloud DB”, “Getting Data from the Cloud and Listing for Adding Friends”, “Sending Friend Requests” and “Sending Response to that Requests by Accepting or Declining”. The article helps you to understand “While accepting or declining a request which part of the created relation table should be updated ?”, “Which values should be kept on that table to add friends ?” and “How to keep and make understandable user’s relations with only one table ?”.

I hope, you found this article helpful and if you have any comments or questions, please let me know in the comments below.

You can also check out other articles for different features of Photography App from below:

--

--

ibrahimertanylmz
Huawei Developers

Android Developer @Huawei 💻, ESOGU Computer Engineering Graduate 🎓, Proactive Self-Starter, Quick Learner, Team Player 👨‍👦‍👦