From d8fa05adfb2f40e84d7c829f77b64ec39aede39e Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 16 Feb 2020 19:03:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/download/DownloadActivity.kt | 42 +++++++++++++++++-- .../legado/app/ui/download/DownloadAdapter.kt | 8 +++- .../app/ui/download/DownloadViewModel.kt | 19 +++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt diff --git a/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt b/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt index 04c185989..0275873d4 100644 --- a/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt +++ b/app/src/main/java/io/legado/app/ui/download/DownloadActivity.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(R.layout.activity_download), + DownloadAdapter.CallBack { + private val exportRequestCode = 32 lateinit var adapter: DownloadAdapter private var bookshelfLiveData: LiveData>? = 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) + } + } + } + + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt b/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt index 2015c614a..6dbbdfa78 100644 --- a/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/download/DownloadAdapter.kt @@ -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(context, R.layout.item_download) { val cacheChapters = hashMapOf>() @@ -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) + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt b/app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt new file mode 100644 index 000000000..0ce1eecf0 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt @@ -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 { + + } + } + + +} \ No newline at end of file