refactor: 修改Repo层和ViewModel层

修改Repo层和ViewModel层为kotlin语言
This commit is contained in:
糕小菜 2024-10-23 20:40:11 +08:00
parent 3a15994c48
commit 614e0af3ba
18 changed files with 214 additions and 272 deletions

View File

@ -1,11 +1,14 @@
package com.kaixed.kchat.model.search
import kotlinx.serialization.Serializable
/**
* @Author: kaixed
* @Date: 2024/9/22 22:59
*/
@Serializable
data class SearchFriends(
val code: String,
val msg: String,
val data: User
val `data`: User
)

View File

@ -2,11 +2,14 @@ package com.kaixed.kchat.model.search
import android.os.Parcel
import android.os.Parcelable
import kotlinx.serialization.Serializable
/**
* @Author: kaixed
* @Date: 2024/9/22 22:58
*/
@Serializable
data class User(
val username: String,
val nickname: String,

View File

@ -5,11 +5,16 @@ 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.friend.FriendItem
import com.kaixed.kchat.model.friend.FriendResponse
import com.kaixed.kchat.model.response.ApplyFriend
import com.kaixed.kchat.model.search.SearchFriends
import com.kaixed.kchat.network.NetworkInterface.ACCEPT_CONTACT_REQUEST
import com.kaixed.kchat.network.NetworkInterface.ADD_FRIEND
import com.kaixed.kchat.network.NetworkInterface.FRIEND_LIST
import com.kaixed.kchat.network.NetworkInterface.FRIEND_REQUEST_LIST
import com.kaixed.kchat.network.NetworkInterface.SERVER_URL
import com.kaixed.kchat.network.NetworkInterface.USER_LIST
import com.kaixed.kchat.network.NetworkRequest
import kotlinx.serialization.json.Json
import okhttp3.Call
@ -121,4 +126,50 @@ class ContactRepo {
return mutableLiveData
}
fun searchContact(username: String): MutableLiveData<SearchFriends?> {
val listMutableLiveData = MutableLiveData<SearchFriends?>()
NetworkRequest().getAsync("$SERVER_URL$USER_LIST$username", object : Callback {
override fun onFailure(call: Call, e: IOException) {
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
response.body?.string()?.let { responseBody ->
val searchFriends = Json.decodeFromString<SearchFriends>(responseBody)
listMutableLiveData.postValue(searchFriends)
} ?: run {
listMutableLiveData.postValue(null)
}
}
})
return listMutableLiveData
}
fun getContactList(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
}
}

View File

@ -1,47 +0,0 @@
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.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
*/
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
}
}

View File

@ -1,54 +0,0 @@
package com.kaixed.kchat.repository;
import static com.kaixed.kchat.network.NetworkInterface.SERVER_URL;
import static com.kaixed.kchat.network.NetworkInterface.USER_LOGIN;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import com.google.gson.Gson;
import com.kaixed.kchat.model.login.Login;
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 hui
*/
public class LoginRepository {
public MutableLiveData<Login> login(String username, String password) {
MutableLiveData<Login> loginResult = new MutableLiveData<>();
RequestBody requestBody = new FormBody.Builder()
.add("username", username)
.add("password", password)
.build();
new NetworkRequest().postAsync(SERVER_URL + USER_LOGIN, requestBody, new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
loginResult.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();
Login login = new Gson().fromJson(responseBody, Login.class);
loginResult.postValue(login);
} else {
loginResult.postValue(null);
}
}
});
return loginResult;
}
}

View File

