feat: 优化代码

pull/123/head
kunfei 5 years ago
parent 82b7e6feb4
commit 7d3c7d23b3
  1. 48
      app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt
  2. 3
      app/src/main/java/io/legado/app/ui/filechooser/FilePicker.kt

@ -16,17 +16,15 @@ 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.help.permission.Permissions
import io.legado.app.help.permission.PermissionsCompat
import io.legado.app.service.help.Download
import io.legado.app.ui.filechooser.FileChooserDialog
import io.legado.app.ui.filechooser.FilePicker
import io.legado.app.utils.*
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.alert
import org.jetbrains.anko.toast
@ -126,44 +124,14 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
override fun export(position: Int) {
exportPosition = position
alert {
titleResource = R.string.select_folder
items(resources.getStringArray(R.array.select_folder).toList()) { _, index ->
when (index) {
0 -> {
val path = ACache.get(this@DownloadActivity).getAsString(exportBookPathKey)
if (path.isNullOrEmpty()) {
toast("没有默认路径")
} else {
startExport(path)
}
}
1 -> {
try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivityForResult(intent, exportRequestCode)
} catch (e: java.lang.Exception) {
e.printStackTrace()
toast(e.localizedMessage ?: "ERROR")
}
}
2 -> {
PermissionsCompat.Builder(this@DownloadActivity)
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted {
FileChooserDialog.show(
supportFragmentManager,
exportRequestCode,
mode = FileChooserDialog.DIRECTORY
)
}
.request()
}
}
FilePicker.selectFolder(this, exportRequestCode) {
val path = ACache.get(this@DownloadActivity).getAsString(exportBookPathKey)
if (path.isNullOrEmpty()) {
toast("没有默认路径")
} else {
startExport(path)
}
}.show()
}
}
private fun startExport(path: String) {

@ -1,6 +1,7 @@
package io.legado.app.ui.filechooser
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import io.legado.app.R
import io.legado.app.base.BaseActivity
@ -12,7 +13,7 @@ import org.jetbrains.anko.toast
object FilePicker {
fun selectFolder(activity: BaseActivity, requestCode: Int, default: (() -> Unit)? = null) {
fun selectFolder(activity: AppCompatActivity, requestCode: Int, default: (() -> Unit)? = null) {
activity.alert(titleResource = R.string.select_folder) {
val selectList =
activity.resources.getStringArray(R.array.select_folder).toMutableList()

Loading…
Cancel
Save