refactor(添加好友模块): 合并添加联系人Repo
合并添加联系人Repo
This commit is contained in:
parent
c1a28b9bef
commit
a2d4513e57
@ -1,58 +0,0 @@
|
||||
package com.kaixed.kchat.repository;
|
||||
|
||||
import static com.kaixed.kchat.network.NetworkInterface.ADD_FRIEND;
|
||||
import static com.kaixed.kchat.network.NetworkInterface.SERVER_URL;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.kaixed.kchat.database.UserManager;
|
||||
import com.kaixed.kchat.model.response.ApplyFriend;
|
||||
import com.kaixed.kchat.network.NetworkRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
/**
|
||||
* @Author: kaixed
|
||||
* @Date: 2024/10/15 17:24
|
||||
* @Description: TODO
|
||||
*/
|
||||
public class AddFriendRepo {
|
||||
|
||||
public MutableLiveData<ApplyFriend> addFriend(String contactId, String message) {
|
||||
MutableLiveData<ApplyFriend> applyFriendMutableLiveData = new MutableLiveData<>();
|
||||
|
||||
RequestBody requestBody = new FormBody.Builder()
|
||||
.add("senderId", UserManager.getInstance().getUsername())
|
||||
.add("receiverId", contactId)
|
||||
.add("message", message)
|
||||
.build();
|
||||
|
||||
new NetworkRequest().postAsync(SERVER_URL + ADD_FRIEND, requestBody, new Callback() {
|
||||
@Override
|
||||
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
||||
applyFriendMutableLiveData.postValue(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
String responseBody = response.body().string();
|
||||
ApplyFriend applyFriend = new Gson().fromJson(responseBody, ApplyFriend.class);
|
||||
applyFriendMutableLiveData.postValue(applyFriend);
|
||||
} else {
|
||||
applyFriendMutableLiveData.postValue(null);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return applyFriendMutableLiveData;
|
||||
}
|
||||
}
|
@ -2,9 +2,12 @@ package com.kaixed.kchat.repository
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.google.gson.Gson
|
||||
import com.kaixed.kchat.database.UserManager
|
||||
import com.kaixed.kchat.model.friend.AcceptContactRequest
|
||||
import com.kaixed.kchat.model.friend.ContactRequestResponse
|
||||
import com.kaixed.kchat.model.response.ApplyFriend
|
||||
import com.kaixed.kchat.network.NetworkInterface.ACCEPT_CONTACT_REQUEST
|
||||
import com.kaixed.kchat.network.NetworkInterface.ADD_FRIEND
|
||||
import com.kaixed.kchat.network.NetworkInterface.FRIEND_REQUEST_LIST
|
||||
import com.kaixed.kchat.network.NetworkInterface.SERVER_URL
|
||||
import com.kaixed.kchat.network.NetworkRequest
|
||||
@ -86,4 +89,33 @@ class ContactRepo {
|
||||
)
|
||||
return acceptFriendMutableLiveData
|
||||
}
|
||||
|
||||
fun addContact(contactId: String, message: String): MutableLiveData<ApplyFriend?> {
|
||||
val mutableLiveData = MutableLiveData<ApplyFriend?>()
|
||||
val requestBody = FormBody.Builder()
|
||||
.add("senderId", UserManager.getInstance().username)
|
||||
.add("receiverId", contactId)
|
||||
.add("message", message)
|
||||
.build()
|
||||
|
||||
NetworkRequest().postAsync(SERVER_URL + ADD_FRIEND, requestBody, object : Callback {
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
if (response.isSuccessful && response.body != null) {
|
||||
val responseBody = response.body!!.string()
|
||||
val applyFriend =
|
||||
Json.decodeFromString<ApplyFriend>(responseBody)
|
||||
mutableLiveData.postValue(applyFriend)
|
||||
} else {
|
||||
mutableLiveData.postValue(null)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call, e: IOException) {
|
||||
mutableLiveData.postValue(null)
|
||||
}
|
||||
|
||||
})
|
||||
return mutableLiveData
|
||||
}
|
||||
|
||||
}
|
@ -13,7 +13,7 @@ import com.kaixed.kchat.databinding.ActivityApplyAddFriendBinding
|
||||
import com.kaixed.kchat.viewmodel.ApplyFriendViewModel
|
||||
import com.kaixed.kchat.viewmodel.SearchFriendsViewModel
|
||||
|
||||
class ApplyAddFriendActivity : AppCompatActivity() {
|
||||
class ApplyAddFriendActivity : BaseActivity() {
|
||||
private lateinit var binding: ActivityApplyAddFriendBinding
|
||||
|
||||
private val applyAddFriendViewModel: ApplyFriendViewModel by viewModels()
|
||||
@ -30,7 +30,7 @@ class ApplyAddFriendActivity : AppCompatActivity() {
|
||||
Log.d("haha", contactId.toString())
|
||||
|
||||
if (contactId != null) {
|
||||
applyAddFriendViewModel.addFriends(contactId, binding.etMessage.text.toString())
|
||||
applyAddFriendViewModel.addContact(contactId, binding.etMessage.text.toString())
|
||||
?.observe(this) { value ->
|
||||
if (value != null) {
|
||||
runOnUiThread {
|
||||
|
@ -3,7 +3,7 @@ package com.kaixed.kchat.viewmodel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.kaixed.kchat.model.response.ApplyFriend
|
||||
import com.kaixed.kchat.repository.AddFriendRepo
|
||||
import com.kaixed.kchat.repository.ContactRepo
|
||||
|
||||
/**
|
||||
* @Author: kaixed
|
||||
@ -11,9 +11,9 @@ import com.kaixed.kchat.repository.AddFriendRepo
|
||||
* @Description: TODO
|
||||
*/
|
||||
class ApplyFriendViewModel : ViewModel() {
|
||||
private var addFriendRepo: AddFriendRepo = AddFriendRepo()
|
||||
private var contactRepo: ContactRepo = ContactRepo()
|
||||
|
||||
fun addContact(contactId: String, message: String): MutableLiveData<ApplyFriend?> =
|
||||
contactRepo.addContact(contactId, message)
|
||||
|
||||
fun addFriends(contactId: String, message: String): MutableLiveData<ApplyFriend>? {
|
||||
return addFriendRepo.addFriend(contactId,message)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user