Created
November 10, 2017 21:17
-
-
Save ykro/0c5bea0ce3e9d6df4fed29054a706f9a to your computer and use it in GitHub Desktop.
Android MVP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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