Skip to content

Commit

Permalink
feat: fix the issue of friend addition failure bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cofbro committed Dec 16, 2023
1 parent ccaba2e commit a9b3da7
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 50 deletions.
80 changes: 37 additions & 43 deletions app/src/main/java/com/cofbro/qian/friend/FriendFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ class FriendFragment : BaseFragment<FriendViewModel, FragmentFriendBinding>(), I

/**
* data分为两部分获取
* 1 -> conv
* 2 -> user
* 1 -> 聊天的conversation
* 2 -> 用户好友请求的conversation
*/
viewModel.realConversationLiveData.observe(this) {
messageListAdapter?.setData(it)
Expand Down Expand Up @@ -304,27 +304,13 @@ class FriendFragment : BaseFragment<FriendViewModel, FragmentFriendBinding>(), I
val username = user["username"].toString()
val url = user["avatar"].toString()
val data =
MsgFactory.createConversationMsg(convList.getOrNull(index), url, username, user.objectId)
MsgFactory.createConversationMsg(
convList.getOrNull(index),
url,
username,
user.objectId
)
messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
// messageConv.add(data)
}
viewModel.realConversationLiveData.postValue(messageConv)
}, onError = {})
Expand Down Expand Up @@ -379,18 +365,6 @@ class FriendFragment : BaseFragment<FriendViewModel, FragmentFriendBinding>(), I
}
}

override fun onMessage(
message: LCIMMessage?,
conversation: LCIMConversation?,
client: LCIMClient?
) {
Log.d(TAG, "onMessage: 收到消息")
insertUserListIfNewFriend(message) { username, url ->
insertMessageAccordingToConv(conversation, username, url)
}
MessageSubscriber.dispatch(conversation, message)
}

