Package com.tomtom.tools.android.api.livedata

Types

Link copied to clipboard
open class CombinedCollectionLiveData<I, O>(calculateValue: (List<I>) -> O) : MutableLiveData<O>

Similar to MediatorLiveData, but with a few key differences:

Link copied to clipboard
class DistinctMutableLiveData<T> : MutableLiveData<T>

An implementation of MutableLiveData that only emits updates when the value actually changed, i.e. when newValue != oldValue. This differs from regular MutableLiveData because if you set its value to an equal same value, it still emits an update, causing observers to get a callback.

Link copied to clipboard
class EmptyLiveData<T> : LiveData<T?>

An empty LiveData implementation for situations where we need a LiveData instance which will always have a null value.

Link copied to clipboard
class ImmutableLiveData<T>(value: T) : LiveData<T>

A LiveData class which has the value set during construction.

Link copied to clipboard
class TimerLiveData<T>(delayMs: Long, computeValue: () -> T) : LiveData<T>

A LiveData which updates its value every delayMs. Override computeValue to provide the new value for each period. Its value will only be updated while there are active observers.

Link copied to clipboard
class TransformationMutableLiveData<T>(transformation: (T) -> T) : MutableLiveData<T>

A LiveData that applies a transformation to the value before setting it.

Link copied to clipboard
class UnsetLiveData<T> : LiveData<T>

An empty LiveData implementation for situations where we need a LiveData instance which will never have a value.

Properties

Link copied to clipboard
val <T> LiveData<T>.observedValue: T?

Same as LiveData.getValue, except this variant throws a IllegalStateException when the LiveData instance does not have an active observer.

Link copied to clipboard
val <T> LiveData<T>.valueUpToDate: T?

Same as LiveData.getValue with the exception that the returned value is up to date even when the LiveData instance does not have an observer.

Functions

Link copied to clipboard
fun <E> MutableLiveData<List<E>>.add(element: E): Boolean

Adds the specified element to the end of this MutableLiveData. The observers are notified of the new value.

Link copied to clipboard
fun <E> MutableLiveData<List<E>>.addAll(elements: Iterable<E>): Boolean

Adds all of the elements of the specified collection to the end of this MutableLiveData. The observers are notified of the new value if the list was changed.

Link copied to clipboard
fun Collection<LiveData<Boolean>>.allTrue(): LiveData<Boolean>

Convenience method for calling allTrue in a functional flow.

fun allTrue(vararg sources: LiveData<Boolean>): LiveData<Boolean>

Transforms sources to a LiveData which value is updated to true when all sources are true, updated to false otherwise.

Link copied to clipboard
fun <E> LiveData<out Collection<E>?>.any(predicate: (E) -> Boolean): Boolean
Link copied to clipboard
fun Collection<LiveData<Boolean>>.anyTrue(): LiveData<Boolean>

Convenience method for calling anyTrue in a functional flow.

fun anyTrue(vararg sources: LiveData<Boolean>): LiveData<Boolean>

Transforms sources to a LiveData which value is updated to true when any of the sources is true, updated to false otherwise.

Link copied to clipboard
fun <T> MutableLiveData<T>.asLiveData(): LiveData<T>

Convenience method to create non-mutable LiveData fields without duplicating the type.

Link copied to clipboard
fun <T> LiveData<T>.check(predicate: (T) -> Boolean): Boolean
Link copied to clipboard
fun <E> MutableLiveData<List<E>>.clear()

Removes all elements from this MutableLiveData. The observers are notified of the new value if the list was changed.

@JvmName(name = "clearKV")
fun <K, V> MutableLiveData<Map<K, V>>.clear()

Removes all elements from a LiveData's Map, notifying observers of the new value.

Link copied to clipboard
fun <S, T> Collection<LiveData<out S>>.combine(combineFunction: (List<S>) -> T): LiveData<T>

Transforms a list of LiveDatas containing any type of value using the given combineFunction. Whenever any of the sources are updated, the returned LiveData value is also updated.

fun <S1, S2, T> combine(source1: LiveData<out S1>, source2: LiveData<out S2>, combineFunction: (S1, S2) -> T): LiveData<T>

Transforms two LiveDatas containing any type of value using the given combineFunction. Whenever any of the sources are updated, the returned LiveData value is also updated.

fun <S1, S2, S3, T> combine(source1: LiveData<out S1>, source2: LiveData<out S2>, source3: LiveData<out S3>, combineFunction: (S1, S2, S3) -> T): LiveData<T>

Transforms three LiveDatas containing any type of value using the given combineFunction. Whenever any of the sources are updated, the returned LiveData value is also updated.

