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) 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) return super.onCompatOptionsItemSelected(item)
} }
@ -142,8 +142,7 @@ class BookSourceEditActivity :
recycler_view.scrollToPosition(0) recycler_view.scrollToPosition(0)
} }
private fun upRecyclerView() { private fun upRecyclerView(source: BookSource? = viewModel.bookSource) {
val source = viewModel.bookSource
source?.let { source?.let {
cb_is_enable.isChecked = it.enabled cb_is_enable.isChecked = it.enabled
cb_is_enable_find.isChecked = it.enabledExplore 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 { execute {
oldSourceUrl?.let { oldSourceUrl?.let {
if (oldSourceUrl != bookSource.bookSourceUrl) { if (oldSourceUrl != source.bookSourceUrl) {
App.db.bookSourceDao().delete(it) App.db.bookSourceDao().delete(it)
} }
} }
oldSourceUrl = bookSource.bookSourceUrl oldSourceUrl = source.bookSourceUrl
App.db.bookSourceDao().insert(bookSource) App.db.bookSourceDao().insert(source)
bookSource = source
}.onSuccess { }.onSuccess {
success?.invoke() success?.invoke()
}.onError { }.onError {
@ -47,21 +48,25 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
} }
} }
fun pasteSource(onSuccess: () -> Unit) { fun pasteSource(onSuccess: (source: BookSource) -> Unit) {
execute { execute {
var source: BookSource? = null
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager? val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
clipboard?.primaryClip?.let { clipboard?.primaryClip?.let {
if (it.itemCount > 0) { if (it.itemCount > 0) {
val json = it.getItemAt(0).text.toString() val json = it.getItemAt(0).text.toString()
OldRule.jsonToBookSource(json)?.let { source -> source = OldRule.jsonToBookSource(json)
bookSource = source
} ?: toast("格式不对")
} }
} }
source
}.onError { }.onError {
toast(it.localizedMessage) toast(it.localizedMessage)
}.onSuccess { }.onSuccess {
onSuccess() if (it != null) {
onSuccess(it)
} else {
toast("格式不对")
}
} }
} }
} }

@ -102,7 +102,7 @@ class RssSourceEditActivity :
clipboard?.primaryClip = ClipData.newPlainText(null, sourceStr) 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) return super.onCompatOptionsItemSelected(item)
} }
@ -115,8 +115,7 @@ class RssSourceEditActivity :
recycler_view.adapter = adapter recycler_view.adapter = adapter
} }
private fun upRecyclerView() { private fun upRecyclerView(rssSource: RssSource? = viewModel.rssSource) {
val rssSource = viewModel.rssSource
rssSource?.let { rssSource?.let {
cb_is_enable.isChecked = rssSource.enabled cb_is_enable.isChecked = rssSource.enabled
cb_enable_js.isChecked = rssSource.enableJs 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 { execute {
oldSourceUrl?.let { oldSourceUrl?.let {
if (oldSourceUrl != rssSource.sourceUrl) { if (oldSourceUrl != source.sourceUrl) {
App.db.rssSourceDao().delete(it) App.db.rssSourceDao().delete(it)
} }
} }
oldSourceUrl = rssSource.sourceUrl oldSourceUrl = source.sourceUrl
App.db.rssSourceDao().insert(rssSource) App.db.rssSourceDao().insert(source)
rssSource = source
}.onSuccess { }.onSuccess {
success() success()
}.onError { }.onError {
@ -48,21 +49,25 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
} }
} }
fun pasteSource(onSuccess: () -> Unit) { fun pasteSource(onSuccess: (source: RssSource) -> Unit) {
execute { execute {
var source: RssSource? = null
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager? val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
clipboard?.primaryClip?.let { clipboard?.primaryClip?.let {
if (it.itemCount > 0) { if (it.itemCount > 0) {
val json = it.getItemAt(0).text.toString().trim() val json = it.getItemAt(0).text.toString().trim()
GSON.fromJsonObject<RssSource>(json)?.let { source -> source = GSON.fromJsonObject<RssSource>(json)
rssSource = source
} ?: toast("格式不对")
} }
} }
source
}.onError { }.onError {
toast(it.localizedMessage) toast(it.localizedMessage)
}.onSuccess { }.onSuccess {
onSuccess() if (it != null) {
onSuccess(it)
} else {
toast("格式不对")
}
} }
} }
} }
Loading…
Cancel
Save