Skip to content

Commit

Permalink
Added Rx module
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumblydore committed May 8, 2019
1 parent ea6614f commit 82b6db0
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 0 deletions.
File renamed without changes.
33 changes: 33 additions & 0 deletions lazylayout-rx/build.gradle
@@ -0,0 +1,33 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 28


defaultConfig {
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(":lazylayout")
implementation deps.rxJava
implementation deps.rxAndroid
implementation deps.androidCore
implementation deps.kotlinStdlib
}
File renamed without changes.
2 changes: 2 additions & 0 deletions lazylayout-rx/src/main/AndroidManifest.xml
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.mauricee.lazylayout.rx" />
@@ -0,0 +1,37 @@
@file:JvmName("RxLazyLayout")

package me.mauricee.lazylayout.rx

import androidx.annotation.CheckResult
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.MainThreadDisposable
import me.mauricee.lazylayout.rx.internal.checkMainThread
import me.mauricee.lazylayout.widget.LazyLayout

@CheckResult
fun LazyLayout.retries(): Observable<Unit> = RetryObservable(this)

private class RetryObservable(private val lazyLayout: LazyLayout) : Observable<Unit>() {

override fun subscribeActual(observer: Observer<in Unit>) {
if (!checkMainThread(observer)) {
return
}
lazyLayout.retryListener = Listener(lazyLayout, observer).also(observer::onSubscribe)
}

private class Listener(private val lazyLayout: LazyLayout, private val observer: Observer<in Unit>) :
MainThreadDisposable(), LazyLayout.RetryListener {
override fun onRetry() {
if (!isDisposed) {
observer.onNext(Unit)
}
}

override fun onDispose() {
lazyLayout.stateUpdateListener = null
}

}
}
@@ -0,0 +1,37 @@
@file:JvmName("RxLazyLayout")

package me.mauricee.lazylayout.rx

import androidx.annotation.CheckResult
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.MainThreadDisposable
import me.mauricee.lazylayout.rx.internal.checkMainThread
import me.mauricee.lazylayout.widget.LazyLayout

@CheckResult
fun LazyLayout.stateChanges(): Observable<Int> = StateObservable(this)

private class StateObservable(private val lazyLayout: LazyLayout) : Observable<Int>() {

override fun subscribeActual(observer: Observer<in Int>) {
if (!checkMainThread(observer)) {
return
}
lazyLayout.stateUpdateListener = Listener(lazyLayout, observer).also(observer::onSubscribe)
}

private class Listener(private val lazyLayout: LazyLayout, private val observer: Observer<in Int>) :
MainThreadDisposable(), LazyLayout.StateUpdateListener {
override fun onDispose() {
lazyLayout.stateUpdateListener = null
}

override fun onStateUpdated(state: Int) {
if (!isDisposed) {
observer.onNext(state)
}
}

}
}
@@ -0,0 +1,16 @@
package me.mauricee.lazylayout.rx.internal

import android.os.Looper
import androidx.annotation.RestrictTo
import io.reactivex.Observer
import io.reactivex.disposables.Disposables

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
fun checkMainThread(observer: Observer<*>): Boolean {
if (Looper.myLooper() != Looper.getMainLooper()) {
observer.onSubscribe(Disposables.empty())
observer.onError(IllegalStateException("Expected to be called on the main thread but was " + Thread.currentThread().name))
return false
}
return true
}
3 changes: 3 additions & 0 deletions lazylayout-rx/src/main/res/values/strings.xml
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">Rx Lib</string>
</resources>

0 comments on commit 82b6db0

Please sign in to comment.