private void initializeSubscriptions() { if (downloadsSubscription != null && !downloadsSubscription.isUnsubscribed()) downloadsSubscription.unsubscribe(); if (threadsNumberSubscription != null && !threadsNumberSubscription.isUnsubscribed()) threadsNumberSubscription.unsubscribe(); threadsNumberSubscription = preferences.downloadThreads().asObservable().subscribe(threadsNumber::onNext); downloadsSubscription = downloadsQueueSubject .lift(new DynamicConcurrentMergeOperator<>(this::downloadChapter, threadsNumber)) .onBackpressureBuffer() .observeOn(AndroidSchedulers.mainThread()) .map(download -> areAllDownloadsFinished()) .subscribe( finished -> { if (finished) { DownloadService.stop(context); } }, e -> Timber.e(e.getCause(), e.getMessage())); if (!isRunning) { isRunning = true; runningSubject.onNext(true); } }
// Get the absolute path to the chapter directory public File getAbsoluteChapterDirectory(Source source, Manga manga, Chapter chapter) { String chapterRelativePath = source.getName() + File.separator + manga.title.replaceAll("[^\\sa-zA-Z0-9.-]", "_") + File.separator + chapter.name.replaceAll("[^\\sa-zA-Z0-9.-]", "_") + " (" + chapter.id + ")"; return new File(preferences.getDownloadsDirectory(), chapterRelativePath); }