diff --git a/app/build.gradle.kts b/app/build.gradle.kts index dc569e2..267e9e7 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -61,9 +61,11 @@ dependencies { implementation(libs.okhttp) implementation(libs.mmkv) implementation(libs.gson) + implementation(libs.objectbox.kotlin) debugImplementation(libs.objectbox.android.objectbrowser) releaseImplementation(libs.objectbox.android) + implementation(libs.glide) implementation(libs.lottie) implementation(libs.kotlinx.serialization.json) diff --git a/app/objectbox-models/default.json b/app/objectbox-models/default.json index 90af46a..2b36fca 100644 --- a/app/objectbox-models/default.json +++ b/app/objectbox-models/default.json @@ -5,7 +5,7 @@ "entities": [ { "id": "1:488582047102418567", - "lastPropertyId": "12:4851920989895940582", + "lastPropertyId": "15:6294917834245722650", "name": "Messages", "properties": [ { @@ -53,13 +53,28 @@ "id": "12:4851920989895940582", "name": "show", "type": 1 + }, + { + "id": "13:4063945019286198809", + "name": "isShowTimer", + "type": 1 + }, + { + "id": "14:3576556630733755597", + "name": "isSender", + "type": 1 + }, + { + "id": "15:6294917834245722650", + "name": "avatarUrl", + "type": 9 } ], "relations": [] }, { "id": "4:6179749773128044271", - "lastPropertyId": "13:6446821128426983596", + "lastPropertyId": "14:5371512009949707960", "name": "Contact", "properties": [ { @@ -71,7 +86,9 @@ { "id": "2:3202277046871450743", "name": "username", - "type": 9 + "indexId": "1:8059396809448816057", + "type": 9, + "flags": 2048 }, { "id": "3:8037315472776382017", @@ -122,6 +139,11 @@ "id": "13:6446821128426983596", "name": "remarkquanpin", "type": 9 + }, + { + "id": "14:5371512009949707960", + "name": "disturb", + "type": 1 } ], "relations": [] @@ -140,7 +162,9 @@ { "id": "2:2771882473472315", "name": "username", - "type": 9 + "indexId": "3:4492440644813580438", + "type": 9, + "flags": 2048 }, { "id": "3:1747776423604377158", @@ -172,7 +196,7 @@ }, { "id": "6:411582187056789368", - "lastPropertyId": "9:6199737871252062125", + "lastPropertyId": "11:1529665629379902832", "name": "Conversation", "properties": [ { @@ -184,7 +208,9 @@ { "id": "2:62500236312534806", "name": "talkerId", - "type": 9 + "indexId": "2:8554188191509595989", + "type": 9, + "flags": 2048 }, { "id": "3:1809723298214930621", @@ -212,8 +238,13 @@ "type": 5 }, { - "id": "8:2020630799900991467", - "name": "show", + "id": "10:1189093311778315437", + "name": "isShow", + "type": 1 + }, + { + "id": "11:1529665629379902832", + "name": "isPinned", "type": 1 } ], @@ -221,7 +252,7 @@ } ], "lastEntityId": "6:411582187056789368", - "lastIndexId": "0:0", + "lastIndexId": "3:4492440644813580438", "lastRelationId": "0:0", "lastSequenceId": "0:0", "modelVersion": 5, @@ -252,7 +283,8 @@ 6315401035981995789, 2123413060720974577, 8705063061921345729, - 6199737871252062125 + 6199737871252062125, + 2020630799900991467 ], "retiredRelationUids": [], "version": 1 diff --git a/app/objectbox-models/default.json.bak b/app/objectbox-models/default.json.bak index dc399b8..f01132f 100644 --- a/app/objectbox-models/default.json.bak +++ b/app/objectbox-models/default.json.bak @@ -5,7 +5,7 @@ "entities": [ { "id": "1:488582047102418567", - "lastPropertyId": "12:4851920989895940582", + "lastPropertyId": "14:3576556630733755597", "name": "Messages", "properties": [ { @@ -53,13 +53,23 @@ "id": "12:4851920989895940582", "name": "show", "type": 1 + }, + { + "id": "13:4063945019286198809", + "name": "isShowTimer", + "type": 1 + }, + { + "id": "14:3576556630733755597", + "name": "isSender", + "type": 1 } ], "relations": [] }, { "id": "4:6179749773128044271", - "lastPropertyId": "13:6446821128426983596", + "lastPropertyId": "14:5371512009949707960", "name": "Contact", "properties": [ { @@ -71,7 +81,9 @@ { "id": "2:3202277046871450743", "name": "username", - "type": 9 + "indexId": "1:8059396809448816057", + "type": 9, + "flags": 2048 }, { "id": "3:8037315472776382017", @@ -122,6 +134,11 @@ "id": "13:6446821128426983596", "name": "remarkquanpin", "type": 9 + }, + { + "id": "14:5371512009949707960", + "name": "disturb", + "type": 1 } ], "relations": [] @@ -140,7 +157,9 @@ { "id": "2:2771882473472315", "name": "username", - "type": 9 + "indexId": "3:4492440644813580438", + "type": 9, + "flags": 2048 }, { "id": "3:1747776423604377158", @@ -172,7 +191,7 @@ }, { "id": "6:411582187056789368", - "lastPropertyId": "9:6199737871252062125", + "lastPropertyId": "11:1529665629379902832", "name": "Conversation", "properties": [ { @@ -184,7 +203,9 @@ { "id": "2:62500236312534806", "name": "talkerId", - "type": 9 + "indexId": "2:8554188191509595989", + "type": 9, + "flags": 2048 }, { "id": "3:1809723298214930621", @@ -212,21 +233,21 @@ "type": 5 }, { - "id": "8:2020630799900991467", - "name": "show", + "id": "10:1189093311778315437", + "name": "isShow", "type": 1 }, { - "id": "9:6199737871252062125", - "name": "msgLocalId", - "type": 6 + "id": "11:1529665629379902832", + "name": "isPinned", + "type": 1 } ], "relations": [] } ], "lastEntityId": "6:411582187056789368", - "lastIndexId": "0:0", + "lastIndexId": "3:4492440644813580438", "lastRelationId": "0:0", "lastSequenceId": "0:0", "modelVersion": 5, @@ -256,7 +277,9 @@ 3457793996580594247, 6315401035981995789, 2123413060720974577, - 8705063061921345729 + 8705063061921345729, + 6199737871252062125, + 2020630799900991467 ], "retiredRelationUids": [], "version": 1 diff --git a/app/src/main/java/com/kaixed/kchat/data/LocalDatabase.kt b/app/src/main/java/com/kaixed/kchat/data/LocalDatabase.kt index a8f5d89..11b6060 100644 --- a/app/src/main/java/com/kaixed/kchat/data/LocalDatabase.kt +++ b/app/src/main/java/com/kaixed/kchat/data/LocalDatabase.kt @@ -3,8 +3,10 @@ package com.kaixed.kchat.data import com.kaixed.kchat.data.local.box.ObjectBox.getBox import com.kaixed.kchat.data.local.entity.Contact import com.kaixed.kchat.data.local.entity.Contact_ -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.data.local.entity.Messages_ +import io.objectbox.Box +import io.objectbox.query.QueryBuilder /** * @Author: kaixed @@ -13,9 +15,27 @@ import com.kaixed.kchat.data.local.entity.Conversation_ object LocalDatabase { private val contactBox by lazy { getBox(Contact::class.java) } - private val conversationBox by lazy { getBox(Conversation::class.java) } - fun getContactByUsername(contactId: String): Contact? { return contactBox.query(Contact_.username.equal(contactId)).build().findFirst() } + + private val messagesBox: Box by lazy { getBox(Messages::class.java) } + + fun getMessagesWithContact(contactId: String, offset: Long, limit: Long): List { + val query = messagesBox + .query(Messages_.takerId.equal(contactId)) + .order(Messages_.timestamp, QueryBuilder.DESCENDING) + .build() + return query.find(offset, limit) + } + + fun getMoreMessages(contactId: String, msgLocalId: Long, limit: Long): List { + val query = messagesBox + .query(Messages_.takerId.equal(contactId)) + .lessOrEqual(Messages_.msgLocalId, msgLocalId) + .order(Messages_.timestamp, QueryBuilder.DESCENDING) + .build() + val offset = 0 + return query.find(offset.toLong(), limit) + } } \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/data/local/entity/Contact.kt b/app/src/main/java/com/kaixed/kchat/data/local/entity/Contact.kt index fc2fb18..6c64899 100644 --- a/app/src/main/java/com/kaixed/kchat/data/local/entity/Contact.kt +++ b/app/src/main/java/com/kaixed/kchat/data/local/entity/Contact.kt @@ -2,6 +2,7 @@ package com.kaixed.kchat.data.local.entity import io.objectbox.annotation.Entity import io.objectbox.annotation.Id +import io.objectbox.annotation.Index import kotlinx.serialization.Serializable /** @@ -13,8 +14,11 @@ import kotlinx.serialization.Serializable data class Contact( @Id var id: Long = 0L, + @Index var username: String, + @Index var nickname: String, + @Index var remark: String? = null, var signature: String? = null, var avatarUrl: String? = null, @@ -24,4 +28,5 @@ data class Contact( var showHeader: Boolean? = false, var address: String? = null, var gender: String? = null, + var disturb: Boolean = true ) diff --git a/app/src/main/java/com/kaixed/kchat/data/local/entity/Conversation.kt b/app/src/main/java/com/kaixed/kchat/data/local/entity/Conversation.kt index 55cca93..6c8330f 100644 --- a/app/src/main/java/com/kaixed/kchat/data/local/entity/Conversation.kt +++ b/app/src/main/java/com/kaixed/kchat/data/local/entity/Conversation.kt @@ -2,6 +2,7 @@ package com.kaixed.kchat.data.local.entity import io.objectbox.annotation.Entity import io.objectbox.annotation.Id +import io.objectbox.annotation.Index /** * @Author: kaixed @@ -12,11 +13,13 @@ import io.objectbox.annotation.Id data class Conversation( @Id var id: Long = 0L, + @Index var talkerId: String, var nickname: String, var avatarUrl: String, var lastContent: String, var timestamp: Long, var unreadCount: Int = 0, - var show: Boolean = true + var isShow: Boolean = true, + var isPinned: Boolean = false ) diff --git a/app/src/main/java/com/kaixed/kchat/data/local/entity/Messages.kt b/app/src/main/java/com/kaixed/kchat/data/local/entity/Messages.kt index 992d173..f24459e 100644 --- a/app/src/main/java/com/kaixed/kchat/data/local/entity/Messages.kt +++ b/app/src/main/java/com/kaixed/kchat/data/local/entity/Messages.kt @@ -19,8 +19,10 @@ data class Messages( var timestamp: Long, var status: String = "normal", var senderId: String, + var avatarUrl: String = "", var takerId: String, var type: String, var show: Boolean = true, -// var isShowTimer: Boolean = false + var isShowTimer: Boolean = false, + var isSender: Boolean = false ) diff --git a/app/src/main/java/com/kaixed/kchat/data/local/entity/UserInfo.kt b/app/src/main/java/com/kaixed/kchat/data/local/entity/UserInfo.kt index b199605..aad45e5 100644 --- a/app/src/main/java/com/kaixed/kchat/data/local/entity/UserInfo.kt +++ b/app/src/main/java/com/kaixed/kchat/data/local/entity/UserInfo.kt @@ -2,6 +2,7 @@ package com.kaixed.kchat.data.local.entity import io.objectbox.annotation.Entity import io.objectbox.annotation.Id +import io.objectbox.annotation.Index import kotlinx.serialization.Serializable /** @@ -14,6 +15,7 @@ import kotlinx.serialization.Serializable data class UserInfo( @Id var id: Long = 0, + @Index var username: String, var nickname: String, var avatarUrl: String, diff --git a/app/src/main/java/com/kaixed/kchat/model/FunctionItem.kt b/app/src/main/java/com/kaixed/kchat/data/model/FunctionItem.kt similarity index 78% rename from app/src/main/java/com/kaixed/kchat/model/FunctionItem.kt rename to app/src/main/java/com/kaixed/kchat/data/model/FunctionItem.kt index a8ef812..d4f899a 100644 --- a/app/src/main/java/com/kaixed/kchat/model/FunctionItem.kt +++ b/app/src/main/java/com/kaixed/kchat/data/model/FunctionItem.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.model +package com.kaixed.kchat.data.model /** * @Author: kaixed diff --git a/app/src/main/java/com/kaixed/kchat/model/HomeItem.kt b/app/src/main/java/com/kaixed/kchat/data/model/HomeItem.kt similarity index 79% rename from app/src/main/java/com/kaixed/kchat/model/HomeItem.kt rename to app/src/main/java/com/kaixed/kchat/data/model/HomeItem.kt index dbfefec..b55cce8 100644 --- a/app/src/main/java/com/kaixed/kchat/model/HomeItem.kt +++ b/app/src/main/java/com/kaixed/kchat/data/model/HomeItem.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.model +package com.kaixed.kchat.data.model /** * @Author: kaixed diff --git a/app/src/main/java/com/kaixed/kchat/data/model/Message.kt b/app/src/main/java/com/kaixed/kchat/data/model/Message.kt new file mode 100644 index 0000000..3d8bd07 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/data/model/Message.kt @@ -0,0 +1,12 @@ +package com.kaixed.kchat.data.model + +/** + * @Author: kaixed + * @Date: 2024/11/28 10:33 + */ +data class Message( + val id: Long, + val talkerId: String, + var lastContent: String, + val timestamp: Long +) diff --git a/app/src/main/java/com/kaixed/kchat/model/friend/FriendRequestItem.kt b/app/src/main/java/com/kaixed/kchat/data/model/friend/FriendRequestItem.kt similarity index 97% rename from app/src/main/java/com/kaixed/kchat/model/friend/FriendRequestItem.kt rename to app/src/main/java/com/kaixed/kchat/data/model/friend/FriendRequestItem.kt index 52d07d0..3e6cf95 100644 --- a/app/src/main/java/com/kaixed/kchat/model/friend/FriendRequestItem.kt +++ b/app/src/main/java/com/kaixed/kchat/data/model/friend/FriendRequestItem.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.model.friend +package com.kaixed.kchat.data.model.friend import android.os.Parcel import android.os.Parcelable diff --git a/app/src/main/java/com/kaixed/kchat/model/item/FriendItem.kt b/app/src/main/java/com/kaixed/kchat/data/model/item/FriendItem.kt similarity index 86% rename from app/src/main/java/com/kaixed/kchat/model/item/FriendItem.kt rename to app/src/main/java/com/kaixed/kchat/data/model/item/FriendItem.kt index 71f9bb0..5998830 100644 --- a/app/src/main/java/com/kaixed/kchat/model/item/FriendItem.kt +++ b/app/src/main/java/com/kaixed/kchat/data/model/item/FriendItem.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.model.item +package com.kaixed.kchat.data.model.item /** * @Author: kaixed diff --git a/app/src/main/java/com/kaixed/kchat/model/request/RegisterRequest.kt b/app/src/main/java/com/kaixed/kchat/data/model/request/RegisterRequest.kt similarity index 85% rename from app/src/main/java/com/kaixed/kchat/model/request/RegisterRequest.kt rename to app/src/main/java/com/kaixed/kchat/data/model/request/RegisterRequest.kt index dd559af..7d914ee 100644 --- a/app/src/main/java/com/kaixed/kchat/model/request/RegisterRequest.kt +++ b/app/src/main/java/com/kaixed/kchat/data/model/request/RegisterRequest.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.model.request +package com.kaixed.kchat.data.model.request import kotlinx.serialization.Serializable diff --git a/app/src/main/java/com/kaixed/kchat/model/request/UserRequest.kt b/app/src/main/java/com/kaixed/kchat/data/model/request/UserRequest.kt similarity index 87% rename from app/src/main/java/com/kaixed/kchat/model/request/UserRequest.kt rename to app/src/main/java/com/kaixed/kchat/data/model/request/UserRequest.kt index d36d4c6..1353e09 100644 --- a/app/src/main/java/com/kaixed/kchat/model/request/UserRequest.kt +++ b/app/src/main/java/com/kaixed/kchat/data/model/request/UserRequest.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.model.request +package com.kaixed.kchat.data.model.request import kotlinx.serialization.Serializable diff --git a/app/src/main/java/com/kaixed/kchat/model/response/register/Register.kt b/app/src/main/java/com/kaixed/kchat/data/model/response/register/Register.kt similarity index 79% rename from app/src/main/java/com/kaixed/kchat/model/response/register/Register.kt rename to app/src/main/java/com/kaixed/kchat/data/model/response/register/Register.kt index 0a1abbb..4b44058 100644 --- a/app/src/main/java/com/kaixed/kchat/model/response/register/Register.kt +++ b/app/src/main/java/com/kaixed/kchat/data/model/response/register/Register.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.model.response.register +package com.kaixed.kchat.data.model.response.register import kotlinx.serialization.Serializable diff --git a/app/src/main/java/com/kaixed/kchat/model/response/search/User.kt b/app/src/main/java/com/kaixed/kchat/data/model/response/search/User.kt similarity index 80% rename from app/src/main/java/com/kaixed/kchat/model/response/search/User.kt rename to app/src/main/java/com/kaixed/kchat/data/model/response/search/User.kt index 790b109..ce7dccf 100644 --- a/app/src/main/java/com/kaixed/kchat/model/response/search/User.kt +++ b/app/src/main/java/com/kaixed/kchat/data/model/response/search/User.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.model.response.search +package com.kaixed.kchat.data.model.response.search import kotlinx.serialization.Serializable diff --git a/app/src/main/java/com/kaixed/kchat/model/search/SearchItem.kt b/app/src/main/java/com/kaixed/kchat/data/model/search/SearchItem.kt similarity index 81% rename from app/src/main/java/com/kaixed/kchat/model/search/SearchItem.kt rename to app/src/main/java/com/kaixed/kchat/data/model/search/SearchItem.kt index f7864d5..4643798 100644 --- a/app/src/main/java/com/kaixed/kchat/model/search/SearchItem.kt +++ b/app/src/main/java/com/kaixed/kchat/data/model/search/SearchItem.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.model.search +package com.kaixed.kchat.data.model.search /** * @Author: kaixed @@ -10,5 +10,4 @@ data class SearchItem( val content: String, val hasMore: Boolean, val type: String, - - ) \ No newline at end of file +) diff --git a/app/src/main/java/com/kaixed/kchat/model/search/SearchUser.kt b/app/src/main/java/com/kaixed/kchat/data/model/search/SearchUser.kt similarity index 96% rename from app/src/main/java/com/kaixed/kchat/model/search/SearchUser.kt rename to app/src/main/java/com/kaixed/kchat/data/model/search/SearchUser.kt index cf1f291..1f21af5 100644 --- a/app/src/main/java/com/kaixed/kchat/model/search/SearchUser.kt +++ b/app/src/main/java/com/kaixed/kchat/data/model/search/SearchUser.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.model.search +package com.kaixed.kchat.data.model.search import android.os.Parcel import android.os.Parcelable diff --git a/app/src/main/java/com/kaixed/kchat/model/search/User.kt b/app/src/main/java/com/kaixed/kchat/data/model/search/User.kt similarity index 96% rename from app/src/main/java/com/kaixed/kchat/model/search/User.kt rename to app/src/main/java/com/kaixed/kchat/data/model/search/User.kt index 89ee9cd..6f3342e 100644 --- a/app/src/main/java/com/kaixed/kchat/model/search/User.kt +++ b/app/src/main/java/com/kaixed/kchat/data/model/search/User.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.model.search +package com.kaixed.kchat.data.model.search import android.os.Parcel import android.os.Parcelable diff --git a/app/src/main/java/com/kaixed/kchat/data/repository/ContactRepository.kt b/app/src/main/java/com/kaixed/kchat/data/repository/ContactRepository.kt index 004ee19..22472df 100644 --- a/app/src/main/java/com/kaixed/kchat/data/repository/ContactRepository.kt +++ b/app/src/main/java/com/kaixed/kchat/data/repository/ContactRepository.kt @@ -3,8 +3,8 @@ package com.kaixed.kchat.data.repository 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.data.model.friend.FriendRequestItem +import com.kaixed.kchat.data.model.search.SearchUser import com.kaixed.kchat.network.ApiCall.apiCall import com.kaixed.kchat.network.RetrofitClient import com.kaixed.kchat.utils.ConstantsUtil.getUsername @@ -26,19 +26,6 @@ class ContactRepository { apiCall = { contactApiService.getContactRequestList(username) }, errorMessage = "获取好友申请列表失败" ) -// return try { -// val response = contactApiService.getContactRequestList(username) -// if (response.isSuccess()) { -// val searchUser = response.getResponseData() -// searchUser?.let { -// Result.success(searchUser) -// } ?: Result.failure(Exception("没有好友申请")) -// } else { -// Result.failure(Exception(response.getResponseMsg())) -// } -// } catch (e: Exception) { -// Result.failure(e) -// } } // 接受联系人请求 @@ -54,20 +41,6 @@ class ContactRepository { it?.let { ContactUtil.handleContact(it) } -// return try { -// val response = contactApiService.acceptContactRequest(contactId, username, remark) -// if (response.isSuccess()) { -// val searchUser = response.getResponseData() -// searchUser?.let { -// ContactUtil.handleContact(searchUser) -// Result.success(searchUser) -// } ?: Result.failure(Exception("添加好友失败")) -// } else { -// Result.failure(Exception(response.getResponseMsg())) -// } -// } catch (e: Exception) { -// Result.failure(e) -// } } } @@ -84,22 +57,6 @@ class ContactRepository { contactBox.query(Contact_.username.equal(contactId)).build().findFirst() contactBox.remove(con!!) } -// return try { -// val response = contactApiService.deleteContact(username, contactId) -// if (response.isSuccess()) { -// val searchUser = response.getResponseData() -// searchUser?.let { -// val con = -// contactBox.query(Contact_.username.equal(contactId)).build().findFirst() -// contactBox.remove(con!!) -// Result.success(searchUser) -// } ?: Result.failure(Exception("删除好友失败")) -// } else { -// Result.failure(Exception(response.getResponseMsg())) -// } -// } catch (e: Exception) { -// Result.failure(e) -// } } // 添加联系人 @@ -108,44 +65,14 @@ class ContactRepository { apiCall = { contactApiService.addContact(contactId, getUsername(), message) }, errorMessage = "添加联系人失败" ) -// return try { -// val response = contactApiService.addContact( -// senderId = contactId, -// receiverId = getUsername(), -// message = message -// ) -// if (response.isSuccess()) { -// val searchUser = response.getResponseData() -// searchUser?.let { -// Result.success(searchUser) -// } ?: Result.failure(Exception("添加联系人失败")) -// } else { -// Result.failure(Exception(response.getResponseMsg())) -// } -// } catch (e: Exception) { -// Result.failure(e) -// } } // 搜索联系人 - suspend fun searchContact(username: String): Result { + suspend fun searchContact(username: String): Result { return apiCall( apiCall = { contactApiService.searchContact(username) }, errorMessage = "搜索用户失败" ) -// return try { -// val response = contactApiService.searchContact(username) -// if (response.isSuccess()) { -// val searchUser = response.getResponseData() -// searchUser?.let { -// Result.success(searchUser) -// } ?: Result.failure(Exception("没有找到用户")) -// } else { -// Result.failure(Exception(response.getResponseMsg())) -// } -// } catch (e: Exception) { -// Result.failure(e) -// } } // 获取联系人列表 diff --git a/app/src/main/java/com/kaixed/kchat/data/repository/FileRepository.kt b/app/src/main/java/com/kaixed/kchat/data/repository/FileRepository.kt new file mode 100644 index 0000000..68be6d6 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/data/repository/FileRepository.kt @@ -0,0 +1,30 @@ +package com.kaixed.kchat.data.repository + +import com.kaixed.kchat.network.ApiCall.apiCall +import com.kaixed.kchat.network.RetrofitClient +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.MultipartBody +import okhttp3.RequestBody.Companion.asRequestBody +import java.io.File + +/** + * @Author: kaixed + * @Date: 2024/11/29 15:15 + */ +class FileRepository { + private val fileService = RetrofitClient.fileApiService + + suspend fun uploadFile(file: File) = + apiCall( + apiCall = { + val multiFile = MultipartBody.Part.createFormData( + "file", + file.name, + file.asRequestBody("application/octet-stream".toMediaTypeOrNull()) + ) + + fileService.uploadFile(multiFile) + }, + errorMessage = "获取好友申请列表失败" + ) +} \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/data/repository/UserAuthRepository.kt b/app/src/main/java/com/kaixed/kchat/data/repository/UserAuthRepository.kt index d68e438..ac58d70 100644 --- a/app/src/main/java/com/kaixed/kchat/data/repository/UserAuthRepository.kt +++ b/app/src/main/java/com/kaixed/kchat/data/repository/UserAuthRepository.kt @@ -3,8 +3,8 @@ package com.kaixed.kchat.data.repository 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.data.model.request.RegisterRequest +import com.kaixed.kchat.data.model.response.register.Register import com.kaixed.kchat.network.ApiCall.apiCall import com.kaixed.kchat.network.RetrofitClient import com.kaixed.kchat.utils.Constants.MMKV_USER_SESSION @@ -33,28 +33,6 @@ class UserAuthRepository { insertUserInfo(register, registerRequest.telephone) } } -// return try { -// val response = userApiService.register(registerRequest) -// if (response.isSuccess()) { -// val register = response.getResponseData() -// register?.let { -// val userInfo = UserInfo( -// username = register.username, -// nickname = register.nickname, -// avatarUrl = "", -// signature = "", -// telephone = registerRequest.telephone -// ) -// userInfoBox.put(userInfo) -// -// Result.success(register) -// } ?: Result.failure(Exception("注册成功,但未返回用户数据")) -// } else { -// Result.failure(Exception(response.getResponseMsg())) -// } -// } catch (e: Exception) { -// Result.failure(e) -// } } // 登录方法 @@ -67,22 +45,6 @@ class UserAuthRepository { updateDb(userInfo) } } -// return try { -// val response = userApiService.loginByUsername(username, password) -// if (response.isSuccess()) { -// val userInfo = response.getResponseData() -// if (userInfo != null) { -// updateDb(userInfo) -// Result.success(userInfo) -// } else { -// Result.failure(Exception("登录成功,但未返回用户数据")) -// } -// } else { -// Result.failure(Exception(response.getResponseMsg())) -// } -// } catch (e: Exception) { -// Result.failure(e) -// } } @@ -95,23 +57,12 @@ class UserAuthRepository { updateDb(userInfo) } } -// return try { -// val response = userApiService.loginByTelephone(telephone, password) -// if (response.isSuccess()) { -// val userInfo = response.getResponseData() -// userInfo?.let { -// updateDb(userInfo) -// Result.success(userInfo) -// } ?: Result.failure(Exception("登录成功,但未返回用户数据")) -// } else { -// Result.failure(Exception(response.getResponseMsg())) -// } -// } catch (e: Exception) { -// Result.failure(e) -// } } - private fun insertUserInfo(register: Register, telephone: String) { + private fun insertUserInfo( + register: Register, + telephone: String + ) { val userInfo = UserInfo( username = register.username, nickname = register.nickname, diff --git a/app/src/main/java/com/kaixed/kchat/data/repository/UserProfileRepository.kt b/app/src/main/java/com/kaixed/kchat/data/repository/UserProfileRepository.kt index 539afea..2ade3ba 100644 --- a/app/src/main/java/com/kaixed/kchat/data/repository/UserProfileRepository.kt +++ b/app/src/main/java/com/kaixed/kchat/data/repository/UserProfileRepository.kt @@ -3,8 +3,8 @@ package com.kaixed.kchat.data.repository 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.data.model.request.UserRequest +import com.kaixed.kchat.data.model.search.SearchUser import com.kaixed.kchat.network.RetrofitClient import com.kaixed.kchat.utils.Constants.MMKV_USER_SESSION import com.kaixed.kchat.utils.Constants.NICKNAME_KEY diff --git a/app/src/main/java/com/kaixed/kchat/data/repository/UserSearchRepository.kt b/app/src/main/java/com/kaixed/kchat/data/repository/UserSearchRepository.kt index 67ae94d..a0f96dc 100644 --- a/app/src/main/java/com/kaixed/kchat/data/repository/UserSearchRepository.kt +++ b/app/src/main/java/com/kaixed/kchat/data/repository/UserSearchRepository.kt @@ -1,6 +1,6 @@ package com.kaixed.kchat.data.repository -import com.kaixed.kchat.model.response.search.User +import com.kaixed.kchat.data.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 deleted file mode 100644 index 8c3be25..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/friend/AcceptContactRequest.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.kaixed.kchat.model.friend - -import com.kaixed.kchat.data.local.entity.Contact -import kotlinx.serialization.Serializable - -@Serializable -data class AcceptContactRequest( - val code: String, - val msg: String, - val `data`: Contact? -) \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/model/friend/ContactRequestResponse.kt b/app/src/main/java/com/kaixed/kchat/model/friend/ContactRequestResponse.kt deleted file mode 100644 index 0bf9ad3..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/friend/ContactRequestResponse.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.kaixed.kchat.model.friend - -import kotlinx.serialization.Serializable - -@Serializable -data class ContactRequestResponse( - val code: String, - val msg: String, - val `data`: List, -) \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/model/friend/FriendItem.kt b/app/src/main/java/com/kaixed/kchat/model/friend/FriendItem.kt deleted file mode 100644 index 20f0d04..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/friend/FriendItem.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.kaixed.kchat.model.friend - -import kotlinx.serialization.Serializable - -/** - * @Author: kaixed - * @Date: 2024/10/14 14:30 - */ - -@Serializable -data class FriendItem( - val username: String, - val avatarUrl: String, - val nickname: String, - val remark: String, - val signature: String, -) \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/model/response/ApplyFriend.kt b/app/src/main/java/com/kaixed/kchat/model/response/ApplyFriend.kt deleted file mode 100644 index ff8b75a..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/response/ApplyFriend.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.kaixed.kchat.model.response - -import kotlinx.serialization.Serializable - -@Serializable -data class ApplyFriend( - val code: String, - val `data`: String, - val msg: String -) \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/model/response/friend/DeleteContact.kt b/app/src/main/java/com/kaixed/kchat/model/response/friend/DeleteContact.kt deleted file mode 100644 index f75311a..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/response/friend/DeleteContact.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.kaixed.kchat.model.response.friend - -data class DeleteContact( - val code: String, - val `data`: String?, - val msg: String -) \ No newline at end of file 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 deleted file mode 100644 index 7568351..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/response/friend/FriendList.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.kaixed.kchat.model.response.friend - -import com.kaixed.kchat.data.local.entity.Contact -import kotlinx.serialization.Serializable - -/** - * @Author: kaixed - * @Date: 2024/10/17 22:00 - */ - -@Serializable -data class FriendList( - val code: String, - val msg: String, - val `data`: List?, -) diff --git a/app/src/main/java/com/kaixed/kchat/model/response/friend/SearchFriends.kt b/app/src/main/java/com/kaixed/kchat/model/response/friend/SearchFriends.kt deleted file mode 100644 index 56d0dc9..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/response/friend/SearchFriends.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.kaixed.kchat.model.response.friend - -import com.kaixed.kchat.model.search.User -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? -) diff --git a/app/src/main/java/com/kaixed/kchat/model/response/login/Data.kt b/app/src/main/java/com/kaixed/kchat/model/response/login/Data.kt deleted file mode 100644 index 4981df1..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/response/login/Data.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.kaixed.kchat.model.response.login - -import kotlinx.serialization.Serializable - -@Serializable -data class Data( - var id: Long, - val avatarUrl: String, - val nickname: String, - val signature: String, - var telephone: String, - val status: String?, - val username: String, -) 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 deleted file mode 100644 index 8fb8b59..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/response/login/Login.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.kaixed.kchat.model.response.login - -import com.kaixed.kchat.data.local.entity.UserInfo -import kotlinx.serialization.Serializable - -@Serializable -data class Login( - val code: String, - val msg: String, - val `data`: UserInfo?, -) diff --git a/app/src/main/java/com/kaixed/kchat/model/response/search/Data.kt b/app/src/main/java/com/kaixed/kchat/model/response/search/Data.kt deleted file mode 100644 index f2b0351..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/response/search/Data.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.kaixed.kchat.model.response.search - -import kotlinx.serialization.Serializable - -/** - * @Author: kaixed - * @Date: 2024/10/16 13:35 - */ - -@Serializable -data class Data( - val userLists: List, -) \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/model/response/search/UserList.kt b/app/src/main/java/com/kaixed/kchat/model/response/search/UserList.kt deleted file mode 100644 index 85169c6..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/response/search/UserList.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.kaixed.kchat.model.response.search - -import kotlinx.serialization.Serializable - -/** - * @Author: kaixed - * @Date: 2024/10/16 13:34 - */ - -@Serializable -data class UserList( - val code: String, - val msg: String, - val data: Data, -) diff --git a/app/src/main/java/com/kaixed/kchat/model/response/user/ChangeNickname.kt b/app/src/main/java/com/kaixed/kchat/model/response/user/ChangeNickname.kt deleted file mode 100644 index 329cfcb..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/response/user/ChangeNickname.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.kaixed.kchat.model.response.user - -import kotlinx.serialization.Serializable - -@Serializable -data class ChangeNickname( - val code: String, - val `data`: String?, - val msg: String -) \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/model/response/user/UploadAvatar.kt b/app/src/main/java/com/kaixed/kchat/model/response/user/UploadAvatar.kt deleted file mode 100644 index aa75730..0000000 --- a/app/src/main/java/com/kaixed/kchat/model/response/user/UploadAvatar.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.kaixed.kchat.model.response.user - -import kotlinx.serialization.Serializable - -/** - * @Author: kaixed - * @Date: 2024/11/14 21:38 - */ -@Serializable -data class UploadAvatar( - var code: String, - var msg: String, - var data: String? -) diff --git a/app/src/main/java/com/kaixed/kchat/network/RetrofitClient.kt b/app/src/main/java/com/kaixed/kchat/network/RetrofitClient.kt index 34d9306..e3c151b 100644 --- a/app/src/main/java/com/kaixed/kchat/network/RetrofitClient.kt +++ b/app/src/main/java/com/kaixed/kchat/network/RetrofitClient.kt @@ -1,7 +1,7 @@ package com.kaixed.kchat.network -import com.kaixed.kchat.network.interceptor.SignInterceptor import com.kaixed.kchat.network.service.ContactService +import com.kaixed.kchat.network.service.FileApiService import com.kaixed.kchat.network.service.UserApiService import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor @@ -42,4 +42,8 @@ object RetrofitClient { val contactApiService: ContactService by lazy { retrofit.create(ContactService::class.java) } + + val fileApiService: FileApiService by lazy { + retrofit.create(FileApiService::class.java) + } } diff --git a/app/src/main/java/com/kaixed/kchat/network/SignUtil.kt b/app/src/main/java/com/kaixed/kchat/network/SignUtil.kt index d3e3931..d94b530 100644 --- a/app/src/main/java/com/kaixed/kchat/network/SignUtil.kt +++ b/app/src/main/java/com/kaixed/kchat/network/SignUtil.kt @@ -3,7 +3,7 @@ package com.kaixed.kchat.network import com.google.gson.Gson import com.google.gson.JsonObject import com.google.gson.JsonParser -import com.kaixed.kchat.model.request.UserRequest +import com.kaixed.kchat.data.model.request.UserRequest import okhttp3.MediaType.Companion.toMediaType import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody @@ -103,7 +103,12 @@ object SignUtil { val request = Request.Builder() .url("https://app.kaixed.com/kchat/users/login/username") .post( - Gson().toJson(UserRequest("username", "password")) + Gson().toJson( + UserRequest( + "username", + "password" + ) + ) .toRequestBody("application/json".toMediaType()) ) .build() 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 5d5ae8f..b06bda8 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,8 +1,8 @@ package com.kaixed.kchat.network.service 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.data.model.friend.FriendRequestItem +import com.kaixed.kchat.data.model.search.SearchUser import com.kaixed.kchat.network.ApiResponse import retrofit2.http.Field import retrofit2.http.FormUrlEncoded @@ -10,6 +10,10 @@ import retrofit2.http.GET import retrofit2.http.POST import retrofit2.http.Path +/** + * @Author: kaixed + * @Date: 2024/11/15 11:00 + */ interface ContactService { // 获取联系人请求列表 @@ -41,7 +45,7 @@ interface ContactService { @GET("users/{username}") suspend fun searchContact( @Path("username") username: String - ): ApiResponse + ): ApiResponse // 获取联系人列表 @FormUrlEncoded diff --git a/app/src/main/java/com/kaixed/kchat/network/service/FileApiService.kt b/app/src/main/java/com/kaixed/kchat/network/service/FileApiService.kt new file mode 100644 index 0000000..05b4d29 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/network/service/FileApiService.kt @@ -0,0 +1,20 @@ +package com.kaixed.kchat.network.service + +import com.kaixed.kchat.network.ApiResponse +import okhttp3.MultipartBody +import retrofit2.http.Multipart +import retrofit2.http.POST +import retrofit2.http.Part + +/** + * @Author: kaixed + * @Date: 2024/11/29 15:11 + */ +interface FileApiService { + + @Multipart + @POST("file/upload") + suspend fun uploadFile( + @Part file: MultipartBody.Part + ): ApiResponse +} \ No newline at end of file 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 28bd577..225ff99 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,11 +1,11 @@ package com.kaixed.kchat.network.service 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.data.model.request.RegisterRequest +import com.kaixed.kchat.data.model.request.UserRequest +import com.kaixed.kchat.data.model.response.register.Register +import com.kaixed.kchat.data.model.response.search.User +import com.kaixed.kchat.data.model.search.SearchUser import com.kaixed.kchat.network.ApiResponse import okhttp3.MultipartBody import retrofit2.http.Body 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 aaf6258..c2d8179 100644 --- a/app/src/main/java/com/kaixed/kchat/service/WebSocketService.kt +++ b/app/src/main/java/com/kaixed/kchat/service/WebSocketService.kt @@ -9,9 +9,12 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import com.google.gson.Gson 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.data.local.entity.Conversation import com.kaixed.kchat.data.local.entity.Conversation_ import com.kaixed.kchat.data.local.entity.Messages +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 @@ -88,9 +91,42 @@ class WebSocketService : Service() { } fun storeOwnerMsg(messages: Messages) { + if (messages.avatarUrl == "") { + val contactBox: Box = getBoxStore().boxFor(Contact::class.java) + val contact = + contactBox.query(Contact_.username.equal(messages.takerId)).build().findFirst() + messages.avatarUrl = contact?.avatarUrl ?: "" + } updateConversationList(messages) } + fun deleteConversationList(con: Conversation) { + val conversation = conversationList.find { it.talkerId == con.talkerId }!! + conversationList.remove(conversation) + conversationBox.remove(conversation.id) + messagesBox.query(Messages_.takerId.equal(con.talkerId)).build().remove() + _conversations.postValue(conversationList.sortedByDescending { it.timestamp }) + } + + + fun updateConversationList(con: Conversation) { + val conversation = conversationList.find { it.talkerId == con.talkerId }!! + if (!con.isShow) { + conversation.apply { + isShow = false + conversationList.remove(conversation) + } + } + + conversation.apply { + isPinned = con.isPinned + unreadCount = con.unreadCount + } + + conversationBox.put(conversation) + _conversations.postValue(conversationList.sortedByDescending { it.timestamp }) + } + private fun establishConnection() { if (webSocket == null) { val request = Request.Builder() @@ -107,32 +143,38 @@ class WebSocketService : Service() { .findFirst() val currentContactId = getCurrentContactId() - - conversation?.let { - conversation.unreadCount = - if (conversation.talkerId == currentContactId || messages.senderId == getUsername()) 0 - else conversation.unreadCount + 1 - - conversation.lastContent = if (messages.type == "4") "[图片]" else messages.content - conversation.timestamp = messages.timestamp + if (conversation != null) { + conversation.apply { + unreadCount = + if (talkerId == currentContactId || messages.senderId == getUsername()) 0 else unreadCount + 1 + lastContent = if (messages.type == "4") "[图片]" else messages.content + timestamp = messages.timestamp + } conversationBox.put(conversation) - } ?: run { - conversationBox.put( - createChatList( - talkerId = messages.takerId, - nickname = messages.takerId, - content = messages.content, - timestamp = messages.timestamp, - unreadCount = if (messages.senderId == getUsername()) 0 else 1 - ) - ) + } else { + val con = createChatList(messages) + conversationBox.put(con) } } + private fun createChatList( + messages: Messages + ): Conversation { + return Conversation( + talkerId = messages.takerId, + nickname = messages.takerId, + avatarUrl = messages.avatarUrl, + lastContent = if (messages.type == "4") "[图片]" else messages.content, + timestamp = messages.timestamp, + unreadCount = if (messages.senderId == getUsername()) 0 else 1 + ) + } + private fun createChatList( talkerId: String, nickname: String, content: String, + avatarUrl: String, timestamp: Long, unreadCount: Int = 1 ): Conversation { @@ -140,7 +182,7 @@ class WebSocketService : Service() { 0L, talkerId = talkerId, nickname = nickname, - avatarUrl = "https://s3.qjqq.cn/49/660ff4a698da0.webp!color", + avatarUrl = avatarUrl, lastContent = content, timestamp = timestamp, unreadCount = unreadCount @@ -215,6 +257,7 @@ class WebSocketService : Service() { nickname = messages.takerId, content = if (messages.type == "4") "[图片]" else messages.content, timestamp = messages.timestamp, + avatarUrl = messages.avatarUrl, unreadCount = if (messages.senderId == getUsername()) 0 else 1 ) ) @@ -223,7 +266,7 @@ class WebSocketService : Service() { } private fun firstLoad() { - conversationList = conversationBox.query(Conversation_.show.equal(true)) + conversationList = conversationBox.query(Conversation_.isShow.equal(true)) .orderDesc(Conversation_.timestamp) .build() .find() diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/ApplyFriendsDetailActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/ApplyFriendsDetailActivity.kt index 3ce3354..141532e 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/ApplyFriendsDetailActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/ApplyFriendsDetailActivity.kt @@ -6,9 +6,9 @@ import android.os.Bundle import androidx.activity.enableEdgeToEdge import androidx.annotation.RequiresApi import com.bumptech.glide.Glide +import com.kaixed.kchat.data.model.friend.FriendRequestItem +import com.kaixed.kchat.data.model.search.User import com.kaixed.kchat.databinding.ActivityApplyFriendsDetailBinding -import com.kaixed.kchat.model.friend.FriendRequestItem -import com.kaixed.kchat.model.search.User import com.kaixed.kchat.ui.base.BaseActivity class ApplyFriendsDetailActivity : BaseActivity() { @@ -33,7 +33,10 @@ class ApplyFriendsDetailActivity : BaseActivity() { 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 cc3a2c5..636d9be 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 @@ -9,7 +9,10 @@ import android.graphics.Color import android.graphics.Rect import android.graphics.drawable.ColorDrawable import android.os.Bundle +import android.os.Handler import android.os.IBinder +import android.os.Looper +import android.util.Log import android.view.KeyEvent import android.view.MotionEvent import android.view.View @@ -18,36 +21,47 @@ import android.view.inputmethod.InputMethodManager import android.widget.LinearLayout import androidx.activity.OnBackPressedCallback import androidx.activity.enableEdgeToEdge +import androidx.activity.viewModels import androidx.core.widget.addTextChangedListener 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.local.box.ObjectBox.getBoxStore +import com.kaixed.kchat.data.LocalDatabase.getMessagesWithContact +import com.kaixed.kchat.data.LocalDatabase.getMoreMessages +import com.kaixed.kchat.data.local.box.ObjectBox.getBox import com.kaixed.kchat.data.local.entity.Messages -import com.kaixed.kchat.data.local.entity.Messages_ +import com.kaixed.kchat.data.model.FunctionItem import com.kaixed.kchat.databinding.ActivityChatBinding -import com.kaixed.kchat.model.FunctionItem import com.kaixed.kchat.service.WebSocketService import com.kaixed.kchat.service.WebSocketService.LocalBinder import com.kaixed.kchat.ui.adapter.ChatAdapter import com.kaixed.kchat.ui.adapter.EmojiAdapter import com.kaixed.kchat.ui.adapter.FunctionPanelAdapter import com.kaixed.kchat.ui.base.BaseActivity +import com.kaixed.kchat.ui.i.IOnItemClickListener import com.kaixed.kchat.ui.i.OnItemClickListener +import com.kaixed.kchat.ui.widget.LoadingDialogFragment import com.kaixed.kchat.utils.Constants.CURRENT_CONTACT_ID import com.kaixed.kchat.utils.Constants.KEYBOARD_HEIGHT_RATIO import com.kaixed.kchat.utils.Constants.MMKV_USER_SESSION import com.kaixed.kchat.utils.ConstantsUtil.getKeyboardHeight import com.kaixed.kchat.utils.ConstantsUtil.getUsername +import com.kaixed.kchat.utils.ImageEngines import com.kaixed.kchat.utils.ImageSpanUtil.insertEmoji +import com.kaixed.kchat.viewmodel.FileViewModel +import com.luck.picture.lib.basic.PictureSelector +import com.luck.picture.lib.config.SelectMimeType +import com.luck.picture.lib.entity.LocalMedia +import com.luck.picture.lib.interfaces.OnResultCallbackListener import com.tencent.mmkv.MMKV import io.objectbox.Box -import io.objectbox.query.QueryBuilder import org.json.JSONObject +import java.io.File import java.util.LinkedList -class ChatActivity : BaseActivity(), OnItemClickListener { +class ChatActivity : BaseActivity(), OnItemClickListener, + IOnItemClickListener { private var chatAdapter: ChatAdapter? = null @@ -55,7 +69,7 @@ class ChatActivity : BaseActivity(), OnItemClickListener { private val messagesList = LinkedList() - private lateinit var messagesBox: Box + private val messagesBox: Box by lazy { getBox(Messages::class.java) } private var contactId: String = "" @@ -85,8 +99,9 @@ class ChatActivity : BaseActivity(), OnItemClickListener { private var lastMessage: Messages? = null + private val fileViewModel: FileViewModel by viewModels() + companion object { - private const val TAG = "ChatActivity" private const val LIMIT: Long = 20L private const val UNBLOCK_DELAY_TIME = 200L } @@ -164,7 +179,7 @@ class ChatActivity : BaseActivity(), OnItemClickListener { false } - binding.ivFunctionPanel.setOnClickListener { v -> + binding.ivFunctionPanel.setOnClickListener { if (binding.clBottomPanel.isShown) { if (binding.rvEmoji.isShown) { handlePanelSwitch(false) @@ -176,7 +191,7 @@ class ChatActivity : BaseActivity(), OnItemClickListener { } } - binding.ivEmoji.setOnClickListener { v -> + binding.ivEmoji.setOnClickListener { if (binding.clBottomPanel.isShown) { if (binding.gvFunctionPanel.isShown) { handlePanelSwitch(true) @@ -334,6 +349,7 @@ class ChatActivity : BaseActivity(), OnItemClickListener { } val functionPanelAdapter = FunctionPanelAdapter(functionItems, this) + functionPanelAdapter.setOnItemClickListener(this) binding.gvFunctionPanel.adapter = functionPanelAdapter } @@ -377,7 +393,6 @@ class ChatActivity : BaseActivity(), OnItemClickListener { contactNickname = intent.getStringExtra("contactNickname") binding.ctb.setTitleName(contactNickname!!) - messagesBox = getBoxStore().boxFor(Messages::class.java) softKeyboardHeight = getKeyboardHeight() mmkv.putString(CURRENT_CONTACT_ID, contactId) } @@ -394,7 +409,7 @@ class ChatActivity : BaseActivity(), OnItemClickListener { * 初次进入进行加载历史数据 */ private fun firstLoadData() { - val messages = queryData(0, LIMIT + 1) + val messages = getMessagesWithContact(contactId, 0, LIMIT + 1) if (messages.isNotEmpty()) { val size = messages.size hasHistory = size > LIMIT @@ -413,7 +428,7 @@ class ChatActivity : BaseActivity(), OnItemClickListener { loading = true - val newMessages: List = queryDataById(tempIndex, LIMIT + 1) + val newMessages: List = getMoreMessages(contactId, tempIndex, LIMIT + 1) val size = newMessages.size tempIndex = newMessages[size - 1].msgLocalId @@ -480,7 +495,7 @@ class ChatActivity : BaseActivity(), OnItemClickListener { jsonObject2.put("receiverId", contactId) jsonObject2.put("content", message) jsonObject2.put("msgLocalId", id) - jsonObject.put("type", "single") + jsonObject.put("type", if (type == "4") "image" else "text") jsonObject.put("body", jsonObject2) webSocketService!!.sendMessage(jsonObject.toString(), id) webSocketService!!.storeOwnerMsg(messages) @@ -494,23 +509,6 @@ class ChatActivity : BaseActivity(), OnItemClickListener { binding.recycleChatList.smoothScrollToPosition(0) } - private fun queryData(offset: Long, limit: Long): List { - val query = messagesBox - .query(Messages_.takerId.equal(contactId)) - .order(Messages_.timestamp, QueryBuilder.DESCENDING) - .build() - return query.find(offset, limit) - } - - private fun queryDataById(msgLocalId: Long, limit: Long): List { - val query = messagesBox - .query(Messages_.takerId.equal(contactId)) - .lessOrEqual(Messages_.msgLocalId, msgLocalId) - .order(Messages_.timestamp, QueryBuilder.DESCENDING) - .build() - val offset = 0 - return query.find(offset.toLong(), limit) - } private fun bindWebSocketService() { bindService( @@ -531,4 +529,64 @@ class ChatActivity : BaseActivity(), OnItemClickListener { bound = false } } + + private fun uploadFile(imagePath: String) { + val loadingDialog: LoadingDialogFragment = + LoadingDialogFragment.newInstance("正在上传...") + + loadingDialog.showLoading(supportFragmentManager) + if (imagePath.isEmpty()) { + return + } + val file = File(imagePath) + fileViewModel.uploadFileResult.observe(this) { result -> + result.onSuccess { + sendMessage(contactId, it!!, "4") + toast("上传成功") + + } + + result.onFailure { + toast("上传失败") + } + + loadingDialog.dismissLoading() + } + fileViewModel.uploadFile(file) + } + + + //TODO: 待优化成微信选择上传模式,当前上传模式耗费用户等待时间 + private fun selectPicture() { + PictureSelector.create(this) + .openGallery(SelectMimeType.ofImage()) + .setImageEngine(ImageEngines()) + .forResult(object : OnResultCallbackListener { + override fun onResult(result: ArrayList) { + result.forEach { localMedia -> + localMedia?.let { + val imagePath = it.realPath + val handler = Handler(Looper.getMainLooper()) + handler.postDelayed({ + uploadFile(imagePath) + }, 200L) + Log.d("haha", "图片路径: $imagePath") + } + } + } + + override fun onCancel() { + toast("已取消") + } + }) + } + + override fun onFunctionItemClick(position: Int) { + when (position) { + 0 -> { + selectPicture() + // 相册 + } + } + } } diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/ContactRequestListActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/ContactRequestListActivity.kt index ec7ff8c..ee2e352 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/ContactRequestListActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/ContactRequestListActivity.kt @@ -5,7 +5,7 @@ import androidx.activity.enableEdgeToEdge import androidx.activity.viewModels import androidx.recyclerview.widget.LinearLayoutManager import com.kaixed.kchat.databinding.ActivityContactRequestListBinding -import com.kaixed.kchat.model.friend.FriendRequestItem +import com.kaixed.kchat.data.model.friend.FriendRequestItem import com.kaixed.kchat.ui.adapter.ContactRequestListAdapter import com.kaixed.kchat.ui.base.BaseActivity import com.kaixed.kchat.utils.ConstantsUtil.getUsername 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 b3a8859..3ae42a3 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 @@ -10,6 +10,7 @@ import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.widget.ViewPager2 +import com.bumptech.glide.Glide import com.kaixed.kchat.R import com.kaixed.kchat.data.local.box.ObjectBox.getBoxStore import com.kaixed.kchat.data.local.entity.Contact 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 b956458..76358f8 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,6 +13,7 @@ import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat +import com.bumptech.glide.Glide 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_ @@ -88,6 +89,7 @@ class ProfileDetailActivity : BaseActivity() { userViewModel.uploadAvatarResult.observe(this) { result -> result.onSuccess { + Glide.with(this).downloadOnly().load(it).submit() userSessionMMKV.putString(AVATAR_URL, it) toast("上传成功") binding.ciAvatar.setItemIcon(uri) 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 6295f10..fa1f3e4 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 @@ -17,7 +17,7 @@ import com.kaixed.kchat.R 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.data.model.request.RegisterRequest import com.kaixed.kchat.ui.base.BaseActivity import com.kaixed.kchat.utils.DensityUtil.dpToPx import com.kaixed.kchat.utils.DrawableUtil.createDrawable 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 3271e60..4b80729 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 @@ -9,7 +9,7 @@ import androidx.core.widget.addTextChangedListener 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.data.model.request.UserRequest import com.kaixed.kchat.ui.base.BaseActivity import com.kaixed.kchat.ui.widget.LoadingDialogFragment import com.kaixed.kchat.utils.ConstantsUtil.getNickName diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/SearchActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/SearchActivity.kt index c575980..3037f47 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/SearchActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/SearchActivity.kt @@ -4,13 +4,12 @@ import android.os.Bundle import android.view.View import android.widget.EdgeEffect import androidx.activity.enableEdgeToEdge -import androidx.appcompat.app.AppCompatActivity import androidx.core.widget.addTextChangedListener import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.EdgeEffectFactory +import com.kaixed.kchat.data.model.search.SearchItem import com.kaixed.kchat.databinding.ActivitySearchBinding -import com.kaixed.kchat.model.search.SearchItem import com.kaixed.kchat.ui.adapter.SearchResultAdapter import com.kaixed.kchat.ui.base.BaseActivity @@ -78,13 +77,19 @@ class SearchActivity : BaseActivity() { } private fun getAdapter(): SearchResultAdapter { - val contactItem = SearchItem("sa", "kaixed", "as", false, "联系人") - val chatHistoryItem = SearchItem("sa", "kaixed", "as", false, "群聊") - val groupItem = SearchItem("sa", "kaixed", "as", false, "聊天记录") + val contactItem = + SearchItem("sa", "kaixed", "as", false, "联系人") + val chatHistoryItem = + SearchItem("sa", "kaixed", "as", false, "群聊") + val groupItem = + SearchItem("sa", "kaixed", "as", false, "聊天记录") - val searchItem1 = SearchItem("sa", "kaixed", "as", true, "联系人") - val searchItem2 = SearchItem("sa", "kaixed", "as", true, "群聊") - val searchItem3 = SearchItem("sa", "kaixed", "as", true, "聊天记录") + val searchItem1 = + SearchItem("sa", "kaixed", "as", true, "联系人") + val searchItem2 = + SearchItem("sa", "kaixed", "as", true, "群聊") + val searchItem3 = + SearchItem("sa", "kaixed", "as", true, "聊天记录") val objects: MutableList = ArrayList(12) diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/SearchFriendsActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/SearchFriendsActivity.kt index 79a99a1..44138a2 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/SearchFriendsActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/SearchFriendsActivity.kt @@ -17,9 +17,9 @@ import androidx.activity.enableEdgeToEdge import androidx.activity.viewModels import androidx.core.widget.addTextChangedListener import com.bumptech.glide.Glide +import com.kaixed.kchat.data.model.search.SearchUser import com.kaixed.kchat.databinding.ActivitySearchFriendsBinding import com.kaixed.kchat.databinding.DialogLoadingBinding -import com.kaixed.kchat.model.search.User import com.kaixed.kchat.ui.base.BaseActivity import com.kaixed.kchat.viewmodel.ContactViewModel @@ -27,7 +27,7 @@ class SearchFriendsActivity : BaseActivity() { private var isSearching = false - private lateinit var userItem: User + private lateinit var userItem: SearchUser private lateinit var loadingDialog: Dialog diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/TestActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/TestActivity.kt index 4bed266..7b17e3e 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/TestActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/TestActivity.kt @@ -2,13 +2,11 @@ package com.kaixed.kchat.ui.activity import android.os.Bundle import androidx.activity.enableEdgeToEdge +import androidx.recyclerview.widget.LinearLayoutManager +import com.kaixed.kchat.data.model.Message import com.kaixed.kchat.databinding.ActivityTestBinding +import com.kaixed.kchat.ui.adapter.MessageAdapter import com.kaixed.kchat.ui.base.BaseActivity -import com.kaixed.kchat.utils.ImageEngines -import com.luck.picture.lib.basic.PictureSelector -import com.luck.picture.lib.config.SelectMimeType -import com.luck.picture.lib.entity.LocalMedia -import com.luck.picture.lib.interfaces.OnResultCallbackListener class TestActivity : BaseActivity() { @@ -20,27 +18,52 @@ class TestActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() + + val adapter = MessageAdapter() + binding.recyclerView.layoutManager = + LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) + binding.recyclerView.adapter = adapter + + val list = mutableListOf() + val message = Message(1, "1", "1", 1) + list.add(message) + adapter.submitList(list) + + binding.button.setOnClickListener { + val list1 = mutableListOf() + val message0 = Message(1, "1", "1", 1) + val message1 = Message(1, "1", "2", 1) + list1.add(message0) + list1.add(message1) + + list1[0].apply { + lastContent = "222" + } + adapter.submitList(list1) + toast("添加成功") + } + } override fun initData() { - PictureSelector.create(this) - .openGallery(SelectMimeType.ofImage()) - .setImageEngine(ImageEngines()) - .forResult(object : OnResultCallbackListener { - override fun onResult(result: ArrayList) { - result.forEach { localMedia -> - localMedia?.let { - val imagePath = it.realPath - toast("图片路径: $imagePath") - } - } - } - - override fun onCancel() { - toast("已取消") - } - }) +// PictureSelector.create(this) +// .openGallery(SelectMimeType.ofImage()) +// .setImageEngine(ImageEngines()) +// .forResult(object : OnResultCallbackListener { +// override fun onResult(result: ArrayList) { +// result.forEach { localMedia -> +// localMedia?.let { +// val imagePath = it.realPath +// toast("图片路径: $imagePath") +// } +// } +// } +// +// override fun onCancel() { +// toast("已取消") +// } +// }) } } \ No newline at end of file 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 8915588..3791f18 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 @@ -130,7 +130,8 @@ class ChatAdapter( class ImageViewHolder(val binding: ChatRecycleItemImageNormalBinding) : RecyclerView.ViewHolder(binding.root) { fun bindData(message: Messages) { - Glide.with(binding.root.context).load(message.content).into(binding.image) + Glide.with(binding.root.context).load(message.content) + .placeholder(R.drawable.bac_contacts_detail).into(binding.image) val sender = message.senderId == getUsername() changeView( parentView = binding.root, diff --git a/app/src/main/java/com/kaixed/kchat/ui/adapter/ContactRequestListAdapter.kt b/app/src/main/java/com/kaixed/kchat/ui/adapter/ContactRequestListAdapter.kt index e2ea265..a126fc1 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/adapter/ContactRequestListAdapter.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/adapter/ContactRequestListAdapter.kt @@ -8,8 +8,8 @@ import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.ViewHolder import com.bumptech.glide.Glide +import com.kaixed.kchat.data.model.friend.FriendRequestItem import com.kaixed.kchat.databinding.FriendRecycleItemAddRequestBinding -import com.kaixed.kchat.model.friend.FriendRequestItem import com.kaixed.kchat.ui.activity.ApproveContactRequestActivity import com.kaixed.kchat.ui.activity.ApproveDetailActivity 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 bb1f34e..545111a 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 @@ -3,6 +3,7 @@ package com.kaixed.kchat.ui.adapter import android.annotation.SuppressLint import android.content.Context import android.content.Intent +import android.graphics.Color import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -46,6 +47,9 @@ class ConversationAdapter( .error(R.drawable.ic_default_avatar) val chatList = getItem(position) + + holder.binding.main.setBackgroundColor(if (chatList.isPinned) Color.parseColor("#EDEDED") else Color.WHITE) + if (chatList.avatarUrl.isNotEmpty()) { Glide.with(context).load(chatList.avatarUrl).apply(options) .into(holder.binding.ifvAvatar) diff --git a/app/src/main/java/com/kaixed/kchat/ui/adapter/FunctionPanelAdapter.kt b/app/src/main/java/com/kaixed/kchat/ui/adapter/FunctionPanelAdapter.kt index b2d066a..710cb6d 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/adapter/FunctionPanelAdapter.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/adapter/FunctionPanelAdapter.kt @@ -5,8 +5,9 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter +import com.kaixed.kchat.data.model.FunctionItem import com.kaixed.kchat.databinding.FunctionGridItemBinding -import com.kaixed.kchat.model.FunctionItem +import com.kaixed.kchat.ui.i.IOnItemClickListener /** * @Author: kaixed @@ -15,33 +16,48 @@ import com.kaixed.kchat.model.FunctionItem class FunctionPanelAdapter( private val items: List, private val context: Context -) : - BaseAdapter() { +) : BaseAdapter() { + + // 监听器接口,外部可以传递点击事件的处理方法 + private var listener: IOnItemClickListener? = null + + // 设置点击事件监听器 + fun setOnItemClickListener(listener: IOnItemClickListener) { + this.listener = listener + } + override fun getCount(): Int = items.size - override fun getItem(position: Int): Any? { - return null - } - override fun getItemId(position: Int): Long { - return 0 - } + override fun getItem(position: Int): Any = items[position] + override fun getItemId(position: Int): Long = position.toLong() + + // 用于设置 GridItem 的视图 override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { - var convertView = convertView val binding: FunctionGridItemBinding + val view: View if (convertView == null) { - // convertView 为空时,说明需要创建新的视图 + // convertView 为空时,需要创建新的视图 binding = FunctionGridItemBinding.inflate(LayoutInflater.from(context), parent, false) - convertView = binding.root - // 将 binding 存入 tag,以后可复用 - convertView.tag = binding + view = binding.root + view.tag = binding // 将 binding 存入 tag,以便复用 } else { // convertView 不为空时,直接复用已有的 View - binding = convertView.tag as FunctionGridItemBinding + view = convertView + binding = view.tag as FunctionGridItemBinding } - binding.tvItemName.text = items[position].itemName - binding.ivItem.setImageResource(items[position].iconResId) - return convertView + + // 设置项的名称和图标 + val functionItem = items[position] + binding.tvItemName.text = functionItem.itemName + binding.ivItem.setImageResource(functionItem.iconResId) + + // 设置点击事件 + binding.main.setOnClickListener { + listener?.onFunctionItemClick(position) + } + + return view } } diff --git a/app/src/main/java/com/kaixed/kchat/ui/adapter/MessageAdapter.kt b/app/src/main/java/com/kaixed/kchat/ui/adapter/MessageAdapter.kt new file mode 100644 index 0000000..5b56137 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/ui/adapter/MessageAdapter.kt @@ -0,0 +1,43 @@ +package com.kaixed.kchat.ui.adapter + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.DiffUtil +import androidx.recyclerview.widget.ListAdapter +import androidx.recyclerview.widget.RecyclerView +import com.kaixed.kchat.data.model.Message +import com.kaixed.kchat.databinding.ItemMessageBinding + +class MessageAdapter : + ListAdapter(MessageDiffCallback()) { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder { + val binding = ItemMessageBinding.inflate(LayoutInflater.from(parent.context), parent, false) + return MessageViewHolder(binding) + } + + override fun onBindViewHolder(holder: MessageViewHolder, position: Int) { + val message = getItem(position) + holder.bind(message) + } + + inner class MessageViewHolder(private val binding: ItemMessageBinding) : + RecyclerView.ViewHolder(binding.root) { + + fun bind(message: Message) { + binding.message.text = message.lastContent + + } + } + + // DiffCallback 用于高效更新数据 + class MessageDiffCallback : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: Message, newItem: Message): Boolean { + return oldItem.id == newItem.id + } + + override fun areContentsTheSame(oldItem: Message, newItem: Message): Boolean { + return oldItem == newItem + } + } +} diff --git a/app/src/main/java/com/kaixed/kchat/ui/adapter/MyGridAdapter.kt b/app/src/main/java/com/kaixed/kchat/ui/adapter/MyGridAdapter.kt index 19c298e..3d169af 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/adapter/MyGridAdapter.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/adapter/MyGridAdapter.kt @@ -6,7 +6,7 @@ import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import com.kaixed.kchat.databinding.ItemGridBinding -import com.kaixed.kchat.model.HomeItem +import com.kaixed.kchat.data.model.HomeItem /** * @Author: kaixed diff --git a/app/src/main/java/com/kaixed/kchat/ui/adapter/SearchResultAdapter.kt b/app/src/main/java/com/kaixed/kchat/ui/adapter/SearchResultAdapter.kt index a058f86..f55475b 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/adapter/SearchResultAdapter.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/adapter/SearchResultAdapter.kt @@ -7,7 +7,7 @@ import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.kaixed.kchat.databinding.SearchRecycleItemContactsBinding import com.kaixed.kchat.databinding.SearchRecycleItemDetailsBinding -import com.kaixed.kchat.model.search.SearchItem +import com.kaixed.kchat.data.model.search.SearchItem /** * @Author: kaixed diff --git a/app/src/main/java/com/kaixed/kchat/ui/base/BaseFragment.kt b/app/src/main/java/com/kaixed/kchat/ui/base/BaseFragment.kt index cea78fc..daf6c58 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/base/BaseFragment.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/base/BaseFragment.kt @@ -7,7 +7,6 @@ import android.view.ViewGroup import androidx.core.view.updatePadding import androidx.fragment.app.Fragment import androidx.viewbinding.ViewBinding -import com.kaixed.kchat.model.friend.FriendItem import com.kaixed.kchat.utils.ConstantsUtil.getStatusBarHeight /** 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 937afd0..21eef16 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 @@ -16,6 +16,7 @@ import android.os.IBinder import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.Toast import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity.BIND_AUTO_CREATE import androidx.recyclerview.widget.LinearLayoutManager @@ -23,38 +24,35 @@ import com.kaixed.kchat.R 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.data.model.HomeItem import com.kaixed.kchat.databinding.FragmentHomeBinding -import com.kaixed.kchat.model.HomeItem import com.kaixed.kchat.service.WebSocketService import com.kaixed.kchat.service.WebSocketService.LocalBinder import com.kaixed.kchat.ui.activity.AddFriendsActivity -import com.kaixed.kchat.ui.activity.ContactUpdatesActivity import com.kaixed.kchat.ui.activity.SearchActivity import com.kaixed.kchat.ui.activity.TestActivity import com.kaixed.kchat.ui.adapter.ConversationAdapter import com.kaixed.kchat.ui.adapter.MyGridAdapter import com.kaixed.kchat.ui.base.BaseFragment -import com.kaixed.kchat.ui.i.OnChatListItemClickListener -import com.kaixed.kchat.utils.Constants.MMKV_COMMON_DATA -import com.tencent.mmkv.MMKV +import com.kaixed.kchat.ui.i.OnDialogFragmentClickListener +import com.kaixed.kchat.ui.i.OnItemListener +import com.kaixed.kchat.ui.widget.HomeDialogFragment import io.objectbox.Box -class HomeFragment : BaseFragment(), OnChatListItemClickListener { +class HomeFragment : BaseFragment(), OnItemListener, + OnDialogFragmentClickListener { private var chatLists: MutableList = mutableListOf() private val conversationAdapter: ConversationAdapter by lazy { - ConversationAdapter(chatLists, context) + ConversationAdapter(requireContext()) } private val conversationBox: Box by lazy { getBoxStore().boxFor(Conversation::class.java) } - private val messagesBox: Box by lazy { getBoxStore().boxFor(Messages::class.java) } - private var talkerId: String? = null - private val items: MutableList = ArrayList() + private val items: List by lazy { getHomeItems() } private var webSocketService: WebSocketService? = null @@ -64,8 +62,6 @@ class HomeFragment : BaseFragment(), OnChatListItemClickLis private lateinit var context: Context - private var handleMsgSvrId = -1L - private var connection: ServiceConnection = object : ServiceConnection { override fun onServiceConnected(className: ComponentName, service: IBinder) { val binder = service as LocalBinder @@ -134,7 +130,7 @@ class HomeFragment : BaseFragment(), OnChatListItemClickLis webSocketService!!.conversations.observe(viewLifecycleOwner) { it?.let { chatLists = it.toMutableList() - conversationAdapter.updateData(chatLists) + conversationAdapter.submitList(chatLists) notifyData() } } @@ -202,58 +198,52 @@ class HomeFragment : BaseFragment(), OnChatListItemClickLis flipped = false } + enum class HomeItems(val title: String, val iconResId: Int, val isEnabled: Boolean) { + CHANGE_BACKGROUND("更换背景", R.drawable.ic_switch, true), + CREATE_GROUP("创建群聊", R.drawable.ic_troop, false), + ADD_FRIENDS("新朋友", R.drawable.ic_friend, true), + CHANGE_MODE("夜间模式", R.drawable.ic_night, true), + FRIEND_CIRCLE("朋友圈", R.drawable.ic_qzone, false), + SCAN("扫一扫", R.drawable.ic_scan, false), + FRIENDS("通讯录", R.drawable.ic_clock_in, true), + CLOSE_APP("关闭应用", R.drawable.ic_exit, false); + + companion object { + fun getAllItems() = entries + } + } + + private fun getHomeItems(): List { + return HomeItems.getAllItems().map { homeItem -> + HomeItem(homeItem.title, homeItem.isEnabled, homeItem.iconResId) + } + } + + private fun setupGridView() { - initMenuData() val myGridAdapter = MyGridAdapter(context, items) binding.gridView.adapter = myGridAdapter binding.gridView.selector = ColorDrawable(Color.TRANSPARENT) binding.gridView.setOnItemClickListener { _, _, position, _ -> - when (position) { - 0 -> {} - 2 -> { + val homeItem = items[position] + when (HomeItems.valueOf(homeItem.name)) { + HomeItems.ADD_FRIENDS -> { val intent = Intent(context, AddFriendsActivity::class.java) startActivity(intent) } - 4 -> { - val intent2 = - Intent( - context, - ContactUpdatesActivity::class.java - ) - startActivity(intent2) + HomeItems.CLOSE_APP -> { + requireActivity().finishAffinity() } - 6 -> { -// val intent1 = -// Intent( -// context, -// FriendListActivity::class.java -// ) -// startActivity(intent1) - } + else -> Toast.makeText(requireContext(), "暂未实现", Toast.LENGTH_SHORT).show() } } + } - private fun initMenuData() { - - val list = listOf( - HomeItem("更换背景", true, R.drawable.ic_switch), - HomeItem("创建群聊", false, R.drawable.ic_troop), - HomeItem("新朋友", true, R.drawable.ic_friend), - HomeItem("夜间模式", true, R.drawable.ic_night), - HomeItem("好友动态", false, R.drawable.ic_qzone), - HomeItem("扫一扫", false, R.drawable.ic_discovery_scan), - HomeItem("通讯录", true, R.drawable.ic_clock_in), - HomeItem("关闭应用", false, R.drawable.ic_exit) - ) - items.addAll(list) - } - - override fun onResume() { super.onResume() if (flipped) { @@ -288,13 +278,17 @@ class HomeFragment : BaseFragment(), OnChatListItemClickLis override fun onItemClick(talkerId: String) { this.talkerId = talkerId +// chatLists.find { it.talkerId == talkerId }?.let { +// it.unreadCount = 0 +// conversationBox.put(it) +// } for (i in chatLists.indices) { if (chatLists[i].talkerId == talkerId) { chatLists[i].unreadCount = 0 } } notifyData() - +// val conversation = conversationBox.query(Conversation_.talkerId.equal(talkerId)).build().findFirst() @@ -303,4 +297,52 @@ class HomeFragment : BaseFragment(), OnChatListItemClickLis conversationBox.put(it) } } + + private var longClickTalkerId = "" + + private var con: Conversation? = null + + + override fun onItemLongClick(talkId: String, nickname: String) { + longClickTalkerId = talkId + con = getConversation(talkId) + val dialogFragment = HomeDialogFragment.newInstance( + contactNickname = nickname, + isPinned = con!!.isPinned, + isHasUnreadMsg = con!!.unreadCount != 0 + ) + dialogFragment.show(childFragmentManager, "HomeDialogFragment") + } + + private fun getConversation(): Conversation = + chatLists.find { it.talkerId == longClickTalkerId }!! + + private fun getConversation(talkId: String): Conversation = + chatLists.find { it.talkerId == talkId }!! + + + override fun onClickSetUnread() { + if (con == null) { + return + } + webSocketService?.updateConversationList(con!!.apply { + this.unreadCount = if (unreadCount != 0) 0 else 1 + }) + } + + override fun onClickHideConversation() { + webSocketService?.updateConversationList(getConversation().apply { + this.isShow = false + }) + } + + override fun onClickSticky() { + webSocketService?.updateConversationList(getConversation().apply { + this.isPinned = !isPinned + }) + } + + override fun onClickDeleteConversation() { + webSocketService?.deleteConversationList(getConversation()) + } } diff --git a/app/src/main/java/com/kaixed/kchat/ui/fragment/MineFragment.kt b/app/src/main/java/com/kaixed/kchat/ui/fragment/MineFragment.kt index 3981b7f..a5163b1 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/fragment/MineFragment.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/fragment/MineFragment.kt @@ -10,6 +10,7 @@ import com.kaixed.kchat.R import com.kaixed.kchat.databinding.FragmentMineBinding import com.kaixed.kchat.ui.activity.ProfileDetailActivity import com.kaixed.kchat.ui.base.BaseFragment +import com.kaixed.kchat.ui.widget.MyBottomSheetFragment import com.kaixed.kchat.utils.ConstantsUtil import com.kaixed.kchat.utils.ConstantsUtil.getAvatarUrl diff --git a/app/src/main/java/com/kaixed/kchat/ui/i/IOnItemClickListener.kt b/app/src/main/java/com/kaixed/kchat/ui/i/IOnItemClickListener.kt new file mode 100644 index 0000000..0914f98 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/ui/i/IOnItemClickListener.kt @@ -0,0 +1,9 @@ +package com.kaixed.kchat.ui.i + +/** + * @Author: kaixed + * @Date: 2024/10/27 18:36 + */ +interface IOnItemClickListener { + fun onFunctionItemClick(position: Int) +} \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/ui/i/OnDialogFragmentClickListener.kt b/app/src/main/java/com/kaixed/kchat/ui/i/OnDialogFragmentClickListener.kt new file mode 100644 index 0000000..cc8dbf7 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/ui/i/OnDialogFragmentClickListener.kt @@ -0,0 +1,8 @@ +package com.kaixed.kchat.ui.i + +interface OnDialogFragmentClickListener { + fun onClickSetUnread() + fun onClickHideConversation() + fun onClickSticky() + fun onClickDeleteConversation() +} \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/ui/i/OnChatListItemClickListener.kt b/app/src/main/java/com/kaixed/kchat/ui/i/OnItemListener.kt similarity index 58% rename from app/src/main/java/com/kaixed/kchat/ui/i/OnChatListItemClickListener.kt rename to app/src/main/java/com/kaixed/kchat/ui/i/OnItemListener.kt index 7cf045c..80a974c 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/i/OnChatListItemClickListener.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/i/OnItemListener.kt @@ -4,6 +4,9 @@ package com.kaixed.kchat.ui.i * @Author: kaixed * @Date: 2024/10/27 18:35 */ -interface OnChatListItemClickListener { +interface OnItemListener { + fun onItemClick(talkerId: String) + + fun onItemLongClick(talkId: String, nickname: String) } \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/ui/widget/HomeDialogFragment.kt b/app/src/main/java/com/kaixed/kchat/ui/widget/HomeDialogFragment.kt new file mode 100644 index 0000000..c19b329 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/ui/widget/HomeDialogFragment.kt @@ -0,0 +1,114 @@ +package com.kaixed.kchat.ui.widget + +import android.app.Dialog +import android.content.Context +import android.graphics.Color +import android.graphics.drawable.ColorDrawable +import android.os.Bundle +import android.view.Gravity +import android.view.Window +import android.view.WindowManager +import androidx.fragment.app.DialogFragment +import com.kaixed.kchat.databinding.DialogHomeBinding +import com.kaixed.kchat.ui.i.OnDialogFragmentClickListener +import kotlin.properties.Delegates + +class HomeDialogFragment : DialogFragment() { + + private lateinit var contactNickname: String + private var isPinned by Delegates.notNull() + private var isHasUnreadMsg by Delegates.notNull() + private var listener: OnDialogFragmentClickListener? = null + + companion object { + fun newInstance( + contactNickname: String, + isPinned: Boolean = false, + isHasUnreadMsg: Boolean = false + ): HomeDialogFragment { + val fragment = HomeDialogFragment() + val args = Bundle() + args.putString("contactNickname", contactNickname) + args.putBoolean("isPinned", isPinned) + args.putBoolean("isHasUnreadMsg", isHasUnreadMsg) + fragment.arguments = args + return fragment + } + } + + override fun onAttach(context: Context) { + super.onAttach(context) + val parentFragment = parentFragment + listener = if (parentFragment is OnDialogFragmentClickListener) { + parentFragment + } else if (context is OnDialogFragmentClickListener) { + context + } else { + throw IllegalStateException( + "Either parent fragment or activity must implement OnDialogFragmentClickListener" + ) + } + } + + override fun onDetach() { + super.onDetach() + listener = null + } + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + contactNickname = arguments?.getString("contactNickname") ?: "" + isPinned = arguments?.getBoolean("isPinned") ?: false + isHasUnreadMsg = arguments?.getBoolean("isHasUnreadMsg") ?: false + + val binding = DialogHomeBinding.inflate(layoutInflater) + val dialog = Dialog(requireContext()) + dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) +// dialog.setCancelable(false) + dialog.setContentView(binding.root) + dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) + + val layoutParams = WindowManager.LayoutParams().apply { + copyFrom(dialog.window?.attributes) + width = (requireContext().resources.displayMetrics.widthPixels * 0.8).toInt() + gravity = Gravity.CENTER + } + dialog.window?.attributes = layoutParams + + setContent(binding) + + setOnClickListener(binding) + return dialog + } + + private fun setContent(binding: DialogHomeBinding) { + binding.tvTitle.text = contactNickname + binding.tvSticky.text = if (isPinned) "取消置顶" else "置顶" + binding.tvSetUnread.text = if (isHasUnreadMsg) "标记已读" else "标记未读" + } + + private fun setOnClickListener(binding: DialogHomeBinding) { + binding.tvSetUnread.setOnClickListener { + listener?.onClickSetUnread() + dismissDialog() + } + + binding.tvSticky.setOnClickListener { + listener?.onClickSticky() + dismissDialog() + } + binding.tvHideConversation.setOnClickListener { + listener?.onClickHideConversation() + dismissDialog() + } + binding.tvDeleteConversation.setOnClickListener { + listener?.onClickDeleteConversation() + dismissDialog() + } + } + + private fun dismissDialog() { + if (isAdded && !isStateSaved) { + dismissAllowingStateLoss() + } + } +} diff --git a/app/src/main/java/com/kaixed/kchat/ui/fragment/MyBottomSheetFragment.kt b/app/src/main/java/com/kaixed/kchat/ui/widget/MyBottomSheetFragment.kt similarity index 98% rename from app/src/main/java/com/kaixed/kchat/ui/fragment/MyBottomSheetFragment.kt rename to app/src/main/java/com/kaixed/kchat/ui/widget/MyBottomSheetFragment.kt index 15a3c14..cec45bd 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/fragment/MyBottomSheetFragment.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/widget/MyBottomSheetFragment.kt @@ -1,4 +1,4 @@ -package com.kaixed.kchat.ui.fragment +package com.kaixed.kchat.ui.widget import android.content.Intent diff --git a/app/src/main/java/com/kaixed/kchat/utils/PopWindowUtil.kt b/app/src/main/java/com/kaixed/kchat/utils/PopWindowUtil.kt index 8b71eef..2c724ff 100644 --- a/app/src/main/java/com/kaixed/kchat/utils/PopWindowUtil.kt +++ b/app/src/main/java/com/kaixed/kchat/utils/PopWindowUtil.kt @@ -17,6 +17,7 @@ import com.kaixed.kchat.utils.DensityUtil.dpToPx * @Date: 2024/11/25 15:44 */ object PopWindowUtil { + fun showPopupWindow(context: Context, parentView: View, isMine: Boolean) { val binding: PopwindowsBinding = PopwindowsBinding.inflate(LayoutInflater.from(context)) val popupWindow: PopupWindow = PopupWindow( 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 4cb2b03..0df631f 100644 --- a/app/src/main/java/com/kaixed/kchat/viewmodel/ContactViewModel.kt +++ b/app/src/main/java/com/kaixed/kchat/viewmodel/ContactViewModel.kt @@ -6,8 +6,8 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope 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.data.model.friend.FriendRequestItem +import com.kaixed.kchat.data.model.search.SearchUser import com.kaixed.kchat.data.repository.ContactRepository import io.objectbox.Box import kotlinx.coroutines.launch @@ -17,7 +17,8 @@ class ContactViewModel : ViewModel() { private val contactRepository = ContactRepository() // 获取联系人请求列表 - private val _contactRequestListResult = MutableLiveData?>>() + private val _contactRequestListResult = + MutableLiveData?>>() val contactRequestListResult: LiveData?>> = _contactRequestListResult @@ -31,8 +32,8 @@ class ContactViewModel : ViewModel() { val addContactResult: LiveData> = _addContactResult // 搜索联系人 - private val _searchContactResult = MutableLiveData>() - val searchContactResult: LiveData> = _searchContactResult + private val _searchContactResult = MutableLiveData>() + val searchContactResult: LiveData> = _searchContactResult // 获取联系人列表 private val _contactListResult = MutableLiveData?>>() diff --git a/app/src/main/java/com/kaixed/kchat/viewmodel/FileViewModel.kt b/app/src/main/java/com/kaixed/kchat/viewmodel/FileViewModel.kt new file mode 100644 index 0000000..4965090 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/viewmodel/FileViewModel.kt @@ -0,0 +1,29 @@ +package com.kaixed.kchat.viewmodel + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.kaixed.kchat.data.repository.FileRepository +import kotlinx.coroutines.launch +import okhttp3.MultipartBody +import java.io.File + +/** + * @Author: kaixed + * @Date: 2024/11/29 15:17 + */ +class FileViewModel : ViewModel() { + private val fileRepo = FileRepository() + + private val _uploadFileResult = MutableLiveData>() + val uploadFileResult: LiveData> = _uploadFileResult + + // 上传头像 + fun uploadFile(file: File) { + viewModelScope.launch { + val result = fileRepo.uploadFile(file) + _uploadFileResult.postValue(result) + } + } +} 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 2543962..7d2b9ff 100644 --- a/app/src/main/java/com/kaixed/kchat/viewmodel/UserViewModel.kt +++ b/app/src/main/java/com/kaixed/kchat/viewmodel/UserViewModel.kt @@ -5,11 +5,11 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope 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.data.model.request.RegisterRequest +import com.kaixed.kchat.data.model.request.UserRequest +import com.kaixed.kchat.data.model.response.register.Register +import com.kaixed.kchat.data.model.response.search.User +import com.kaixed.kchat.data.model.search.SearchUser import com.kaixed.kchat.data.repository.UserAuthRepository import com.kaixed.kchat.data.repository.UserProfileRepository import com.kaixed.kchat.data.repository.UserSearchRepository diff --git a/app/src/main/res/drawable/ic_more_home.xml b/app/src/main/res/drawable/ic_more_home.xml new file mode 100644 index 0000000..0700711 --- /dev/null +++ b/app/src/main/res/drawable/ic_more_home.xml @@ -0,0 +1,22 @@ + + + + + + diff --git a/app/src/main/res/drawable/ic_search_home.xml b/app/src/main/res/drawable/ic_search_home.xml new file mode 100644 index 0000000..8579b5b --- /dev/null +++ b/app/src/main/res/drawable/ic_search_home.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/activity_test.xml b/app/src/main/res/layout/activity_test.xml index 5bbfee8..50b5763 100644 --- a/app/src/main/res/layout/activity_test.xml +++ b/app/src/main/res/layout/activity_test.xml @@ -1,29 +1,21 @@ - - + android:layout_height="0dp" + android:layout_weight="1.0" /> - - - +