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.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity 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.help.permission.PermissionsCompat
import io.legado.app.service.help.Download import io.legado.app.service.help.Download
import io.legado.app.ui.filechooser.FileChooserDialog import io.legado.app.ui.filechooser.FileChooserDialog
import io.legado.app.utils.ACache import io.legado.app.utils.*
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.android.synthetic.main.activity_download.*
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
@ -137,9 +135,7 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
if (path.isNullOrEmpty()) { if (path.isNullOrEmpty()) {
toast("没有默认路径") toast("没有默认路径")
} else { } else {
adapter.getItem(exportPosition)?.let { startExport(path)
viewModel.export(path, it)
}
} }
} }
1 -> { 1 -> {
@ -170,13 +166,21 @@ class DownloadActivity : VMBaseActivity<DownloadViewModel>(R.layout.activity_dow
}.show() }.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) { override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) { when (requestCode) {
exportRequestCode -> { exportRequestCode -> {
ACache.get(this@DownloadActivity).put(exportBookPathKey, currentPath) ACache.get(this@DownloadActivity).put(exportBookPathKey, currentPath)
adapter.getItem(exportPosition)?.let { startExport(currentPath)
viewModel.export(currentPath, it)
}
} }
} }
} }
@ -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 Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) )
ACache.get(this@DownloadActivity).put(exportBookPathKey, uri.toString()) ACache.get(this@DownloadActivity).put(exportBookPathKey, uri.toString())
adapter.getItem(exportPosition)?.let { startExport(uri.toString())
viewModel.export(uri.toString(), it)
}
} }
} }

@ -18,7 +18,7 @@ import java.io.File
class DownloadViewModel(application: Application) : BaseViewModel(application) { class DownloadViewModel(application: Application) : BaseViewModel(application) {
fun export(path: String, book: Book) { fun export(path: String, book: Book, finally: (msg: String) -> Unit) {
execute { execute {
if (path.isContentPath()) { if (path.isContentPath()) {
val uri = Uri.parse(path) val uri = Uri.parse(path)
@ -29,7 +29,9 @@ class DownloadViewModel(application: Application) : BaseViewModel(application) {
export(FileUtils.createFolderIfNotExist(path), book) export(FileUtils.createFolderIfNotExist(path), book)
} }
}.onError { }.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="book_type_audio">音频</string>
<string name="to_backstage">转到后台</string> <string name="to_backstage">转到后台</string>
<string name="importing">正在导入</string> <string name="importing">正在导入</string>
<string name="exporting">正在导出</string>
<string name="custom_page_key">自定义翻页按键</string> <string name="custom_page_key">自定义翻页按键</string>
<string name="prev_page_key">上一页按键</string> <string name="prev_page_key">上一页按键</string>
<string name="next_page_key">下一页按键</string> <string name="next_page_key">下一页按键</string>

Loading…
Cancel
Save