Created
August 22, 2018 15:42
-
-
Save kaeawc/2f64810d387125dd565103f82b7310c8 to your computer and use it in GitHub Desktop.
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
// Just a small snippet of an SDK | |
public final class ChatSDK { | |
private static final Handler sUIThreadHandler | |
public static void connect(SDKConnectCallback handler) { | |
// do some weird obfuscated internal SDK processing stuff here | |
User user = connectAndGetUser(); | |
if (sUIThreadHandler != null) { | |
sUIThreadHandler.post({ | |
handler.onConnected(user, null); | |
}) | |
} | |
} | |
// Imagine a ConnectHandler interface here that has a onConnected method | |
} |
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
open class ConnectHandler(open val emitter: MaybeEmitter<User>) : ChatSDK.ConnectHandler { | |
override fun onConnected(user: User?, error: Exception?) { | |
when { | |
error != null -> onError(error) | |
user == null -> onMissingUser() | |
else -> onSuccess(user) | |
} | |
} | |
fun onSuccess(user: User) { | |
if (!emitter.isDisposed) emitter.onSuccess(user) | |
} | |
fun onError(error: Exception) { | |
if (!emitter.isDisposed) emitter.onError(error) | |
} | |
fun onMissingUser() { | |
if (!emitter.isDisposed) emitter.onError(NullPointerException("Missing user")) | |
} | |
} |
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
class SDKWrapper { | |
fun connect(observer: MaybeObserver<User>) { | |
Maybe<User>.create { ChatSDK.connect(ConnectHandler(it)) } | |
.subscribeOn(Schedulers.io()) | |
.observeOn(Schedulers.io()) | |
.subscribe(observer) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment