Insert or remove chapters with one subscriber
This commit is contained in:
parent
74ccd8bd24
commit
ea8ded549b
@ -0,0 +1,39 @@
|
|||||||
|
package com.pushtorefresh.storio.sqlite.operations.post;
|
||||||
|
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by len on 08/10/2015.
|
||||||
|
*/
|
||||||
|
public class PostResult {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private final Integer numberOfRowsUpdated;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private final Integer numberOfRowsInserted;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private final Integer numberOfRowsDeleted;
|
||||||
|
|
||||||
|
public PostResult(Integer numberOfRowsUpdated, Integer numberOfRowsInserted, Integer numberOfRowsDeleted) {
|
||||||
|
this.numberOfRowsUpdated = numberOfRowsUpdated;
|
||||||
|
this.numberOfRowsInserted = numberOfRowsInserted;
|
||||||
|
this.numberOfRowsDeleted = numberOfRowsDeleted;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public Integer getNumberOfRowsUpdated() {
|
||||||
|
return numberOfRowsUpdated;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public Integer getNumberOfRowsInserted() {
|
||||||
|
return numberOfRowsInserted;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public Integer getNumberOfRowsDeleted() {
|
||||||
|
return numberOfRowsDeleted;
|
||||||
|
}
|
||||||
|
}
|
@ -4,11 +4,11 @@ import com.pushtorefresh.storio.sqlite.StorIOSQLite;
|
|||||||
import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult;
|
import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult;
|
||||||
import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults;
|
import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults;
|
||||||
import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetListOfObjects;
|
import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetListOfObjects;
|
||||||
|
import com.pushtorefresh.storio.sqlite.operations.post.PostResult;
|
||||||
import com.pushtorefresh.storio.sqlite.operations.put.PutResult;
|
import com.pushtorefresh.storio.sqlite.operations.put.PutResult;
|
||||||
import com.pushtorefresh.storio.sqlite.operations.put.PutResults;
|
import com.pushtorefresh.storio.sqlite.operations.put.PutResults;
|
||||||
import com.pushtorefresh.storio.sqlite.queries.Query;
|
import com.pushtorefresh.storio.sqlite.queries.Query;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import eu.kanade.mangafeed.data.models.Chapter;
|
import eu.kanade.mangafeed.data.models.Chapter;
|
||||||
@ -22,7 +22,7 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager {
|
|||||||
super(db);
|
super(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
private PreparedGetListOfObjects<Chapter> prepareGet(Manga manga) {
|
private PreparedGetListOfObjects<Chapter> prepareGetChapters(Manga manga) {
|
||||||
return db.get()
|
return db.get()
|
||||||
.listOfObjects(Chapter.class)
|
.listOfObjects(Chapter.class)
|
||||||
.withQuery(Query.builder()
|
.withQuery(Query.builder()
|
||||||
@ -35,7 +35,7 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Observable<List<Chapter>> getChapters(Manga manga) {
|
public Observable<List<Chapter>> getChapters(Manga manga) {
|
||||||
return prepareGet(manga).createObservable();
|
return prepareGetChapters(manga).createObservable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -56,36 +56,31 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager {
|
|||||||
|
|
||||||
// Add new chapters or delete if the source deletes them
|
// Add new chapters or delete if the source deletes them
|
||||||
@Override
|
@Override
|
||||||
public Observable insertOrRemoveChapters(Manga manga, List<Chapter> chapters) {
|
public Observable<PostResult> insertOrRemoveChapters(Manga manga, List<Chapter> chapters) {
|
||||||
// I don't know a better approach
|
Observable<List<Chapter>> chapterList = Observable.create(subscriber -> {
|
||||||
// TODO Fix this method
|
subscriber.onNext(prepareGetChapters(manga).executeAsBlocking());
|
||||||
return Observable.create(subscriber -> {
|
subscriber.onCompleted();
|
||||||
List<Chapter> dbChapters = prepareGet(manga).executeAsBlocking();
|
});
|
||||||
|
|
||||||
Observable<Integer> newChaptersObs =
|
Observable<Integer> newChaptersObs =
|
||||||
Observable.from(chapters)
|
chapterList
|
||||||
|
.flatMap(dbChapters -> Observable.from(chapters)
|
||||||
.filter(c -> !dbChapters.contains(c))
|
.filter(c -> !dbChapters.contains(c))
|
||||||
.toList()
|
.toList()
|
||||||
.flatMap(this::insertChapters)
|
.flatMap(this::insertChapters)
|
||||||
.map(PutResults::numberOfInserts);
|
.map(PutResults::numberOfInserts));
|
||||||
|
|
||||||
Observable<Integer> deletedChaptersObs =
|
Observable<Integer> deletedChaptersObs =
|
||||||
Observable.from(dbChapters)
|
chapterList
|
||||||
|
.flatMap(dbChapters -> Observable.from(dbChapters)
|
||||||
.filter(c -> !chapters.contains(c))
|
.filter(c -> !chapters.contains(c))
|
||||||
.toList()
|
.toList()
|
||||||
.flatMap(this::deleteChapters)
|
.flatMap(this::deleteChapters)
|
||||||
.map(result -> result.results().size());
|
.map( d -> d.results().size() ));
|
||||||
|
|
||||||
Observable.zip(newChaptersObs, deletedChaptersObs,
|
return Observable.zip(newChaptersObs, deletedChaptersObs,
|
||||||
(newChapters, deletedChapters) -> {
|
(insertions, deletions) -> new PostResult(0, insertions, deletions)
|
||||||
ArrayList<Integer> results = new ArrayList<>();
|
);
|
||||||
results.add(newChapters);
|
|
||||||
results.add(deletedChapters);
|
|
||||||
subscriber.onNext(results);
|
|
||||||
subscriber.onCompleted();
|
|
||||||
return results;
|
|
||||||
}).subscribe();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user