taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

CombinedLiveData.kt (1595B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2020 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.common
     18 
     19 import androidx.lifecycle.LiveData
     20 import androidx.lifecycle.MediatorLiveData
     21 import androidx.lifecycle.Observer
     22 
     23 class CombinedLiveData<T, K, S>(
     24     source1: LiveData<T>,
     25     source2: LiveData<K>,
     26     private val combine: (data1: T?, data2: K?) -> S
     27 ) : MediatorLiveData<S>() {
     28 
     29     private var data1: T? = null
     30     private var data2: K? = null
     31 
     32     init {
     33         super.addSource(source1) { t ->
     34             data1 = t
     35             value = combine(data1, data2)
     36         }
     37         super.addSource(source2) { k ->
     38             data2 = k
     39             value = combine(data1, data2)
     40         }
     41     }
     42 
     43     override fun <S : Any?> addSource(source: LiveData<S>, onChanged: Observer<in S>) {
     44         throw UnsupportedOperationException()
     45     }
     46 
     47     override fun <T : Any?> removeSource(toRemote: LiveData<T>) {
     48         throw UnsupportedOperationException()
     49     }
     50 
     51 }