fix(通讯录模块): 完善通讯录模块
完善通讯录模块
This commit is contained in:
parent
e02cf3de04
commit
5aa3f5e526
@ -0,0 +1,12 @@
|
|||||||
|
package com.kaixed.kchat.model.friend
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: kaixed
|
||||||
|
* @Date: 2024/10/17 22:00
|
||||||
|
* @Description: TODO
|
||||||
|
*/
|
||||||
|
data class FriendResponse(
|
||||||
|
val code: String,
|
||||||
|
val message: String,
|
||||||
|
val data: List<FriendItem>,
|
||||||
|
)
|
@ -20,4 +20,5 @@ object NetworkInterface {
|
|||||||
const val USER_LIST = "/users/list/"
|
const val USER_LIST = "/users/list/"
|
||||||
|
|
||||||
const val ADD_FRIEND = "/friend/request"
|
const val ADD_FRIEND = "/friend/request"
|
||||||
|
const val FRIEND_LIST = "/friend/list"
|
||||||
}
|
}
|
@ -24,9 +24,6 @@ class NetworkRequest {
|
|||||||
.post(formBody)
|
.post(formBody)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
Log.d("haha", url)
|
|
||||||
Log.d("haha", formBody.toString())
|
|
||||||
|
|
||||||
val call = client.newCall(request)
|
val call = client.newCall(request)
|
||||||
call.enqueue(callback)
|
call.enqueue(callback)
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.kaixed.kchat.repository
|
||||||
|
|
||||||
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.kaixed.kchat.model.friend.FriendItem
|
||||||
|
import com.kaixed.kchat.model.friend.FriendResponse
|
||||||
|
import com.kaixed.kchat.model.response.ApplyFriend
|
||||||
|
import com.kaixed.kchat.network.NetworkInterface.FRIEND_LIST
|
||||||
|
import com.kaixed.kchat.network.NetworkInterface.SERVER_URL
|
||||||
|
import com.kaixed.kchat.network.NetworkRequest
|
||||||
|
import okhttp3.Call
|
||||||
|
import okhttp3.Callback
|
||||||
|
import okhttp3.FormBody
|
||||||
|
import okhttp3.Response
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: kaixed
|
||||||
|
* @Date: 2024/10/17 21:53
|
||||||
|
* @Description: TODO
|
||||||
|
*/
|
||||||
|
class FriendListRepo {
|
||||||
|
fun getFriendList(username: String): MutableLiveData<List<FriendItem>?> {
|
||||||
|
val applyFriendMutableLiveData = MutableLiveData<List<FriendItem>?>()
|
||||||
|
|
||||||
|
val requestBody = FormBody.Builder()
|
||||||
|
.add("userid", username)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
NetworkRequest().postAsync(SERVER_URL + FRIEND_LIST, requestBody, object : Callback {
|
||||||
|
override fun onFailure(call: Call, e: IOException) {
|
||||||
|
applyFriendMutableLiveData.postValue(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResponse(call: Call, response: Response) {
|
||||||
|
if (response.isSuccessful && response.body != null) {
|
||||||
|
val responseBody = response.body!!.string()
|
||||||
|
val friendList = Gson().fromJson(responseBody, FriendResponse::class.java)
|
||||||
|
applyFriendMutableLiveData.postValue(friendList.data)
|
||||||
|
} else {
|
||||||
|
applyFriendMutableLiveData.postValue(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return applyFriendMutableLiveData
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,30 +1,48 @@
|
|||||||
package com.kaixed.kchat.view.activity
|
package com.kaixed.kchat.view.activity
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
|
import android.view.View
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import com.kaixed.kchat.database.UserManager
|
||||||
import com.kaixed.kchat.databinding.ActivityFriendListBinding
|
import com.kaixed.kchat.databinding.ActivityFriendListBinding
|
||||||
import com.kaixed.kchat.model.friend.FriendItem
|
import com.kaixed.kchat.model.friend.FriendItem
|
||||||
import com.kaixed.kchat.view.adapter.FriendListAdapter
|
import com.kaixed.kchat.view.adapter.FriendListAdapter
|
||||||
|
import com.kaixed.kchat.viewmodel.ApplyFriendViewModel
|
||||||
|
import com.kaixed.kchat.viewmodel.FriendListViewModel
|
||||||
|
|
||||||
class FriendListActivity : AppCompatActivity() {
|
class FriendListActivity : AppCompatActivity() {
|
||||||
private lateinit var binding: ActivityFriendListBinding
|
private lateinit var binding: ActivityFriendListBinding
|
||||||
|
private val friendListViewModel: FriendListViewModel by viewModels()
|
||||||
|
private var friendList: MutableList<FriendItem> = mutableListOf()
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
binding = ActivityFriendListBinding.inflate(layoutInflater)
|
binding = ActivityFriendListBinding.inflate(layoutInflater)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
val items = mutableListOf(
|
getFriendList(UserManager.getInstance().username)
|
||||||
FriendItem("hui", "aaa", "aaa", "aa", ""),
|
|
||||||
FriendItem("hui", "aaa", "aaa", "aa", ""),
|
if (friendList.isEmpty()) {
|
||||||
FriendItem("hui", "aaa", "aaa", "aa", ""),
|
binding.tvNothing.visibility = View.VISIBLE
|
||||||
)
|
} else {
|
||||||
|
binding.tvNothing.visibility = View.GONE
|
||||||
|
}
|
||||||
|
|
||||||
binding.recycleFriendList.layoutManager =
|
binding.recycleFriendList.layoutManager =
|
||||||
LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
|
LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
|
||||||
binding.recycleFriendList.adapter = FriendListAdapter(items, this)
|
binding.recycleFriendList.adapter = FriendListAdapter(friendList, this)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getFriendList(username: String) {
|
||||||
|
friendListViewModel.getFriendList(username).observe(this) { value ->
|
||||||
|
if (value != null) {
|
||||||
|
friendList = value as MutableList<FriendItem>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.kaixed.kchat.viewmodel
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import com.kaixed.kchat.repository.FriendListRepo
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: kaixed
|
||||||
|
* @Date: 2024/10/17 22:02
|
||||||
|
* @Description: TODO
|
||||||
|
*/
|
||||||
|
class FriendListViewModel : ViewModel() {
|
||||||
|
private val friendListRepo = FriendListRepo()
|
||||||
|
|
||||||
|
fun getFriendList(username: String) = friendListRepo.getFriendList(username)
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:id="@+id/main"
|
android:id="@+id/main"
|
||||||
@ -10,14 +10,31 @@
|
|||||||
tools:context=".view.activity.FriendListActivity">
|
tools:context=".view.activity.FriendListActivity">
|
||||||
|
|
||||||
<com.kaixed.kchat.view.widget.CustomTitleBar
|
<com.kaixed.kchat.view.widget.CustomTitleBar
|
||||||
|
android:id="@+id/ctb"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
app:titleName="通讯录" />
|
app:titleName="通讯录" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_nothing"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="您当前还没有好友哦。"
|
||||||
|
android:textColor="@color/black"
|
||||||
|
android:textSize="18sp"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/ctb"
|
||||||
|
app:layout_constraintVertical_bias="0.3" />
|
||||||
|
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/recycle_friend_list"
|
android:id="@+id/recycle_friend_list"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="0dp"
|
||||||
android:overScrollMode="never" />
|
android:overScrollMode="never"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/ctb" />
|
||||||
|
|
||||||
</LinearLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue
Block a user