Creating the MainInteractor

We want the user to be able to view other users (contacts) on the Messenger platform and view their active conversations on the main screen. In addition to this, we want a user to be able to log out of the platform directly from the main screen. As a result of these requirements, MainInteractor must be capable of loading contacts, loading conversations, and logging the user out of the platform. The following is the MainInteractor interface. Ensure to put it and all other Main_ files in the com.example.messenger.ui.main package:

package com.example.messenger.ui.main

import com.example.messenger.data.vo.ConversationListVO
import com.example.messenger.data.vo.UserListVO

interface MainInteractor {

interface OnConversationsLoadFinishedListener {
fun onConversationsLoadSuccess(
conversationsListVo: ConversationListVO)

fun onConversationsLoadError()
}

interface OnContactsLoadFinishedListener {
fun onContactsLoadSuccess(userListVO: UserListVO)
fun onContactsLoadError()
}

interface OnLogoutFinishedListener {
fun onLogoutSuccess()
}

fun loadContacts(
listener: MainInteractor.OnContactsLoadFinishedListener)

fun loadConversations(
listener: MainInteractor.OnConversationsLoadFinishedListener)

fun logout(listener: MainInteractor.OnLogoutFinishedListener)
}

We added the OnConversationsLoadFinishedListener, OnContactsLoadFinishedListener, and OnLogoutFinishedListener interfaces to the MainInteractor interface. These are all interfaces that will be implemented by a MainPresenter. These callbacks are necessary for the presenter to perform appropriate actions regardless of the success or failure of a conversation load, contact load, or user logout process.

The MainInteractorImpl class with an implemented loadContacts() method is given below:

package com.example.messenger.ui.main

import android.content.Context
import android.util.Log
import com.example.messenger.data.local.AppPreferences
import com.example.messenger.data.remote.repository.ConversationRepository
import com.example.messenger.data.remote.repository.ConversationRepositoryImpl
import com.example.messenger.data.remote.repository.UserRepository
import com.example.messenger.data.remote.repository.UserRepositoryImpl
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class MainInteractorImpl(val context: Context) : MainInteractor {

private val userRepository: UserRepository =
UserRepositoryImpl(context)
private val conversationRepository: ConversationRepository =
ConversationRepositoryImpl(context)

override fun loadContacts(listener:
MainInteractor.OnContactsLoadFinishedListener) {

Let's load all users registered on the Messenger API platform. These users are contacts that can be communicated with by the currently logged in user:

    userRepository.all()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ res ->

Now, the contacts were loaded successfully. onContactsLoadSuccess() is called with the API response data passed as an argument:

    listener.onContactsLoadSuccess(res) },
{ error ->

If the contact load failed, hence, onContactsLoadError() is called:

      listener.onContactsLoadError()
error.printStackTrace()})
}
}

loadContacts() makes use of UserRepository to load a list of all available users on the messenger platform. If the users were successfully retrieved, the listener's onContactsLoadSuccess() is invoked with the list of the users loaded passed as an argument. Otherwise, onContactsLoadError() is invoked and the error is printed to the standard system output .

We are not done with MainInteractorImpl yet. We must still add functions for loadConversations() and logout(). These two required functions are given in the following code snippet. Add them to MainInteractorImpl.

  override fun loadConversations(
listener: MainInteractor.OnConversationsLoadFinishedListener) {

It retrieves all conversations of the currently logged in user using conversational repository instance:

    conversationRepository.all()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ res -> listener.onConversationsLoadSuccess(res) },
{ error ->
listener.onConversationsLoadError()
error.printStackTrace()})
}

override fun logout(
listener: MainInteractor.OnLogoutFinishedListener) {

When login out clear user data from shared preferences file and invokes listener's onLogoutSuccess() callback:

  val preferences: AppPreferences = AppPreferences.create(context)
preferences.clear()
listener.onLogoutSuccess()
}

loadConversations() works similarly to loadContacts(). The difference being that ConversationRepository is being used to retrieve active conversations that the user currently has instead of a list of contacts. logout() simply clears the preferences file used by the application to remove the currently logged in user's data, after which the onLogoutSuccess() method of the provided OnLogoutFinishedListener invoked. 

That's all for the MainInteractorImpl class. Next on our agenda is the implementation of the MainPresenter.

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

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