Created
November 10, 2017 21:18
-
-
Save ykro/d54f0522e98e4caa5297e2f5d2aced25 to your computer and use it in GitHub Desktop.
Android Clean
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() | |
} | |
interface Interactor { | |
suspend fun provideGreetingData(): GreetingData | |
} | |
interface Repository { | |
suspend fun getPerson(): Person | |
} | |
class Person(var firstName: String, var lastName: String) //entity | |
class GreetingData(var greeting: String, var subject: String) //transport structure | |
class GreetingPresenter(var view: View?, var interactor: Interactor) : Presenter { | |
override fun onDestroy() { | |
view = null | |
} | |
override fun getGreeting() { | |
view?.let{ v -> | |
async(UI) { | |
val greetingData: GreetingData = asyncGetGreetingData().await() | |
val greeting = "${greetingData.greeting} ${greetingData.subject}" | |
v.setGreeting(greeting) | |
} | |
} | |
} | |
private fun asyncGetGreetingData(): Deferred<GreetingData> = async(CommonPool) { interactor.provideGreetingData() } | |
} | |
class GreetingInteractor(val repository: GreetingRepository) : Interactor { | |
override suspend fun provideGreetingData() : GreetingData { | |
var person: Person = asyncGetPerson().await() | |
var subject = "${person.firstName} ${person.lastName}" | |
return GreetingData("Hello", subject) | |
} | |
private fun asyncGetPerson(): Deferred<Person> = async(CommonPool) { repository.getPerson() } | |
} | |
class GreetingRepository : Repository { | |
override suspend fun getPerson(): Person { | |
return Person("Adrian", "Catalan") | |
} | |
} | |
class GreetingComponents(view: View) { | |
var repository: GreetingRepository = GreetingRepository() | |
var interactor: GreetingInteractor = GreetingInteractor(repository) | |
var presenter: GreetingPresenter = GreetingPresenter(view, interactor) | |
} | |
class GreetingActivity : AppCompatActivity(), View { | |
var components: GreetingComponents = GreetingComponents(this) | |
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