[Dialog/Events] Create adapter outside of the observer.

This commit is contained in:
Kacper Ziubryniewicz 2019-12-01 20:25:28 +01:00
parent 8482c27689
commit 8df24dc1c4
2 changed files with 14 additions and 4 deletions

View File

@ -23,12 +23,13 @@ import pl.szczodrzynski.edziennik.utils.models.Date
class EventListAdapter(
val context: Context,
val eventList: List<EventFull>,
val parentDialog: EventListDialog
) : RecyclerView.Adapter<EventListAdapter.ViewHolder>() {
private val app by lazy { context.applicationContext as App }
val eventList = mutableListOf<EventFull>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view: RowDialogEventListItemBinding = DataBindingUtil.inflate(inflater, R.layout.row_dialog_event_list_item, parent, false)

View File

@ -43,6 +43,7 @@ class EventListDialog(
private val app by lazy { activity.application as App }
private lateinit var b: DialogEventListBinding
private lateinit var dialog: AlertDialog
private lateinit var adapter: EventListAdapter
private var lesson: LessonFull? = null
@ -111,16 +112,24 @@ class EventListDialog(
b.eventListView.apply {
setHasFixedSize(false)
isNestedScrollingEnabled = true
layoutManager = LinearLayoutManager(context)
layoutManager = LinearLayoutManager(activity)
}
adapter = EventListAdapter(activity, this@EventListDialog)
b.eventListView.adapter = adapter
app.db.eventDao().getAllByDateTime(profileId, date, time).observe(activity, Observer { events ->
if (events.isNullOrEmpty()) {
b.eventListView.visibility = View.GONE
b.textNoEvents.visibility = View.VISIBLE
} else {
val adapter = EventListAdapter(activity, events, this)
b.eventListView.adapter = adapter
adapter.run {
eventList.apply {
clear()
addAll(events)
}
notifyDataSetChanged()
}
}
})
}