package de.philipphager.disclosure.feature.sync.api.usecases;

import de.philipphager.disclosure.api.DisclosureApi;
import de.philipphager.disclosure.database.library.model.Library;
import de.philipphager.disclosure.service.LibraryService;
import java.util.List;
import javax.inject.Inject;
import rx.Observable;

public class SyncLibraries {
  private final DisclosureApi disclosureApi;
  private final LibraryService libraryService;

  @Inject public SyncLibraries(DisclosureApi disclosureApi,
      LibraryService libraryService) {
    this.disclosureApi = disclosureApi;
    this.libraryService = libraryService;
  }

  public Observable<List<Library>> run() {
    return libraryService.lastUpdated()
        .first()
        .flatMap(lastUpdated -> new Paginator<Library>() {
          @Override protected Observable<List<Library>> queryPage(int page, int limit) {
            return disclosureApi.allLibraries(lastUpdated, page, limit);
          }
        }.join())
        .doOnNext(libraryService::insertOrUpdate);
  }
}

@@ -305,16 +305,42 @@
 ervable;
+%0Aimport timber.log.Timber;
 %0A%0Apublic
@@ -885,24 +885,108 @@
 nt limit) %7B%0A
+            Timber.d(%22fetch libraries from api, page: %25d, limit: %25d%22, page, limit);%0A
             

Add log message to library syncer