ChapterManagerImpl.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package eu.kanade.mangafeed.data.managers;
  2. import com.pushtorefresh.storio.sqlite.StorIOSQLite;
  3. import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult;
  4. import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults;
  5. import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetListOfObjects;
  6. import com.pushtorefresh.storio.sqlite.operations.put.PutResult;
  7. import com.pushtorefresh.storio.sqlite.operations.put.PutResults;
  8. import com.pushtorefresh.storio.sqlite.queries.Query;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import eu.kanade.mangafeed.data.models.Chapter;
  12. import eu.kanade.mangafeed.data.models.Manga;
  13. import eu.kanade.mangafeed.data.tables.ChaptersTable;
  14. import rx.Observable;
  15. public class ChapterManagerImpl extends BaseManager implements ChapterManager {
  16. public ChapterManagerImpl(StorIOSQLite db) {
  17. super(db);
  18. }
  19. private PreparedGetListOfObjects<Chapter> prepareGet(Manga manga) {
  20. return db.get()
  21. .listOfObjects(Chapter.class)
  22. .withQuery(Query.builder()
  23. .table(ChaptersTable.TABLE)
  24. .where(ChaptersTable.COLUMN_MANGA_ID + "=?")
  25. .whereArgs(manga.id)
  26. .build())
  27. .prepare();
  28. }
  29. @Override
  30. public Observable<List<Chapter>> getChapters(Manga manga) {
  31. return prepareGet(manga).createObservable();
  32. }
  33. @Override
  34. public Observable<PutResult> insertChapter(Chapter chapter) {
  35. return db.put()
  36. .object(chapter)
  37. .prepare()
  38. .createObservable();
  39. }
  40. @Override
  41. public Observable<PutResults<Chapter>> insertChapters(List<Chapter> chapters) {
  42. return db.put()
  43. .objects(chapters)
  44. .prepare()
  45. .createObservable();
  46. }
  47. // Add new chapters or delete if the source deletes them
  48. @Override
  49. public Observable insertOrRemoveChapters(Manga manga, List<Chapter> chapters) {
  50. // I don't know a better approach
  51. // TODO Fix this method
  52. return Observable.create(subscriber -> {
  53. List<Chapter> dbChapters = prepareGet(manga).executeAsBlocking();
  54. Observable<List<Chapter>> newChaptersObs =
  55. Observable.from(chapters)
  56. .filter(c -> !dbChapters.contains(c))
  57. .toList();
  58. Observable<List<Chapter>> deletedChaptersObs =
  59. Observable.from(dbChapters)
  60. .filter(c -> !chapters.contains(c))
  61. .toList();
  62. Observable.zip(newChaptersObs, deletedChaptersObs,
  63. (newChapters, deletedChapters) -> {
  64. insertChapters(newChapters).subscribe();
  65. deleteChapters(deletedChapters).subscribe();
  66. subscriber.onCompleted();
  67. return null;
  68. }).subscribe();
  69. });
  70. }
  71. @Override
  72. public Observable<DeleteResult> deleteChapter(Chapter chapter) {
  73. return db.delete()
  74. .object(chapter)
  75. .prepare()
  76. .createObservable();
  77. }
  78. @Override
  79. public Observable<DeleteResults<Chapter>> deleteChapters(List<Chapter> chapters) {
  80. return db.delete()
  81. .objects(chapters)
  82. .prepare()
  83. .createObservable();
  84. }
  85. }