@ -1,47 +0,0 @@
package com.kaixed.kchat.repository;
import static com.kaixed.kchat.network.NetworkInterface.SERVER_URL;
import static com.kaixed.kchat.network.NetworkInterface.USER_LIST;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import com.google.gson.Gson;
import com.kaixed.kchat.model.search.SearchFriends;
import com.kaixed.kchat.network.NetworkRequest;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* @Author: kaixed
* @Date: 2024/6/1 20:53
*/
public class SearchFriendsRepository {
public MutableLiveData<SearchFriends> searchFriends(String username) {
MutableLiveData<SearchFriends> listMutableLiveData = new MutableLiveData<>();
new NetworkRequest().getAsync(SERVER_URL + USER_LIST + username, new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (response.isSuccessful() && response.body() != null) {
String responseBody = response.body().string();
SearchFriends searchFriends = new Gson().fromJson(responseBody, SearchFriends.class);
listMutableLiveData.postValue(searchFriends);
} else {
listMutableLiveData.postValue(null);
}
}
});
return listMutableLiveData;
}
}

View File

@ -0,0 +1,65 @@
package com.kaixed.kchat.repository
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.kaixed.kchat.model.login.Login
import com.kaixed.kchat.model.user.UserList
import com.kaixed.kchat.network.NetworkInterface.SERVER_URL
import com.kaixed.kchat.network.NetworkInterface.USER_LIST
import com.kaixed.kchat.network.NetworkInterface.USER_LOGIN
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/23 20:21
*/
class UserRepo {
fun login(username: String, password: String): MutableLiveData<Login?> {
val loginMutableLiveData = MutableLiveData<Login?>()
val requestBody = FormBody.Builder()
.add("username", username)
.add("password", password)
.build()
NetworkRequest().postAsync(
"$SERVER_URL$USER_LOGIN",
requestBody,
object : Callback {
override fun onFailure(call: Call, e: IOException) {
loginMutableLiveData.postValue(null)
}
override fun onResponse(call: Call, response: Response) {
response.body?.string()?.let { responseBody ->
val login = Gson().fromJson(responseBody, Login::class.java)
loginMutableLiveData.postValue(login)
}
}
})
return loginMutableLiveData
}
fun getUserListByNickname(username: String): MutableLiveData<UserList> {
val listMutableLiveData = MutableLiveData<UserList>()
NetworkRequest().getAsync("$SERVER_URL$USER_LIST$username", object : Callback {
override fun onFailure(call: Call, e: IOException) {
}
override fun onResponse(call: Call, response: Response) {
response.body?.string()?.let { responseBody ->
val userList = Gson().fromJson(responseBody, UserList::class.java)
listMutableLiveData.postValue(userList)
}
}
})
return listMutableLiveData
}
}

View File

@ -1,47 +0,0 @@
package com.kaixed.kchat.repository;
import static com.kaixed.kchat.network.NetworkInterface.SERVER_URL;
import static com.kaixed.kchat.network.NetworkInterface.USER_LIST;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import com.google.gson.Gson;
import com.kaixed.kchat.model.user.UserList;
import com.kaixed.kchat.network.NetworkRequest;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* @Author: kaixed
* @Date: 2024/6/13 12:34
*/
public class UserRepository {
public MutableLiveData<UserList> getUserListByNickname(String username) {
MutableLiveData<UserList> listMutableLiveData = new MutableLiveData<>();
new NetworkRequest().getAsync(SERVER_URL + USER_LIST + username, new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (response.isSuccessful() && response.body() != null) {
String responseBody = response.body().string();
UserList userList = new Gson().fromJson(responseBody, UserList.class);
listMutableLiveData.postValue(userList);
} else {
listMutableLiveData.postValue(null);
}
}
});
return listMutableLiveData;
}
}

View File

@ -0,0 +1,34 @@
package com.kaixed.kchat.utils
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.text.style.ImageSpan
/**
* @Author: kaixed
* @Date: 2024/10/23 18:07
*/
internal class CenteredImageSpan(drawable: Drawable?) :
ImageSpan(drawable!!, ALIGN_BASELINE) {
override fun draw(
canvas: Canvas,
text: CharSequence?,
start: Int,
end: Int,
x: Float,
top: Int,
y: Int,
bottom: Int,
paint: Paint
) {
val drawable = drawable
val fm = paint.fontMetricsInt
val transY = (y + fm.descent + y + fm.ascent) / 2 - drawable.bounds.bottom / 2
canvas.save()
canvas.translate(x, transY.toFloat())
drawable.draw(canvas)
canvas.restore()
}
}

