pull/48/head
kunfei 5 years ago
parent 5e40e8b115
commit fa92c468a6
  1. 4
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt
  2. 15
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt
  3. 21
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt

@ -24,10 +24,6 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
source?.let { source?.let {
oldSourceUrl = it.bookSourceUrl oldSourceUrl = it.bookSourceUrl
bookSource = it bookSource = it
} ?: let {
bookSource = BookSource().apply {
customOrder = App.db.bookSourceDao().maxOrder + 1
}
} }
}.onFinally { }.onFinally {
onFinally() onFinally()

@ -12,7 +12,6 @@ import android.view.MenuItem
import android.view.ViewTreeObserver import android.view.ViewTreeObserver
import android.widget.EditText import android.widget.EditText
import android.widget.PopupWindow import android.widget.PopupWindow
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
@ -46,10 +45,9 @@ class RssSourceEditActivity :
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
initView() initView()
viewModel.sourceLiveData.observe(this, Observer { viewModel.initData(intent) {
upRecyclerView(it) upRecyclerView()
}) }
viewModel.initData(intent)
} }
override fun onDestroy() { override fun onDestroy() {
@ -85,7 +83,7 @@ class RssSourceEditActivity :
clipboard?.primaryClip = ClipData.newPlainText(null, sourceStr) clipboard?.primaryClip = ClipData.newPlainText(null, sourceStr)
} }
} }
R.id.menu_paste_source -> viewModel.pasteSource() R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView() }
} }
return super.onCompatOptionsItemSelected(item) return super.onCompatOptionsItemSelected(item)
} }
@ -98,7 +96,8 @@ class RssSourceEditActivity :
recycler_view.adapter = adapter recycler_view.adapter = adapter
} }
private fun upRecyclerView(rssSource: RssSource?) { private fun upRecyclerView() {
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
@ -136,7 +135,7 @@ class RssSourceEditActivity :
} }
private fun getRssSource(): RssSource? { private fun getRssSource(): RssSource? {
val source = viewModel.sourceLiveData.value ?: RssSource() val source = viewModel.rssSource?.copy() ?: RssSource()
source.enabled = cb_is_enable.isChecked source.enabled = cb_is_enable.isChecked
source.enableJs = cb_enable_js.isChecked source.enableJs = cb_enable_js.isChecked
source.loadWithBaseUrl = cb_enable_base_url.isChecked source.loadWithBaseUrl = cb_enable_base_url.isChecked

@ -4,7 +4,6 @@ import android.app.Application
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
@ -13,10 +12,10 @@ import io.legado.app.utils.fromJsonObject
class RssSourceEditViewModel(application: Application) : BaseViewModel(application) { class RssSourceEditViewModel(application: Application) : BaseViewModel(application) {
val sourceLiveData: MutableLiveData<RssSource> = MutableLiveData() var rssSource: RssSource? = null
private var oldSourceUrl: String? = null private var oldSourceUrl: String? = null
fun initData(intent: Intent) { fun initData(intent: Intent, onFinally: () -> Unit) {
execute { execute {
val key = intent.getStringExtra("data") val key = intent.getStringExtra("data")
var source: RssSource? = null var source: RssSource? = null
@ -25,12 +24,10 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
} }
source?.let { source?.let {
oldSourceUrl = it.sourceUrl oldSourceUrl = it.sourceUrl
sourceLiveData.postValue(it) rssSource = it
} ?: let {
sourceLiveData.postValue(RssSource().apply {
customOrder = App.db.rssSourceDao().maxOrder + 1
})
} }
}.onFinally {
onFinally()
} }
} }
@ -51,17 +48,21 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
} }
} }
fun pasteSource() { fun pasteSource(onSuccess: () -> Unit) {
execute { execute {
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 -> GSON.fromJsonObject<RssSource>(json)?.let { source ->
sourceLiveData.postValue(source) rssSource = source
} ?: toast("格式不对") } ?: toast("格式不对")
} }
} }
}.onError {
toast(it.localizedMessage)
}.onSuccess {
onSuccess()
} }
} }
} }
Loading…
Cancel
Save