pull/48/head
kunfei 5 years ago
parent e7ac36825d
commit a48ed64d8e
  1. 26
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  2. 43
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt
  3. 1
      app/src/main/res/values/strings.xml

@ -15,6 +15,7 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.ItemTouchHelper
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
@ -105,7 +106,10 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
intent.data?.let {
when (it.path) {
"/importonline" -> it.getQueryParameter("src")?.let { url ->
viewModel.importSource(url)
Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE)
viewModel.importSource(url) { msg ->
title_bar.snackbar(msg)
}
}
else -> {
toast("格式不对")
@ -197,7 +201,10 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
cacheUrls.add(0, it)
aCache.put("sourceUrl", cacheUrls.joinToString(","))
}
viewModel.importSource(it)
Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE)
viewModel.importSource(it) { msg ->
title_bar.snackbar(msg)
}
}
}
cancelButton()
@ -228,7 +235,10 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
override fun onFilePicked(requestCode: Int, currentPath: String) {
if (requestCode == importSource) {
viewModel.importSourceFromFilePath(currentPath)
Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE)
viewModel.importSourceFromFilePath(currentPath) { msg ->
title_bar.snackbar(msg)
}
}
}
@ -268,14 +278,20 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
when (requestCode) {
qrRequestCode -> if (resultCode == RESULT_OK) {
data?.getStringExtra("result")?.let {
viewModel.importSource(it)
Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE)
viewModel.importSource(it) { msg ->
title_bar.snackbar(msg)
}
}
}
importSource -> if (resultCode == Activity.RESULT_OK) {
data?.data?.let {
val path = FileUtils.getPath(this, it)
if (path != null) {
viewModel.importSourceFromFilePath(path)
Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE)
viewModel.importSourceFromFilePath(path) { msg ->
title_bar.snackbar(msg)
}
} else {
toast(R.string.uri_to_path_fail)
}

@ -120,28 +120,34 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
}
}
fun importSourceFromFilePath(path: String) {
fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) {
execute {
val file = File(path)
if (file.exists()) {
importSource(file.readText())
importSource(file.readText(), finally)
} else {
finally("文件无法打开")
}
}
}
fun importSource(text: String) {
fun importSource(text: String, finally: (msg: String) -> Unit) {
execute {
val text1 = text.trim()
if (text1.isJsonObject()) {
val json = JsonPath.parse(text1)
val urls = json.read<List<String>>("$.sourceUrls")
if (!urls.isNullOrEmpty()) {
urls.forEach { importSourceUrl(it) }
var count = 0
urls.forEach {
count += importSourceUrl(it)
}
finally("导入${count}")
} else {
OldRule.jsonToBookSource(text1)?.let {
App.db.bookSourceDao().insert(it)
}
toast("成功导入1条")
finally("导入1条")
}
} else if (text1.isJsonArray()) {
val bookSources = mutableListOf<BookSource>()
@ -153,9 +159,10 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
}
}
App.db.bookSourceDao().insert(*bookSources.toTypedArray())
toast("成功导入${bookSources.size}")
finally("导入${bookSources.size}")
} else if (text1.isAbsUrl()) {
importSourceUrl(text1)
val count = importSourceUrl(text1)
finally("导入${count}")
} else {
toast("格式不对")
}
@ -164,16 +171,22 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
}
}
private fun importSourceUrl(url: String) {
execute {
NetworkUtils.getBaseUrl(url)?.let {
val response = HttpHelper.getApiService<IHttpGetApi>(it).get(url, mapOf()).execute()
response.body()?.let { body ->
importSource(body)
private fun importSourceUrl(url: String): Int {
NetworkUtils.getBaseUrl(url)?.let {
val response = HttpHelper.getApiService<IHttpGetApi>(it).get(url, mapOf()).execute()
response.body()?.let { body ->
val bookSources = mutableListOf<BookSource>()
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
for (item in items) {
val jsonItem = jsonPath.parse(item)
OldRule.jsonToBookSource(jsonItem.jsonString())?.let { source ->
bookSources.add(source)
}
}
App.db.bookSourceDao().insert(*bookSources.toTypedArray())
return bookSources.size
}
}.onError {
toast(it.localizedMessage)
}
return 0
}
}

@ -559,5 +559,6 @@
<string name="book_type_text">文本</string>
<string name="book_type_audio">音频</string>
<string name="to_backstage">转到后台</string>
<string name="importing">正在导入</string>
</resources>

Loading…
Cancel
Save