mirror of
https://github.com/aniyomiorg/aniyomi.git
synced 2024-11-27 08:16:36 +03:00
Insert or remove method (meh)
This commit is contained in:
parent
a3463addc3
commit
2888023eb1
1 changed files with 17 additions and 18 deletions
|
@ -53,28 +53,27 @@ public class ChapterManager extends BaseManager {
|
|||
// Add new chapters or delete if the source deletes them
|
||||
public Observable insertOrRemove(Manga manga, List<Chapter> chapters) {
|
||||
// I don't know a better approach
|
||||
// TODO Fix this method
|
||||
return Observable.create(subscriber -> {
|
||||
List<Chapter> dbGet = prepareGet(manga).executeAsBlocking();
|
||||
List<Chapter> dbChapters = prepareGet(manga).executeAsBlocking();
|
||||
|
||||
Observable.just(dbGet)
|
||||
.doOnNext(dbChapters -> {
|
||||
Observable<List<Chapter>> newChaptersObs =
|
||||
Observable.from(chapters)
|
||||
.filter(c -> !dbChapters.contains(c))
|
||||
.toList()
|
||||
.subscribe(newChapters -> {
|
||||
if (newChapters.size() > 0)
|
||||
insert(newChapters).subscribe();
|
||||
});
|
||||
})
|
||||
.flatMap(Observable::from)
|
||||
.filter(c -> !chapters.contains(c))
|
||||
.toList()
|
||||
.subscribe(removedChapters -> {
|
||||
if (removedChapters.size() > 0)
|
||||
delete(removedChapters).subscribe();
|
||||
subscriber.onCompleted();
|
||||
});
|
||||
.toList();
|
||||
|
||||
Observable<List<Chapter>> deletedChaptersObs =
|
||||
Observable.from(dbChapters)
|
||||
.filter(c -> !chapters.contains(c))
|
||||
.toList();
|
||||
|
||||
Observable.zip(newChaptersObs, deletedChaptersObs,
|
||||
(newChapters, deletedChapters) -> {
|
||||
insert(newChapters).subscribe();
|
||||
delete(deletedChapters).subscribe();
|
||||
subscriber.onCompleted();
|
||||
return null;
|
||||
}).subscribe();
|
||||
});
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue