setCurrentPageIndex method

Future<bool> setCurrentPageIndex(
  1. String bookId,
  2. int pageIndex
)

Implementation

Future<bool> setCurrentPageIndex(String bookId, int pageIndex) async {
  try {
    BookProgressModel? oldBookProgressModel = await isar.bookProgressModels
        .where()
        .filter()
        .bookIdEqualTo(bookId)
        .findFirst();

    if (oldBookProgressModel != null) {
      oldBookProgressModel.currentPageIndex = pageIndex;
      await isar.writeTxn(() async {
        isar.bookProgressModels.put(oldBookProgressModel);
      });
    } else {
      var newBookProgressModel = BookProgressModel(
          currentPageIndex: pageIndex,
          currentChapterIndex: 0,
          bookId: bookId);
      await isar.writeTxn(() async {
        isar.bookProgressModels.put(newBookProgressModel);
      });
    }
    return true;
  } catch (e) {
    return false;
  }
}