From 57b6fd166edda52f829d1782ad2d782ee96a08a7 Mon Sep 17 00:00:00 2001 From: kaixed Date: Tue, 26 Nov 2024 13:13:39 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84data=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.重构data层 2.重构ChatAdapter减少大量重复代码 --- app/src/main/java/com/kaixed/kchat/App.kt | 5 +- .../data/{objectbox/data => }/LocalContact.kt | 9 +- .../kchat/data/local/box/ObjectBoxManager.kt | 26 +++ .../{objectbox => local}/entity/Contact.kt | 2 +- .../entity/Conversation.kt | 2 +- .../{objectbox => local}/entity/Messages.kt | 2 +- .../{objectbox => local}/entity/UserInfo.kt | 2 +- .../kaixed/kchat/data/objectbox/ObjectBox.kt | 21 -- .../repository/ContactRepository.kt | 8 +- .../repository/UserAuthRepository.kt | 8 +- .../repository/UserProfileRepository.kt | 8 +- .../repository/UserSearchRepository.kt | 2 +- .../model/friend/AcceptContactRequest.kt | 2 +- .../kchat/model/response/friend/FriendList.kt | 2 +- .../kchat/model/response/login/Login.kt | 2 +- .../network/service/ContactApiService.kt | 2 +- .../kchat/network/service/UserApiService.kt | 6 +- .../kaixed/kchat/service/WebSocketService.kt | 8 +- .../kaixed/kchat/ui/activity/ChatActivity.kt | 6 +- .../kchat/ui/activity/ChatDetailActivity.kt | 2 +- .../ui/activity/ContactsDetailActivity.kt | 6 +- .../kaixed/kchat/ui/activity/MainActivity.kt | 4 +- .../ui/activity/ProfileDetailActivity.kt | 6 +- .../kchat/ui/activity/RegisterActivity.kt | 4 +- .../kchat/ui/activity/RenameActivity.kt | 4 +- .../ui/activity/SetRemarkAndLabelActivity.kt | 7 +- .../kaixed/kchat/ui/adapter/ChatAdapter.kt | 193 ++++++------------ .../kchat/ui/adapter/ConversationAdapter.kt | 2 +- .../kchat/ui/adapter/FriendListAdapter.kt | 2 +- .../kchat/ui/fragment/ContactFragment.kt | 2 +- .../kaixed/kchat/ui/fragment/HomeFragment.kt | 8 +- .../ui/fragment/MyBottomSheetFragment.kt | 6 +- .../com/kaixed/kchat/utils/DensityUtil.kt | 14 +- .../java/com/kaixed/kchat/utils/TextUtil.kt | 2 +- .../java/com/kaixed/kchat/utils/ViewUtil.kt | 68 ++++++ .../kaixed/kchat/utils/handle/ContactUtil.kt | 4 +- .../kchat/viewmodel/ContactViewModel.kt | 8 +- .../kaixed/kchat/viewmodel/UserViewModel.kt | 8 +- .../chat_recycle_item_custom_normal.xml | 45 ++++ .../layout/chat_recycle_item_image_mine.xml | 33 ++- .../layout/chat_recycle_item_image_normal.xml | 35 ++++ .../layout/chat_recycle_item_image_other.xml | 5 +- 42 files changed, 348 insertions(+), 243 deletions(-) rename app/src/main/java/com/kaixed/kchat/data/{objectbox/data => }/LocalContact.kt (59%) create mode 100644 app/src/main/java/com/kaixed/kchat/data/local/box/ObjectBoxManager.kt rename app/src/main/java/com/kaixed/kchat/data/{objectbox => local}/entity/Contact.kt (92%) rename app/src/main/java/com/kaixed/kchat/data/{objectbox => local}/entity/Conversation.kt (89%) rename app/src/main/java/com/kaixed/kchat/data/{objectbox => local}/entity/Messages.kt (91%) rename app/src/main/java/com/kaixed/kchat/data/{objectbox => local}/entity/UserInfo.kt (89%) delete mode 100644 app/src/main/java/com/kaixed/kchat/data/objectbox/ObjectBox.kt rename app/src/main/java/com/kaixed/kchat/{ => data}/repository/ContactRepository.kt (97%) rename app/src/main/java/com/kaixed/kchat/{ => data}/repository/UserAuthRepository.kt (95%) rename app/src/main/java/com/kaixed/kchat/{ => data}/repository/UserProfileRepository.kt (93%) rename app/src/main/java/com/kaixed/kchat/{ => data}/repository/UserSearchRepository.kt (95%) create mode 100644 app/src/main/java/com/kaixed/kchat/utils/ViewUtil.kt create mode 100644 app/src/main/res/layout/chat_recycle_item_custom_normal.xml create mode 100644 app/src/main/res/layout/chat_recycle_item_image_normal.xml diff --git a/app/src/main/java/com/kaixed/kchat/App.kt b/app/src/main/java/com/kaixed/kchat/App.kt index 9cbffab..9ab3def 100644 --- a/app/src/main/java/com/kaixed/kchat/App.kt +++ b/app/src/main/java/com/kaixed/kchat/App.kt @@ -1,8 +1,8 @@ package com.kaixed.kchat import android.app.Application -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.ObjectBox.init +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.box.ObjectBox.init import com.kaixed.kchat.utils.DensityUtil import com.tencent.mmkv.MMKV import io.objectbox.android.Admin @@ -18,6 +18,7 @@ class App : Application() { MMKV.initialize(this) init(this) + Admin(getBoxStore()).start(this) DensityUtil.init(this) diff --git a/app/src/main/java/com/kaixed/kchat/data/objectbox/data/LocalContact.kt b/app/src/main/java/com/kaixed/kchat/data/LocalContact.kt similarity index 59% rename from app/src/main/java/com/kaixed/kchat/data/objectbox/data/LocalContact.kt rename to app/src/main/java/com/kaixed/kchat/data/LocalContact.kt index 8a45577..6c5f03d 100644 --- a/app/src/main/java/com/kaixed/kchat/data/objectbox/data/LocalContact.kt +++ b/app/src/main/java/com/kaixed/kchat/data/LocalContact.kt @@ -1,8 +1,8 @@ -package com.kaixed.kchat.data.objectbox.data +package com.kaixed.kchat.data -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.Contact -import com.kaixed.kchat.data.objectbox.entity.Contact_ +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.Contact +import com.kaixed.kchat.data.local.entity.Contact_ /** * @Author: kaixed @@ -14,5 +14,4 @@ object LocalContact { fun getContactByUsername(contactId: String): Contact? { return contactBox.query(Contact_.username.equal(contactId)).build().findFirst() } - } \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/data/local/box/ObjectBoxManager.kt b/app/src/main/java/com/kaixed/kchat/data/local/box/ObjectBoxManager.kt new file mode 100644 index 0000000..6eb1cf1 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/data/local/box/ObjectBoxManager.kt @@ -0,0 +1,26 @@ +package com.kaixed.kchat.data.local.box + +import android.content.Context +import com.kaixed.kchat.data.local.entity.MyObjectBox +import io.objectbox.Box +import io.objectbox.BoxStore + +/** + * @Author: kaixed + * @Date: 2024/10/24 16:57 + */ +object ObjectBox { + private lateinit var boxStore: BoxStore + + fun init(context: Context) { + boxStore = MyObjectBox.builder() + .androidContext(context) + .build() + } + + fun getBoxStore(): BoxStore = boxStore + + fun getBox(entityClass: Class): Box { + return boxStore.boxFor(entityClass) + } +} diff --git a/app/src/main/java/com/kaixed/kchat/data/objectbox/entity/Contact.kt b/app/src/main/java/com/kaixed/kchat/data/local/entity/Contact.kt similarity index 92% rename from app/src/main/java/com/kaixed/kchat/data/objectbox/entity/Contact.kt rename to app/src/main/java/com/kaixed/kchat/data/local/entity/Contact.kt index c81bb2c..fc2fb18 100644 --- a/app/src/main/java/com/kaixed/kchat/data/objectbox/entity/Contact.kt +++ b/app/src/main/java/com/kaixed/kchat/data/local/entity/Contact.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.data.objectbox.entity +package com.kaixed.kchat.data.local.entity import io.objectbox.annotation.Entity import io.objectbox.annotation.Id diff --git a/app/src/main/java/com/kaixed/kchat/data/objectbox/entity/Conversation.kt b/app/src/main/java/com/kaixed/kchat/data/local/entity/Conversation.kt similarity index 89% rename from app/src/main/java/com/kaixed/kchat/data/objectbox/entity/Conversation.kt rename to app/src/main/java/com/kaixed/kchat/data/local/entity/Conversation.kt index e1d1cd6..55cca93 100644 --- a/app/src/main/java/com/kaixed/kchat/data/objectbox/entity/Conversation.kt +++ b/app/src/main/java/com/kaixed/kchat/data/local/entity/Conversation.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.data.objectbox.entity +package com.kaixed.kchat.data.local.entity import io.objectbox.annotation.Entity import io.objectbox.annotation.Id diff --git a/app/src/main/java/com/kaixed/kchat/data/objectbox/entity/Messages.kt b/app/src/main/java/com/kaixed/kchat/data/local/entity/Messages.kt similarity index 91% rename from app/src/main/java/com/kaixed/kchat/data/objectbox/entity/Messages.kt rename to app/src/main/java/com/kaixed/kchat/data/local/entity/Messages.kt index 894fc6e..992d173 100644 --- a/app/src/main/java/com/kaixed/kchat/data/objectbox/entity/Messages.kt +++ b/app/src/main/java/com/kaixed/kchat/data/local/entity/Messages.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.data.objectbox.entity +package com.kaixed.kchat.data.local.entity import io.objectbox.annotation.Entity import io.objectbox.annotation.Id diff --git a/app/src/main/java/com/kaixed/kchat/data/objectbox/entity/UserInfo.kt b/app/src/main/java/com/kaixed/kchat/data/local/entity/UserInfo.kt similarity index 89% rename from app/src/main/java/com/kaixed/kchat/data/objectbox/entity/UserInfo.kt rename to app/src/main/java/com/kaixed/kchat/data/local/entity/UserInfo.kt index 80b97df..b199605 100644 --- a/app/src/main/java/com/kaixed/kchat/data/objectbox/entity/UserInfo.kt +++ b/app/src/main/java/com/kaixed/kchat/data/local/entity/UserInfo.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.data.objectbox.entity +package com.kaixed.kchat.data.local.entity import io.objectbox.annotation.Entity import io.objectbox.annotation.Id diff --git a/app/src/main/java/com/kaixed/kchat/data/objectbox/ObjectBox.kt b/app/src/main/java/com/kaixed/kchat/data/objectbox/ObjectBox.kt deleted file mode 100644 index 68a562b..0000000 --- a/app/src/main/java/com/kaixed/kchat/data/objectbox/ObjectBox.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.kaixed.kchat.data.objectbox - -import android.content.Context -import com.kaixed.kchat.data.objectbox.entity.MyObjectBox -import io.objectbox.BoxStore - -/** - * @Author: kaixed - * @Date: 2024/10/24 16:57 - */ -object ObjectBox { - private var store: BoxStore? = null - - fun init(context: Context) { - store = MyObjectBox.builder() - .androidContext(context) - .build() - } - - fun getBoxStore(): BoxStore = checkNotNull(store) { "ObjectBox is not initialized." } -} diff --git a/app/src/main/java/com/kaixed/kchat/repository/ContactRepository.kt b/app/src/main/java/com/kaixed/kchat/data/repository/ContactRepository.kt similarity index 97% rename from app/src/main/java/com/kaixed/kchat/repository/ContactRepository.kt rename to app/src/main/java/com/kaixed/kchat/data/repository/ContactRepository.kt index 5962b37..004ee19 100644 --- a/app/src/main/java/com/kaixed/kchat/repository/ContactRepository.kt +++ b/app/src/main/java/com/kaixed/kchat/data/repository/ContactRepository.kt @@ -1,8 +1,8 @@ -package com.kaixed.kchat.repository +package com.kaixed.kchat.data.repository -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.Contact -import com.kaixed.kchat.data.objectbox.entity.Contact_ +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.Contact +import com.kaixed.kchat.data.local.entity.Contact_ import com.kaixed.kchat.model.friend.FriendRequestItem import com.kaixed.kchat.model.search.User import com.kaixed.kchat.network.ApiCall.apiCall diff --git a/app/src/main/java/com/kaixed/kchat/repository/UserAuthRepository.kt b/app/src/main/java/com/kaixed/kchat/data/repository/UserAuthRepository.kt similarity index 95% rename from app/src/main/java/com/kaixed/kchat/repository/UserAuthRepository.kt rename to app/src/main/java/com/kaixed/kchat/data/repository/UserAuthRepository.kt index 7b872c2..d68e438 100644 --- a/app/src/main/java/com/kaixed/kchat/repository/UserAuthRepository.kt +++ b/app/src/main/java/com/kaixed/kchat/data/repository/UserAuthRepository.kt @@ -1,8 +1,8 @@ -package com.kaixed.kchat.repository +package com.kaixed.kchat.data.repository -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.UserInfo -import com.kaixed.kchat.data.objectbox.entity.UserInfo_ +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.UserInfo +import com.kaixed.kchat.data.local.entity.UserInfo_ import com.kaixed.kchat.model.request.RegisterRequest import com.kaixed.kchat.model.response.register.Register import com.kaixed.kchat.network.ApiCall.apiCall diff --git a/app/src/main/java/com/kaixed/kchat/repository/UserProfileRepository.kt b/app/src/main/java/com/kaixed/kchat/data/repository/UserProfileRepository.kt similarity index 93% rename from app/src/main/java/com/kaixed/kchat/repository/UserProfileRepository.kt rename to app/src/main/java/com/kaixed/kchat/data/repository/UserProfileRepository.kt index b3e9924..539afea 100644 --- a/app/src/main/java/com/kaixed/kchat/repository/UserProfileRepository.kt +++ b/app/src/main/java/com/kaixed/kchat/data/repository/UserProfileRepository.kt @@ -1,8 +1,8 @@ -package com.kaixed.kchat.repository +package com.kaixed.kchat.data.repository -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.UserInfo -import com.kaixed.kchat.data.objectbox.entity.UserInfo_ +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.UserInfo +import com.kaixed.kchat.data.local.entity.UserInfo_ import com.kaixed.kchat.model.request.UserRequest import com.kaixed.kchat.model.search.SearchUser import com.kaixed.kchat.network.RetrofitClient diff --git a/app/src/main/java/com/kaixed/kchat/repository/UserSearchRepository.kt b/app/src/main/java/com/kaixed/kchat/data/repository/UserSearchRepository.kt similarity index 95% rename from app/src/main/java/com/kaixed/kchat/repository/UserSearchRepository.kt rename to app/src/main/java/com/kaixed/kchat/data/repository/UserSearchRepository.kt index 69e0b04..67ae94d 100644 --- a/app/src/main/java/com/kaixed/kchat/repository/UserSearchRepository.kt +++ b/app/src/main/java/com/kaixed/kchat/data/repository/UserSearchRepository.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.repository +package com.kaixed.kchat.data.repository import com.kaixed.kchat.model.response.search.User import com.kaixed.kchat.network.RetrofitClient diff --git a/app/src/main/java/com/kaixed/kchat/model/friend/AcceptContactRequest.kt b/app/src/main/java/com/kaixed/kchat/model/friend/AcceptContactRequest.kt index 4c1eb99..8c3be25 100644 --- a/app/src/main/java/com/kaixed/kchat/model/friend/AcceptContactRequest.kt +++ b/app/src/main/java/com/kaixed/kchat/model/friend/AcceptContactRequest.kt @@ -1,6 +1,6 @@ package com.kaixed.kchat.model.friend -import com.kaixed.kchat.data.objectbox.entity.Contact +import com.kaixed.kchat.data.local.entity.Contact import kotlinx.serialization.Serializable @Serializable diff --git a/app/src/main/java/com/kaixed/kchat/model/response/friend/FriendList.kt b/app/src/main/java/com/kaixed/kchat/model/response/friend/FriendList.kt index b4cc458..7568351 100644 --- a/app/src/main/java/com/kaixed/kchat/model/response/friend/FriendList.kt +++ b/app/src/main/java/com/kaixed/kchat/model/response/friend/FriendList.kt @@ -1,6 +1,6 @@ package com.kaixed.kchat.model.response.friend -import com.kaixed.kchat.data.objectbox.entity.Contact +import com.kaixed.kchat.data.local.entity.Contact import kotlinx.serialization.Serializable /** diff --git a/app/src/main/java/com/kaixed/kchat/model/response/login/Login.kt b/app/src/main/java/com/kaixed/kchat/model/response/login/Login.kt index 5af8ec5..8fb8b59 100644 --- a/app/src/main/java/com/kaixed/kchat/model/response/login/Login.kt +++ b/app/src/main/java/com/kaixed/kchat/model/response/login/Login.kt @@ -1,6 +1,6 @@ package com.kaixed.kchat.model.response.login -import com.kaixed.kchat.data.objectbox.entity.UserInfo +import com.kaixed.kchat.data.local.entity.UserInfo import kotlinx.serialization.Serializable @Serializable diff --git a/app/src/main/java/com/kaixed/kchat/network/service/ContactApiService.kt b/app/src/main/java/com/kaixed/kchat/network/service/ContactApiService.kt index 2dd49ec..5d5ae8f 100644 --- a/app/src/main/java/com/kaixed/kchat/network/service/ContactApiService.kt +++ b/app/src/main/java/com/kaixed/kchat/network/service/ContactApiService.kt @@ -1,6 +1,6 @@ package com.kaixed.kchat.network.service -import com.kaixed.kchat.data.objectbox.entity.Contact +import com.kaixed.kchat.data.local.entity.Contact import com.kaixed.kchat.model.friend.FriendRequestItem import com.kaixed.kchat.model.search.User import com.kaixed.kchat.network.ApiResponse diff --git a/app/src/main/java/com/kaixed/kchat/network/service/UserApiService.kt b/app/src/main/java/com/kaixed/kchat/network/service/UserApiService.kt index e66b4b4..28bd577 100644 --- a/app/src/main/java/com/kaixed/kchat/network/service/UserApiService.kt +++ b/app/src/main/java/com/kaixed/kchat/network/service/UserApiService.kt @@ -1,17 +1,13 @@ package com.kaixed.kchat.network.service -import com.kaixed.kchat.data.objectbox.entity.UserInfo +import com.kaixed.kchat.data.local.entity.UserInfo import com.kaixed.kchat.model.request.RegisterRequest import com.kaixed.kchat.model.request.UserRequest -import com.kaixed.kchat.model.response.friend.SearchFriends import com.kaixed.kchat.model.response.register.Register import com.kaixed.kchat.model.response.search.User -import com.kaixed.kchat.model.response.user.ChangeNickname -import com.kaixed.kchat.model.response.user.UploadAvatar import com.kaixed.kchat.model.search.SearchUser import com.kaixed.kchat.network.ApiResponse import okhttp3.MultipartBody -import retrofit2.Response import retrofit2.http.Body import retrofit2.http.Field import retrofit2.http.FormUrlEncoded diff --git a/app/src/main/java/com/kaixed/kchat/service/WebSocketService.kt b/app/src/main/java/com/kaixed/kchat/service/WebSocketService.kt index 4cb930b..8598adf 100644 --- a/app/src/main/java/com/kaixed/kchat/service/WebSocketService.kt +++ b/app/src/main/java/com/kaixed/kchat/service/WebSocketService.kt @@ -8,10 +8,10 @@ import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import com.google.gson.Gson -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.Conversation -import com.kaixed.kchat.data.objectbox.entity.Conversation_ -import com.kaixed.kchat.data.objectbox.entity.Messages +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.Conversation +import com.kaixed.kchat.data.local.entity.Conversation_ +import com.kaixed.kchat.data.local.entity.Messages import com.kaixed.kchat.network.NetworkInterface.WEBSOCKET import com.kaixed.kchat.network.NetworkInterface.WEBSOCKET_SERVER_URL import com.kaixed.kchat.network.OkhttpHelper diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/ChatActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/ChatActivity.kt index ede9f02..301e988 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/ChatActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/ChatActivity.kt @@ -24,9 +24,9 @@ import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.kaixed.kchat.R -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.Messages -import com.kaixed.kchat.data.objectbox.entity.Messages_ +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.Messages +import com.kaixed.kchat.data.local.entity.Messages_ import com.kaixed.kchat.databinding.ActivityChatBinding import com.kaixed.kchat.model.FunctionItem import com.kaixed.kchat.service.WebSocketService diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/ChatDetailActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/ChatDetailActivity.kt index 266a0ae..94ae309 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/ChatDetailActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/ChatDetailActivity.kt @@ -4,7 +4,7 @@ import android.content.Intent import android.os.Bundle import androidx.activity.enableEdgeToEdge import com.bumptech.glide.Glide -import com.kaixed.kchat.data.objectbox.data.LocalContact.getContactByUsername +import com.kaixed.kchat.data.LocalContact.getContactByUsername import com.kaixed.kchat.databinding.ActivityChatDetailBinding import com.kaixed.kchat.ui.base.BaseActivity diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/ContactsDetailActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/ContactsDetailActivity.kt index c1a0f9e..cc9ac39 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/ContactsDetailActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/ContactsDetailActivity.kt @@ -5,9 +5,9 @@ import android.content.Intent import android.os.Bundle import android.view.View import androidx.activity.enableEdgeToEdge -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.Contact -import com.kaixed.kchat.data.objectbox.entity.Contact_ +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +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 diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/MainActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/MainActivity.kt index 0915638..b3a8859 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/MainActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/MainActivity.kt @@ -11,8 +11,8 @@ import androidx.lifecycle.lifecycleScope import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.widget.ViewPager2 import com.kaixed.kchat.R -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.Contact +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.Contact import com.kaixed.kchat.databinding.ActivityMainBinding import com.kaixed.kchat.ui.base.BaseActivity import com.kaixed.kchat.ui.fragment.ContactFragment diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/ProfileDetailActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/ProfileDetailActivity.kt index 70f70a4..b956458 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/ProfileDetailActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/ProfileDetailActivity.kt @@ -13,9 +13,9 @@ import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.UserInfo -import com.kaixed.kchat.data.objectbox.entity.UserInfo_ +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.UserInfo +import com.kaixed.kchat.data.local.entity.UserInfo_ import com.kaixed.kchat.databinding.ActivityProfileDetailBinding import com.kaixed.kchat.ui.base.BaseActivity import com.kaixed.kchat.ui.widget.LoadingDialogFragment diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/RegisterActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/RegisterActivity.kt index ee4c2f3..6295f10 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/RegisterActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/RegisterActivity.kt @@ -14,8 +14,8 @@ import androidx.activity.viewModels import androidx.core.content.ContextCompat import androidx.core.widget.addTextChangedListener import com.kaixed.kchat.R -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.UserInfo +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.UserInfo import com.kaixed.kchat.databinding.ActivityRegisterBinding import com.kaixed.kchat.model.request.RegisterRequest import com.kaixed.kchat.ui.base.BaseActivity diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/RenameActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/RenameActivity.kt index 3e72304..3271e60 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/RenameActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/RenameActivity.kt @@ -6,8 +6,8 @@ import android.os.Looper import androidx.activity.enableEdgeToEdge import androidx.activity.viewModels import androidx.core.widget.addTextChangedListener -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.UserInfo +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.UserInfo import com.kaixed.kchat.databinding.ActivityRenameBinding import com.kaixed.kchat.model.request.UserRequest import com.kaixed.kchat.ui.base.BaseActivity diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/SetRemarkAndLabelActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/SetRemarkAndLabelActivity.kt index 448b1a6..cd14f64 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/SetRemarkAndLabelActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/SetRemarkAndLabelActivity.kt @@ -12,10 +12,9 @@ import androidx.core.widget.addTextChangedListener import androidx.lifecycle.lifecycleScope import androidx.localbroadcastmanager.content.LocalBroadcastManager import com.kaixed.kchat.R -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.data.LocalContact -import com.kaixed.kchat.data.objectbox.entity.Contact -import com.kaixed.kchat.data.objectbox.entity.Contact_ +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.LocalContact +import com.kaixed.kchat.data.local.entity.Contact import com.kaixed.kchat.databinding.ActivitySetRemarkAndLabelBinding import com.kaixed.kchat.ui.base.BaseActivity import com.kaixed.kchat.ui.widget.LoadingDialogFragment diff --git a/app/src/main/java/com/kaixed/kchat/ui/adapter/ChatAdapter.kt b/app/src/main/java/com/kaixed/kchat/ui/adapter/ChatAdapter.kt index 3490cdb..8915588 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/adapter/ChatAdapter.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/adapter/ChatAdapter.kt @@ -2,23 +2,20 @@ package com.kaixed.kchat.ui.adapter import android.content.Context import android.view.LayoutInflater -import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide import com.drake.spannable.replaceSpan import com.drake.spannable.span.CenterImageSpan import com.kaixed.kchat.R -import com.kaixed.kchat.data.objectbox.ObjectBox -import com.kaixed.kchat.data.objectbox.entity.Messages -import com.kaixed.kchat.databinding.ChatRecycleItemCustomMineBinding -import com.kaixed.kchat.databinding.ChatRecycleItemCustomOtherBinding -import com.kaixed.kchat.databinding.ChatRecycleItemImageMineBinding -import com.kaixed.kchat.databinding.ChatRecycleItemImageOtherBinding +import com.kaixed.kchat.data.local.box.ObjectBox +import com.kaixed.kchat.data.local.entity.Messages +import com.kaixed.kchat.databinding.ChatRecycleItemCustomNormalBinding +import com.kaixed.kchat.databinding.ChatRecycleItemImageNormalBinding import com.kaixed.kchat.databinding.ChatRecycleItemTipBinding import com.kaixed.kchat.utils.ConstantsUtil.getUsername -import com.kaixed.kchat.utils.PopWindowUtil.showPopupWindow -import com.kaixed.kchat.utils.TextUtil +import com.kaixed.kchat.utils.ViewUtil.changeTimerVisibility +import com.kaixed.kchat.utils.ViewUtil.changeView import io.objectbox.Box import java.util.LinkedList @@ -28,72 +25,47 @@ class ChatAdapter( ) : RecyclerView.Adapter() { companion object { - // 普通文本消息 - private const val ITEM_TYPE_CUSTOM_MINE = 0 - private const val ITEM_TYPE_CUSTOM_OTHER = 1 + const val CUSTOM = 0 - // 提示消息,如撤回消息 - private const val ITEM_TYPE_TIP_MINE = 2 - private const val ITEM_TYPE_TIP_OTHER = 3 + // 提示消息 + const val TIP = 2 // 图片消息 - private const val ITEM_TYPE_IMAGE_MINE = 4 - private const val ITEM_TYPE_IMAGE_OTHER = 5 + const val IMAGE = 4 // 语音消息 - private const val ITEM_TYPE_VOICE_MINE = 6 - private const val ITEM_TYPE_VOICE_OTHER = 7 + const val VOICE = 6 // 位置消息 - private const val ITEM_TYPE_LOCATION_MINE = 8 - private const val ITEM_TYPE_LOCATION_OTHER = 9 + const val LOCATION = 8 // 表情消息 - private const val ITEM_TYPE_EMOJI_MINE = 10 - private const val ITEM_TYPE_EMOJI_OTHER = 11 + const val EMOJI = 10 // 红包消息 - private const val ITEM_TYPE_RED_PACKET_MINE = 12 - private const val ITEM_TYPE_RED_PACKET_OTHER = 13 + const val RED_PACKET = 12 private const val TAG = "ChatAdapter" } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { return when (viewType) { - ITEM_TYPE_CUSTOM_MINE -> { - CustomViewMineHolder( - ChatRecycleItemCustomMineBinding.inflate( + CUSTOM -> { + CustomViewHolder( + ChatRecycleItemCustomNormalBinding.inflate( LayoutInflater.from(parent.context), parent, false ) ) } - ITEM_TYPE_CUSTOM_OTHER -> { - CustomViewOtherHolder( - ChatRecycleItemCustomOtherBinding.inflate( + IMAGE -> { + ImageViewHolder( + ChatRecycleItemImageNormalBinding.inflate( LayoutInflater.from(parent.context), parent, false ) ) } - ITEM_TYPE_IMAGE_OTHER -> { - ImageViewOtherHolder( - ChatRecycleItemImageOtherBinding.inflate( - LayoutInflater.from(parent.context), parent, false - ) - ) - } - - ITEM_TYPE_IMAGE_MINE -> { - ImageViewMineHolder( - ChatRecycleItemImageMineBinding.inflate( - LayoutInflater.from(parent.context), parent, false - ) - ) - } - - else -> { TipViewHolder( ChatRecycleItemTipBinding.inflate( @@ -108,83 +80,20 @@ class ChatAdapter( val singleMessage = messages[position] when (holder) { - is ImageViewOtherHolder -> { - Glide.with(context).load(singleMessage.content).into(holder.binding.image) - if (position == messages.size - 1) { - holder.binding.tvTimer.visibility = View.VISIBLE - holder.binding.tvTimer.text = - TextUtil.getTimestampString(singleMessage.timestamp) - } else { - if (singleMessage.timestamp - messages[position + 1].timestamp < 1L * 60 * 1000) { - holder.binding.tvTimer.visibility = View.GONE - } else { - holder.binding.tvTimer.visibility = View.VISIBLE - holder.binding.tvTimer.text = - TextUtil.getTimestampString(singleMessage.timestamp) - } - } + is CustomViewHolder -> { + holder.bindData(singleMessage) + changeTimerVisibility(position, messages, holder.binding.tvTimer, singleMessage) + } + + is ImageViewHolder -> { + holder.bindData(singleMessage) + changeTimerVisibility(position, messages, holder.binding.tvTimer, singleMessage) } is TipViewHolder -> { holder.bindData(singleMessage) } - is CustomViewMineHolder -> { - if (position == messages.size - 1) { - holder.binding.tvTimer.visibility = View.VISIBLE - holder.binding.tvTimer.text = - TextUtil.getTimestampString(singleMessage.timestamp) - } else { - if (singleMessage.timestamp - messages[position + 1].timestamp < 1L * 60 * 1000) { - holder.binding.tvTimer.visibility = View.GONE - } else { - holder.binding.tvTimer.visibility = View.VISIBLE - holder.binding.tvTimer.text = - TextUtil.getTimestampString(singleMessage.timestamp) - } - } - holder.binding.tvMessageContent.setOnLongClickListener { - showPopupWindow( - context, - holder.binding.tvMessageContent, - singleMessage.senderId == getUsername() - ) - true - } - - holder.binding.tvMessageContent.text = singleMessage.content.replaceSpan("[委屈]") { - CenterImageSpan(context, R.drawable.emoji).setDrawableSize(55) - } - } - - is CustomViewOtherHolder -> { - if (position == messages.size - 1) { - holder.binding.tvTimer.visibility = View.VISIBLE - holder.binding.tvTimer.text = - TextUtil.getTimestampString(singleMessage.timestamp) - } else { - if (singleMessage.timestamp - messages[position + 1].timestamp < 1L * 60 * 1000) { - holder.binding.tvTimer.visibility = View.GONE - } else { - holder.binding.tvTimer.visibility = View.VISIBLE - holder.binding.tvTimer.text = - TextUtil.getTimestampString(singleMessage.timestamp) - } - } - holder.binding.tvMessageContent.setOnLongClickListener { - showPopupWindow( - context, - holder.binding.tvMessageContent, - singleMessage.senderId == getUsername() - ) - true - } - - - holder.binding.tvMessageContent.text = singleMessage.content.replaceSpan("[委屈]") { - CenterImageSpan(context, R.drawable.emoji).setDrawableSize(55) - } - } } } @@ -199,25 +108,42 @@ class ChatAdapter( } } - class CustomViewMineHolder(val binding: ChatRecycleItemCustomMineBinding) : - RecyclerView.ViewHolder(binding.root) - class CustomViewOtherHolder(val binding: ChatRecycleItemCustomOtherBinding) : - RecyclerView.ViewHolder(binding.root) + class CustomViewHolder(val binding: ChatRecycleItemCustomNormalBinding) : + RecyclerView.ViewHolder(binding.root) { + fun bindData(message: Messages) { + val sender = message.senderId == getUsername() + binding.tvMessageContent.text = message.content.replaceSpan("[委屈]") { + CenterImageSpan(binding.root.context, R.drawable.emoji).setDrawableSize(55) + } - class ImageViewMineHolder(val binding: ChatRecycleItemImageMineBinding) : - RecyclerView.ViewHolder(binding.root) + changeView( + parentView = binding.root, + sender = sender, + avatarId = binding.ifvAvatar.id, + contentId = binding.tvMessageContent.id + ) + } + } - class ImageViewOtherHolder(val binding: ChatRecycleItemImageOtherBinding) : - RecyclerView.ViewHolder(binding.root) + + class ImageViewHolder(val binding: ChatRecycleItemImageNormalBinding) : + RecyclerView.ViewHolder(binding.root) { + fun bindData(message: Messages) { + Glide.with(binding.root.context).load(message.content).into(binding.image) + val sender = message.senderId == getUsername() + changeView( + parentView = binding.root, + sender = sender, + avatarId = binding.ifvAvatar.id, + contentId = binding.image.id + ) + } + } override fun getItemViewType(position: Int): Int { - val message = messages[position] - return if (message.senderId == getUsername()) { - message.type.toInt() - } else { - message.type.toInt() + 1 - } + return messages[position].type.toInt() + } private fun updateDb(message: Messages) { @@ -226,5 +152,4 @@ class ChatAdapter( } override fun getItemCount(): Int = messages.size - } diff --git a/app/src/main/java/com/kaixed/kchat/ui/adapter/ConversationAdapter.kt b/app/src/main/java/com/kaixed/kchat/ui/adapter/ConversationAdapter.kt index 91d969a..788da0a 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/adapter/ConversationAdapter.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/adapter/ConversationAdapter.kt @@ -13,7 +13,7 @@ import com.bumptech.glide.request.RequestOptions import com.drake.spannable.replaceSpan import com.drake.spannable.span.CenterImageSpan import com.kaixed.kchat.R -import com.kaixed.kchat.data.objectbox.entity.Conversation +import com.kaixed.kchat.data.local.entity.Conversation import com.kaixed.kchat.databinding.ChatMainItemBinding import com.kaixed.kchat.ui.activity.ChatActivity import com.kaixed.kchat.ui.i.OnChatListItemClickListener diff --git a/app/src/main/java/com/kaixed/kchat/ui/adapter/FriendListAdapter.kt b/app/src/main/java/com/kaixed/kchat/ui/adapter/FriendListAdapter.kt index 73b61e4..1f33117 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/adapter/FriendListAdapter.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/adapter/FriendListAdapter.kt @@ -11,7 +11,7 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder import com.bumptech.glide.Glide import com.bumptech.glide.request.RequestOptions import com.kaixed.kchat.R -import com.kaixed.kchat.data.objectbox.entity.Contact +import com.kaixed.kchat.data.local.entity.Contact import com.kaixed.kchat.databinding.FriendRecycleFooterItemBinding import com.kaixed.kchat.databinding.FriendRecycleItemBinding import com.kaixed.kchat.ui.activity.ContactRequestListActivity diff --git a/app/src/main/java/com/kaixed/kchat/ui/fragment/ContactFragment.kt b/app/src/main/java/com/kaixed/kchat/ui/fragment/ContactFragment.kt index c664d82..e8dc3b1 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/fragment/ContactFragment.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/fragment/ContactFragment.kt @@ -11,7 +11,7 @@ import android.view.ViewGroup import androidx.fragment.app.viewModels import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.recyclerview.widget.LinearLayoutManager -import com.kaixed.kchat.data.objectbox.entity.Contact +import com.kaixed.kchat.data.local.entity.Contact import com.kaixed.kchat.databinding.FragmentContactBinding import com.kaixed.kchat.ui.adapter.FriendListAdapter import com.kaixed.kchat.ui.base.BaseFragment diff --git a/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt b/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt index fa9445c..18e46e3 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt @@ -20,10 +20,10 @@ import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity.BIND_AUTO_CREATE import androidx.recyclerview.widget.LinearLayoutManager import com.kaixed.kchat.R -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.Conversation -import com.kaixed.kchat.data.objectbox.entity.Conversation_ -import com.kaixed.kchat.data.objectbox.entity.Messages +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.Conversation +import com.kaixed.kchat.data.local.entity.Conversation_ +import com.kaixed.kchat.data.local.entity.Messages import com.kaixed.kchat.databinding.FragmentHomeBinding import com.kaixed.kchat.model.HomeItem import com.kaixed.kchat.service.WebSocketService diff --git a/app/src/main/java/com/kaixed/kchat/ui/fragment/MyBottomSheetFragment.kt b/app/src/main/java/com/kaixed/kchat/ui/fragment/MyBottomSheetFragment.kt index 8dacbae..15a3c14 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/fragment/MyBottomSheetFragment.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/fragment/MyBottomSheetFragment.kt @@ -8,9 +8,9 @@ import android.view.View import android.view.ViewGroup import com.google.android.material.bottomsheet.BottomSheetDialogFragment import com.kaixed.kchat.R -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.Conversation -import com.kaixed.kchat.data.objectbox.entity.Messages +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.Conversation +import com.kaixed.kchat.data.local.entity.Messages import com.kaixed.kchat.databinding.BottomSheetLayoutBinding import com.kaixed.kchat.ui.activity.LoginActivity import com.kaixed.kchat.utils.Constants.MMKV_USER_SESSION diff --git a/app/src/main/java/com/kaixed/kchat/utils/DensityUtil.kt b/app/src/main/java/com/kaixed/kchat/utils/DensityUtil.kt index 1d4434d..dcb4b57 100644 --- a/app/src/main/java/com/kaixed/kchat/utils/DensityUtil.kt +++ b/app/src/main/java/com/kaixed/kchat/utils/DensityUtil.kt @@ -1,6 +1,7 @@ package com.kaixed.kchat.utils import android.content.Context +import android.util.TypedValue /** * @Author: kaixed @@ -15,7 +16,18 @@ object DensityUtil { } fun dpToPx(dp: Int): Int = - (appContext.resources.displayMetrics.density * dp).toInt() + TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, + dp.toFloat(), + appContext.resources.displayMetrics + ).toInt() + + fun dp2Px(dp: Int): Int = + TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, + dp.toFloat(), + appContext.resources.displayMetrics + ).toInt() fun pxToDp(px: Int): Int = (px / appContext.resources.displayMetrics.density).toInt() diff --git a/app/src/main/java/com/kaixed/kchat/utils/TextUtil.kt b/app/src/main/java/com/kaixed/kchat/utils/TextUtil.kt index c0deb50..0e6fba6 100644 --- a/app/src/main/java/com/kaixed/kchat/utils/TextUtil.kt +++ b/app/src/main/java/com/kaixed/kchat/utils/TextUtil.kt @@ -22,7 +22,7 @@ object TextUtil { // 判断是否是昨天 if (isYesterday(timestamp)) { - return "昨天${formatTimestamp(timestamp, "HH:mm")}" + return "昨天 ${formatTimestamp(timestamp, "HH:mm")}" } // 判断是否超过两天 diff --git a/app/src/main/java/com/kaixed/kchat/utils/ViewUtil.kt b/app/src/main/java/com/kaixed/kchat/utils/ViewUtil.kt new file mode 100644 index 0000000..cd58bcd --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/utils/ViewUtil.kt @@ -0,0 +1,68 @@ +package com.kaixed.kchat.utils + +import android.view.View +import android.widget.TextView +import androidx.constraintlayout.widget.ConstraintLayout +import androidx.constraintlayout.widget.ConstraintSet +import com.kaixed.kchat.data.local.entity.Messages +import com.kaixed.kchat.utils.DensityUtil.dp2Px + +/** + * @Author: kaixed + * @Date: 2024/11/26 12:12 + */ +object ViewUtil { + fun changeView( + parentView: ConstraintLayout, + sender: Boolean, + avatarId: Int, + contentId: Int + ) { + ConstraintSet().apply { + clone(parentView) + if (sender) { + connect( + avatarId, ConstraintSet.END, + parentView.id, ConstraintSet.END, + dp2Px(10) + ) + connect( + contentId, ConstraintSet.END, + avatarId, ConstraintSet.START, + dp2Px(10) + ) + } else { + connect( + avatarId, ConstraintSet.START, + parentView.id, ConstraintSet.START, + dp2Px(10) + ) + connect( + contentId, ConstraintSet.START, + avatarId, ConstraintSet.END, + dp2Px(10) + ) + } + applyTo(parentView) + } + } + + fun changeTimerVisibility( + position: Int, + messages: List, + tvTimer: TextView, + singleMessage: Messages + ) { + val showTimer = + if (position == messages.size - 1) true else + singleMessage.timestamp - messages[position + 1].timestamp >= 1L * 60 * 1000 + if (showTimer) { + tvTimer.visibility = View.VISIBLE + tvTimer.text = TextUtil.getTimestampString(singleMessage.timestamp) + } else { + tvTimer.visibility = View.GONE + } + tvTimer.visibility = if (showTimer) View.VISIBLE else View.GONE + tvTimer.text = TextUtil.getTimestampString(singleMessage.timestamp) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/utils/handle/ContactUtil.kt b/app/src/main/java/com/kaixed/kchat/utils/handle/ContactUtil.kt index 6e15aa9..3d74814 100644 --- a/app/src/main/java/com/kaixed/kchat/utils/handle/ContactUtil.kt +++ b/app/src/main/java/com/kaixed/kchat/utils/handle/ContactUtil.kt @@ -1,7 +1,7 @@ package com.kaixed.kchat.utils.handle -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.Contact +import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.local.entity.Contact import com.kaixed.kchat.utils.Pinyin4jUtil import io.objectbox.Box 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 88224cc..4cb2b03 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,11 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.kaixed.kchat.data.objectbox.ObjectBox.getBoxStore -import com.kaixed.kchat.data.objectbox.entity.Contact +import com.kaixed.kchat.data.local.box.ObjectBox.getBox +import com.kaixed.kchat.data.local.entity.Contact import com.kaixed.kchat.model.friend.FriendRequestItem import com.kaixed.kchat.model.search.User -import com.kaixed.kchat.repository.ContactRepository +import com.kaixed.kchat.data.repository.ContactRepository import io.objectbox.Box import kotlinx.coroutines.launch @@ -95,7 +95,7 @@ class ContactViewModel : ViewModel() { } fun loadFriendListInDb(): List { - val contactBox: Box = getBoxStore().boxFor(Contact::class.java) + val contactBox: Box = getBox(Contact::class.java) val sortedContacts = contactBox.query() .sort { contact1, contact2 -> 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 76fec1f..2543962 100644 --- a/app/src/main/java/com/kaixed/kchat/viewmodel/UserViewModel.kt +++ b/app/src/main/java/com/kaixed/kchat/viewmodel/UserViewModel.kt @@ -4,15 +4,15 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.kaixed.kchat.data.objectbox.entity.UserInfo +import com.kaixed.kchat.data.local.entity.UserInfo import com.kaixed.kchat.model.request.RegisterRequest import com.kaixed.kchat.model.request.UserRequest import com.kaixed.kchat.model.response.register.Register import com.kaixed.kchat.model.response.search.User import com.kaixed.kchat.model.search.SearchUser -import com.kaixed.kchat.repository.UserAuthRepository -import com.kaixed.kchat.repository.UserProfileRepository -import com.kaixed.kchat.repository.UserSearchRepository +import com.kaixed.kchat.data.repository.UserAuthRepository +import com.kaixed.kchat.data.repository.UserProfileRepository +import com.kaixed.kchat.data.repository.UserSearchRepository import kotlinx.coroutines.launch import okhttp3.MultipartBody diff --git a/app/src/main/res/layout/chat_recycle_item_custom_normal.xml b/app/src/main/res/layout/chat_recycle_item_custom_normal.xml new file mode 100644 index 0000000..d5d4068 --- /dev/null +++ b/app/src/main/res/layout/chat_recycle_item_custom_normal.xml @@ -0,0 +1,45 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/chat_recycle_item_image_mine.xml b/app/src/main/res/layout/chat_recycle_item_image_mine.xml index b464f46..710dfdd 100644 --- a/app/src/main/res/layout/chat_recycle_item_image_mine.xml +++ b/app/src/main/res/layout/chat_recycle_item_image_mine.xml @@ -6,14 +6,33 @@ android:paddingVertical="10dp"> + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/chat_recycle_item_image_normal.xml b/app/src/main/res/layout/chat_recycle_item_image_normal.xml new file mode 100644 index 0000000..de61e60 --- /dev/null +++ b/app/src/main/res/layout/chat_recycle_item_image_normal.xml @@ -0,0 +1,35 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/chat_recycle_item_image_other.xml b/app/src/main/res/layout/chat_recycle_item_image_other.xml index 09e1dae..e33695e 100644 --- a/app/src/main/res/layout/chat_recycle_item_image_other.xml +++ b/app/src/main/res/layout/chat_recycle_item_image_other.xml @@ -25,13 +25,14 @@ app:layout_constraintTop_toBottomOf="@id/tv_timer" app:roundPercent="0.3" /> - + app:layout_constraintTop_toTopOf="@id/ifv_avatar" + app:roundPercent="0.2" /> \ No newline at end of file