Skip to content

Instantly share code, notes, and snippets.

@ziginsider
Created December 3, 2017 20:22
Show Gist options
  • Save ziginsider/e6c38fb782c171bb627b69e79c40973d to your computer and use it in GitHub Desktop.
Save ziginsider/e6c38fb782c171bb627b69e79c40973d to your computer and use it in GitHub Desktop.
private void subscribeToDbChanges() {
// как преобразовать LiveData
// входная сигнатура - List<LoanWithUserAndBook>
// выходная - String
LiveData<List<LoanWithUserAndBook>> loans
= mDb.loanModel().findLoansByNameAfter("Alexander", getYesterdayDate());
// Instead of exposing the list of Loans, we can apply a transformation and expose Strings.
mLoansResult = Transformations.map(loans,
new Function<List<LoanWithUserAndBook>, String>() {
@Override
public String apply(List<LoanWithUserAndBook> loansWithUserAndBook) {
StringBuilder sb = new StringBuilder();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm",
Locale.US);
for (LoanWithUserAndBook loan : loansWithUserAndBook) {
sb.append(String.format("%s\n (Returned: %s)\n",
loan.bookTitle,
simpleDateFormat.format(loan.endTime)));
}
return sb.toString();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment