feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 68ee7bae76
commit d8fa05adfb
  1. 42
      app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt
  2. 8
      app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt
  3. 19
      app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt

@ -1,5 +1,7 @@
package io.legado.app.ui.download
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
@ -8,26 +10,33 @@ import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseActivity
import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.EventBus
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
import io.legado.app.service.help.Download
import io.legado.app.utils.applyTint
import io.legado.app.utils.getViewModel
import io.legado.app.utils.observeEvent
import kotlinx.android.synthetic.main.activity_download.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast
class DownloadActivity : BaseActivity(R.layout.activity_download) {
class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_download),
DownloadAdapter.CallBack {
private val exportRequestCode = 32
lateinit var adapter: DownloadAdapter
private var bookshelfLiveData: LiveData<List<Book>>? = null
private var menu: Menu? = null
private var exportPosition = -1
override val viewModel: DownloadViewModel
get() = getViewModel(DownloadViewModel::class.java)
override fun onActivityCreated(savedInstanceState: Bundle?) {
initRecyclerView()
@ -58,7 +67,7 @@ class DownloadActivity : BaseActivity(R.layout.activity_download) {
private fun initRecyclerView() {
recycler_view.layoutManager = LinearLayoutManager(this)
adapter = DownloadAdapter(this)
adapter = DownloadAdapter(this, this)
recycler_view.adapter = adapter
}
@ -104,4 +113,29 @@ class DownloadActivity : BaseActivity(R.layout.activity_download) {
adapter.cacheChapters[it.bookUrl]?.add(it.url)
}
}
override fun export(position: Int) {
exportPosition = position
try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivityForResult(intent, exportRequestCode)
} catch (e: Exception) {
toast("选择文件夹出错")
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
exportRequestCode -> if (resultCode == Activity.RESULT_OK) {
data?.data?.let { uri ->
adapter.getItem(exportPosition)?.let {
viewModel.export(it, uri)
}
}
}
}
}
}

@ -9,7 +9,7 @@ import kotlinx.android.synthetic.main.item_download.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
class DownloadAdapter(context: Context) :
class DownloadAdapter(context: Context, private val callBack: CallBack) :
SimpleRecyclerAdapter<Book>(context, R.layout.item_download) {
val cacheChapters = hashMapOf<String, HashSet<String>>()
@ -36,7 +36,11 @@ class DownloadAdapter(context: Context) :
override fun registerListener(holder: ItemViewHolder) {
holder.itemView.tv_export.onClick {
callBack.export(holder.layoutPosition)
}
}
interface CallBack {
fun export(position: Int)
}
}

@ -0,0 +1,19 @@
package io.legado.app.ui.download
import android.app.Application
import android.net.Uri
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book
class DownloadViewModel(application: Application) : BaseViewModel(application) {
fun export(book: Book, uri: Uri) {
execute {
}
}
}
Loading…
Cancel
Save