Convert some classes to Kotlin

This commit is contained in:
len 2016-06-15 16:37:48 +02:00
parent b1d7167112
commit 5cfd5da338
11 changed files with 118 additions and 199 deletions

View file

@ -187,7 +187,7 @@ class DownloadManager(
private fun downloadChapter(download: Download): Observable<Download> {
DiskUtils.createDirectory(download.directory)
val pageListObservable = if (download.pages == null)
val pageListObservable: Observable<List<Page>> = 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 {

View file

@ -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)
}
}

View file

@ -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<Page> pages;
public File directory;
public transient volatile int totalProgress;
public transient volatile int downloadedImages;
private transient volatile int status;
private transient PublishSubject<Download> 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<Download> subject) {
this.statusSubject = subject;
}
private void notifyStatus() {
if (statusSubject != null)
statusSubject.onNext(this);
}
}

View file

@ -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<Page>? = 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<Download>? = null
fun setStatusSubject(subject: PublishSubject<Download>?) {
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
}
}

View file

@ -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<Manga> mangas;
public int page;
public String url;
public String nextPageUrl;
public MangasPage(int page) {
this.page = page;
}
}

View file

@ -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<Manga> = mutableListOf()
lateinit var url: String
var nextPageUrl: String? = null
}

View file

@ -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<Integer> 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<Integer> subject) {
this.statusSubject = subject;
}
public ReaderChapter getChapter() {
return chapter;
}
public void setChapter(ReaderChapter chapter) {
this.chapter = chapter;
}
}

View file

@ -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<Int>? = null
override fun update(bytesRead: Long, contentLength: Long, done: Boolean) {
progress = (100 * bytesRead / contentLength).toInt()
}
fun setStatusSubject(subject: PublishSubject<Int>?) {
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
}
}

View file

@ -84,10 +84,8 @@ abstract class OnlineSource(context: Context) : Source {
.newCall(popularMangaRequest(page))
.asObservable()
.map { response ->
page.apply {
mangas = mutableListOf<Manga>()
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<Manga>()
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<Page> {
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> {
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 }
}

View file

@ -220,7 +220,7 @@ class CataloguePresenter : BasePresenter<CatalogueFragment>() {
private fun getMangasPageObservable(page: Int): Observable<List<Manga>> {
val nextMangasPage = MangasPage(page)
if (page != 1) {
nextMangasPage.url = lastMangasPage!!.nextPageUrl
nextMangasPage.url = lastMangasPage!!.nextPageUrl!!
}
val observable = if (query.isEmpty())

View file

@ -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}"
}
}