👨🏼‍💻Using Auth Service with MVVM Architecture in Fragment

Feyza Ürkut
Huawei Developers
Published in
4 min readNov 16, 2022

Introduction

Hello everyone 🙌 In this article I will tell you how to use the Huawei Auth Service, which needs the onActivityResult() function, in the fragment and how to adapt it to the MVVM architecture.

HMS Auth Service

Auth Service helps you create a fast and secure user authentication system for our applications to authenticate users. And it supports multiple third-party authentication methods. In order to use Auth Service in our application, let’s first complete the preliminary preparations for our project.

Implementations & Permissions

1- Create an app in App Gallery Connect and integrate the App Gallery Connect SDK into your app. For details, you can follow the preparations codelab: Preparations for Integrating HUAWEI HMS Core

2- Enable Auth Service and Account Kit in AppGallery Connect.

3- Integrate the Auth Service SDK into your app

app-level gradle file

4- Add the necessary permissions to the AndroidManifest.xml file.

Now we can start using Auth Service in our application🎊

Getting a Result from an Activity

The startActivityForResult() method was used to start an activity to get results, and the onActivityResult() method was used to get results from the started activity and perform operations. But now these functions have been DEPRECATED.

While the basic startActivityForResult() and onActivityResult() APIs can be used in the Activity class at all API levels, it is now recommended to use the Activity Result APIs introduced in AndroidX Activity and Fragment.

So, what is this new method, let’s see together.

Activity Result Contracts

The process of using this new Activity Results API looks like this.

Photo by Wajahat Karim

First, you need to define your contract as custom or use the existing one. A contract is the implementation of the Activity Result Contracts interface. Since we are going through Huawei Auth Service, we can use ActivityResultContracts.StartActivityForResult.

After deciding on our contract, the next step is to register it in the Activity Result API using the registerForActivityResult() method.

Finally, we run the contract we created with the launch() call to generate the result.

With Activity Results Contracts, which we will now use instead of the onActivityResult() method, we have made our codes cleaner, organized and reusable 🎉

Now let’s look at how we can adapt Huawei Auth Service to MVVM architecture with clean code writing methods.

Dependency Injection

Here, we separate the objects that will create dependencies by creating a Module class and minimize the dependency within the system by exporting them from the outside.

Repository

A Repository Pattern is a design pattern that isolates the data layer from the rest of the application. The main purpose of this model is to attract data processing and queries to a centralized structure by avoiding repetitions. At this stage, we define the Sign In process in the repository class.

ViewModel

In the ViewModel class, we must first create an observable object. Then, by calling our Sign in function in the repository and assigning the result to the object we created, we can observe this object on the UI side and make the corresponding changes according to the incoming situation. With this, the View only notifies the ViewModel of its requests and then listens to the ViewModel.

Result

Only we knew the type of data that would be returned to us in the onActivityResult() method, and until now we managed all situations ourselves in a single function. In this case, there was a possibility of encountering a different value, and for this, it was necessary to be sure of the type of incoming value.

With the new method, Activity Result Contracts, we keep the control of each process separate by clearly defining the input parameter for the desired results.

In this way, we both increase the readability of the code and avoid problems caused by different types of returns, and possible NPE (NullPointerException) errors.

And with MVVM, one of our preferred architectures, we have seen how we can adhere to the Separation of Concerns principle when integrating Huawei Auth Service into our projects.

I hope it was a useful article. Pleasant work for everyone 😊

References

--

--