Diana
Event handler, designed for retrofit and coroutine
Download
Add jitpack repository in settings.gradle
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
// ...
maven { url "https://jitpack.io" }
}
}
Add dependencies
// core of event state
implementation 'com.github.utsmannn.diana-state-handle:core:1.0.3'
// adapter for retrofit
implementation 'com.github.utsmannn.diana-state-handle:adapter:1.0.3'
// susbcriber interface (optional)
implementation 'com.github.utsmannn.diana-state-handle:subscriber:1.0.3'
Retrofit Adapter
For the use adapter, return parameter must be StateEventResponse
and set adapter to StateEventAdapterFactory.create()
interface WebServices {
@GET(EndPoint.GET_USER)
suspend fun getList(): StateEventResponse<UserResponses>
companion object {
fun build(): WebServices {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(StateEventAdapterFactory.create()) // use this adapter
.client(okHttpClient)
.build()
.create(WebServices::class.java)
}
}
}
Handler
class UseCase(val webServices: WebServices) {
suspend fun getList() {
when (val state = webServices.getList().eventFlow()) {
is StateEvent.Idle -> { RenderIdle() }
is StateEvent.Loading -> { RenderLoading() }
is StateEvent.Failure -> { RenderFailure(state.exception) }
is StateEvent.Success -> { RenderSuccess(state.data) }
}
}
}
Sample
Check sample folder for advanced used