Signal-Android/app/src/main/java/org/thoughtcrime/securesms/payments/PaymentTransactionLiveData....

45 wiersze
1.6 KiB
Java

package org.thoughtcrime.securesms.payments;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import org.signal.core.util.concurrent.SignalExecutors;
import org.thoughtcrime.securesms.database.DatabaseObserver;
import org.thoughtcrime.securesms.database.PaymentDatabase;
import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor;
import java.util.UUID;
import java.util.concurrent.Executor;
public final class PaymentTransactionLiveData extends LiveData<PaymentDatabase.PaymentTransaction> {
private final UUID paymentId;
private final PaymentDatabase paymentDatabase;
private final DatabaseObserver.Observer observer;
private final Executor executor;
public PaymentTransactionLiveData(@NonNull UUID paymentId) {
this.paymentId = paymentId;
this.paymentDatabase = SignalDatabase.payments();
this.observer = this::getPaymentTransaction;
this.executor = new SerialMonoLifoExecutor(SignalExecutors.BOUNDED);
}
@Override
protected void onActive() {
getPaymentTransaction();
ApplicationDependencies.getDatabaseObserver().registerPaymentObserver(paymentId, observer);
}
@Override
protected void onInactive() {
ApplicationDependencies.getDatabaseObserver().unregisterObserver(observer);
}
private void getPaymentTransaction() {
executor.execute(() -> postValue(paymentDatabase.getPayment(paymentId)));
}
}