fix: 修复搜索添加好友跳转出错问题

This commit is contained in:
糕小菜 2024-12-15 23:04:11 +08:00
parent 00e34df0fb
commit 910d26c0f4
6 changed files with 77 additions and 82 deletions

View File

@ -11,6 +11,9 @@ class ApplyAddFriendActivity : BaseActivity<ActivityApplyAddFriendBinding>() {
private val contactViewModel: ContactViewModel by viewModels()
private lateinit var contactId: String
private lateinit var contactNickname: String
override fun inflateBinding(): ActivityApplyAddFriendBinding =
ActivityApplyAddFriendBinding.inflate(layoutInflater)
@ -18,16 +21,19 @@ class ApplyAddFriendActivity : BaseActivity<ActivityApplyAddFriendBinding>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setListener()
}
val contactId = intent.getStringExtra("contactId")
private fun setListener() {
binding.tvSendApply.setOnClickListener {
sendContactRequest(contactId)
}
}
override fun initData() {
contactId = intent.getStringExtra("contactId") ?: ""
contactNickname = intent.getStringExtra("contactNickname") ?: ""
binding.etRemark.setHint(contactNickname)
}
private fun sendContactRequest(contactId: String?) {

View File

@ -45,11 +45,13 @@ class ApplyFriendsDetailActivity : BaseActivity<ActivityApplyFriendsDetailBindin
}
private fun setOnClick() {
binding.rlSendMessage.setOnClickListener {
binding.tvAddContact.setOnClickListener {
val contactId = user?.username
contactId?.let {
val intent = Intent(this, ApplyAddFriendActivity::class.java)
intent.putExtra("contactId", it)
val intent = Intent(this, ApplyAddFriendActivity::class.java).apply {
putExtra("contactId", it)
putExtra("contactNickname", user?.nickname)
}
startActivity(intent)
}
}

View File

@ -5,17 +5,14 @@ import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore
import com.kaixed.kchat.data.DataBase
import com.kaixed.kchat.data.local.entity.Contact
import com.kaixed.kchat.data.local.entity.Contact_
import com.kaixed.kchat.databinding.ActivityContactsDetailBinding
import com.kaixed.kchat.ui.base.BaseActivity
import io.objectbox.Box
class ContactsDetailActivity : BaseActivity<ActivityContactsDetailBinding>() {
private val contactBox: Box<Contact> by lazy { getBoxStore().boxFor(Contact::class.java) }
private var contactId: String? = null
private var contactNickname: String? = null
@ -94,7 +91,7 @@ class ContactsDetailActivity : BaseActivity<ActivityContactsDetailBinding>() {
}
private fun getContactInfo(contactId: String): Contact {
return contactBox
return DataBase.contactBox
.query(Contact_.username.equal(contactId))
.build()
.findFirst()!!

View File

@ -38,10 +38,56 @@ class SearchFriendsActivity : BaseActivity<ActivitySearchFriendsBinding>() {
enableEdgeToEdge()
initView()
setObserver()
setOnListener()
}
setOnClick()
private fun setOnListener() {
binding.etSearch.postDelayed({
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(binding.etSearch, InputMethodManager.SHOW_IMPLICIT)
}, 200)
binding.clSearchUser.setOnClickListener {
loadingDialog = LoadingDialogFragment.newInstance("正在搜索中...")
loadingDialog.showLoading(supportFragmentManager)
val username = binding.etSearch.text.toString()
searchUser(username)
}
binding.tvCancel.setOnClickListener {
finish()
}
binding.clFriends.setOnClickListener {
val intent = Intent(this, ApplyFriendsDetailActivity::class.java).apply {
putExtra("user", userItem)
}
startActivity(intent)
}
binding.etSearch.requestFocus()
binding.etSearch.addTextChangedListener(afterTextChanged = {
it?.let {
binding.tvNothing.visibility = View.INVISIBLE
if (it.isEmpty()) {
binding.clFriends.visibility = View.INVISIBLE
binding.clSearchUser.visibility = View.INVISIBLE
} else {
binding.clSearchUser.visibility = View.VISIBLE
val spannableString = SpannableString("搜索:$it")
val colorSpan = ForegroundColorSpan(Color.parseColor("#2BA245"))
spannableString.setSpan(
colorSpan, 3, it.length + 3,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
binding.tvSearchContent.text = spannableString
}
isSearching = it.isNotEmpty()
}
})
}
private fun setObserver() {
@ -73,61 +119,9 @@ class SearchFriendsActivity : BaseActivity<ActivitySearchFriendsBinding>() {
override fun initData() {
}
private fun setOnClick() {
binding.clFriends.setOnClickListener {
Intent(this, ApplyFriendsDetailActivity::class.java).apply {
putExtra("user", userItem)
}
startActivity(intent)
}
}
private fun initView() {
binding.clFriends.visibility = View.INVISIBLE
binding.tvTitle.visibility = View.INVISIBLE
binding.etSearch.requestFocus()
binding.etSearch.addTextChangedListener(afterTextChanged = {
it?.let {
binding.tvNothing.visibility = View.INVISIBLE
if (it.isEmpty()) {
binding.clFriends.visibility = View.INVISIBLE
binding.clSearchUser.visibility = View.INVISIBLE
} else {
binding.clSearchUser.visibility = View.VISIBLE
val spannableString = SpannableString("搜索:$it")
val colorSpan = ForegroundColorSpan(Color.parseColor("#2BA245"))
spannableString.setSpan(
colorSpan,
3,
it.length + 3,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
binding.tvSearchContent.text = spannableString
}
isSearching = it.isNotEmpty()
}
})
binding.etSearch.postDelayed({
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(binding.etSearch, InputMethodManager.SHOW_IMPLICIT)
}, 200)
binding.clSearchUser.setOnClickListener {
loadingDialog = LoadingDialogFragment.newInstance("正在搜索中...")
loadingDialog.showLoading(supportFragmentManager)
val username = binding.etSearch.text.toString()
searchUser(username)
}
binding.tvCancel.setOnClickListener {
finish()
}
}
private fun searchUser(username: String) {

View File

@ -69,11 +69,13 @@
app:cardMaxElevation="0dp">
<EditText
android:id="@+id/et_remark"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F7F7F7"
android:hint="hh"
android:paddingHorizontal="12dp" />
android:paddingHorizontal="12dp"
android:textCursorDrawable="@drawable/cursor"
android:textSize="15sp" />
</androidx.cardview.widget.CardView>

View File

@ -178,25 +178,19 @@
android:layout_height="8dp"
android:background="#E5E5E5" />
<RelativeLayout
android:id="@+id/rl_send_message"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingStart="15dp"
tools:ignore="RtlSymmetry">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/tv_add_contact"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:gravity="center"
android:text="添加到通讯录"
android:textColor="@color/normal"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.7dp"