Created
January 28, 2019 14:46
-
-
Save gilgoldzweig/559c41925399509c47bc325c1ec09470 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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() | |
} | |
} |
This file contains hidden or 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
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" | |
} | |
} | |
} |
This file contains hidden or 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
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