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. 37
      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.DividerItemDecoration
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
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
@ -105,7 +106,10 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
intent.data?.let { intent.data?.let {
when (it.path) { when (it.path) {
"/importonline" -> it.getQueryParameter("src")?.let { url -> "/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 -> { else -> {
toast("格式不对") toast("格式不对")
@ -197,7 +201,10 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
cacheUrls.add(0, it) cacheUrls.add(0, it)
aCache.put("sourceUrl", cacheUrls.joinToString(",")) 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() cancelButton()
@ -228,7 +235,10 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
override fun onFilePicked(requestCode: Int, currentPath: String) { override fun onFilePicked(requestCode: Int, currentPath: String) {
if (requestCode == importSource) { 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) { when (requestCode) {
qrRequestCode -> if (resultCode == RESULT_OK) { qrRequestCode -> if (resultCode == RESULT_OK) {
data?.getStringExtra("result")?.let { 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) { importSource -> if (resultCode == Activity.RESULT_OK) {
data?.data?.let { data?.data?.let {
val path = FileUtils.getPath(this, it) val path = FileUtils.getPath(this, it)
if (path != null) { 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 { } else {
toast(R.string.uri_to_path_fail) 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 { execute {
val file = File(path) val file = File(path)
if (file.exists()) { 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 { execute {
val text1 = text.trim() val text1 = text.trim()
if (text1.isJsonObject()) { if (text1.isJsonObject()) {
val json = JsonPath.parse(text1) val json = JsonPath.parse(text1)
val urls = json.read<List<String>>("$.sourceUrls") val urls = json.read<List<String>>("$.sourceUrls")
if (!urls.isNullOrEmpty()) { if (!urls.isNullOrEmpty()) {
urls.forEach { importSourceUrl(it) } var count = 0
urls.forEach {
count += importSourceUrl(it)
}
finally("导入${count}")
} else { } else {
OldRule.jsonToBookSource(text1)?.let { OldRule.jsonToBookSource(text1)?.let {
App.db.bookSourceDao().insert(it) App.db.bookSourceDao().insert(it)
} }
toast("成功导入1条") finally("导入1条")
} }
} else if (text1.isJsonArray()) { } else if (text1.isJsonArray()) {
val bookSources = mutableListOf<BookSource>() val bookSources = mutableListOf<BookSource>()
@ -153,9 +159,10 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
} }
} }
App.db.bookSourceDao().insert(*bookSources.toTypedArray()) App.db.bookSourceDao().insert(*bookSources.toTypedArray())
toast("成功导入${bookSources.size}") finally("导入${bookSources.size}")
} else if (text1.isAbsUrl()) { } else if (text1.isAbsUrl()) {
importSourceUrl(text1) val count = importSourceUrl(text1)
finally("导入${count}")
} else { } else {
toast("格式不对") toast("格式不对")
} }
@ -164,16 +171,22 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application)
} }
} }
private fun importSourceUrl(url: String) { private fun importSourceUrl(url: String): Int {
execute {
NetworkUtils.getBaseUrl(url)?.let { NetworkUtils.getBaseUrl(url)?.let {
val response = HttpHelper.getApiService<IHttpGetApi>(it).get(url, mapOf()).execute() val response = HttpHelper.getApiService<IHttpGetApi>(it).get(url, mapOf()).execute()
response.body()?.let { body -> response.body()?.let { body ->
importSource(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)
} }
} }
}.onError { App.db.bookSourceDao().insert(*bookSources.toTypedArray())
toast(it.localizedMessage) return bookSources.size
}
} }
return 0
} }
} }

@ -559,5 +559,6 @@
<string name="book_type_text">文本</string> <string name="book_type_text">文本</string>
<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>
</resources> </resources>

Loading…
Cancel
Save