feat: 优化代码

pull/105/head
kunfei 5 years ago
parent c2eaa3e34b
commit 99bbdb558d
  1. 28
      app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt
  2. 6
      app/src/main/java/io/legado/app/ui/download/DownloadViewModel.kt
  3. 1
      app/src/main/res/values/strings.xml

@ -8,6 +8,7 @@ import android.view.MenuItem
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
@ -19,10 +20,7 @@ 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.utils.ACache
import io.legado.app.utils.applyTint
import io.legado.app.utils.getViewModel
import io.legado.app.utils.observeEvent
import io.legado.app.utils.*
import kotlinx.android.synthetic.main.activity_download.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
@ -137,9 +135,7 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
if (path.isNullOrEmpty()) {
toast("没有默认路径")
} else {
adapter.getItem(exportPosition)?.let {
viewModel.export(path, it)
}
startExport(path)
}
}
1 -> {
@ -170,13 +166,21 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
}.show()
}
private fun startExport(path: String) {
adapter.getItem(exportPosition)?.let { book ->
Snackbar.make(title_bar, R.string.exporting, Snackbar.LENGTH_INDEFINITE)
.show()
viewModel.export(path, book) {
title_bar.snackbar(it)
}
}
}
override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) {
exportRequestCode -> {
ACache.get(this@DownloadActivity).put(exportBookPathKey, currentPath)
adapter.getItem(exportPosition)?.let {
viewModel.export(currentPath, it)
}
startExport(currentPath)
}
}
}
@ -191,9 +195,7 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
ACache.get(this@DownloadActivity).put(exportBookPathKey, uri.toString())
adapter.getItem(exportPosition)?.let {
viewModel.export(uri.toString(), it)
}
startExport(uri.toString())
}
}

@ -18,7 +18,7 @@ import java.io.File
class DownloadViewModel(application: Application) : BaseViewModel(application) {
fun export(path: String, book: Book) {
fun export(path: String, book: Book, finally: (msg: String) -> Unit) {
execute {
if (path.isContentPath()) {
val uri = Uri.parse(path)
@ -29,7 +29,9 @@ class DownloadViewModel(application: Application) : BaseViewModel(application) {
export(FileUtils.createFolderIfNotExist(path), book)
}
}.onError {
toast(it.localizedMessage ?: "ERROR")
finally(it.localizedMessage ?: "ERROR")
}.onSuccess {
finally(context.getString(R.string.success))
}
}

@ -580,6 +580,7 @@
<string name="book_type_audio">音频</string>
<string name="to_backstage">转到后台</string>
<string name="importing">正在导入</string>
<string name="exporting">正在导出</string>
<string name="custom_page_key">自定义翻页按键</string>
<string name="prev_page_key">上一页按键</string>
<string name="next_page_key">下一页按键</string>

Loading…
Cancel
Save