mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-19 12:18:59 +00:00
Fixed the final bug. Now there is a bug of opening a new chat with a new user. Not sure it is because of my changes.
This commit is contained in:
parent
75b6167565
commit
876e0da557
4 changed files with 48 additions and 31 deletions
|
|
@ -3,7 +3,7 @@
|
|||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_height="200dp"
|
||||
android:gravity="center_horizontal"
|
||||
android:orientation="vertical">
|
||||
|
||||
|
|
@ -19,7 +19,7 @@
|
|||
tools:visibility="visible" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/filler"
|
||||
android:id="@+id/filler_1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="16dp"
|
||||
android:layout_gravity="bottom|center_horizontal"
|
||||
|
|
@ -53,7 +53,7 @@
|
|||
tools:text="@Username" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/Filler"
|
||||
android:id="@+id/filler_2"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="2dp"
|
||||
android:layout_gravity="bottom|center_horizontal"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package com.habitrpg.android.habitica.ui.adapter.social
|
||||
|
||||
import android.view.ViewGroup
|
||||
import androidx.paging.DataSource
|
||||
import androidx.paging.PagedList
|
||||
import androidx.paging.PagedListAdapter
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import com.habitrpg.android.habitica.R
|
||||
|
|
@ -16,8 +18,10 @@ import io.reactivex.subjects.PublishSubject
|
|||
import com.habitrpg.android.habitica.models.members.Member
|
||||
|
||||
class InboxAdapter(private var user: User?, private var replyToUser : Member) : PagedListAdapter<ChatMessage, ChatRecyclerViewHolder>(DIFF_CALLBACK) {
|
||||
private var expandedMessageId: String? = null
|
||||
private val FIRST_MESSAGE = 0
|
||||
private val NORMAL_MESSAGE = 1
|
||||
|
||||
private var expandedMessageId: String? = null
|
||||
private val likeMessageEvents = PublishSubject.create<ChatMessage>()
|
||||
private val userLabelClickEvents = PublishSubject.create<String>()
|
||||
private val deleteMessageEvents = PublishSubject.create<ChatMessage>()
|
||||
|
|
@ -25,29 +29,29 @@ class InboxAdapter(private var user: User?, private var replyToUser : Member) :
|
|||
private val replyMessageEvents = PublishSubject.create<String>()
|
||||
private val copyMessageEvents = PublishSubject.create<ChatMessage>()
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return when (position == super.getItemCount()) {
|
||||
true -> 1
|
||||
false -> 0
|
||||
}
|
||||
private fun isPositionIntroMessage(position: Int) : Boolean {
|
||||
return (position == super.getItemCount() - 1)
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return super.getItemCount() + 1
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return if (isPositionIntroMessage(position)) FIRST_MESSAGE else NORMAL_MESSAGE
|
||||
}
|
||||
|
||||
override fun getItemId(position: Int): Long {
|
||||
return if (isPositionIntroMessage(position)) -1 else super.getItemId(position)
|
||||
}
|
||||
|
||||
override fun getItem(position: Int) : ChatMessage? {
|
||||
return if (isPositionIntroMessage(position)) ChatMessage() else super.getItem(position)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatRecyclerViewHolder {
|
||||
return if (viewType == 1) ChatRecyclerIntroViewHolder(parent.inflate(R.layout.tavern_chat_intro_item), replyToUser.id!!)
|
||||
return if (viewType == FIRST_MESSAGE) ChatRecyclerIntroViewHolder(parent.inflate(R.layout.tavern_chat_intro_item), replyToUser.id!!)
|
||||
else ChatRecyclerMessageViewHolder(parent.inflate(R.layout.tavern_chat_item), user?.id ?: "", false)
|
||||
}
|
||||
|
||||
fun getFirstMessage() : ChatMessage
|
||||
{
|
||||
var firstMessage = ChatMessage()
|
||||
return firstMessage
|
||||
}
|
||||
override fun onBindViewHolder(holder: ChatRecyclerViewHolder, position: Int) {
|
||||
val firstMessage : Boolean = getItemViewType(position) == 1
|
||||
val firstMessage : Boolean = getItemViewType(position) == FIRST_MESSAGE
|
||||
if (firstMessage) {
|
||||
val introHolder = holder as ChatRecyclerIntroViewHolder
|
||||
introHolder.bind(replyToUser)
|
||||
|
|
@ -95,6 +99,8 @@ class InboxAdapter(private var user: User?, private var replyToUser : Member) :
|
|||
}
|
||||
|
||||
private fun expandMessage(id: String, position: Int) {
|
||||
if (isPositionIntroMessage(position))
|
||||
return
|
||||
expandedMessageId = if (expandedMessageId == id) {
|
||||
null
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ import android.view.*
|
|||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.paging.DataSource
|
||||
import androidx.paging.PagedList
|
||||
import com.habitrpg.android.habitica.MainNavDirections
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.components.UserComponent
|
||||
|
|
@ -31,6 +33,7 @@ import io.reactivex.functions.Action
|
|||
import io.reactivex.functions.Consumer
|
||||
import kotlinx.android.synthetic.main.fragment_inbox_message_list.*
|
||||
import kotlinx.android.synthetic.main.tavern_chat_new_entry_item.*
|
||||
import java.lang.Exception
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
|
||||
|
|
@ -67,15 +70,11 @@ class InboxMessageListFragment : BaseMainFragment(), androidx.swiperefreshlayout
|
|||
|
||||
val layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this.getActivity())
|
||||
recyclerView.layoutManager = layoutManager
|
||||
|
||||
compositeSubscription.add(apiClient.getMember(replyToUserUUID!!).subscribe( Consumer
|
||||
{ member ->
|
||||
chatAdapter = InboxAdapter(user, member)
|
||||
viewModel?.messages?.observe(this.viewLifecycleOwner, Observer { chatAdapter?.submitList(it) })
|
||||
|
||||
viewModel?.getMemberData()?.observe(this.viewLifecycleOwner, Observer {
|
||||
activity?.binding?.toolbarTitle?.text = it?.profile?.name
|
||||
})
|
||||
recyclerView.adapter = chatAdapter
|
||||
recyclerView.itemAnimator = SafeDefaultItemAnimator()
|
||||
chatAdapter?.let { adapter ->
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ class InboxViewModel(recipientID: String?, recipientUsername: String?) : BaseVie
|
|||
.setEnablePlaceholders(false)
|
||||
.build()
|
||||
|
||||
private val dataSourceFactory = MessagesDataSourceFactory(socialRepository, recipientID)
|
||||
private val dataSourceFactory = MessagesDataSourceFactory(socialRepository, recipientID, ChatMessage())
|
||||
val messages: LiveData<PagedList<ChatMessage>> = dataSourceFactory.toLiveData(config)
|
||||
private val member: MutableLiveData<Member?> by lazy {
|
||||
MutableLiveData<Member?>()
|
||||
|
|
@ -86,7 +86,7 @@ class InboxViewModel(recipientID: String?, recipientUsername: String?) : BaseVie
|
|||
}
|
||||
}
|
||||
|
||||
private class MessagesDataSource(val socialRepository: SocialRepository, var recipientID: String?):
|
||||
private class MessagesDataSource(val socialRepository: SocialRepository, var recipientID: String?, var footer : ChatMessage?):
|
||||
PositionalDataSource<ChatMessage>() {
|
||||
private var lastFetchWasEnd = false
|
||||
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<ChatMessage>) {
|
||||
|
|
@ -99,8 +99,15 @@ private class MessagesDataSource(val socialRepository: SocialRepository, var rec
|
|||
val page = ceil(params.startPosition.toFloat() / params.loadSize.toFloat()).toInt()
|
||||
socialRepository.retrieveInboxMessages(recipientID ?: "", page)
|
||||
.subscribe(Consumer {
|
||||
if (it.size != 10) lastFetchWasEnd = true
|
||||
callback.onResult(it)
|
||||
if (it.size < 10) {
|
||||
lastFetchWasEnd = true
|
||||
if (footer != null)
|
||||
callback.onResult(it.plusElement(footer!!))
|
||||
else
|
||||
callback.onResult(it)
|
||||
}
|
||||
else
|
||||
callback.onResult(it)
|
||||
}, RxErrorHandler.handleEmptyError())
|
||||
}
|
||||
}
|
||||
|
|
@ -116,23 +123,28 @@ private class MessagesDataSource(val socialRepository: SocialRepository, var rec
|
|||
if (recipientID?.isNotBlank() != true) { return@flatMapPublisher Flowable.just(it) }
|
||||
socialRepository.retrieveInboxMessages(recipientID ?: "", 0)
|
||||
.doOnNext {
|
||||
messages -> if (messages.size != 10) lastFetchWasEnd = true
|
||||
messages -> if (messages.size < 10) {
|
||||
lastFetchWasEnd = true
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Flowable.just(it)
|
||||
}
|
||||
}
|
||||
.subscribe(Consumer {
|
||||
callback.onResult(it, 0)
|
||||
if (it.size < 10 && footer != null)
|
||||
callback.onResult(it.plusElement(footer!!), 0)
|
||||
else
|
||||
callback.onResult(it, 0)
|
||||
}, RxErrorHandler.handleEmptyError())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class MessagesDataSourceFactory(val socialRepository: SocialRepository, var recipientID: String?) :
|
||||
private class MessagesDataSourceFactory(val socialRepository: SocialRepository, var recipientID: String?, val footer : ChatMessage?) :
|
||||
DataSource.Factory<Int, ChatMessage>() {
|
||||
val sourceLiveData = MutableLiveData<MessagesDataSource>()
|
||||
var latestSource: MessagesDataSource = MessagesDataSource(socialRepository, recipientID)
|
||||
var latestSource: MessagesDataSource = MessagesDataSource(socialRepository, recipientID, footer)
|
||||
|
||||
fun updateRecipientID(newID: String?) {
|
||||
recipientID = newID
|
||||
|
|
@ -140,7 +152,7 @@ private class MessagesDataSourceFactory(val socialRepository: SocialRepository,
|
|||
}
|
||||
|
||||
override fun create(): DataSource<Int, ChatMessage> {
|
||||
latestSource = MessagesDataSource(socialRepository, recipientID)
|
||||
latestSource = MessagesDataSource(socialRepository, recipientID, footer)
|
||||
sourceLiveData.postValue(latestSource)
|
||||
return latestSource
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue