feat: 优化代码

pull/105/head
kunfei 5 years ago
parent 9d869ce50e
commit 187c2c17b7
  1. 2
      app/src/main/java/io/legado/app/service/DownloadService.kt
  2. 37
      app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt
  3. 19
      app/src/main/res/layout/item_download.xml

@ -67,6 +67,8 @@ class DownloadService : BaseService() {
tasks.clear() tasks.clear()
searchPool.close() searchPool.close()
handler.removeCallbacks(runnable) handler.removeCallbacks(runnable)
downloadMap.clear()
finalMap.clear()
super.onDestroy() super.onDestroy()
postEvent(EventBus.UP_DOWNLOAD, downloadMap) postEvent(EventBus.UP_DOWNLOAD, downloadMap)
} }

@ -1,11 +1,13 @@
package io.legado.app.ui.download package io.legado.app.ui.download
import android.content.Context import android.content.Context
import android.widget.ImageView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.service.help.Download
import kotlinx.android.synthetic.main.item_download.view.* import kotlinx.android.synthetic.main.item_download.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
@ -29,27 +31,48 @@ class DownloadAdapter(context: Context, private val callBack: CallBack) :
tv_download.text = tv_download.text =
context.getString(R.string.download_count, cs.size, item.totalChapterNum) context.getString(R.string.download_count, cs.size, item.totalChapterNum)
} }
upDownloadIv(iv_download, item)
} else { } else {
val cacheSize = cacheChapters[item.bookUrl]?.size ?: 0 val cacheSize = cacheChapters[item.bookUrl]?.size ?: 0
tv_download.text = tv_download.text =
context.getString(R.string.download_count, cacheSize, item.totalChapterNum) context.getString(R.string.download_count, cacheSize, item.totalChapterNum)
upDownloadIv(iv_download, item)
} }
} }
} }
override fun registerListener(holder: ItemViewHolder) { override fun registerListener(holder: ItemViewHolder) {
holder.itemView.tv_export.onClick { holder.itemView.apply {
getItem(holder.layoutPosition)?.let { iv_download.onClick {
val cacheSize = cacheChapters[it.bookUrl]?.size ?: 0 getItem(holder.layoutPosition)?.let {
if (cacheSize < it.totalChapterNum) { Download.start(context, it.bookUrl, 0, it.totalChapterNum)
context.toast("未下载完成") }
} else { }
callBack.export(holder.layoutPosition) tv_export.onClick {
getItem(holder.layoutPosition)?.let {
val cacheSize = cacheChapters[it.bookUrl]?.size ?: 0
if (cacheSize < it.totalChapterNum) {
context.toast("未下载完成")
} else {
callBack.export(holder.layoutPosition)
}
} }
} }
} }
} }
private fun upDownloadIv(iv: ImageView, book: Book) {
downloadMap?.let {
if (it.containsKey(book.bookUrl)) {
iv.setImageResource(R.drawable.ic_stop_black_24dp)
} else {
iv.setImageResource(R.drawable.ic_play_24dp)
}
} ?: let {
iv.setImageResource(R.drawable.ic_play_24dp)
}
}
interface CallBack { interface CallBack {
fun export(position: Int) fun export(position: Int)
} }

@ -10,7 +10,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:singleLine="true" android:singleLine="true"
app:layout_constraintRight_toLeftOf="@+id/tv_export" app:layout_constraintRight_toLeftOf="@+id/iv_download"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent" /> app:layout_constraintLeft_toLeftOf="parent" />
@ -19,7 +19,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:singleLine="true" android:singleLine="true"
app:layout_constraintRight_toLeftOf="@+id/tv_export" app:layout_constraintRight_toLeftOf="@+id/iv_download"
app:layout_constraintTop_toBottomOf="@+id/tv_name" app:layout_constraintTop_toBottomOf="@+id/tv_name"
app:layout_constraintLeft_toLeftOf="parent" /> app:layout_constraintLeft_toLeftOf="parent" />
@ -28,10 +28,23 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:singleLine="true" android:singleLine="true"
app:layout_constraintRight_toLeftOf="@+id/tv_export" app:layout_constraintRight_toLeftOf="@+id/iv_download"
app:layout_constraintTop_toBottomOf="@id/tv_author" app:layout_constraintTop_toBottomOf="@id/tv_author"
app:layout_constraintLeft_toLeftOf="parent" /> app:layout_constraintLeft_toLeftOf="parent" />
<ImageView
android:id="@+id/iv_download"
android:layout_width="36dp"
android:layout_height="36dp"
android:padding="6dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:tint="@color/tv_text_default"
android:src="@drawable/ic_play_24dp"
android:contentDescription="@string/start"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="@+id/tv_export" />
<TextView <TextView
android:id="@+id/tv_export" android:id="@+id/tv_export"
android:layout_width="wrap_content" android:layout_width="wrap_content"

Loading…
Cancel
Save