MVP in Android

Overview

MVP stands for Model-View-Presenter. MVP overcomes the challenges of MVC. It provides modularity, testability and a more clean codebase. 

Key points of MVP architecture
  1. Communication between View-Presenter and Model-Presenter happens with the help of an interface.
  2. One Presenter manages one View at a time.
  3. Model and View doesn't have knowledge about each other. 

Steps to follow

  • Create an interface to communicate between View-Presenter and Model-Presenter
 interface Contract
{
/* This interface is used for view */
interface View
{
/* Shows the progress bar */
fun showProgressBar()

/* Hides the progress bar */
fun hideProgressBar()

/* Sets the text of the view with the String */
fun setString(string : String?)
}

interface Model
{
interface OnFinished
{
fun finish(string : String?)
}

// This function is used to check if the String is set
fun goNext(onFinished : OnFinished)
}

interface Presenter
{
/* This function is used for the button of view */
fun onNextClick()

/* This function is used to destroy the view */
fun onDestroy()
}
}
  • Create a Model class and implement the interface that you created for Model-Presenter
 class Model : Contract.Model
{
private val arrayList = listOf(
"DSA Self Paced: Master the basics of Data Structures and Algorithms to solve complex problems efficiently. " ,
"Placement 100: This course will guide you for placement with theory, lecture videos, weekly assignments contests and doubt assistance." ,
"Amazon SDE Test Series: Test your skill & give the final touch to your preparation before applying for product based against like Amazon, Microsoft, etc." ,
"Complete Interview Preparation: Cover all the important concepts and topics required for the interviews. Get placement ready before the interviews begin" ,
"Low Level Design for SDE 1 Interview: Learn Object-oriented Analysis and Design to prepare for SDE 1 Interviews in top companies"
)

override fun goNext(onFinished : Contract.Model.OnFinished)
{
Handler(Looper.getMainLooper()).postDelayed({
onFinished.finish(getRandomString)
} , 1200)
}

private val getRandomString : String
private get()
{
val random = Random()
val index = random.nextInt(arrayList.size)
return arrayList[index]
}

}
  • Create a Presenter class and implement the interface of Presenter and an interface of Model
 class Presenter(private var mainView : Contract.View? , private val model : Contract.Model) :
Contract.Presenter , Contract.Model.OnFinished
{
override fun onNextClick()
{
if (mainView != null)
{
mainView !!.showProgressBar()
}
model.goNext(this)
}

override fun onDestroy()
{
mainView = null
}

override fun finish(string : String?)
{
if (mainView != null)
{
mainView?.setString(string)
mainView?.hideProgressBar()
}
}
}
  • Create a View class and implement the interface that you created for View-Presenter
 class MainActivity : AppCompatActivity() , Contract.View
{
private var presenter : Presenter? = null
override fun onCreate(savedInstanceState : Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
presenter = Presenter(this , Model())
btnNext.setOnClickListener {
presenter !!.onNextClick()
}
}

override fun showProgressBar()
{
progressBar !!.visibility = View.VISIBLE
textView !!.visibility = View.VISIBLE
}

override fun hideProgressBar()
{
progressBar !!.visibility = View.GONE
textView !!.visibility = View.VISIBLE
}

override fun setString(string : String?)
{
textView !!.text = string
}

override fun onDestroy()
{
super.onDestroy()
presenter !!.onDestroy()
}
}

Reference

Comments

Popular posts from this blog

SSLSocketFactory in Android

DataBinding in Android

Convert Password Characters into a placeholder character.