MangaManagerImpl.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.put.PutResult;
  6. import com.pushtorefresh.storio.sqlite.operations.put.PutResults;
  7. import com.pushtorefresh.storio.sqlite.queries.Query;
  8. import com.pushtorefresh.storio.sqlite.queries.RawQuery;
  9. import java.util.List;
  10. import eu.kanade.mangafeed.data.models.Manga;
  11. import eu.kanade.mangafeed.data.tables.ChaptersTable;
  12. import eu.kanade.mangafeed.data.tables.MangasTable;
  13. import rx.Observable;
  14. public class MangaManagerImpl extends BaseManager implements MangaManager {
  15. public MangaManagerImpl(StorIOSQLite db) {
  16. super(db);
  17. }
  18. private final String mangasWithUnreadQuery = String.format(
  19. "SELECT %1$s.*, COUNT(C.%4$s) AS %5$s FROM %1$s LEFT JOIN " +
  20. "(SELECT %4$s FROM %2$s WHERE %6$s = 0) AS C ON %3$s = C.%4$s " +
  21. "GROUP BY %3$s",
  22. MangasTable.TABLE,
  23. ChaptersTable.TABLE,
  24. MangasTable.TABLE + "." + MangasTable.COLUMN_ID,
  25. ChaptersTable.COLUMN_MANGA_ID,
  26. MangasTable.COLUMN_UNREAD,
  27. ChaptersTable.COLUMN_READ
  28. );
  29. public Observable<List<Manga>> getMangas() {
  30. return db.get()
  31. .listOfObjects(Manga.class)
  32. .withQuery(Query.builder()
  33. .table(MangasTable.TABLE)
  34. .build())
  35. .prepare()
  36. .createObservable();
  37. }
  38. public Observable<List<Manga>> getMangasWithUnread() {
  39. return db.get()
  40. .listOfObjects(Manga.class)
  41. .withQuery(RawQuery.builder()
  42. .query(mangasWithUnreadQuery)
  43. .observesTables(MangasTable.TABLE, ChaptersTable.TABLE)
  44. .build())
  45. .prepare()
  46. .createObservable();
  47. }
  48. public Observable<List<Manga>> getManga(String url) {
  49. return db.get()
  50. .listOfObjects(Manga.class)
  51. .withQuery(Query.builder()
  52. .table(MangasTable.TABLE)
  53. .where(MangasTable.COLUMN_URL + "=?")
  54. .whereArgs(url)
  55. .build())
  56. .prepare()
  57. .createObservable();
  58. }
  59. public Observable<List<Manga>> getManga(int id) {
  60. return null;
  61. }
  62. @Override
  63. public Manga getMangaBlock(String url) {
  64. List<Manga> result = db.get()
  65. .listOfObjects(Manga.class)
  66. .withQuery(Query.builder()
  67. .table(MangasTable.TABLE)
  68. .where(MangasTable.COLUMN_URL + "=?")
  69. .whereArgs(url)
  70. .build())
  71. .prepare()
  72. .executeAsBlocking();
  73. if (result.isEmpty())
  74. return null;
  75. return result.get(0);
  76. }
  77. public Observable<PutResult> insertManga(Manga manga) {
  78. return db.put()
  79. .object(manga)
  80. .prepare()
  81. .createObservable();
  82. }
  83. public Observable<PutResults<Manga>> insertMangas(List<Manga> mangas) {
  84. return db.put()
  85. .objects(mangas)
  86. .prepare()
  87. .createObservable();
  88. }
  89. public PutResult insertMangaBlock(Manga manga) {
  90. return db.put()
  91. .object(manga)
  92. .prepare()
  93. .executeAsBlocking();
  94. }
  95. public Observable<DeleteResult> deleteManga(Manga manga) {
  96. return db.delete()
  97. .object(manga)
  98. .prepare()
  99. .createObservable();
  100. }
  101. public Observable<DeleteResults<Manga>> deleteMangas(List<Manga> mangas) {
  102. return db.delete()
  103. .objects(mangas)
  104. .prepare()
  105. .createObservable();
  106. }
  107. }