# LiveData.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2021 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */

    package dk.nsp.mingraviditet.util.extensions

    import androidx.lifecycle.*
    import dk.nsp.mingraviditet.util.livedata.SingleEvent
    import kotlin.reflect.KProperty

    /**
     * Extension to wrap [Transformations.map]. The [action] gets called every time a new value
     * is sent to the [LiveData] this function was called on
     * @receiver LiveData<T>
     * @param action Function1<T, R> - The mapping action to perform
     * @return LiveData<R> - The result of the [action] wrapped in a LiveData object
     */
    fun <T, R> LiveData<T>.transform(action: (T) -> R): LiveData<R> {
        return Transformations.map(this, action)
    }

    /**
     * allows to use "by" instead of explicitly writing getters for [LiveData] to [MutableLiveData]
     * @receiver MutableLiveData<T>
     * @param caller Any who is the caller
     * @param property KProperty<*> what property is it on the caller
     * @return [LiveData]<T>
     */
    operator fun <T> LiveData<T>.getValue(
        caller: Any,
        property: KProperty<*>
    ): LiveData<T> {
        return this
    }

    inline fun <T> LiveData<SingleEvent<T>>.observeIfNotHandled(
        owner: LifecycleOwner,
        crossinline onChanged: (T) -> Unit
    ): Observer<SingleEvent<T>> {
        val observer = Observer<SingleEvent<T>> {
            it.getIfNotHandled()?.let(onChanged)
        }
        observe(owner, observer)
        return observer
    }

    fun MediatorLiveData<*>.addSources(
        sources: List<LiveData<*>>,
        onAnyChanged: () -> Unit
    ) = sources.forEach {
        addSource(it) {
            onAnyChanged()
        }
    }

    fun <T> MutableLiveData<SingleEvent<T>>.postSingleEvent(data: T) {
        postValue(data.toSingleEvent())
    }

    fun MutableLiveData<SingleEvent<Unit>>.postSingleEvent() {
        postValue(SingleEvent(Unit))
    }