From 614e0af3ba5c87aab19470fbf498253c438b1324 Mon Sep 17 00:00:00 2001 From: kaixed Date: Wed, 23 Oct 2024 20:40:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9Repo=E5=B1=82?= =?UTF-8?q?=E5=92=8CViewModel=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改Repo层和ViewModel层为kotlin语言 --- .../kchat/model/search/SearchFriends.kt | 5 +- .../com/kaixed/kchat/model/search/User.kt | 3 + .../kaixed/kchat/repository/ContactRepo.kt | 51 +++++++++++++++ .../kaixed/kchat/repository/FriendListRepo.kt | 47 -------------- .../kchat/repository/LoginRepository.java | 54 --------------- .../repository/SearchFriendsRepository.java | 47 -------------- .../com/kaixed/kchat/repository/UserRepo.kt | 65 +++++++++++++++++++ .../kchat/repository/UserRepository.java | 47 -------------- .../kaixed/kchat/utils/CenteredImageSpan.kt | 34 ++++++++++ .../com/kaixed/kchat/utils/ImageSpanUtil.java | 16 +++-- .../kchat/view/activity/FriendListActivity.kt | 2 +- .../kchat/view/activity/ProfileActivity.kt | 18 ++--- .../kchat/viewmodel/AddFriendViewModel.java | 12 ---- .../kchat/viewmodel/ContactViewModel.kt | 13 ++-- .../kchat/viewmodel/FriendListViewModel.kt | 9 ++- .../kaixed/kchat/viewmodel/LoginViewModel.kt | 28 ++++---- .../kchat/viewmodel/SearchFriendsViewModel.kt | 8 +-- .../kaixed/kchat/viewmodel/UserViewModel.kt | 27 ++++---- 18 files changed, 214 insertions(+), 272 deletions(-) delete mode 100644 app/src/main/java/com/kaixed/kchat/repository/FriendListRepo.kt delete mode 100644 app/src/main/java/com/kaixed/kchat/repository/LoginRepository.java delete mode 100644 app/src/main/java/com/kaixed/kchat/repository/SearchFriendsRepository.java create mode 100644 app/src/main/java/com/kaixed/kchat/repository/UserRepo.kt delete mode 100644 app/src/main/java/com/kaixed/kchat/repository/UserRepository.java create mode 100644 app/src/main/java/com/kaixed/kchat/utils/CenteredImageSpan.kt delete mode 100644 app/src/main/java/com/kaixed/kchat/viewmodel/AddFriendViewModel.java diff --git a/app/src/main/java/com/kaixed/kchat/model/search/SearchFriends.kt b/app/src/main/java/com/kaixed/kchat/model/search/SearchFriends.kt index 2fa1ab7..23f6207 100644 --- a/app/src/main/java/com/kaixed/kchat/model/search/SearchFriends.kt +++ b/app/src/main/java/com/kaixed/kchat/model/search/SearchFriends.kt @@ -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 ) diff --git a/app/src/main/java/com/kaixed/kchat/model/search/User.kt b/app/src/main/java/com/kaixed/kchat/model/search/User.kt index 3be9ba6..c2a8896 100644 --- a/app/src/main/java/com/kaixed/kchat/model/search/User.kt +++ b/app/src/main/java/com/kaixed/kchat/model/search/User.kt @@ -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, diff --git a/app/src/main/java/com/kaixed/kchat/repository/ContactRepo.kt b/app/src/main/java/com/kaixed/kchat/repository/ContactRepo.kt index 1c2eb4d..658df27 100644 --- a/app/src/main/java/com/kaixed/kchat/repository/ContactRepo.kt +++ b/app/src/main/java/com/kaixed/kchat/repository/ContactRepo.kt @@ -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 { + val listMutableLiveData = MutableLiveData() + + 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(responseBody) + listMutableLiveData.postValue(searchFriends) + } ?: run { + listMutableLiveData.postValue(null) + } + } + }) + return listMutableLiveData + } + + fun getContactList(username: String): MutableLiveData?> { + val applyFriendMutableLiveData = MutableLiveData?>() + + 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 + } + } \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/repository/FriendListRepo.kt b/app/src/main/java/com/kaixed/kchat/repository/FriendListRepo.kt deleted file mode 100644 index bb50d64..0000000 --- a/app/src/main/java/com/kaixed/kchat/repository/FriendListRepo.kt +++ /dev/null @@ -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?> { - val applyFriendMutableLiveData = MutableLiveData?>() - - 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 - } - -} \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/repository/LoginRepository.java b/app/src/main/java/com/kaixed/kchat/repository/LoginRepository.java deleted file mode 100644 index ea31471..0000000 --- a/app/src/main/java/com/kaixed/kchat/repository/LoginRepository.java +++ /dev/null @@ -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(String username, String password) { - MutableLiveData 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; - } -} diff --git a/app/src/main/java/com/kaixed/kchat/repository/SearchFriendsRepository.java b/app/src/main/java/com/kaixed/kchat/repository/SearchFriendsRepository.java deleted file mode 100644 index 9e9488b..0000000 --- a/app/src/main/java/com/kaixed/kchat/repository/SearchFriendsRepository.java +++ /dev/null @@ -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(String username) { - - MutableLiveData 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; - } -} diff --git a/app/src/main/java/com/kaixed/kchat/repository/UserRepo.kt b/app/src/main/java/com/kaixed/kchat/repository/UserRepo.kt new file mode 100644 index 0000000..3753c13 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/repository/UserRepo.kt @@ -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 { + val loginMutableLiveData = MutableLiveData() + + 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 { + val listMutableLiveData = MutableLiveData() + 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 + } + + +} \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/repository/UserRepository.java b/app/src/main/java/com/kaixed/kchat/repository/UserRepository.java deleted file mode 100644 index 386a084..0000000 --- a/app/src/main/java/com/kaixed/kchat/repository/UserRepository.java +++ /dev/null @@ -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 getUserListByNickname(String username) { - - MutableLiveData 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; - } -} diff --git a/app/src/main/java/com/kaixed/kchat/utils/CenteredImageSpan.kt b/app/src/main/java/com/kaixed/kchat/utils/CenteredImageSpan.kt new file mode 100644 index 0000000..1f35889 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/utils/CenteredImageSpan.kt @@ -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() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/utils/ImageSpanUtil.java b/app/src/main/java/com/kaixed/kchat/utils/ImageSpanUtil.java index 1287de8..eee5d66 100644 --- a/app/src/main/java/com/kaixed/kchat/utils/ImageSpanUtil.java +++ b/app/src/main/java/com/kaixed/kchat/utils/ImageSpanUtil.java @@ -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); } } } diff --git a/app/src/main/java/com/kaixed/kchat/view/activity/FriendListActivity.kt b/app/src/main/java/com/kaixed/kchat/view/activity/FriendListActivity.kt index 4c37f40..48d83ad 100644 --- a/app/src/main/java/com/kaixed/kchat/view/activity/FriendListActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/view/activity/FriendListActivity.kt @@ -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() diff --git a/app/src/main/java/com/kaixed/kchat/view/activity/ProfileActivity.kt b/app/src/main/java/com/kaixed/kchat/view/activity/ProfileActivity.kt index c756a30..850316c 100644 --- a/app/src/main/java/com/kaixed/kchat/view/activity/ProfileActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/view/activity/ProfileActivity.kt @@ -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 } } diff --git a/app/src/main/java/com/kaixed/kchat/viewmodel/AddFriendViewModel.java b/app/src/main/java/com/kaixed/kchat/viewmodel/AddFriendViewModel.java deleted file mode 100644 index dd1c584..0000000 --- a/app/src/main/java/com/kaixed/kchat/viewmodel/AddFriendViewModel.java +++ /dev/null @@ -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 { - - -} diff --git a/app/src/main/java/com/kaixed/kchat/viewmodel/ContactViewModel.kt b/app/src/main/java/com/kaixed/kchat/viewmodel/ContactViewModel.kt index 71bd013..dc3fb42 100644 --- a/app/src/main/java/com/kaixed/kchat/viewmodel/ContactViewModel.kt +++ b/app/src/main/java/com/kaixed/kchat/viewmodel/ContactViewModel.kt @@ -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 = contactRepo.getContactRequestList(username) - fun searchFriends(username: String): MutableLiveData? = - searchFriendsRepo.searchFriends(username) + fun searchFriends(username: String): MutableLiveData = + contactRepo.searchContact(username) - fun acceptContactRequest(username: String, contactId: String): MutableLiveData = + fun acceptContactRequest( + username: String, + contactId: String + ): MutableLiveData = contactRepo.acceptContactRequest(username, contactId) diff --git a/app/src/main/java/com/kaixed/kchat/viewmodel/FriendListViewModel.kt b/app/src/main/java/com/kaixed/kchat/viewmodel/FriendListViewModel.kt index ba6d333..83662ba 100644 --- a/app/src/main/java/com/kaixed/kchat/viewmodel/FriendListViewModel.kt +++ b/app/src/main/java/com/kaixed/kchat/viewmodel/FriendListViewModel.kt @@ -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?> = + contactRepo.getContactList(username) } \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/viewmodel/LoginViewModel.kt b/app/src/main/java/com/kaixed/kchat/viewmodel/LoginViewModel.kt index 6499762..9e9a832 100644 --- a/app/src/main/java/com/kaixed/kchat/viewmodel/LoginViewModel.kt +++ b/app/src/main/java/com/kaixed/kchat/viewmodel/LoginViewModel.kt @@ -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(); - } + fun login(username: String, password: String): MutableLiveData = + userRepo.login(username, password) - public MutableLiveData login(String username, String password) { - return loginRepository.login(username, password); - } - -} +} \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/viewmodel/SearchFriendsViewModel.kt b/app/src/main/java/com/kaixed/kchat/viewmodel/SearchFriendsViewModel.kt index c367bd4..599cc86 100644 --- a/app/src/main/java/com/kaixed/kchat/viewmodel/SearchFriendsViewModel.kt +++ b/app/src/main/java/com/kaixed/kchat/viewmodel/SearchFriendsViewModel.kt @@ -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? = - searchFriendsRepo.searchFriends(username) + fun searchFriends(username: String): MutableLiveData = + contactRepo.searchContact(username) } \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/viewmodel/UserViewModel.kt b/app/src/main/java/com/kaixed/kchat/viewmodel/UserViewModel.kt index 301bfb9..828d0dd 100644 --- a/app/src/main/java/com/kaixed/kchat/viewmodel/UserViewModel.kt +++ b/app/src/main/java/com/kaixed/kchat/viewmodel/UserViewModel.kt @@ -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 = + userRepo.getUserListByNickname(username) - public MutableLiveData getUserListByNickname(String username) { - return userRepository.getUserListByNickname(username); - } -} +} \ No newline at end of file