/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tv.reference.lifecycle.ext

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData

/**
 * Extension functions for [LiveData], which use [MediatorLiveData], to allow observing on non-null
 * events.
 *
 * Usage:
 * ```
 * fooLiveData
 *     .observe(this, Observer {
 *       // foo is nullable
 *     })
 *
 * fooLiveData
 *     .nonNull()
 *     .observe(this, {
 *       // foo is non-null
 *     })
 * ```
 */

class NonNullMediatorLiveData<T> : MediatorLiveData<T>()

fun <T> LiveData<T>.nonNull(): NonNullMediatorLiveData<T> {
    val mediator: NonNullMediatorLiveData<T> = NonNullMediatorLiveData()
    mediator.addSource(this) { it?.let { mediator.value = it } }
    return mediator
}

fun <T> NonNullMediatorLiveData<T>.observe(owner: LifecycleOwner, observer: (t: T) -> Unit) {
    this.observe(owner) { it?.let(observer) }
}

Add non-null event emitter for Live Data
Add non-null event emitter for Live Data

These extension functions came from https://proandroiddev.com/nonnull-livedata-with-kotlin-extension-26963ffd0333 and are an easy way to clean up Observers with non-null events.

Bug: 173779685
Change-Id: I2db02bebec407502e6397aebfb6edca11ef3d158

android/tv-samples,android/tv-samples,android/tv-samples,android/tv-samples,android/tv-samples,android/tv-samples,android/tv-samples