-
Notifications
You must be signed in to change notification settings - Fork 0
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
a15fc72
commit fd0fc9f
Showing
85 changed files
with
383 additions
and
89,676 deletions.
There are no files selected for viewing
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
87 changes: 0 additions & 87 deletions
87
livedata_transformer/src/main/java/com/microtears/orange/livedata/transformer/Observable.kt
This file was deleted.
Oops, something went wrong.
122 changes: 32 additions & 90 deletions
122
livedata_transformer/src/main/java/com/microtears/orange/livedata/transformer/Transform.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 |
---|---|---|
@@ -1,147 +1,89 @@ | ||
package com.microtears.orange.livedata.transformer | ||
|
||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.Observer | ||
import com.microtears.orange.livedata.transformer.observers.* | ||
import java.util.concurrent.TimeUnit | ||
|
||
class Transform { | ||
companion object { | ||
|
||
@JvmStatic | ||
fun <S> from(collection: Collection<S>): LiveData<S> { | ||
val result = MutableLiveData<S>() | ||
collection.forEach(result::setValue) | ||
return result | ||
} | ||
fun <S> from(collection: Collection<S>): LiveData<S> = MutableLiveData<S>().from(collection) | ||
|
||
@JvmStatic | ||
fun <S, T> map(source: LiveData<S>, mapFunction: (S) -> T): LiveData<T> { | ||
return TransformerBase(MapObserver(mapFunction)).transform(source) | ||
} | ||
fun <S, T> map(source: LiveData<S>, mapFunction: (S) -> T): LiveData<T> = source.map(mapFunction) | ||
|
||
@JvmStatic | ||
fun <S, T> mapIndex(source: LiveData<S>, mapIndexFunction: (Int, S) -> T): LiveData<T> { | ||
return TransformerBase(MapIndexObserver(mapIndexFunction)).transform(source) | ||
} | ||
fun <S, T> mapIndex(source: LiveData<S>, mapIndexFunction: (Int, S) -> T): LiveData<T> = source.mapIndex(mapIndexFunction) | ||
|
||
@JvmStatic | ||
fun <S, T> switchMap( | ||
source: LiveData<S>, | ||
switchMapFunction: (S) -> LiveData<T> | ||
): LiveData<T> { | ||
return TransformerBase(SwitchMapObserver(switchMapFunction)).transform(source) | ||
} | ||
fun <S, T> switchMap(source: LiveData<S>, switchMapFunction: (S) -> LiveData<T>): LiveData<T> = source.switchMap(switchMapFunction) | ||
|
||
@JvmStatic | ||
fun <S> distinctUntilChanged(source: LiveData<S>): LiveData<S> { | ||
return TransformerBase<S, S>(DistinctUntilChangedObserver()).transform(source) | ||
} | ||
fun <S> distinctUntilChanged(source: LiveData<S>): LiveData<S> = source.distinctUntilChanged() | ||
|
||
@JvmStatic | ||
fun <S, T> flatMap(source: LiveData<S>, flatMapFunction: (S) -> LiveData<T>): LiveData<T> { | ||
return TransformerBase(FlatMapObserver(flatMapFunction)).transform(source) | ||
} | ||
fun <S, T> flatMap(source: LiveData<S>, flatMapFunction: (S) -> LiveData<T>): LiveData<T> = source.flatMap(flatMapFunction) | ||
|
||
@JvmStatic | ||
fun <S> where(source: LiveData<S>, testFunction: (S) -> Boolean): LiveData<S> { | ||
return TransformerBase(WhereObservable(testFunction)).transform(source) | ||
} | ||
fun <S> where(source: LiveData<S>, testFunction: (S) -> Boolean): LiveData<S> = source.where(testFunction) | ||
|
||
@JvmStatic | ||
fun <S> distinct(source: LiveData<S>): LiveData<S> { | ||
return TransformerBase(DistinctObserver<S, S> { it }).transform(source) | ||
} | ||
fun <S> distinct(source: LiveData<S>): LiveData<S> = source.distinct() | ||
|
||
@JvmStatic | ||
fun <S, K> distinct(source: LiveData<S>, keys: (S) -> K): LiveData<S> { | ||
return TransformerBase(DistinctObserver(keys)).transform(source) | ||
} | ||
fun <S, K> distinct(source: LiveData<S>, keys: (S) -> K): LiveData<S> = source.distinct(keys) | ||
|
||
@JvmStatic | ||
fun <S> throttleFirst( | ||
source: LiveData<S>, | ||
timeValue: Long, | ||
timeUnit: TimeUnit | ||
): LiveData<S> { | ||
return TransformerBase<S, S>(ThrottleFirstObservable(timeValue, timeUnit)).transform( | ||
source | ||
) | ||
} | ||
fun <S> throttleFirst(source: LiveData<S>, timeValue: Long, timeUnit: TimeUnit): LiveData<S> = source.throttleFirst(timeValue, timeUnit) | ||
|
||
@JvmStatic | ||
fun <S> collect(source: LiveData<S>): LiveData<Collection<S>> { | ||
return collect(source, mutableListOf()) | ||
} | ||
fun <S> collect(source: LiveData<S>): LiveData<Collection<S>> = source.collect() | ||
|
||
@JvmStatic | ||
fun <S> collect( | ||
source: LiveData<S>, | ||
mutableCollection: MutableCollection<S> = mutableListOf(), | ||
collectFunction: (collection: MutableCollection<S>, S) -> Unit = { collection, it -> | ||
collection.add(it) | ||
} | ||
): LiveData<Collection<S>> { | ||
return TransformerBase(CollectObservable(mutableCollection, collectFunction)).transform( | ||
source | ||
) | ||
} | ||
source: LiveData<S>, | ||
mutableCollection: MutableCollection<S> = mutableListOf(), | ||
collectFunction: (collection: MutableCollection<S>, S) -> Unit = { collection, it -> | ||
collection.add(it) | ||
} | ||
): LiveData<Collection<S>> = source.collect(mutableCollection, collectFunction) | ||
|
||
@JvmStatic | ||
fun <S, T> reduce( | ||
source: LiveData<S>, | ||
initValue: T, | ||
reduceFunction: (T, S) -> T | ||
): LiveData<T> { | ||
return TransformerBase(ReduceObservable(initValue, reduceFunction)).transform(source) | ||
} | ||
source: LiveData<S>, | ||
initValue: T, | ||
reduceFunction: (T, S) -> T | ||
): LiveData<T> = source.reduce(initValue, reduceFunction) | ||
|
||
@JvmStatic | ||
fun <S> count(source: LiveData<S>): LiveData<Int> { | ||
return TransformerBase<S, Int>(CountObservable()).transform(source) | ||
} | ||
fun <S> count(source: LiveData<S>): LiveData<Int> = source.count() | ||
|
||
@JvmStatic | ||
fun <S, T> cast(source: LiveData<S>): LiveData<T> { | ||
return TransformerBase(CastObservable<S, T>()).transform(source) | ||
} | ||
fun <S, T> cast(source: LiveData<S>): LiveData<T> = source.cast() | ||
|
||
@JvmStatic | ||
fun <S> timestamp(source: LiveData<S>): LiveData<Pair<S, Long>> { | ||
return TransformerBase(TimestampObservable<S>()).transform(source) | ||
} | ||
fun <S> timestamp(source: LiveData<S>): LiveData<Pair<S, Long>> = source.timestamp() | ||
|
||
@JvmStatic | ||
fun <S> take(source: LiveData<S>, size: Int): LiveData<S> { | ||
return TransformerBase(TakeObservable<S>(size)).transform(source) | ||
} | ||
fun <S> take(source: LiveData<S>, size: Int): LiveData<S> = source.take(size) | ||
|
||
@JvmStatic | ||
fun <S> takeWhile(source: LiveData<S>, testFunction: (S) -> Boolean): LiveData<S> { | ||
return TransformerBase(TakeWhileObservable(testFunction)).transform(source) | ||
} | ||
fun <S> takeWhile(source: LiveData<S>, testFunction: (S) -> Boolean): LiveData<S> = source.takeWhile(testFunction) | ||
|
||
@JvmStatic | ||
fun <S> takeUntil(source: LiveData<S>, testFunction: (S) -> Boolean): LiveData<S> { | ||
return TransformerBase(TakeUntilObservable(testFunction)).transform(source) | ||
} | ||
|
||
fun <S> takeUntil(source: LiveData<S>, testFunction: (S) -> Boolean): LiveData<S> = source.takeUntil(testFunction) | ||
|
||
@JvmStatic | ||
fun <S> skip(source: LiveData<S>, size: Int): LiveData<S> { | ||
return TransformerBase(SkipObservable<S>(size)).transform(source) | ||
} | ||
fun <S> skip(source: LiveData<S>, size: Int): LiveData<S> = source.skip(size) | ||
|
||
@JvmStatic | ||
fun <S> at(source: LiveData<S>, index: Int): LiveData<S> { | ||
return TransformerBase(AtObservable<S>(index)).transform(source) | ||
} | ||
fun <S> at(source: LiveData<S>, index: Int): LiveData<S> = source.at(index) | ||
|
||
@JvmStatic | ||
fun <S, T> type(source: LiveData<S>, clazz: Class<T>): LiveData<T> { | ||
return TransformerBase<S, T>(TypeObservable(clazz)).transform(source) | ||
} | ||
fun <S, T> type(source: LiveData<S>, clazz: Class<T>): LiveData<T> = source.type(clazz) | ||
|
||
fun <S> skipNull(source: LiveData<S>): LiveData<S> = source.skipNull() | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
livedata_transformer/src/main/java/com/microtears/orange/livedata/transformer/Transformer.kt
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
...a_transformer/src/main/java/com/microtears/orange/livedata/transformer/TransformerBase.kt
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...rmer/src/main/java/com/microtears/orange/livedata/transformer/UncaughtExceptionHandler.kt
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
livedata_transformer/src/main/java/com/microtears/orange/livedata/transformer/ValueOr.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.