diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt index 8830ff947..0bbe38f4f 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt @@ -1,9 +1,11 @@ package io.legado.app.ui.sourceedit import android.os.Bundle +import androidx.lifecycle.Observer import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.BaseActivity +import io.legado.app.data.entities.BookSource import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_source_edit.* @@ -13,16 +15,39 @@ class SourceEditActivity : BaseActivity() { override val layoutID: Int get() = R.layout.activity_source_edit - val adapter = SourceEditAdapter() + private val adapter = SourceEditAdapter() + private val sourceEditEntities:ArrayList = ArrayList() override fun onViewModelCreated(viewModel: SourceEditViewModel, savedInstanceState: Bundle?) { - val sourceID = intent.getStringExtra("data") - sourceID?.let { viewModel.setBookSource(sourceID)} initRecyclerView() + viewModel.sourceLiveData.observe(this, Observer { + upRecyclerView(it) + }) + if (viewModel.sourceLiveData.value == null) { + val sourceID = intent.getStringExtra("data") + if (sourceID == null) { + upRecyclerView(null) + } else { + sourceID.let { viewModel.setBookSource(sourceID) } + } + } else { + upRecyclerView(viewModel.sourceLiveData.value) + } } private fun initRecyclerView() { recycler_view.layoutManager = LinearLayoutManager(this) recycler_view.adapter = adapter } + + private fun upRecyclerView(bookSource: BookSource?) { + sourceEditEntities.clear() + sourceEditEntities.add(SourceEditEntity("origin", bookSource?.origin, R.string.book_source_url)) + sourceEditEntities.add(SourceEditEntity("name", bookSource?.name, R.string.book_source_name)) + sourceEditEntities.add(SourceEditEntity("group", bookSource?.group, R.string.book_source_group)) + adapter.sourceEditEntities = sourceEditEntities + adapter.notifyDataSetChanged() + } + + class SourceEditEntity(var key: String, var value:String?, var hint:Int) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditAdapter.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditAdapter.kt index 31f4cf989..be301e4c1 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditAdapter.kt @@ -11,7 +11,7 @@ import kotlinx.android.synthetic.main.item_source_edit.view.* class SourceEditAdapter : RecyclerView.Adapter() { - var sourceEditEntities:ArrayList = ArrayList() + var sourceEditEntities:ArrayList = ArrayList() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_source_edit, parent, false)) @@ -26,7 +26,7 @@ class SourceEditAdapter : RecyclerView.Adapter() } class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { - fun bind(sourceEditEntity: SourceEditEntity) = with(itemView) { + fun bind(sourceEditEntity: SourceEditActivity.SourceEditEntity) = with(itemView) { if (editText.getTag(R.id.tag1) == null) { val listener = object : View.OnAttachStateChangeListener { override fun onViewAttachedToWindow(v: View) { @@ -49,7 +49,7 @@ class SourceEditAdapter : RecyclerView.Adapter() } } editText.setText(sourceEditEntity.value) - sourceEditEntity.hint?.let { textInputLayout.hint = context.getString(it) } + textInputLayout.hint = context.getString(sourceEditEntity.hint) val textWatcher = object : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { @@ -68,9 +68,5 @@ class SourceEditAdapter : RecyclerView.Adapter() } } - class SourceEditEntity { - var key:String?=null - var value:String?=null - var hint:Int?=null - } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt index f0d341fe9..7e0fd2958 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt @@ -9,10 +9,12 @@ import io.legado.app.data.entities.BookSource class SourceEditViewModel(application: Application) : AndroidViewModel(application) { - private val sourceLiveData:MutableLiveData = MutableLiveData() + val sourceLiveData:MutableLiveData = MutableLiveData() fun setBookSource(key: String) { sourceLiveData.value = App.db.bookSourceDao().findByKey(key) } + + } \ No newline at end of file