pull/48/head
kunfei 5 years ago
parent 41a291d92d
commit 3dd99d7095
  1. 5
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt
  2. 23
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt
  3. 5
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt
  4. 23
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt

@ -84,7 +84,7 @@ class BookSourceEditActivity :
clipboard?.primaryClip = ClipData.newPlainText(null, sourceStr)
}
}
R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView() }
R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView(it) }
}
return super.onCompatOptionsItemSelected(item)
}
@ -142,8 +142,7 @@ class BookSourceEditActivity :
recycler_view.scrollToPosition(0)
}
private fun upRecyclerView() {
val source = viewModel.bookSource
private fun upRecyclerView(source: BookSource? = viewModel.bookSource) {
source?.let {
cb_is_enable.isChecked = it.enabled
cb_is_enable_find.isChecked = it.enabledExplore

@ -30,15 +30,16 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
}
}
fun save(bookSource: BookSource, success: (() -> Unit)? = null) {
fun save(source: BookSource, success: (() -> Unit)? = null) {
execute {
oldSourceUrl?.let {
if (oldSourceUrl != bookSource.bookSourceUrl) {
if (oldSourceUrl != source.bookSourceUrl) {
App.db.bookSourceDao().delete(it)
}
}
oldSourceUrl = bookSource.bookSourceUrl
App.db.bookSourceDao().insert(bookSource)
oldSourceUrl = source.bookSourceUrl
App.db.bookSourceDao().insert(source)
bookSource = source
}.onSuccess {
success?.invoke()
}.onError {
@ -47,21 +48,25 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
}
}
fun pasteSource(onSuccess: () -> Unit) {
fun pasteSource(onSuccess: (source: BookSource) -> Unit) {
execute {
var source: BookSource? = null
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
clipboard?.primaryClip?.let {
if (it.itemCount > 0) {
val json = it.getItemAt(0).text.toString()
OldRule.jsonToBookSource(json)?.let { source ->
bookSource = source
} ?: toast("格式不对")
source = OldRule.jsonToBookSource(json)
}
}
source
}.onError {
toast(it.localizedMessage)
}.onSuccess {
onSuccess()
if (it != null) {
onSuccess(it)
} else {
toast("格式不对")
}
}
}
}

@ -102,7 +102,7 @@ class RssSourceEditActivity :
clipboard?.primaryClip = ClipData.newPlainText(null, sourceStr)
}
}
R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView() }
R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView(it) }
}
return super.onCompatOptionsItemSelected(item)
}
@ -115,8 +115,7 @@ class RssSourceEditActivity :
recycler_view.adapter = adapter
}
private fun upRecyclerView() {
val rssSource = viewModel.rssSource
private fun upRecyclerView(rssSource: RssSource? = viewModel.rssSource) {
rssSource?.let {
cb_is_enable.isChecked = rssSource.enabled
cb_enable_js.isChecked = rssSource.enableJs

@ -31,15 +31,16 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
}
}
fun save(rssSource: RssSource, success: (() -> Unit)) {
fun save(source: RssSource, success: (() -> Unit)) {
execute {
oldSourceUrl?.let {
if (oldSourceUrl != rssSource.sourceUrl) {
if (oldSourceUrl != source.sourceUrl) {
App.db.rssSourceDao().delete(it)
}
}
oldSourceUrl = rssSource.sourceUrl
App.db.rssSourceDao().insert(rssSource)
oldSourceUrl = source.sourceUrl
App.db.rssSourceDao().insert(source)
rssSource = source
}.onSuccess {
success()
}.onError {
@ -48,21 +49,25 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
}
}
fun pasteSource(onSuccess: () -> Unit) {
fun pasteSource(onSuccess: (source: RssSource) -> Unit) {
execute {
var source: RssSource? = null
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
clipboard?.primaryClip?.let {
if (it.itemCount > 0) {
val json = it.getItemAt(0).text.toString().trim()
GSON.fromJsonObject<RssSource>(json)?.let { source ->
rssSource = source
} ?: toast("格式不对")
source = GSON.fromJsonObject<RssSource>(json)
}
}
source
}.onError {
toast(it.localizedMessage)
}.onSuccess {
onSuccess()
if (it != null) {
onSuccess(it)
} else {
toast("格式不对")
}
}
}
}
Loading…
Cancel
Save