Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

android - Is it possible to make one LiveData of two LiveDatas?

I have two DAOs, two Repositories and two POJOs. There is some way to create one Livedata of two? I need it to make single list for Recyclerview. POJOs are similar objects.

ExpenseRepository:

public class ExpenseRepository {

private ExpenseDao expenseDao;
private LiveData<List<Expense>> allExpenses;

public ExpenseRepository(Application application) {
    ExpenseIncomeDatabase database = ExpenseIncomeDatabase.getInstance(application);
    expenseDao = database.expenseDao();
    allExpenses = expenseDao.getExpensesByDay();
}

public LiveData<List<Expense>> getAllExpensesByDay() {
    return allExpenses;
}

IncomeRepository:

public class IncomeRepository {

private IncomeDao incomeDao;
private LiveData<List<Income>> allIncomes;

public IncomeRepository(Application application) {
    ExpenseIncomeDatabase database = ExpenseIncomeDatabase.getInstance(application);
    incomeDao = database.incomeDao();
    allIncomes = incomeDao.getIncomesByDay();
}

public LiveData<List<Income>> getAllIncomesByDay() {
    return allIncomes;
}

ExpenseDao:

@Dao
public interface ExpenseDao {

@Query("SELECT * FROM expense_table ORDER BY day") 
LiveData<List<Expense>> getExpensesByDay();

IncomeDao:

@Dao
public interface IncomeDao {

@Query("SELECT * FROM income_table ORDER BY day") 
LiveData<List<Income>> getIncomesByDay();

DailyViewModel:

public class DailyFragmentViewModel extends AndroidViewModel {

private ExpenseRepository expenseRepository;
private IncomeRepository incomeRepository;
private LiveData<Pair<List<Expense>, List<Income>>> combined;
private ExpenseDao expenseDao;
private IncomeDao incomeDao;

public DailyFragmentViewModel(@NonNull Application application) {
    super(application);
    expenseRepository = new ExpenseRepository(application);
    incomeRepository = new IncomeRepository(application);
    combined = new DailyCombinedLiveData(expenseDao.getExpensesByDay(), incomeDao.getIncomesByDay());
}

public LiveData<Pair<List<Expense>, List<Income>>> getExpensesAndIncomes() {
    return combined;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume you want to combine them, yes? You'll need a MediatorLiveData, but the guy saying you now need Object is wrong. What you need is a MediatorLiveData<Pair<List<Expense>, List<Income>>>.

public class CombinedLiveData extends MediatorLiveData<Pair<List<Expense>, List<Income>>> {
    private List<Expense> expenses = Collections.emptyList();
    private List<Income> incomes = Collections.emptyList();

    public CombinedLiveData(LiveData<List<Expense>> ld1, LiveData<List<Income>> ld2) {
        setValue(Pair.create(expenses, incomes));

        addSource(ld1, expenses -> { 
             if(expenses != null) {
                this.expenses = expenses;
             } 
             setValue(Pair.create(expenses, incomes)); 
        });

        addSource(ld2, incomes -> { 
            if(incomes != null) {
                this.incomes = incomes;
            } 
            setValue(Pair.create(expenses, incomes));
        });
    }
}

You could potentially make this generic and it'd be the implementation of combineLatest for two LiveData using tuples of 2-arity (Pair).

EDIT: like this:

public class CombinedLiveData2<A, B> extends MediatorLiveData<Pair<A, B>> {
    private A a;
    private B b;

    public CombinedLiveData2(LiveData<A> ld1, LiveData<B> ld2) {
        setValue(Pair.create(a, b));

        addSource(ld1, a -> { 
             if(a != null) {
                this.a = a;
             } 
             setValue(Pair.create(a, b)); 
        });

        addSource(ld2, b -> { 
            if(b != null) {
                this.b = b;
            } 
            setValue(Pair.create(a, b));
        });
    }
}

Beware that I lost the ability to set Collections.emptyList() as initial values of A and B with this scenario, and you WILL need to check for nulls when you access the data inside the pair.

EDIT: You can use the library https://github.com/Zhuinden/livedata-combinetuple-kt (Kotlin) or https://github.com/Zhuinden/livedata-combineutil-java/ (Java) which does the same thing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...