Still WIP for Paging integration

This commit is contained in:
ganfra
2018-10-21 20:27:50 +02:00
parent 702abccb38
commit d71ae02162
11 changed files with 127 additions and 31 deletions

View File

@ -0,0 +1,14 @@
package im.vector.riotredesign.features.home
import android.support.v7.util.DiffUtil
import im.vector.matrix.android.api.session.events.model.Event
class EventDiffUtilCallback : DiffUtil.ItemCallback<Event>() {
override fun areItemsTheSame(p0: Event, p1: Event): Boolean {
return p0.eventId == p1.eventId
}
override fun areContentsTheSame(p0: Event, p1: Event): Boolean {
return p0 == p1
}
}

View File

@ -14,10 +14,10 @@ import im.vector.matrix.android.api.session.room.Room
import im.vector.riotredesign.R
import im.vector.riotredesign.core.platform.RiotFragment
import im.vector.riotredesign.core.utils.FragmentArgumentDelegate
import kotlinx.android.synthetic.main.fragment_room_list.*
import kotlinx.android.synthetic.main.fragment_room_detail.*
import org.koin.android.ext.android.inject
class RoomDetailFragment : RiotFragment() {
class RoomDetailFragment : RiotFragment(), TimelineAdapter.Callback {
companion object {
@ -31,7 +31,7 @@ class RoomDetailFragment : RiotFragment() {
private val matrix by inject<Matrix>()
private val currentSession = matrix.currentSession!!
private var roomId by FragmentArgumentDelegate<String>()
private val timelineController = TimelineEventController()
private val adapter = TimelineAdapter(this)
private lateinit var room: Room
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -46,14 +46,27 @@ class RoomDetailFragment : RiotFragment() {
}
private fun renderEvents(events: PagedList<Event>?) {
timelineController.submitList(events)
adapter.submitList(events)
}
private fun setupRecyclerView() {
val linearLayoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
linearLayoutManager.stackFromEnd = true
epoxyRecyclerView.layoutManager = linearLayoutManager
epoxyRecyclerView.setController(timelineController)
val layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
if (layoutManager.findLastCompletelyVisibleItemPosition() == positionStart - itemCount) {
layoutManager.scrollToPosition(adapter.itemCount - 1)
}
}
})
recyclerView.layoutManager = layoutManager
recyclerView.adapter = adapter
recyclerView.setHasFixedSize(true)
}
override fun onEventsListChanged(oldList: List<Event>?, newList: List<Event>?) {
if (oldList == null && newList != null) {
recyclerView.scrollToPosition(newList.size - 1)
}
}

View File

@ -0,0 +1,57 @@
package im.vector.riotredesign.features.home
import android.arch.paging.PagedList
import android.arch.paging.PagedListAdapter
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import im.vector.matrix.android.api.session.events.model.Event
import im.vector.riotredesign.R
/**
* Created by francois on 14/05/2018.
*/
class TimelineAdapter(private val callback: Callback? = null)
: PagedListAdapter<Event, TimelineAdapter.ViewHolder>(EventDiffUtilCallback()) {
private var currentList: List<Event>? = null
override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_event, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val event = getItem(position)
viewHolder.bind(event)
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val titleView = view.findViewById<TextView>(R.id.titleView)!!
fun bind(event: Event?) {
if (event == null) {
} else {
titleView.text = event.toString()
}
}
}
override fun onCurrentListChanged(newList: PagedList<Event>?) {
callback?.onEventsListChanged(currentList, newList)
currentList = newList
}
interface Callback {
fun onEventsListChanged(oldList: List<Event>?, newList: List<Event>?)
}
}

View File

@ -6,7 +6,6 @@ import com.airbnb.epoxy.paging.PagedListEpoxyController
import im.vector.matrix.android.api.session.events.model.Event
class TimelineEventController : PagedListEpoxyController<Event>(
modelBuildingHandler = EpoxyAsyncUtil.getAsyncBackgroundHandler(),
diffingHandler = EpoxyAsyncUtil.getAsyncBackgroundHandler()
) {

View File

@ -11,13 +11,14 @@
android:id="@+id/loginField"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:inputType="text"
android:singleLine="false"
android:text="ganfra07086"
app:layout_constraintBottom_toTopOf="@+id/passwordField"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
@ -32,6 +33,7 @@
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:text="111111"
app:layout_constraintEnd_toEndOf="@+id/loginField"
app:layout_constraintStart_toStartOf="@+id/loginField"
app:layout_constraintTop_toBottomOf="@+id/loginField" />
@ -40,10 +42,10 @@
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
@ -55,9 +57,9 @@
android:id="@+id/authenticateButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="8dp"
android:text="Authenticate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"

View File

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.airbnb.epoxy.EpoxyRecyclerView
android:id="@+id/epoxyRecyclerView"
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />