use of io.objectbox.relation.ToOne in project Hentoid by avluis.
the class BaseWebActivity method processDownload.
/**
* Add current content (i.e. content of the currently viewed book) to the download queue
*
* @param quickDownload True if the action has been triggered by a quick download
* (which means we're not on a book gallery page but on the book list page)
*/
void processDownload(boolean quickDownload, boolean isDownloadPlus) {
if (null == currentContent)
return;
if (currentContent.getId() > 0)
currentContent = dao.selectContent(currentContent.getId());
if (null == currentContent)
return;
if (!isDownloadPlus && StatusContent.DOWNLOADED == currentContent.getStatus()) {
ToastHelper.toast(R.string.already_downloaded);
if (!quickDownload)
setActionMode(ActionMode.READ);
return;
}
if (isDownloadPlus) {
// Copy the _current_ content's download params to the extra images
String downloadParamsStr = currentContent.getDownloadParams();
if (downloadParamsStr != null && downloadParamsStr.length() > 2) {
for (ImageFile i : extraImages) i.setDownloadParams(downloadParamsStr);
}
// Determine base book : browsed downloaded book or best duplicate ?
if (!ContentHelper.isInLibrary(currentContent.getStatus()) && duplicateId > 0) {
currentContent = dao.selectContent(duplicateId);
if (null == currentContent)
return;
}
// Append additional pages & chapters to the base book's list of pages & chapters
// Entire image set to update
List<ImageFile> updatedImgs = new ArrayList<>();
// URLs of known images
Set<String> existingImageUrls = new HashSet<>();
// Positions of known chapters
Set<Integer> existingChapterOrders = new HashSet<>();
if (currentContent.getImageFiles() != null) {
existingImageUrls.addAll(Stream.of(currentContent.getImageFiles()).map(ImageFile::getUrl).toList());
existingChapterOrders.addAll(Stream.of(currentContent.getImageFiles()).map(i -> {
if (null == i.getChapter())
return -1;
if (null == i.getChapter().getTarget())
return -1;
return i.getChapter().getTarget().getOrder();
}).toList());
updatedImgs.addAll(currentContent.getImageFiles());
}
// Save additional pages references to stored book, without duplicate URLs
List<ImageFile> additionalNonExistingImages = Stream.of(extraImages).filterNot(i -> existingImageUrls.contains(i.getUrl())).toList();
if (!additionalNonExistingImages.isEmpty()) {
updatedImgs.addAll(additionalNonExistingImages);
currentContent.setImageFiles(updatedImgs);
}
// Save additional chapters to stored book
List<Chapter> additionalNonExistingChapters = Stream.of(additionalNonExistingImages).map(ImageFile::getChapter).withoutNulls().map(ToOne::getTarget).withoutNulls().filterNot(c -> existingChapterOrders.contains(c.getOrder())).toList();
if (!additionalNonExistingChapters.isEmpty()) {
List<Chapter> updatedChapters;
if (currentContent.getChapters() != null)
updatedChapters = new ArrayList<>(currentContent.getChapters());
else
updatedChapters = new ArrayList<>();
updatedChapters.addAll(additionalNonExistingChapters);
currentContent.setChapters(updatedChapters);
}
currentContent.setStatus(StatusContent.SAVED);
dao.insertContent(currentContent);
}
// Check if the tag blocker applies here
List<String> blockedTagsLocal = ContentHelper.getBlockedTags(currentContent);
if (!blockedTagsLocal.isEmpty()) {
if (Preferences.getTagBlockingBehaviour() == Preferences.Constant.DL_TAG_BLOCKING_BEHAVIOUR_DONT_QUEUE) {
// Stop right here
ToastHelper.toast(R.string.blocked_tag, blockedTagsLocal.get(0));
} else {
// Insert directly as an error
List<ErrorRecord> errors = new ArrayList<>();
errors.add(new ErrorRecord(ErrorType.BLOCKED, currentContent.getUrl(), "tags", "blocked tags : " + TextUtils.join(", ", blockedTagsLocal), Instant.now()));
currentContent.setErrorLog(errors);
currentContent.setStatus(StatusContent.ERROR);
dao.insertContent(currentContent);
ToastHelper.toast(R.string.blocked_tag_queued, blockedTagsLocal.get(0));
setActionMode(ActionMode.VIEW_QUEUE);
}
return;
}
// No reason to block or ignore -> actually add to the queue
if (Preferences.getQueueNewDownloadPosition() == QUEUE_NEW_DOWNLOADS_POSITION_ASK)
AddQueueMenu.show(this, webView, this, (position, item) -> addToQueue((0 == position) ? QUEUE_NEW_DOWNLOADS_POSITION_TOP : QUEUE_NEW_DOWNLOADS_POSITION_BOTTOM, Preferences.getBrowserDlAction()));
else
addToQueue(Preferences.getQueueNewDownloadPosition(), Preferences.getBrowserDlAction());
}
Aggregations