private fun insertUserListIfNewFriend(
message: LCIMMessage?,
onSuccess: (String, String) -> Unit
Expand All @@ -404,6 +378,7 @@ class FriendFragment : BaseFragment<FriendViewModel, FragmentFriendBinding>(), I
return
}
}
// 不是好友,则向好友列表插入数据
IMClientUtils.queryContainUserForConversation(message?.from ?: "",
onSuccess = {
it.getOrNull(0)?.apply {
Expand All @@ -425,14 +400,14 @@ class FriendFragment : BaseFragment<FriendViewModel, FragmentFriendBinding>(), I
username: String = "",
url: String = ""
) {
notifyConversationMsgChanged(conversation)
// 如果找不到一样的,说明列表中没有该对话,应该将其插入对话列表中
val data = MsgFactory.createConversationMsg(conversation, url, username)
messageConv.add(0, data)
messageListAdapter?.insertBeforeFirst(data)
if (!notifyConversationMsgChanged(conversation)) {
// 如果找不到一样的,说明列表中没有该对话,应该将其插入对话列表中
val data = MsgFactory.createConversationMsg(conversation, url, username)
insertMessageConv(data)
}
}

fun notifyConversationMsgChanged(conversation: LCIMConversation?) {
fun notifyConversationMsgChanged(conversation: LCIMConversation?): Boolean {
messageConv.forEachIndexed { index, convItem ->
val conv = convItem.getObject("conv", LCIMConversation::class.java)
if (conversation?.conversationId == conv.conversationId) {
Expand All @@ -441,9 +416,22 @@ class FriendFragment : BaseFragment<FriendViewModel, FragmentFriendBinding>(), I
convItem["time"] = conversation?.lastMessageAt?.time.toString()
convItem["unReadCount"] = conversation?.unreadMessagesCount.toString()
messageListAdapter?.notifyItemChanged(index)
return
return true
}
}
return false
}

override fun onMessage(
message: LCIMMessage?,
conversation: LCIMConversation?,
client: LCIMClient?
) {
Log.d(TAG, "onMessage: 收到消息")
insertUserListIfNewFriend(message) { username, url ->
insertMessageAccordingToConv(conversation, username, url)
}
MessageSubscriber.dispatch(conversation, message)
}

override fun onInvite(client: LCIMClient?, conversation: LCIMConversation?, operator: String?) {
Expand Down Expand Up @@ -530,6 +518,13 @@ class FriendFragment : BaseFragment<FriendViewModel, FragmentFriendBinding>(), I
fun insertDataIntoUserList(data: List<LCObject>) {
friendList.addAll(0, data)
userListAdapter?.insertItemRange(data)
userListAdapter?.setMessageConv(messageConv)
}

private fun insertMessageConv(data: JSONObject) {
messageConv.add(0, data)
userListAdapter?.setMessageConv(messageConv)
messageListAdapter?.insertBeforeFirst(data)
}

/**
Expand All @@ -545,8 +540,7 @@ class FriendFragment : BaseFragment<FriendViewModel, FragmentFriendBinding>(), I
val url = user["avatar"].toString()
val data = MsgFactory.createConversationMsg(conversation, url, username)
// 更新数据源
messageConv.add(data)
messageListAdapter?.insertBeforeFirst(data)
insertMessageConv(data)
onSuccess(user)
}, onError = {
Log.d(TAG, "insertConv: 查询单个用户失败")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class ChatActivity : BaseActivity<ChatViewModel, ActivityChatBinding>(), IMessag
super.onDestroy()
MessageSubscriber.unsubscribe(this)
unregisterKeyboardHeight()
conv?.read()
}

override fun onMessage(conv: LCIMConversation, message: LCIMMessage?) {
Expand Down Expand Up @@ -250,6 +249,7 @@ class ChatActivity : BaseActivity<ChatViewModel, ActivityChatBinding>(), IMessag
}

override fun onStop() {
conv?.read()
findFriendFragment()?.notifyConversationMsgChanged(conv)
super.onStop()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ class FriendRequestAdapter : RecyclerView.Adapter<FriendRequestAdapter.FriendReq
data = ArrayList(d)
notifyDataSetChanged()
}

fun remove(pos: Int) {
data.removeAt(pos)
notifyItemRemoved(pos)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class FriendRequestFragment(private val conv: List<LCIMConversation>) : DialogFr
setOnItemClickListener { pos ->
findFriendFragment()?.let { friendFragment ->
conv.getOrNull(pos)?.let {
// 更新好友请求列表
mAdapter?.remove(pos)
// 更新聊天列表
friendFragment.responseFriendRequest(it, true)
// 更新用户列表
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class SearchFriendActivity : BaseActivity<SearchFriendViewModel, ActivitySearchF
IMClientUtils.createNewConversation(
uid,
onSuccess = {
clearText()
ToastUtils.show("好友申请发送成功")
},
onError = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ class FriendsViewHolder<T : Friends>(private val binding: ItemSearchfrendsListBi
if (t?.isfriend == true) {
visibility = View.GONE
} else {
setItemClickListener {
itemClick?.invoke()
}
bindAction()
}
}
}

fun setItemClickListener(itemClickListener: () -> Unit) {
itemClick = itemClickListener
}

private fun bindAction() {
itemView.setOnClickListener {
itemClick?.invoke()
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/cofbro/qian/utils/MsgFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object MsgFactory {
item["avatar"] = friend.getString("avatar")
item["uid"] = friend.getString("objectId")
item["isCreator"] = isCreator
item["content"] = if (isCreator) "请求添加好友" else "好友申请已发送~"
item["content"] = if (isCreator) "好友申请已发送~" else "请求添加您为好友"
item["status"] = conversation?.get("agree").toString()
return item
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<string name="advice_title">如何与我们取得联系?</string>
<string name="advice_qq_title">1. 我们拥有QQ群聊</string>
<string name="advice_qq_content">目前已经拥有 100+ 群友,在这里你可以发表任何关于技术或者使用体验方面的话题。无论您是否懂技术,欢迎随时加入我们!</string>
<string name="advice_qq_content">目前已经拥有 300+ 群友,在这里你可以发表任何关于技术或者使用体验方面的话题。无论您是否懂技术,欢迎随时加入我们!</string>
<string name="advice_qq_number">QQ群聊:567290278</string>
<string name="advice_github_title">2. Github当然必不可少</string>
<string name="advice_github_content">Github作为最出名的开源社区,当然不能少。如果您使用本软件的同时,恰好也喜欢技术,欢迎在fork后进行pr,或者您可以在issue中留下更好的建议!</string>
Expand Down

0 comments on commit a9b3da7

Please sign in to comment.