pull/32/head
gedoor 6 years ago
parent 1e21de79f3
commit e35797030f
  1. 31
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt
  2. 12
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditAdapter.kt
  3. 4
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt

@ -1,9 +1,11 @@
package io.legado.app.ui.sourceedit package io.legado.app.ui.sourceedit
import android.os.Bundle import android.os.Bundle
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.BaseActivity import io.legado.app.base.BaseActivity
import io.legado.app.data.entities.BookSource
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_source_edit.* import kotlinx.android.synthetic.main.activity_source_edit.*
@ -13,16 +15,39 @@ class SourceEditActivity : BaseActivity<SourceEditViewModel>() {
override val layoutID: Int override val layoutID: Int
get() = R.layout.activity_source_edit get() = R.layout.activity_source_edit
val adapter = SourceEditAdapter() private val adapter = SourceEditAdapter()
private val sourceEditEntities:ArrayList<SourceEditEntity> = ArrayList()
override fun onViewModelCreated(viewModel: SourceEditViewModel, savedInstanceState: Bundle?) { override fun onViewModelCreated(viewModel: SourceEditViewModel, savedInstanceState: Bundle?) {
val sourceID = intent.getStringExtra("data")
sourceID?.let { viewModel.setBookSource(sourceID)}
initRecyclerView() 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() { private fun initRecyclerView() {
recycler_view.layoutManager = LinearLayoutManager(this) recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.adapter = adapter 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)
} }

@ -11,7 +11,7 @@ import kotlinx.android.synthetic.main.item_source_edit.view.*
class SourceEditAdapter : RecyclerView.Adapter<SourceEditAdapter.MyViewHolder>() { class SourceEditAdapter : RecyclerView.Adapter<SourceEditAdapter.MyViewHolder>() {
var sourceEditEntities:ArrayList<SourceEditEntity> = ArrayList() var sourceEditEntities:ArrayList<SourceEditActivity.SourceEditEntity> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_source_edit, parent, false)) return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_source_edit, parent, false))
@ -26,7 +26,7 @@ class SourceEditAdapter : RecyclerView.Adapter<SourceEditAdapter.MyViewHolder>()
} }
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { 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) { if (editText.getTag(R.id.tag1) == null) {
val listener = object : View.OnAttachStateChangeListener { val listener = object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(v: View) { override fun onViewAttachedToWindow(v: View) {
@ -49,7 +49,7 @@ class SourceEditAdapter : RecyclerView.Adapter<SourceEditAdapter.MyViewHolder>()
} }
} }
editText.setText(sourceEditEntity.value) editText.setText(sourceEditEntity.value)
sourceEditEntity.hint?.let { textInputLayout.hint = context.getString(it) } textInputLayout.hint = context.getString(sourceEditEntity.hint)
val textWatcher = object : TextWatcher { val textWatcher = object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
@ -68,9 +68,5 @@ class SourceEditAdapter : RecyclerView.Adapter<SourceEditAdapter.MyViewHolder>()
} }
} }
class SourceEditEntity {
var key:String?=null
var value:String?=null
var hint:Int?=null
}
} }

@ -9,10 +9,12 @@ import io.legado.app.data.entities.BookSource
class SourceEditViewModel(application: Application) : AndroidViewModel(application) { class SourceEditViewModel(application: Application) : AndroidViewModel(application) {
private val sourceLiveData:MutableLiveData<BookSource> = MutableLiveData() val sourceLiveData:MutableLiveData<BookSource> = MutableLiveData()
fun setBookSource(key: String) { fun setBookSource(key: String) {
sourceLiveData.value = App.db.bookSourceDao().findByKey(key) sourceLiveData.value = App.db.bookSourceDao().findByKey(key)
} }
} }
Loading…
Cancel
Save