feat: 优化代码

pull/103/head
kunfei 5 years ago
parent d8fa05adfb
commit 87481f12a1
  1. 2
      app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt
  2. 10
      app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt
  3. 14
      app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt

@ -131,7 +131,7 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
exportRequestCode -> if (resultCode == Activity.RESULT_OK) { exportRequestCode -> if (resultCode == Activity.RESULT_OK) {
data?.data?.let { uri -> data?.data?.let { uri ->
adapter.getItem(exportPosition)?.let { adapter.getItem(exportPosition)?.let {
viewModel.export(it, uri) viewModel.export(uri, it)
} }
} }
} }

@ -7,6 +7,7 @@ import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
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
class DownloadAdapter(context: Context, private val callBack: CallBack) : class DownloadAdapter(context: Context, private val callBack: CallBack) :
@ -36,7 +37,14 @@ class DownloadAdapter(context: Context, private val callBack: CallBack) :
override fun registerListener(holder: ItemViewHolder) { override fun registerListener(holder: ItemViewHolder) {
holder.itemView.tv_export.onClick { holder.itemView.tv_export.onClick {
callBack.export(holder.layoutPosition) getItem(holder.layoutPosition)?.let {
val cacheSize = cacheChapters[it.bookUrl]?.size ?: 0
if (cacheSize < it.totalChapterNum) {
context.toast("未下载完成")
} else {
callBack.export(holder.layoutPosition)
}
}
} }
} }

@ -2,6 +2,8 @@ package io.legado.app.ui.download
import android.app.Application import android.app.Application
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
@ -9,9 +11,17 @@ import io.legado.app.data.entities.Book
class DownloadViewModel(application: Application) : BaseViewModel(application) { class DownloadViewModel(application: Application) : BaseViewModel(application) {
fun export(book: Book, uri: Uri) { fun export(uri: Uri, book: Book) {
execute { execute {
DocumentFile.fromTreeUri(context, uri)
?.createFile("txt", book.name)
?.let {
context.contentResolver.openOutputStream(it.uri)?.let {
App.db.bookChapterDao().getChapterList(book.bookUrl).forEach {
}
}
}
} }
} }

Loading…
Cancel
Save