Merge branch 'screen-features'

This commit is contained in:
shaulascr
2025-05-28 22:39:32 +07:00
5 changed files with 59 additions and 10 deletions

View File

@ -91,7 +91,7 @@ class ChatRepository @Inject constructor(
}
suspend fun sendChatMessageStore(
storeId: Int,
userId: Int,
message: String,
productId: Int?, // Nullable and optional
imageFile: File? = null // Nullable and optional
@ -100,7 +100,7 @@ class ChatRepository @Inject constructor(
val parts = mutableMapOf<String, RequestBody>()
// Required fields
parts["store_id"] = storeId.toString().toRequestBody("text/plain".toMediaType())
parts["user_id"] = userId.toString().toRequestBody("text/plain".toMediaType())
parts["message"] = message.toRequestBody("text/plain".toMediaType())
// Optional: Only include if productId is valid

View File

@ -134,6 +134,47 @@ class ChatViewModel @Inject constructor(
}
}
fun setChatParametersStore(
storeId: Int,
userId: Int,
productId: Int? = 0,
productName: String? = null,
productPrice: String? = null,
productImage: String? = null,
productRating: Float? = 0f,
storeName: String
) {
this.productId = if (productId != null && productId > 0) productId else 0
this.storeId = storeId
this.defaultUserId = userId // Store the user_id for store-side chat
this.productName = productName.toString()
this.productPrice = productPrice.toString()
this.productImage = productImage.toString()
this.productRating = productRating!!
this.storeName = storeName
// Update state with product info
updateState {
it.copy(
productName = productName.toString(),
productPrice = productPrice.toString(),
productImageUrl = productImage.toString(),
productRating = productRating,
storeName = storeName
)
}
// Connect to socket and load chat history
val existingChatRoomId = _chatRoomId.value ?: 0
if (existingChatRoomId > 0) {
// If we already have a chat room ID, we can load the chat history
loadChatHistory(existingChatRoomId)
// And join the Socket.IO room
joinSocketRoom(existingChatRoomId)
}
}
fun joinSocketRoom(roomId: Int) {
if (roomId <= 0) {
Log.e(TAG, "Cannot join room: Invalid room ID")
@ -404,18 +445,20 @@ class ChatViewModel @Inject constructor(
}
}
if (defaultUserId <= 0) { // Check userId instead of storeId
Log.e(TAG, "Cannot send message: User ID is invalid")
updateState { it.copy(error = "Cannot send message. Invalid user ID.") }
return
}
viewModelScope.launch {
updateState { it.copy(isSending = true) }
try {
// Send the message using the repository
// Note: We keep the chatRoomId parameter for compatibility with the repository method signature,
// but it's not actually used in the API call
val safeProductId = if (productId == 0) null else productId
val result = chatRepository.sendChatMessageStore(
storeId = storeId,
userId = defaultUserId, // Pass userId instead of storeId
message = message,
productId = safeProductId,
imageFile = selectedImageFile

View File

@ -92,7 +92,8 @@ class ChatListStoreActivity : AppCompatActivity() {
productRating = null,
storeName = chatItem.storeName,
chatRoomId = chatItem.chatRoomId,
storeImage = chatItem.storeImage
storeImage = chatItem.storeImage,
userId = chatItem.userId
)
startActivity(intent)
}

View File

@ -110,6 +110,7 @@ class ChatStoreActivity : AppCompatActivity() {
// Get parameters from intent
val storeId = intent.getIntExtra(Constants.EXTRA_STORE_ID, 0)
val userId = intent.getIntExtra(Constants.EXTRA_USER_ID, 0)
val productId = intent.getIntExtra(Constants.EXTRA_PRODUCT_ID, 0)
val productName = intent.getStringExtra(Constants.EXTRA_PRODUCT_NAME) ?: ""
val productPrice = intent.getStringExtra(Constants.EXTRA_PRODUCT_PRICE) ?: ""
@ -222,8 +223,9 @@ class ChatStoreActivity : AppCompatActivity() {
})
// Set chat parameters to ViewModel
viewModel.setChatParameters(
viewModel.setChatParametersStore(
storeId = storeId,
userId = userId, // The user you want to chat with
productId = productId,
productName = productName,
productPrice = productPrice,
@ -540,7 +542,8 @@ class ChatStoreActivity : AppCompatActivity() {
productRating: String? = null,
storeName: String? = null,
chatRoomId: Int = 0,
storeImage: String? = null
storeImage: String? = null,
userId: Int
): Intent {
return Intent(context, ChatStoreActivity::class.java).apply {
putExtra(Constants.EXTRA_STORE_ID, storeId)
@ -549,6 +552,7 @@ class ChatStoreActivity : AppCompatActivity() {
putExtra(Constants.EXTRA_PRODUCT_PRICE, productPrice)
putExtra(Constants.EXTRA_PRODUCT_IMAGE, productImage)
putExtra(Constants.EXTRA_STORE_IMAGE, storeImage)
putExtra(Constants.EXTRA_USER_ID, userId)
// Convert productRating string to float if provided
if (productRating != null) {

View File

@ -21,6 +21,7 @@ object Constants {
const val EXTRA_PRODUCT_IMAGE = "product_image"
const val EXTRA_PRODUCT_RATING = "product_rating"
const val EXTRA_STORE_IMAGE = "store_image"
const val EXTRA_USER_ID = "user_id"
// Request codes
const val REQUEST_IMAGE_PICK = 1001