Creating the MainPresenter

As always, the first thing we must do is create a presenter interface that defines functions to be implemented by a presenter implementation class. The following is the MainPresenter interface:

package com.example.messenger.ui.main

interface MainPresenter {
fun loadConversations()
fun loadContacts()
fun executeLogout()
}

The loadConversations(), loadContacts()and executeLogout() functions will be invoked by the MainView and must be implemented by the MainPresenterImpl class. Our MainPresenterImpl class with its defined properties, and onConversationsLoadSuccess() and onConversationsLoadError() methods is given as follows:

package com.iyanuadelekan.messenger.ui.main

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

class MainPresenterImpl(val view: MainView) : MainPresenter,
MainInteractor.OnConversationsLoadFinishedListener,
MainInteractor.OnContactsLoadFinishedListener,
MainInteractor.OnLogoutFinishedListener {

private val interactor: MainInteractor = MainInteractorImpl
(view.getContext())

override fun onConversationsLoadSuccess(conversationsListVo:
ConversationListVO) {

Let's check if currently logged in user has active conversations:

    if (!conversationsListVo.conversations.isEmpty()) {
val conversationsFragment = view.getConversationsFragment()
val conversations = conversationsFragment.conversations
val adapter = conversationsFragment.conversationsAdapter

conversations.clear()
adapter.notifyDataSetChanged()

After retrieving conversations from API, we add each conversation to ConversationFragment's conversations list and conversations adapter is notified after every item addition:

      conversationsListVo.conversations.forEach { contact ->
conversations.add(contact)
adapter.notifyItemInserted(conversations.size - 1)
}
} else {
view.showNoConversations()
}
}

override fun onConversationsLoadError() {
view.showConversationsLoadError()
}
}

In addition, add the onContactsLoadSuccess(), onContactsLoadError() , onLogoutSuccess(), loadConversations(), loadContacts() and executeLogout() functions given below to MainPresenterImpl:

  override fun onContactsLoadSuccess(userListVO: UserListVO) {
val contactsFragment = view.getContactsFragment()
val contacts = contactsFragment.contacts
val adapter = contactsFragment.contactsAdapter

Let's clear previously loaded contacts in contacts list and notify adapter pf data set change:

contacts.clear()
adapter.notifyDataSetChanged()

Now, let's add each contact retrieved from API to ContactsFragment's contacts list and contacts adapter is notified after every item addition:

    userListVO.users.forEach { contact ->
contacts.add(contact)
contactsFragment.contactsAdapter.notifyItemInserted(contacts.size-1)
}
}

override fun onContactsLoadError() {
view.showContactsLoadError()
}

override fun onLogoutSuccess() {
view.navigateToLogin()
}

override fun loadConversations() {
interactor.loadConversations(this)
}

override fun loadContacts() {
interactor.loadContacts(this)
}

override fun executeLogout() {
interactor.logout(this)
}

We have successfully created our MainInteractor and MainPresenter. At this point, it is time to finish up our work on the MainView and its layouts.

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

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