Skip to content

Instantly share code, notes, and snippets.

@ykro
Created November 10, 2017 21:17
Show Gist options
  • Save ykro/0c5bea0ce3e9d6df4fed29054a706f9a to your computer and use it in GitHub Desktop.
Save ykro/0c5bea0ce3e9d6df4fed29054a706f9a to your computer and use it in GitHub Desktop.
Android MVP
interface View {
fun setGreeting(greeting: String)
}
interface Presenter {
fun onDestroy()
fun getGreeting()
}
class Person(var firstName: String, var lastName: String)
class GreetingPresenter(var view: View?, var model: Person) : Presenter {
override fun onDestroy() {
view = null
}
override fun getGreeting() {
view?.let{ v ->
v.setGreeting("Hello ${model.firstName} ${model.lastName}")
}
}
}
class GreetingComponents(view: View) {
var person: Person = Person("Adrian", "Catalan")
var presenter: GreetingPresenter = GreetingPresenter(view, person)
}
class GreetingActivity : AppCompatActivity(), View {
private var components: GreetingComponents = GreetingComponents(this)
private var presenter: Presenter = components.presenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_greeting)
btnShowGreet.setOnClickListener {
presenter.getGreeting()
}
}
override fun onDestroy() {
presenter.onDestroy()
super.onDestroy()
}
override fun setGreeting(greeting: String) {
txtGreet.text = greeting
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment