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