Skip to content

Instantly share code, notes, and snippets.

@gilgoldzweig
Created January 28, 2019 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilgoldzweig/559c41925399509c47bc325c1ec09470 to your computer and use it in GitHub Desktop.
Save gilgoldzweig/559c41925399509c47bc325c1ec09470 to your computer and use it in GitHub Desktop.
interface ExampleContract : BaseContract {
interface View : BaseContract.View {
/**
* example of successful response
*/
fun onProfileNameReceived(name: String)
/**
* example of a failure response
*/
fun onProfileNameRequestFailed(exception: Exception)
}
interface Presenter : BaseContract.Presenter<View> {
/**
* example of a network/database/logic request
* non blocking request that will run on another thread
* and return the result using
*
* [View.onProfileNameReceived] for successful result
* [View.onProfileNameRequestFailed] for failure result
*/
fun fetchProfileName()
}
}
open class ExamplePresenter : BasePresenter<ExampleContract.View>(),
ExampleContract.Presenter {
override fun fetchProfileName() {
launch {
try {
val name = fetchProfileNameFromDatabase()
//request completed successfully
withContext(uiContext) {
view.onProfileNameReceived(name)
}
} catch (io: IOException) {
withContext(uiContext) {
view.onProfileNameRequestFailed(io)
}
}
}
}
@Throws(IOException::class)
open suspend fun fetchProfileNameFromDatabase(): String {
//Perform long operation that might work and might not
return withContext(databaseContext) {
"Gil Goldzweig"
}
}
}
class ExampleView : ExampleContract.View {
override fun onProfileNameReceived(name: String) {
//Some action
}
override fun onProfileNameRequestFailed(exception: Exception) {
//Some action
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment