markChapterReadOrUnRead method

Future<Result> markChapterReadOrUnRead(
  1. String mangaId,
  2. String sessionToken, {
  3. List<String>? chapterIdsRead,
  4. List<String>? chapterIdsUnread,
})
inherited

Marks a chapter of a manga identified by it's mangaId as read or unread for user identified by their sessionToken.

chapterIdsRead should be an array of chapterIDs to be marked as READ and chapterIdsUnread should be an array of chapterIDs to be marked as UNREAD.

Implementation

Future<Result> markChapterReadOrUnRead(String mangaId, String sessionToken,
    {List<String>? chapterIdsRead, List<String>? chapterIdsUnread}) async {
  if ((chapterIdsRead != null && chapterIdsRead.isEmpty) &&
      (chapterIdsUnread != null && chapterIdsUnread.isEmpty)) {
    throw Exception(
        'Both chapterIdsRead and ChapterIdsUnread CANNOT be empty, atleast one list must be non-null and must have atleast one chapterID.');
  } else {
    chapterIdsRead = chapterIdsRead ?? [];
    chapterIdsUnread = chapterIdsUnread ?? [];
    var unencodedPath = '/manga/$mangaId/read';
    var body = jsonEncode({
      'chapterIdsRead': chapterIdsRead,
      'chapterIdsUnread': chapterIdsUnread,
    });
    final uri = 'https://$AUTHORITY$unencodedPath';
    var response = await http.post(
      Uri.parse(uri),
      headers: {
        HttpHeaders.contentTypeHeader: 'application/json',
        HttpHeaders.authorizationHeader: 'Bearer $sessionToken',
      },
      body: body,
    );
    try {
      return Result.fromJson(jsonDecode(response.body));
    } on Exception {
      throw MangadexServerException(jsonDecode(response.body));
    }
  }
}