New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do we use coroutine adapter? I can't find an example for the life of me. #114
Comments
@NaaleinGrohiik For dependencies of Scarlett2: // Networking I'm using Dagger2 for config Scarlet2:
Custom interface:
Api message is a base class for a list of different messages, converted to json
Our API has websocket requests with responses, so I've created BaseRep mechanism:
LoginRepository is just an interface to be used from domain layer:
|
@ilyaklyukin Thank you so much my good sir! Not only did this answer my question it also gave me new ideas for wrapping results. Really appreciate it! |
If you prefer to use Kotlin Flows, here is the adapter implementation:
Then call
|
I actually just started working with flow. Thank you for sharing! |
@Orpheus007 @lambdatamer do you have an example using Flows? |
I think this is much better way, emit the response and then catch the response with viewmodel |
Few important points:
import com.tinder.scarlet.Stream
import com.tinder.scarlet.StreamAdapter
import com.tinder.scarlet.utils.getRawType
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.BroadcastChannel
import java.lang.reflect.Type
class BroadcastChannelStreamAdapter<T> : StreamAdapter<T, BroadcastChannel<T>> {
@ExperimentalCoroutinesApi
override fun adapt(stream: Stream<T>): BroadcastChannel<T> {
val channel = BroadcastChannel<T>(100)
stream.start(object : Stream.Observer<T> {
override fun onComplete() {
channel.cancel()
}
override fun onError(throwable: Throwable) {
channel.close(cause = throwable)
}
override fun onNext(data: T) {
if (!channel.isClosedForSend) {
channel.offer(data)
}
}
})
return channel
}
object Factory : StreamAdapter.Factory {
override fun create(type: Type): StreamAdapter<Any, Any> {
return when (type.getRawType()) {
BroadcastChannel::class.java -> BroadcastChannelStreamAdapter()
else -> throw IllegalArgumentException()
}
}
}
}
|
|
Can someone please give me an example on how to use kotlin coroutines with scarlet?
Like how to set it up and how to "Observe" the data etc..
Thank you
The text was updated successfully, but these errors were encountered: