From 5cfd5da3389d2c8cd4c92f2ca33c53c84aab4ebd Mon Sep 17 00:00:00 2001 From: len Date: Wed, 15 Jun 2016 16:37:48 +0200 Subject: [PATCH] Convert some classes to Kotlin --- .../data/download/DownloadManager.kt | 6 +- .../data/download/DownloadNotifier.kt | 6 +- .../data/download/model/Download.java | 55 ----------- .../tachiyomi/data/download/model/Download.kt | 40 ++++++++ .../data/source/model/MangasPage.java | 18 ---- .../tachiyomi/data/source/model/MangasPage.kt | 13 +++ .../tachiyomi/data/source/model/Page.java | 99 ------------------- .../tachiyomi/data/source/model/Page.kt | 43 ++++++++ .../data/source/online/OnlineSource.kt | 26 ++--- .../ui/catalogue/CataloguePresenter.kt | 2 +- .../tachiyomi/ui/download/DownloadHolder.kt | 9 +- 11 files changed, 118 insertions(+), 199 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/source/model/MangasPage.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/source/model/MangasPage.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/source/model/Page.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/source/model/Page.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index 9bff95683..ce5968bdf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -187,7 +187,7 @@ class DownloadManager( private fun downloadChapter(download: Download): Observable { DiskUtils.createDirectory(download.directory) - val pageListObservable = if (download.pages == null) + val pageListObservable: Observable> = if (download.pages == null) // Pull page list from network and add them to download object download.source.fetchPageListFromNetwork(download.chapter) .doOnNext { pages -> @@ -324,7 +324,7 @@ class DownloadManager( var actualProgress = 0 var status = Download.DOWNLOADED // If any page has an error, the download result will be error - for (page in download.pages) { + for (page in download.pages!!) { actualProgress += page.progress if (page.status != Page.READY) { status = Download.ERROR @@ -377,7 +377,7 @@ class DownloadManager( // Shortcut for the method above private fun savePageList(download: Download) { - savePageList(download.source, download.manga, download.chapter, download.pages) + savePageList(download.source, download.manga, download.chapter, download.pages!!) } fun getAbsoluteMangaDirectory(source: Source, manga: Manga): File { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt index 8b90b0871..01f3a7b7f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt @@ -79,7 +79,7 @@ class DownloadNotifier(private val context: Context) { return } } else { - if (download != null && download.pages.size == download.downloadedImages) { + if (download != null && download.pages!!.size == download.downloadedImages) { onComplete(download) return } @@ -107,8 +107,8 @@ class DownloadNotifier(private val context: Context) { setContentTitle(it.chapter.name) setContentText(context.getString(R.string.chapter_downloading_progress) - .format(it.downloadedImages, it.pages.size)) - setProgress(it.pages.size, it.downloadedImages, false) + .format(it.downloadedImages, it.pages!!.size)) + setProgress(it.pages!!.size, it.downloadedImages, false) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.java b/app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.java deleted file mode 100644 index 65409969d..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.java +++ /dev/null @@ -1,55 +0,0 @@ -package eu.kanade.tachiyomi.data.download.model; - -import java.io.File; -import java.util.List; - -import eu.kanade.tachiyomi.data.database.models.Chapter; -import eu.kanade.tachiyomi.data.database.models.Manga; -import eu.kanade.tachiyomi.data.source.model.Page; -import eu.kanade.tachiyomi.data.source.online.OnlineSource; -import rx.subjects.PublishSubject; - -public class Download { - public OnlineSource source; - public Manga manga; - public Chapter chapter; - public List pages; - public File directory; - - public transient volatile int totalProgress; - public transient volatile int downloadedImages; - private transient volatile int status; - - private transient PublishSubject statusSubject; - - public static final int NOT_DOWNLOADED = 0; - public static final int QUEUE = 1; - public static final int DOWNLOADING = 2; - public static final int DOWNLOADED = 3; - public static final int ERROR = 4; - - - public Download(OnlineSource source, Manga manga, Chapter chapter) { - this.source = source; - this.manga = manga; - this.chapter = chapter; - } - - public int getStatus() { - return status; - } - - public void setStatus(int status) { - this.status = status; - notifyStatus(); - } - - public void setStatusSubject(PublishSubject subject) { - this.statusSubject = subject; - } - - private void notifyStatus() { - if (statusSubject != null) - statusSubject.onNext(this); - } -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.kt new file mode 100644 index 000000000..e283d93f9 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.kt @@ -0,0 +1,40 @@ +package eu.kanade.tachiyomi.data.download.model + +import eu.kanade.tachiyomi.data.database.models.Chapter +import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.source.model.Page +import eu.kanade.tachiyomi.data.source.online.OnlineSource +import rx.subjects.PublishSubject +import java.io.File + +class Download(val source: OnlineSource, val manga: Manga, val chapter: Chapter) { + + lateinit var directory: File + + var pages: List? = null + + @Volatile @Transient var totalProgress: Int = 0 + + @Volatile @Transient var downloadedImages: Int = 0 + + @Volatile @Transient var status: Int = 0 + set(status) { + field = status + statusSubject?.onNext(this) + } + + @Transient private var statusSubject: PublishSubject? = null + + fun setStatusSubject(subject: PublishSubject?) { + statusSubject = subject + } + + companion object { + + const val NOT_DOWNLOADED = 0 + const val QUEUE = 1 + const val DOWNLOADING = 2 + const val DOWNLOADED = 3 + const val ERROR = 4 + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/source/model/MangasPage.java b/app/src/main/java/eu/kanade/tachiyomi/data/source/model/MangasPage.java deleted file mode 100644 index 7ca1cd831..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/source/model/MangasPage.java +++ /dev/null @@ -1,18 +0,0 @@ -package eu.kanade.tachiyomi.data.source.model; - -import java.util.List; - -import eu.kanade.tachiyomi.data.database.models.Manga; - -public class MangasPage { - - public List mangas; - public int page; - public String url; - public String nextPageUrl; - - public MangasPage(int page) { - this.page = page; - } - -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/source/model/MangasPage.kt b/app/src/main/java/eu/kanade/tachiyomi/data/source/model/MangasPage.kt new file mode 100644 index 000000000..78b9aa054 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/source/model/MangasPage.kt @@ -0,0 +1,13 @@ +package eu.kanade.tachiyomi.data.source.model + +import eu.kanade.tachiyomi.data.database.models.Manga + +class MangasPage(val page: Int) { + + val mangas: MutableList = mutableListOf() + + lateinit var url: String + + var nextPageUrl: String? = null + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/source/model/Page.java b/app/src/main/java/eu/kanade/tachiyomi/data/source/model/Page.java deleted file mode 100644 index 898a7e7a8..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/source/model/Page.java +++ /dev/null @@ -1,99 +0,0 @@ -package eu.kanade.tachiyomi.data.source.model; - -import eu.kanade.tachiyomi.data.network.ProgressListener; -import eu.kanade.tachiyomi.ui.reader.ReaderChapter; -import rx.subjects.PublishSubject; - -public class Page implements ProgressListener { - - private int pageNumber; - private String url; - private String imageUrl; - private transient ReaderChapter chapter; - private transient String imagePath; - private transient volatile int status; - private transient volatile int progress; - - private transient PublishSubject statusSubject; - - public static final int QUEUE = 0; - public static final int LOAD_PAGE = 1; - public static final int DOWNLOAD_IMAGE = 2; - public static final int READY = 3; - public static final int ERROR = 4; - - public Page(int pageNumber, String url) { - this(pageNumber, url, null, null); - } - - public Page(int pageNumber, String url, String imageUrl) { - this(pageNumber, url, imageUrl, null); - } - - public Page(int pageNumber, String url, String imageUrl, String imagePath) { - this.pageNumber = pageNumber; - this.url = url; - this.imageUrl = imageUrl; - this.imagePath = imagePath; - } - - public int getPageNumber() { - return pageNumber; - } - - public String getUrl() { - return url; - } - - public String getImageUrl() { - return imageUrl; - } - - public void setImageUrl(String imageUrl) { - this.imageUrl = imageUrl; - } - - public String getImagePath() { - return imagePath; - } - - public void setImagePath(String imagePath) { - this.imagePath = imagePath; - } - - public int getStatus() { - return status; - } - - public void setStatus(int status) { - this.status = status; - if (statusSubject != null) - statusSubject.onNext(status); - } - - public int getProgress() { - return progress; - } - - public void setProgress(int value) { - progress = value; - } - - @Override - public void update(long bytesRead, long contentLength, boolean done) { - progress = (int) ((100 * bytesRead) / contentLength); - } - - public void setStatusSubject(PublishSubject subject) { - this.statusSubject = subject; - } - - public ReaderChapter getChapter() { - return chapter; - } - - public void setChapter(ReaderChapter chapter) { - this.chapter = chapter; - } - -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/source/model/Page.kt b/app/src/main/java/eu/kanade/tachiyomi/data/source/model/Page.kt new file mode 100644 index 000000000..89f19566b --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/source/model/Page.kt @@ -0,0 +1,43 @@ +package eu.kanade.tachiyomi.data.source.model + +import eu.kanade.tachiyomi.data.network.ProgressListener +import eu.kanade.tachiyomi.ui.reader.ReaderChapter +import rx.subjects.PublishSubject + +class Page( + val pageNumber: Int, + val url: String, + var imageUrl: String? = null, + @Transient var imagePath: String? = null +) : ProgressListener { + + @Transient lateinit var chapter: ReaderChapter + + @Transient @Volatile var status: Int = 0 + set(value) { + field = value + statusSubject?.onNext(value) + } + + @Transient @Volatile var progress: Int = 0 + + @Transient private var statusSubject: PublishSubject? = null + + override fun update(bytesRead: Long, contentLength: Long, done: Boolean) { + progress = (100 * bytesRead / contentLength).toInt() + } + + fun setStatusSubject(subject: PublishSubject?) { + this.statusSubject = subject + } + + companion object { + + const val QUEUE = 0 + const val LOAD_PAGE = 1 + const val DOWNLOAD_IMAGE = 2 + const val READY = 3 + const val ERROR = 4 + } + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt b/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt index 6d286e526..746666cf0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt @@ -84,10 +84,8 @@ abstract class OnlineSource(context: Context) : Source { .newCall(popularMangaRequest(page)) .asObservable() .map { response -> - page.apply { - mangas = mutableListOf() - popularMangaParse(response, this) - } + popularMangaParse(response, page) + page } /** @@ -129,10 +127,8 @@ abstract class OnlineSource(context: Context) : Source { .newCall(searchMangaRequest(page, query)) .asObservable() .map { response -> - page.apply { - mangas = mutableListOf() - searchMangaParse(response, this, query) - } + searchMangaParse(response, page, query) + page } /** @@ -358,7 +354,7 @@ abstract class OnlineSource(context: Context) : Source { * @param page the chapter whose page list has to be fetched */ open protected fun imageRequest(page: Page): Request { - return GET(page.imageUrl, headers) + return GET(page.imageUrl!!, headers) } /** @@ -368,20 +364,18 @@ abstract class OnlineSource(context: Context) : Source { * @param page the page. */ fun getCachedImage(page: Page): Observable { - val pageObservable = Observable.just(page) - if (page.imageUrl.isNullOrEmpty()) - return pageObservable + val imageUrl = page.imageUrl ?: return Observable.just(page) - return pageObservable + return Observable.just(page) .flatMap { - if (!chapterCache.isImageInCache(page.imageUrl)) { + if (!chapterCache.isImageInCache(imageUrl)) { cacheImage(page) } else { Observable.just(page) } } .doOnNext { - page.imagePath = chapterCache.getImagePath(page.imageUrl) + page.imagePath = chapterCache.getImagePath(imageUrl) page.status = Page.READY } .doOnError { page.status = Page.ERROR } @@ -396,7 +390,7 @@ abstract class OnlineSource(context: Context) : Source { private fun cacheImage(page: Page): Observable { page.status = Page.DOWNLOAD_IMAGE return imageResponse(page) - .doOnNext { chapterCache.putImageToCache(page.imageUrl, it, preferences.reencodeImage()) } + .doOnNext { chapterCache.putImageToCache(page.imageUrl!!, it, preferences.reencodeImage()) } .map { page } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt index a22c95edf..6d1b9426a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.kt @@ -220,7 +220,7 @@ class CataloguePresenter : BasePresenter() { private fun getMangasPageObservable(page: Int): Observable> { val nextMangasPage = MangasPage(page) if (page != 1) { - nextMangasPage.url = lastMangasPage!!.nextPageUrl + nextMangasPage.url = lastMangasPage!!.nextPageUrl!! } val observable = if (query.isEmpty()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadHolder.kt index 5022e378a..9d7ab3dff 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadHolder.kt @@ -32,12 +32,13 @@ class DownloadHolder(private val view: View) : RecyclerView.ViewHolder(view) { view.manga_title.text = download.manga.title // Update the progress bar and the number of downloaded pages - if (download.pages == null) { + val pages = download.pages + if (pages == null) { view.download_progress.progress = 0 view.download_progress.max = 1 view.download_progress_text.text = "" } else { - view.download_progress.max = download.pages.size * 100 + view.download_progress.max = pages.size * 100 notifyProgress() notifyDownloadedPages() } @@ -48,7 +49,7 @@ class DownloadHolder(private val view: View) : RecyclerView.ViewHolder(view) { */ fun notifyProgress() { if (view.download_progress.max == 1) { - view.download_progress.max = download.pages.size * 100 + view.download_progress.max = download.pages!!.size * 100 } view.download_progress.progress = download.totalProgress } @@ -57,7 +58,7 @@ class DownloadHolder(private val view: View) : RecyclerView.ViewHolder(view) { * Updates the text field of the number of downloaded pages. */ fun notifyDownloadedPages() { - view.download_progress_text.text = "${download.downloadedImages}/${download.pages.size}" + view.download_progress_text.text = "${download.downloadedImages}/${download.pages!!.size}" } }