mirror of
https://github.com/shaulascr/ecommerce_serang.git
synced 2025-08-10 09:22:21 +00:00
update chat store
This commit is contained in:
@ -91,7 +91,7 @@ class ChatRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun sendChatMessageStore(
|
suspend fun sendChatMessageStore(
|
||||||
storeId: Int,
|
userId: Int,
|
||||||
message: String,
|
message: String,
|
||||||
productId: Int?, // Nullable and optional
|
productId: Int?, // Nullable and optional
|
||||||
imageFile: File? = null // Nullable and optional
|
imageFile: File? = null // Nullable and optional
|
||||||
@ -100,7 +100,7 @@ class ChatRepository @Inject constructor(
|
|||||||
val parts = mutableMapOf<String, RequestBody>()
|
val parts = mutableMapOf<String, RequestBody>()
|
||||||
|
|
||||||
// Required fields
|
// 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())
|
parts["message"] = message.toRequestBody("text/plain".toMediaType())
|
||||||
|
|
||||||
// Optional: Only include if productId is valid
|
// Optional: Only include if productId is valid
|
||||||
|
@ -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) {
|
fun joinSocketRoom(roomId: Int) {
|
||||||
if (roomId <= 0) {
|
if (roomId <= 0) {
|
||||||
Log.e(TAG, "Cannot join room: Invalid room ID")
|
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 {
|
viewModelScope.launch {
|
||||||
updateState { it.copy(isSending = true) }
|
updateState { it.copy(isSending = true) }
|
||||||
|
|
||||||
try {
|
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 safeProductId = if (productId == 0) null else productId
|
||||||
|
|
||||||
|
|
||||||
val result = chatRepository.sendChatMessageStore(
|
val result = chatRepository.sendChatMessageStore(
|
||||||
storeId = storeId,
|
userId = defaultUserId, // Pass userId instead of storeId
|
||||||
message = message,
|
message = message,
|
||||||
productId = safeProductId,
|
productId = safeProductId,
|
||||||
imageFile = selectedImageFile
|
imageFile = selectedImageFile
|
||||||
|
@ -92,7 +92,8 @@ class ChatListStoreActivity : AppCompatActivity() {
|
|||||||
productRating = null,
|
productRating = null,
|
||||||
storeName = chatItem.storeName,
|
storeName = chatItem.storeName,
|
||||||
chatRoomId = chatItem.chatRoomId,
|
chatRoomId = chatItem.chatRoomId,
|
||||||
storeImage = chatItem.storeImage
|
storeImage = chatItem.storeImage,
|
||||||
|
userId = chatItem.userId
|
||||||
)
|
)
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
}
|
}
|
||||||
|
@ -110,6 +110,7 @@ class ChatStoreActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
// Get parameters from intent
|
// Get parameters from intent
|
||||||
val storeId = intent.getIntExtra(Constants.EXTRA_STORE_ID, 0)
|
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 productId = intent.getIntExtra(Constants.EXTRA_PRODUCT_ID, 0)
|
||||||
val productName = intent.getStringExtra(Constants.EXTRA_PRODUCT_NAME) ?: ""
|
val productName = intent.getStringExtra(Constants.EXTRA_PRODUCT_NAME) ?: ""
|
||||||
val productPrice = intent.getStringExtra(Constants.EXTRA_PRODUCT_PRICE) ?: ""
|
val productPrice = intent.getStringExtra(Constants.EXTRA_PRODUCT_PRICE) ?: ""
|
||||||
@ -222,8 +223,9 @@ class ChatStoreActivity : AppCompatActivity() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Set chat parameters to ViewModel
|
// Set chat parameters to ViewModel
|
||||||
viewModel.setChatParameters(
|
viewModel.setChatParametersStore(
|
||||||
storeId = storeId,
|
storeId = storeId,
|
||||||
|
userId = userId, // The user you want to chat with
|
||||||
productId = productId,
|
productId = productId,
|
||||||
productName = productName,
|
productName = productName,
|
||||||
productPrice = productPrice,
|
productPrice = productPrice,
|
||||||
@ -540,7 +542,8 @@ class ChatStoreActivity : AppCompatActivity() {
|
|||||||
productRating: String? = null,
|
productRating: String? = null,
|
||||||
storeName: String? = null,
|
storeName: String? = null,
|
||||||
chatRoomId: Int = 0,
|
chatRoomId: Int = 0,
|
||||||
storeImage: String? = null
|
storeImage: String? = null,
|
||||||
|
userId: Int
|
||||||
): Intent {
|
): Intent {
|
||||||
return Intent(context, ChatStoreActivity::class.java).apply {
|
return Intent(context, ChatStoreActivity::class.java).apply {
|
||||||
putExtra(Constants.EXTRA_STORE_ID, storeId)
|
putExtra(Constants.EXTRA_STORE_ID, storeId)
|
||||||
@ -549,6 +552,7 @@ class ChatStoreActivity : AppCompatActivity() {
|
|||||||
putExtra(Constants.EXTRA_PRODUCT_PRICE, productPrice)
|
putExtra(Constants.EXTRA_PRODUCT_PRICE, productPrice)
|
||||||
putExtra(Constants.EXTRA_PRODUCT_IMAGE, productImage)
|
putExtra(Constants.EXTRA_PRODUCT_IMAGE, productImage)
|
||||||
putExtra(Constants.EXTRA_STORE_IMAGE, storeImage)
|
putExtra(Constants.EXTRA_STORE_IMAGE, storeImage)
|
||||||
|
putExtra(Constants.EXTRA_USER_ID, userId)
|
||||||
|
|
||||||
// Convert productRating string to float if provided
|
// Convert productRating string to float if provided
|
||||||
if (productRating != null) {
|
if (productRating != null) {
|
||||||
|
@ -21,6 +21,7 @@ object Constants {
|
|||||||
const val EXTRA_PRODUCT_IMAGE = "product_image"
|
const val EXTRA_PRODUCT_IMAGE = "product_image"
|
||||||
const val EXTRA_PRODUCT_RATING = "product_rating"
|
const val EXTRA_PRODUCT_RATING = "product_rating"
|
||||||
const val EXTRA_STORE_IMAGE = "store_image"
|
const val EXTRA_STORE_IMAGE = "store_image"
|
||||||
|
const val EXTRA_USER_ID = "user_id"
|
||||||
|
|
||||||
// Request codes
|
// Request codes
|
||||||
const val REQUEST_IMAGE_PICK = 1001
|
const val REQUEST_IMAGE_PICK = 1001
|
||||||
|
Reference in New Issue
Block a user