Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea6614f
commit 82b6db0
Showing
8 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="me.mauricee.lazylayout.rx" /> |
37 changes: 37 additions & 0 deletions
37
lazylayout-rx/src/main/java/me/mauricee/lazylayout/rx/RetryObservable.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
lazylayout-rx/src/main/java/me/mauricee/lazylayout/rx/StateObservable.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
|
||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
lazylayout-rx/src/main/java/me/mauricee/lazylayout/rx/internal/MainThread.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<resources> | ||
<string name="app_name">Rx Lib</string> | ||
</resources> |