View File

@ -16,7 +16,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Author: kaixed
* @Date: 2024/5/30 19:47
@ -48,9 +47,10 @@ public class ImageSpanUtil {
if (emojiMap.containsKey(matcher.group())) {
Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), emojiMap.get(matcher.group()), null);
assert drawable != null;
drawable.setBounds(0, 0, textSize, textSize);
ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
spannableString.setSpan(imageSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
int newSize = (int) (textSize * 1.2);
drawable.setBounds(0, 0, newSize, newSize);
CenteredImageSpan centeredImageSpan = new CenteredImageSpan(drawable);
spannableString.setSpan(centeredImageSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
return spannableString;
@ -64,10 +64,12 @@ public class ImageSpanUtil {
if (emojiMap.containsKey(emojiStr)) {
Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), emojiMap.get(emojiStr), null);
if (drawable != null) {
drawable.setBounds(0, 0, textSize, textSize);
ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
int newSize = (int) (textSize * 1.2);
drawable.setBounds(0, 0, newSize, newSize);
CenteredImageSpan centeredImageSpan = new CenteredImageSpan(drawable);
// ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_CENTER);
// 应用ImageSpan到Editable对象
editable.setSpan(imageSpan, index, index + emojiStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
editable.setSpan(centeredImageSpan, index, index + emojiStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}

View File

@ -40,7 +40,7 @@ class FriendListActivity : AppCompatActivity() {
private fun getFriendList(username: String) {
friendListViewModel.getFriendList(username).observe(this) { value ->
if (value != null) {
value?.let {
friendList.addAll(value)
binding.recycleFriendList.adapter?.notifyDataSetChanged()

View File

@ -26,15 +26,15 @@ class ProfileActivity : AppCompatActivity() {
binding.rlSetting.setOnClickListener {
// val intent = Intent(this, SettingActivity::class.java)
// startActivity(intent)
supportFragmentManager
.beginTransaction()
.replace(
binding.fragmentContainer.id,
SettingsFragment()
) // fragment_container 是你的布局中的容器ID
.commit()
binding.clProfile.visibility = View.GONE
binding.llSettingItems.visibility = View.GONE
// supportFragmentManager
// .beginTransaction()
// .replace(
// binding.fragmentContainer.id,
// SettingsFragment()
// ) // fragment_container 是你的布局中的容器ID
// .commit()
// binding.clProfile.visibility = View.GONE
// binding.llSettingItems.visibility = View.GONE
}
}

View File

@ -1,12 +0,0 @@
package com.kaixed.kchat.viewmodel;
import androidx.lifecycle.ViewModel;
/**
* @Author: kaixed
* @Date: 2024/6/1 20:52
*/
public class AddFriendViewModel extends ViewModel {
}

View File

@ -4,11 +4,8 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.kaixed.kchat.model.friend.AcceptContactRequest
import com.kaixed.kchat.model.friend.ContactRequestResponse
import com.kaixed.kchat.model.friend.FriendRequestItem
import com.kaixed.kchat.model.friend.FriendResponse
import com.kaixed.kchat.model.search.SearchFriends
import com.kaixed.kchat.repository.ContactRepo
import com.kaixed.kchat.repository.SearchFriendsRepository
/**
* @Author: kaixed
@ -16,17 +13,19 @@ import com.kaixed.kchat.repository.SearchFriendsRepository
*/
class ContactViewModel : ViewModel() {
private val contactRepo = ContactRepo()
private val searchFriendsRepo = SearchFriendsRepository()
fun getContactRequestList(username: String): MutableLiveData<ContactRequestResponse?> =
contactRepo.getContactRequestList(username)
fun searchFriends(username: String): MutableLiveData<SearchFriends>? =
searchFriendsRepo.searchFriends(username)
fun searchFriends(username: String): MutableLiveData<SearchFriends?> =
contactRepo.searchContact(username)
fun acceptContactRequest(username: String, contactId: String): MutableLiveData<AcceptContactRequest?> =
fun acceptContactRequest(
username: String,
contactId: String
): MutableLiveData<AcceptContactRequest?> =
contactRepo.acceptContactRequest(username, contactId)

View File

@ -1,15 +1,18 @@
package com.kaixed.kchat.viewmodel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.kaixed.kchat.repository.FriendListRepo
import com.kaixed.kchat.model.friend.FriendItem
import com.kaixed.kchat.repository.ContactRepo
/**
* @Author: kaixed
* @Date: 2024/10/17 22:02
*/
class FriendListViewModel : ViewModel() {
private val friendListRepo = FriendListRepo()
private val contactRepo = ContactRepo()
fun getFriendList(username: String) = friendListRepo.getFriendList(username)
fun getFriendList(username: String): MutableLiveData<List<FriendItem>?> =
contactRepo.getContactList(username)
}

View File

@ -1,24 +1,18 @@
package com.kaixed.kchat.viewmodel;
package com.kaixed.kchat.viewmodel
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.kaixed.kchat.model.login.Login;
import com.kaixed.kchat.repository.LoginRepository;
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.kaixed.kchat.model.login.Login
import com.kaixed.kchat.repository.UserRepo
/**
* @Author: kaixed
* @Date: 2024/5/27 9:29
* @Date: 2024/10/23 20:19
*/
public class LoginViewModel extends ViewModel {
private final LoginRepository loginRepository;
class LoginViewModel : ViewModel() {
private val userRepo: UserRepo = UserRepo()
public LoginViewModel() {
loginRepository = new LoginRepository();
}
public MutableLiveData<Login> login(String username, String password) {
return loginRepository.login(username, password);
}
fun login(username: String, password: String): MutableLiveData<Login?> =
userRepo.login(username, password)
}

View File

@ -3,16 +3,16 @@ package com.kaixed.kchat.viewmodel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.kaixed.kchat.model.search.SearchFriends
import com.kaixed.kchat.repository.SearchFriendsRepository
import com.kaixed.kchat.repository.ContactRepo
/**
* @Author: kaixed
* @Date: 2024/9/22 23:04
*/
class SearchFriendsViewModel : ViewModel() {
private var searchFriendsRepo: SearchFriendsRepository = SearchFriendsRepository()
private var contactRepo: ContactRepo = ContactRepo()
fun searchFriends(username: String): MutableLiveData<SearchFriends>? =
searchFriendsRepo.searchFriends(username)
fun searchFriends(username: String): MutableLiveData<SearchFriends?> =
contactRepo.searchContact(username)
}

View File

@ -1,23 +1,18 @@
package com.kaixed.kchat.viewmodel;
package com.kaixed.kchat.viewmodel
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.kaixed.kchat.model.user.UserList;
import com.kaixed.kchat.repository.UserRepository;
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.kaixed.kchat.model.user.UserList
import com.kaixed.kchat.repository.UserRepo
/**
* @Author: kaixed
* @Date: 2024/6/13 12:49
* @Date: 2024/10/23 20:14
*/
public class UserViewModel extends ViewModel {
private final UserRepository userRepository;
class UserViewModel : ViewModel() {
private val userRepo: UserRepo = UserRepo()
public UserViewModel() {
this.userRepository = new UserRepository();
}
fun getUserListByNickname(username: String): MutableLiveData<UserList> =
userRepo.getUserListByNickname(username)
public MutableLiveData<UserList> getUserListByNickname(String username) {
return userRepository.getUserListByNickname(username);
}
}