fun <S1, S2, S3, S4, T> combine(source1: LiveData<out S1>, source2: LiveData<out S2>, source3: LiveData<out S3>, source4: LiveData<out S4>, combineFunction: (S1, S2, S3, S4) -> T): LiveData<T>

Transforms four LiveDatas containing any type of value using the given combineFunction. Whenever any of the sources are updated, the returned LiveData value is also updated.

fun <S1, S2, S3, S4, S5, T> combine(source1: LiveData<out S1>, source2: LiveData<out S2>, source3: LiveData<out S3>, source4: LiveData<out S4>, source5: LiveData<out S5>, combineFunction: (S1, S2, S3, S4, S5) -> T): LiveData<T>

Transforms five LiveDatas containing any type of value using the given combineFunction. Whenever any of the sources are updated, the returned LiveData value is also updated.

Link copied to clipboard
fun <S, T> combineMap(vararg sources: LiveData<out List<S>>, combineMapFunction: (S) -> T): LiveData<List<T>>

Transforms multiple LiveDatas containing lists of type S, into one LiveData with a single list of type T. combineMapFunction is used to map each of the sources' items into a list.

Link copied to clipboard
fun <E> LiveData<out Collection<E>?>.contains(element: E): Boolean

operator fun <K> LiveData<out Map<K, *>>.contains(key: K): Boolean

Returns true if the LiveData's value contains the specified key.

Link copied to clipboard
fun <E> LiveData<out Collection<E>?>.containsAll(elements: Collection<E>): Boolean
Link copied to clipboard
fun <K> LiveData<out Map<K, *>>.containsKey(key: K): Boolean

Returns true if the LiveData's value contains the specified key.

Link copied to clipboard
fun <K, V> LiveData<out Map<K, V>>.containsValue(value: V): Boolean

Returns true if the LiveData's value maps one or more keys to the specified value.

Link copied to clipboard
fun <K, V> LiveData<out Map<K, V>>.find(predicate: (Map.Entry<K, V>) -> Boolean): Map.Entry<K, V>?

Finds the first entry from a LiveData's Map's value that matches the given predicate. Returns null if no entry is found or if the LiveData has no value.

Link copied to clipboard
fun <T, S> LiveData<out Collection<S>>.flatMap(mapFunction: (S) -> LiveData<out Collection<T>>): LiveData<Collection<T>>

Transforms a LiveData containing a list of type T, which contains all the child elements from each element in thisLiveData list by flattening it.

Link copied to clipboard
fun <T, S> LiveData<out Collection<S>>.flatMapBySource(mapFunction: (S) -> LiveData<out Collection<T>>): LiveData<Collection<Pair<S, T>>>

Transforms a LiveData containing a list of type T, which contains all the child elements from each element in thisLiveData list together with its source, by flattening it.

Link copied to clipboard
fun <E> LiveData<out Iterable<E>?>.forEach(operation: (E) -> Unit)

Performs operation on each entry in a LiveData's collection. Does nothing if the LiveData has no value.

fun <K, V> LiveData<out Map<K, V>>.forEach(operation: (Map.Entry<K, V>) -> Unit)

Performs operation on each entry in a LiveData's Map. Does nothing if the LiveData has no value.

Link copied to clipboard
operator fun <K, V> LiveData<out Map<K, V>>.get(key: K): V?

Returns the value corresponding to the given key in the LiveData's value, or null if such a key is not present in the map or the LiveData has no value.

Link copied to clipboard
fun <K, V> LiveData<out Map<K, V>>.getOrDefault(key: K, defaultValue: V): V

Returns the value corresponding to the given key in the LiveData's value, or defaultValue if such a key is not present in the map or the LiveData has no value.

Link copied to clipboard
fun <K, V> LiveData<out Map<K, V>>.getOrElse(key: K, defaultValue: () -> V): V

Returns the value for the given key in the LiveData's value, or the result of the defaultValue function if there was no entry for the given key or the LiveData has no value.

Link copied to clipboard
fun <K, V> MutableLiveData<Map<K, V>>.getOrPut(key: K, defaultValue: () -> V): V

Returns the value for the given key in the LiveData's value. If the key is not found, calls the defaultValue function, puts its result into the map under the given key and returns it. Creates a map if the LiveData has no value yet.

Link copied to clipboard
fun <T, B : Boolean?> LiveData<B>.ifTrue(block: () -> LiveData<T>): LiveData<T?>

Executes and switchMaps to block if this has a true value. If this is false or null, it returns LiveData with value null. If this should not return null, ifTrueOrDefault can be used instead.

Link copied to clipboard
fun <T : Any, B : Boolean?> LiveData<B>.ifTrueOrDefault(default: T, block: () -> LiveData<T>): LiveData<T>

