Commit 2fcc8b33 authored by Shailesh Baldaniya's avatar Shailesh Baldaniya

feat: Add Header for chatroom types

parent 844403b9
......@@ -25,6 +25,7 @@ import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.android.util.extensions.*
import chat.rocket.android.widget.DividerItemDecoration
import chat.rocket.common.model.RoomType
import chat.rocket.core.internal.realtime.State
import chat.rocket.core.model.ChatRoom
import dagger.android.support.AndroidSupportInjection
......@@ -47,7 +48,8 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
private val handler = Handler()
private var listJob: Job? = null
private val sectionedAdapter:SimpleSectionedRecyclerViewAdapter? = null
private var baseAdapter: ChatRoomsAdapter? = null
private var sectionedAdapter: SimpleSectionedRecyclerViewAdapter? = null
companion object {
fun newInstance() = ChatRoomsFragment()
......@@ -148,7 +150,6 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
activity?.apply {
listJob?.cancel()
listJob = launch(UI) {
setSections()
val adapter = recycler_view.adapter as SimpleSectionedRecyclerViewAdapter
// FIXME https://fabric.io/rocketchat3/android/apps/chat.rocket.android.dev/issues/5a90d4718cb3c2fa63b3f557?time=last-seven-days
// TODO - fix this bug to reenable DiffUtil
......@@ -159,6 +160,9 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
if (isActive) {
adapter.baseAdapter.updateRooms(newDataSet)
diff.dispatchUpdatesTo(adapter)
//Set sections always after data set is updated
setSections()
}
}
}
......@@ -228,23 +232,41 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
settingsRepository.get(serverInteractor.get()!!), localRepository) { chatRoom ->
presenter.loadChatRoom(chatRoom)
}
baseAdapter = ChatRoomsAdapter(this,
settingsRepository.get(serverInteractor.get()!!)) { chatRoom -> presenter.loadChatRoom(chatRoom) }
//Add your adapter to the sectionAdapter
val mSectionedAdapter = SimpleSectionedRecyclerViewAdapter(this, R.layout.item_chatroom_header, R.id.text_chatroom_header, baseAdapter)
//Apply this adapter to the RecyclerView
recycler_view.adapter = mSectionedAdapter
sectionedAdapter = SimpleSectionedRecyclerViewAdapter(this, R.layout.item_chatroom_header, R.id.text_chatroom_header, baseAdapter!!)
recycler_view.adapter = sectionedAdapter
}
}
private fun setSections(){
//This is the code to provide a sectioned list
private fun setSections() {
//Don't add section if not grouping by RoomType
if (!SharedPreferenceHelper.getBoolean(Constants.CHATROOM_GROUP_BY_TYPE_KEY, false)){
sectionedAdapter?.clearSections()
return
}
val sections = ArrayList<SimpleSectionedRecyclerViewAdapter.Section>()
//Sections
sections.add(SimpleSectionedRecyclerViewAdapter.Section(0, "Section 1"))
sections.add(SimpleSectionedRecyclerViewAdapter.Section(2, "Section 2"))
sections.add(SimpleSectionedRecyclerViewAdapter.Section(5, "Section 3"))
baseAdapter?.dataSet?.let {
var previousChatRoomType = ""
for ((position, chatRoom) in it.withIndex()) {
val type = chatRoom.type.toString()
if (type != previousChatRoomType) {
val title = when (type) {
RoomType.CHANNEL.toString() -> "Channels"
RoomType.PRIVATE_GROUP.toString() -> "Private Groups"
RoomType.DIRECT_MESSAGE.toString() -> "Direct Messages"
RoomType.LIVECHAT.toString() -> "Live Chats"
else -> "UNKNOWN"
}
sections.add(SimpleSectionedRecyclerViewAdapter.Section(position, title))
}
previousChatRoomType = chatRoom.type.toString()
}
}
val dummy = arrayOfNulls<SimpleSectionedRecyclerViewAdapter.Section>(sections.size)
sectionedAdapter?.setSections(sections.toArray(dummy))
......
......@@ -13,11 +13,9 @@ class SimpleSectionedRecyclerViewAdapter(private val context: Context, private v
val baseAdapter: ChatRoomsAdapter) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var isValid = true
private val layoutInflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
private val sectionsHeaders = SparseArray<Section>()
init {
baseAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onChanged() {
isValid = baseAdapter.itemCount > 0
......@@ -41,9 +39,8 @@ class SimpleSectionedRecyclerViewAdapter(private val context: Context, private v
})
}
class SectionViewHolder(view: View, mTextResourceid: Int) : RecyclerView.ViewHolder(view) {
var title: TextView = view.findViewById<View>(mTextResourceid) as TextView
class SectionViewHolder(view: View, textResourceId: Int) : RecyclerView.ViewHolder(view) {
var title: TextView = view.findViewById<View>(textResourceId) as TextView
}
override fun onCreateViewHolder(parent: ViewGroup, typeView: Int): RecyclerView.ViewHolder {
......@@ -61,7 +58,6 @@ class SimpleSectionedRecyclerViewAdapter(private val context: Context, private v
} else {
baseAdapter.onBindViewHolder(viewHolder as ChatRoomsAdapter.ViewHolder, sectionedPositionToPosition(position))
}
}
override fun getItemViewType(position: Int): Int {
......@@ -71,25 +67,17 @@ class SimpleSectionedRecyclerViewAdapter(private val context: Context, private v
baseAdapter.getItemViewType(sectionedPositionToPosition(position)) + 1
}
class Section(internal var firstPosition: Int, title: CharSequence) {
class Section(internal var firstPosition: Int, var title: CharSequence) {
internal var sectionedPosition: Int = 0
var title: CharSequence
internal set
init {
this.title = title
}
}
fun setSections(sections: Array<Section>) {
sectionsHeaders.clear()
Arrays.sort(sections) { o, o1 ->
Arrays.sort(sections) { section1, section2 ->
when {
o.firstPosition == o1.firstPosition -> 0
o.firstPosition < o1.firstPosition -> -1
section1.firstPosition == section2.firstPosition -> 0
section1.firstPosition < section2.firstPosition -> -1
else -> 1
}
}
......@@ -102,6 +90,11 @@ class SimpleSectionedRecyclerViewAdapter(private val context: Context, private v
notifyDataSetChanged()
}
fun clearSections(){
sectionsHeaders.clear()
notifyDataSetChanged()
}
fun positionToSectionedPosition(position: Int): Int {
var offset = 0
for (i in 0 until sectionsHeaders.size()) {
......@@ -132,7 +125,6 @@ class SimpleSectionedRecyclerViewAdapter(private val context: Context, private v
return sectionsHeaders.get(position) != null
}
override fun getItemId(position: Int): Long {
return when (isSectionHeaderPosition(position)) {
true -> (Integer.MAX_VALUE - sectionsHeaders.indexOfKey(position)).toLong()
......@@ -147,5 +139,4 @@ class SimpleSectionedRecyclerViewAdapter(private val context: Context, private v
companion object {
private const val SECTION_TYPE = 0
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/darkGray" />
<TextView
android:id="@+id/text_chatroom_header"
style="@style/ChatRooms.Header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="@string/chatroom_header"
android:textSize="18sp" />
android:padding="8dp"
android:paddingEnd="@dimen/screen_edge_left_and_right_padding"
android:paddingStart="@dimen/screen_edge_left_and_right_padding"
android:text="@string/chatroom_header" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/darkGray" />
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
</LinearLayout>
\ No newline at end of file
......@@ -71,6 +71,10 @@
<item name="android:paddingStart">@dimen/edit_text_margin</item>
</style>
<style name="ChatRooms.Header" parent="TextAppearance.AppCompat.Headline">
<item name="android:textSize">18sp</item>
</style>
<style name="ChatRoom.Name.TextView" parent="TextAppearance.AppCompat.Title">
<item name="android:ellipsize">end</item>
<item name="android:maxLines">1</item>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment