Skip to content

Instantly share code, notes, and snippets.

View vvsevolodovich's full-sized avatar

Vladimir Ivanov vvsevolodovich

View GitHub Profile
public actual fun launch(
context: CoroutineContext = DefaultDispatcher,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
block: suspend CoroutineScope.() -> Unit
): Job {
val newContext = newCoroutineContext(context, parent)
val coroutine = if (start.isLazy)
LazyStandaloneCoroutine(newContext, block) else
StandaloneCoroutine(newContext, active = true)
abstract fun resume(value: T)
abstract fun resumeWithException(exception: Throwable)
@Override
protected void subscribeActual(final SingleObserver<? super T> s) {
source.subscribe(new ObserveOnSingleObserver<T>(s, scheduler));
}
public static LoadWeatherForecastTask extends AsyncTask<String, Void, WeatherForecast> {
private Activity activity;
LoadWeatherForecastTask(Activity activity) {
this.activity = activity;
}
}
public static void main() {
System.out.println("Hello, world");
}
@vvsevolodovich
vvsevolodovich / search.kt
Created January 21, 2019 13:59
Channels search
launch {
broadcast.consumeEach {
delay(300)
val foundRepositories =
apiClient.searchRepositories(it).await()
repos.adapter = ReposAdapter(
foundRepositories.map { it.full_name },
this@RepositoriesActivity
)
}
@vvsevolodovich
vvsevolodovich / search.kt
Created January 21, 2019 13:50
RxJava Search
publishSubject
.debounce(300, TimeUnit.MILLISECONDS)
.distinctUntilChanged()
.switchMap {
searchQuery -> apiClientRxImpl.searchRepositories(searchQuery)
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
repos.adapter = ReposAdapter(
@vvsevolodovich
vvsevolodovich / RxUgly.kt
Last active September 15, 2018 23:07
Ugly code written with RxJava due to lack of RxJava understanding
fun connectionObserveration(context: Context): Disposable? {
if ((firstObserver == null || firstObserver!!.isDisposed) && (secondObserver == null || secondObserver!!.isDisposed())) {
secondObserver = ReactiveNetwork.observeNetworkConnectivity(context)
.subscribeOn(AndroidSchedulers.mainThread())
.filter(ConnectivityPredicate.hasState(NetworkInfo.State.CONNECTED, NetworkInfo.State.DISCONNECTED))
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ connectivity ->
if (connectivity.state == NetworkInfo.State.CONNECTED) {
firstObserver = ReactiveNetwork.observeInternetConnectivity()
.subscribeOn(AndroidSchedulers.mainThread())
@vvsevolodovich
vvsevolodovich / ObserveOnSingleObserver.java
Created April 17, 2018 19:32
ObserveOnSingleObserver
ObserveOnSingleObserver(SingleObserver<? super T> actual, Scheduler scheduler) {
this.actual = actual;
this.scheduler = scheduler;
}
@Override
public void onSuccess(T value) {
this.value = value;
Disposable d = scheduler.scheduleDirect(this);
DisposableHelper.replace(this, d);
@vvsevolodovich
vvsevolodovich / SingleSubscribeOn.java
Last active April 17, 2018 15:35
SingleSubscribeOn
@Override
protected void subscribeActual(final SingleObserver<? super T> s) {
final SubscribeOnObserver<T> parent = new SubscribeOnObserver<T>(s, source);
s.onSubscribe(parent);
Disposable f = scheduler.scheduleDirect(parent);
parent.task.replace(f);
}