Implementing data repositories

You are already familiar with repositories, so there is no need for an introduction to them. The repositories we are about to create are similar to those created for the Messenger API in Chapter 4Designing and Implementing the Messenger Backend with Spring Boot 2.0. The only difference is that the data source for the repositories we are about to implement is a remote server, not a database residing on a host.

Create a repository package within the remote package. First and foremost, we are going to implement a user repository to retrieve data pertaining to application users. Add a UserRepository interface to the repository, as follows:

package com.example.messenger.data.remote.repository

import com.example.messenger.data.vo.UserListVO
import com.example.messenger.data.vo.UserVO
import io.reactivex.Observable

interface UserRepository {

fun findById(id: Long): Observable<UserVO>
fun all(): Observable<UserListVO>
fun echoDetails(): Observable<UserVO>
}

As this is an interface, we need to create a class that implements the functions specified within UserRepository. We will name this class UserRepositoryImpl. Create a new UserRepositoryImpl within the repositories package, as follows:

package com.example.messenger.data.remote.repository

import android.content.Context
import com.example.messenger.service.MessengerApiService
import com.example.messenger.data.local.AppPreferences
import com.example.messenger.data.vo.UserListVO
import com.example.messenger.data.vo.UserVO
import io.reactivex.Observable

class UserRepositoryImpl(ctx: Context) : UserRepository {

private val preferences: AppPreferences = AppPreferences.create(ctx)
private val service: MessengerApiService = MessengerApiService.getInstance()

override fun findById(id: Long): Observable<UserVO> {
return service.showUser(id, preferences.accessToken as String)
}

override fun all(): Observable<UserListVO> {
return service.listUsers(preferences.accessToken as String)
}

override fun echoDetails(): Observable<UserVO> {
return service.echoDetails(preferences.accessToken as String)
}
}

The preceding UserRepositoryImpl class has two instance variables: preferences and service. The preferences variable is an instance of the AppPreferences class we created earlier and service is an instance of MessengerApiService retrieved by the getInstance() function defined in the Factory companion object in the MessengerApiService interface.

UserRepositoryImpl provides implementations of the findById(), all(), and echoDetails() functions defined in UserRepository. The three functions implemented make use of service to retrieve the required data residing on the server via HTTP-appropriate requests. findById() calls the showUser() function in service to send a request to the Messenger API to retrieve the details of the user with the specified user ID. The showUser() function requires the authorization token of the currently logged-in user as its second argument. We provide this required token via the AppPreferences instance by passing preferences.accessToken as the second argument to the function.

The all() function makes use of MessengerApiService#listUsers() to retrieve all the users that are registered on the messenger service. The echoDetails() function makes use of the MessengerApiService#echoDetails() function to get the details of the currently logged-in user.

Let's create a conversation repository to facilitate the access of data pertaining to conversations. Add a ConversationRepository interface to com.example.messenger.data.remote.repository with the following content:

package com.example.messenger.data.remote.repository

import com.example.messenger.data.vo.ConversationListVO
import com.example.messenger.data.vo.ConversationVO
import io.reactivex.Observable

interface ConversationRepository {
fun findConversationById(id: Long): Observable<ConversationVO>

fun all(): Observable<ConversationListVO>
}

Now create a corresponding ConversationRepositoryImpl class in the package, as follows:

package com.example.messenger.data.remote.repository

import android.content.Context
import com.example.messenger.service.MessengerApiService
import com.example.messenger.data.local.AppPreferences
import com.example.messenger.data.vo.ConversationListVO
import com.example.messenger.data.vo.ConversationVO
import io.reactivex.Observable

class ConversationRepositoryImpl(ctx: Context) : ConversationRepository {

private val preferences: AppPreferences = AppPreferences.create(ctx)
private val service: MessengerApiService = MessengerApiService
.getInstance()

It retrieves information pertaining to a conversation with the requested conversation ID from the Messenger API:

  override fun findConversationById(id: Long): Observable<ConversationVO> {
return service.showConversation(id, preferences.accessToken as String)
}

It retrieves all active conversations of current user from API when invoked:

  override fun all(): Observable<ConversationListVO> {
return service.listConversations(preferences.accessToken as String)
}
}

findConversationById(Long) retrieves the conversation thread with the corresponding ID passed to the function. The all() function simply retrieves all of the current user's active conversations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.254.118