Executes and switchMaps to block if this has a true value. If this is false or null, it returns LiveData with value default. This can be used instead of ifTrue when the default value should be something other than null. Note that this requires a non-null value for T in order for Kotlin's compiler to properly spot missing null-checks.

Link copied to clipboard
fun <K, V> LiveData<Map<K, V>>.mapToValueOrUnset(key: K): LiveData<V>

Maps the value of the provided key, if that key exists in this map. Otherwise, no value is set. This is useful in cases where the map may not contain the requested entry but the resulting LiveData is not nullable as the client does not care about a null value. This removes the need for the client to have null checks in its code wherever the resulting LiveData is used.

Link copied to clipboard
fun <T : Any, S : Any> LiveData<out Collection<S>>.merge(mapFunction: (S) -> LiveData<out T?>): LiveData<out Collection<T>>

Transforms LiveData containing a list that holds other LiveData instances into LiveData that contains the values from those LiveData instances. The returned LiveData will be updated when the source LiveData changes as well as when the internal LiveData instances change.

Link copied to clipboard
operator fun <E> MutableLiveData<List<E>>.minusAssign(element: E)

Removes a single instance of the specified element from a MutableLiveData, if it is present. The observers are notified of the new value if any of the specified elements was removed.

operator fun <E> MutableLiveData<List<E>>.minusAssign(elements: Iterable<E>)

Removes all elements from this MutableLiveData that are also contained in the given elements collection. The observers are notified of the new value if the list was changed.

operator fun <K, V> MutableLiveData<Map<K, V>>.minusAssign(key: K)

Removes the entry mapped by key from the LiveData's value, notifying observers of the new value.

Link copied to clipboard
fun <E> LiveData<out Collection<E>?>.none(predicate: (E) -> Boolean): Boolean
Link copied to clipboard
operator fun <T : Boolean?> LiveData<T>.not(): LiveData<T>

Negates the value of a boolean LiveData.

Link copied to clipboard
inline fun <T : Any> LiveData<T>.observeNonNull(owner: LifecycleOwner, crossinline onChanged: (T) -> Unit): Observer<T>

Adds the given onChanged lambda as an observer within the lifespan of the given owner and returns a reference to observer. The events are dispatched on the main thread.

Link copied to clipboard
inline fun <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner, crossinline onChanged: (T) -> Unit): Observer<T>

Observes this LiveData for one change only and returns the one-time observer. The returned observer can be used to stop observing the LiveData when a callback is no longer desired. It is safe to remove the observer while it is executing or has been executed already.

Link copied to clipboard
inline fun <T> LiveData<T>.observeWhile(lifecycleOwner: LifecycleOwner, crossinline onChanged: (T) -> Boolean): Observer<T>

Observes this LiveData as long as onChanged returns true and returns the observer. The returned observer can be used to stop observing the LiveData when a callback is no longer desired. It is safe to remove the observer while it is executing or after onChanged returns true.

Link copied to clipboard
operator fun <E> MutableLiveData<List<E>>.plusAssign(element: E)

Adds the specified element to the end of this MutableLiveData. The observers are notified of the new value.

operator fun <E> MutableLiveData<List<E>>.plusAssign(elements: Iterable<E>)

Adds all of the elements of the specified collection to the end of this MutableLiveData. The observers are notified of the new value if the list was changed.

operator fun <K, V> MutableLiveData<Map<K, V>>.plusAssign(map: Map<K, V>)

Appends all entries from the given map in a LiveData, notifying observers of the new value. Creates a map if the LiveData has no value yet.

Link copied to clipboard
fun <E> MutableLiveData<List<E>>.remove(element: E): Boolean

Removes a single instance of the specified element from a MutableLiveData, if it is present. The observers are notified of the new value if any of the specified elements was removed.

Link copied to clipboard
fun <E> MutableLiveData<List<E>>.removeAll(predicate: (E) -> Boolean): Boolean

Removes all elements from this MutableLiveData that match the given predicate. The observers are notified of the new value if the list was changed.

fun <E> MutableLiveData<List<E>>.removeAll(elements: Iterable<E>): Boolean

Removes all elements from this MutableLiveData that are also contained in the given elements collection. The observers are notified of the new value if the list was changed.

Link copied to clipboard
fun <T : R?, R : Any> LiveData<T>.requireValue(): R

Gets the value stored in the LiveData object that should not be null.

Link copied to clipboard
operator fun <K, V> MutableLiveData<Map<K, V>>.set(key: K, valueToAdd: V)

Maps key to valueToAdd in LiveData's value. Creates a map if the LiveData has no value yet.

Link copied to clipboard
fun <T> LiveData<T>.withSetter(setter: (T) -> Unit): MutableLiveData<T>

Creates a MutableLiveData from LiveData by specifying the setter to use to the change the value.