Created
          December 3, 2017 20:22 
        
      - 
      
- 
        Save ziginsider/e6c38fb782c171bb627b69e79c40973d to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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