From 6196480d1d486bca233c8de41f00a20e83f18d6a Mon Sep 17 00:00:00 2001 From: len Date: Fri, 3 Jun 2016 18:25:47 +0200 Subject: [PATCH] Minor improvements --- .../data/network/CloudflareInterceptor.kt | 14 ++--- .../tachiyomi/ui/download/DownloadFragment.kt | 54 +++++++++---------- .../ui/manga/chapter/ChaptersFragment.kt | 38 +++++-------- .../ui/manga/info/MangaInfoFragment.kt | 4 +- .../tachiyomi/ui/reader/ReaderActivity.kt | 25 ++++----- .../eu/kanade/tachiyomi/util/RxExtensions.kt | 8 +++ 6 files changed, 69 insertions(+), 74 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/RxExtensions.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/network/CloudflareInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/data/network/CloudflareInterceptor.kt index 26dd74f1ae..23b9111728 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/network/CloudflareInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/network/CloudflareInterceptor.kt @@ -1,7 +1,7 @@ package eu.kanade.tachiyomi.data.network -import android.net.Uri import com.squareup.duktape.Duktape +import okhttp3.HttpUrl import okhttp3.Interceptor import okhttp3.Request import okhttp3.Response @@ -42,8 +42,8 @@ class CloudflareInterceptor(private val cookies: PersistentCookieStore) : Interc val domain = originalRequest.url().host() val content = response.body().string() - // CloudFlare requires waiting 5 seconds before resolving the challenge - Thread.sleep(5000) + // CloudFlare requires waiting 4 seconds before resolving the challenge + Thread.sleep(4000) val operation = operationPattern.find(content)?.groups?.get(1)?.value val challenge = challengePattern.find(content)?.groups?.get(1)?.value @@ -65,10 +65,10 @@ class CloudflareInterceptor(private val cookies: PersistentCookieStore) : Interc val answer = "${result + domain.length}" - val url = Uri.parse("http://$domain/cdn-cgi/l/chk_jschl").buildUpon() - .appendQueryParameter("jschl_vc", challenge) - .appendQueryParameter("pass", pass) - .appendQueryParameter("jschl_answer", answer) + val url = HttpUrl.parse("http://$domain/cdn-cgi/l/chk_jschl").newBuilder() + .addQueryParameter("jschl_vc", challenge) + .addQueryParameter("pass", pass) + .addQueryParameter("jschl_answer", answer) .toString() val referer = originalRequest.url().toString() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadFragment.kt index ff0a0738e4..a3846dac6b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadFragment.kt @@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.data.download.DownloadService import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment import eu.kanade.tachiyomi.ui.main.MainActivity +import eu.kanade.tachiyomi.util.plusAssign import eu.kanade.tachiyomi.widget.NpaLinearLayoutManager import kotlinx.android.synthetic.main.fragment_download_queue.* import nucleus.factory.RequiresPresenter @@ -46,9 +47,9 @@ class DownloadFragment : BaseRxFragment() { private var clearButton: MenuItem? = null /** - * Subscription list to be cleared during [onPause]. + * Subscription list to be cleared during [onDestroyView]. */ - private val resumeSubscriptions by lazy { CompositeSubscription() } + private val subscriptions by lazy { CompositeSubscription() } /** * Map of subscriptions for active downloads. @@ -94,6 +95,28 @@ class DownloadFragment : BaseRxFragment() { // Set the layout manager for the recycler and fixed size. recycler.layoutManager = NpaLinearLayoutManager(activity) recycler.setHasFixedSize(true) + + // Suscribe to changes + subscriptions += presenter.downloadManager.runningSubject + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { onQueueStatusChange(it) } + + subscriptions += presenter.getStatusObservable() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { onStatusChange(it) } + + subscriptions += presenter.getProgressObservable() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { onUpdateDownloadedPages(it) } + } + + override fun onDestroyView() { + for (subscription in progressSubscriptions.values) { + subscription.unsubscribe() + } + progressSubscriptions.clear() + subscriptions.clear() + super.onDestroyView() } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { @@ -131,33 +154,6 @@ class DownloadFragment : BaseRxFragment() { return true } - override fun onResume() { - super.onResume() - presenter.downloadManager.runningSubject - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { onQueueStatusChange(it) } - .apply { resumeSubscriptions.add(this) } - - presenter.getStatusObservable() - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { onStatusChange(it) } - .apply { resumeSubscriptions.add(this) } - - presenter.getProgressObservable() - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { onUpdateDownloadedPages(it) } - .apply { resumeSubscriptions.add(this) } - } - - override fun onPause() { - for (subscription in progressSubscriptions.values) { - subscription.unsubscribe() - } - progressSubscriptions.clear() - resumeSubscriptions.clear() - super.onPause() - } - /** * Called when the status of a download changes. * diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt index fb7329cf73..2a053034d0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersFragment.kt @@ -240,37 +240,27 @@ class ChaptersFragment : BaseRxFragment(), ActionMode.Callbac .negativeText(android.R.string.cancel) .items(modes.map { getString(it) }) .itemsCallback { dialog, view, i, charSequence -> - var chapters: MutableList = arrayListOf() + + fun getUnreadChaptersSorted() = presenter.chapters + .filter { !it.read && !it.isDownloaded } + .sortedByDescending { it.source_order } // i = 0: Download 1 // i = 1: Download 5 // i = 2: Download 10 // i = 3: Download unread // i = 4: Download all - for (chapter in presenter.chapters) { - if (!chapter.isDownloaded) { - if (i == 4 || (i != 4 && !chapter.read)) { - chapters.add(chapter) - } - } + val chaptersToDownload = when (i) { + 0 -> getUnreadChaptersSorted().take(1) + 1 -> getUnreadChaptersSorted().take(5) + 2 -> getUnreadChaptersSorted().take(10) + 3 -> presenter.chapters.filter { !it.read } + 4 -> presenter.chapters + else -> emptyList() } - if (chapters.size > 0) { - if (!presenter.sortDescending()) { - chapters.reverse() - } - when (i) { - // Set correct chapters size if desired - 0 -> chapters = chapters.subList(0, 1) - 1 -> { - if (chapters.size >= 5) - chapters = chapters.subList(0, 5) - } - 2 -> { - if (chapters.size >= 10) - chapters = chapters.subList(0, 10) - } - } - downloadChapters(chapters) + + if (chaptersToDownload.isNotEmpty()) { + downloadChapters(chaptersToDownload) } } .show() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoFragment.kt index ffdda1949a..86c8005dbd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoFragment.kt @@ -114,13 +114,13 @@ class MangaInfoFragment : BaseRxFragment() { // Set cover if it wasn't already. if (manga_cover.drawable == null && !manga.thumbnail_url.isNullOrEmpty()) { - Glide.with(context) + Glide.with(this) .load(manga) .diskCacheStrategy(DiskCacheStrategy.RESULT) .centerCrop() .into(manga_cover) - Glide.with(context) + Glide.with(this) .load(manga) .diskCacheStrategy(DiskCacheStrategy.RESULT) .centerCrop() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index 15f6e72050..c59bc313b4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -29,6 +29,7 @@ import eu.kanade.tachiyomi.ui.reader.viewer.pager.vertical.VerticalReader import eu.kanade.tachiyomi.ui.reader.viewer.webtoon.WebtoonReader import eu.kanade.tachiyomi.util.GLUtil import eu.kanade.tachiyomi.util.SharedData +import eu.kanade.tachiyomi.util.plusAssign import eu.kanade.tachiyomi.util.toast import eu.kanade.tachiyomi.widget.SimpleAnimationListener import eu.kanade.tachiyomi.widget.SimpleSeekBarListener @@ -451,10 +452,10 @@ class ReaderActivity : BaseRxActivity() { } private fun initializeSettings() { - subscriptions.add(preferences.showPageNumber().asObservable() - .subscribe { setPageNumberVisibility(it) }) + subscriptions += preferences.showPageNumber().asObservable() + .subscribe { setPageNumberVisibility(it) } - subscriptions.add(preferences.rotation().asObservable() + subscriptions += preferences.rotation().asObservable() .subscribe { setRotation(it) @@ -468,20 +469,20 @@ class ReaderActivity : BaseRxActivity() { R.drawable.ic_screen_lock_landscape_white_24dp lock_orientation.setImageResource(resourceId) - }) + } - subscriptions.add(preferences.hideStatusBar().asObservable() - .subscribe { setStatusBarVisibility(it) }) + subscriptions += preferences.hideStatusBar().asObservable() + .subscribe { setStatusBarVisibility(it) } - subscriptions.add(preferences.keepScreenOn().asObservable() - .subscribe { setKeepScreenOn(it) }) + subscriptions += preferences.keepScreenOn().asObservable() + .subscribe { setKeepScreenOn(it) } - subscriptions.add(preferences.customBrightness().asObservable() - .subscribe { setCustomBrightness(it) }) + subscriptions += preferences.customBrightness().asObservable() + .subscribe { setCustomBrightness(it) } - subscriptions.add(preferences.readerTheme().asObservable() + subscriptions += preferences.readerTheme().asObservable() .distinctUntilChanged() - .subscribe { applyTheme(it) }) + .subscribe { applyTheme(it) } } private fun setRotation(rotation: Int) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/RxExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/RxExtensions.kt new file mode 100644 index 0000000000..9561dfe69d --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/RxExtensions.kt @@ -0,0 +1,8 @@ +package eu.kanade.tachiyomi.util + +import rx.Subscription +import rx.subscriptions.CompositeSubscription + +fun Subscription?.isNullOrUnsubscribed() = this == null || isUnsubscribed + +operator fun CompositeSubscription.plusAssign(subscription: Subscription) = add(subscription) \ No newline at end of file